r18947 - gnucash/trunk/src/gnc - Cutecash: Add more event handler to the account windows so that they will be deleted upon book closing.
Christian Stimming
cstim at code.gnucash.org
Sun Mar 21 17:38:33 EDT 2010
Author: cstim
Date: 2010-03-21 17:38:33 -0400 (Sun, 21 Mar 2010)
New Revision: 18947
Trac: http://svn.gnucash.org/trac/changeset/18947
Modified:
gnucash/trunk/src/gnc/AccountItemModel.cpp
gnucash/trunk/src/gnc/AccountItemModel.hpp
gnucash/trunk/src/gnc/Cmd.cpp
gnucash/trunk/src/gnc/QofEventWrapper.hpp
gnucash/trunk/src/gnc/SplitListModel.cpp
gnucash/trunk/src/gnc/SplitListView.cpp
gnucash/trunk/src/gnc/SplitListView.hpp
Log:
Cutecash: Add more event handler to the account windows so that they will be deleted upon book closing.
Modified: gnucash/trunk/src/gnc/AccountItemModel.cpp
===================================================================
--- gnucash/trunk/src/gnc/AccountItemModel.cpp 2010-03-21 21:37:50 UTC (rev 18946)
+++ gnucash/trunk/src/gnc/AccountItemModel.cpp 2010-03-21 21:38:33 UTC (rev 18947)
@@ -21,6 +21,8 @@
*/
#include "AccountItemModel.hpp"
+
+#include "engine/gnc-event.h" // for GNC_EVENT_ITEM_ADDED
#include "gnc/Numeric.hpp"
#include <QDebug>
@@ -168,6 +170,35 @@
// ////////////////////////////////////////////////////////////
+AccountListModel::AccountListModel(Account rootaccount, QObject *parent)
+ : base_class(rootaccount, parent)
+ , m_list()
+ , m_eventWrapperAccount(*this, &AccountListModel::accountEvent)
+{
+ recreateCache();
+}
+
+void AccountListModel::recreateCache()
+{
+ m_list = Account::fromGList(m_root.get_descendants());
+ reset();
+}
+
+void AccountListModel::accountEvent( ::Account* acc, QofEventId event_type)
+{
+ //qDebug() << "AccountListModel::accountEvent, id=" << qofEventToString(event_type);
+
+ switch (event_type)
+ {
+ case QOF_EVENT_CREATE:
+ case QOF_EVENT_DESTROY:
+ recreateCache();
+ break;
+ default:
+ break;
+ }
+}
+
QModelIndex AccountListModel::index(int row, int column,
const QModelIndex &parent) const
{
Modified: gnucash/trunk/src/gnc/AccountItemModel.hpp
===================================================================
--- gnucash/trunk/src/gnc/AccountItemModel.hpp 2010-03-21 21:37:50 UTC (rev 18946)
+++ gnucash/trunk/src/gnc/AccountItemModel.hpp 2010-03-21 21:38:33 UTC (rev 18947)
@@ -24,6 +24,7 @@
#define GNC_ACCOUNTITEMMODEL_HPP
#include "gnc/Account.hpp"
+#include "gnc/QofEventWrapper.hpp"
#include <QAbstractItemModel>
@@ -72,10 +73,7 @@
Q_OBJECT
public:
typedef AccountTreeModel base_class;
- AccountListModel(Account rootaccount, QObject *parent = 0)
- : base_class(rootaccount, parent)
- , m_list(Account::fromGList(rootaccount.get_descendants()))
- {}
+ AccountListModel(Account rootaccount, QObject *parent = 0);
int rowCount(const QModelIndex& parent = QModelIndex()) const { return m_list.size(); }
@@ -87,8 +85,14 @@
int indexOf(AccountQList::value_type value) const { return m_list.indexOf(value); }
const AccountQList::value_type at(int i) const { return m_list.at(i); }
+public slots:
+ void accountEvent( ::Account* v, QofEventId event_type);
+
private:
+ void recreateCache();
+
AccountQList m_list;
+ QofEventWrapper<AccountListModel, ::Account*> m_eventWrapperAccount;
};
/** Specialization of the account list model that only shows the
Modified: gnucash/trunk/src/gnc/Cmd.cpp
===================================================================
--- gnucash/trunk/src/gnc/Cmd.cpp 2010-03-21 21:37:50 UTC (rev 18946)
+++ gnucash/trunk/src/gnc/Cmd.cpp 2010-03-21 21:38:33 UTC (rev 18947)
@@ -365,6 +365,9 @@
, m_previousValue(previousValue)
, m_newValue(newValue)
{
+ Q_ASSERT(m_target.getParent());
+ Q_ASSERT(m_target.getOtherSplit());
+ Q_ASSERT(m_target.getAccount());
}
virtual void redo() { set(m_newValue); }
@@ -372,6 +375,7 @@
private:
void set(const value_type& value)
{
+ Q_ASSERT(m_target.getParent());
const TmpTransaction& trans = *m_target.getParent();
if (trans.countSplits() != 2)
return;
@@ -380,6 +384,7 @@
TmpSplit& other = *p_other;
Commodity originCommodity = Account(m_target.getAccount()).getCommodity();
Commodity transCommodity = trans.getCommodity();
+ Q_ASSERT(other.getAccount());
Commodity otherCommodity = Account(other.getAccount()).getCommodity();
if (originCommodity != transCommodity
|| transCommodity != otherCommodity)
@@ -414,12 +419,11 @@
{
public:
typedef QUndoCommand base_class;
- typedef TmpTransaction target_type;
/** Constructor
*/
TransactionCreateCmd(const QString& text,
- const target_type target,
+ const TmpTransaction& target,
QUndoCommand *parent = 0)
: base_class(text, parent)
, m_template(target)
@@ -438,7 +442,7 @@
}
protected:
- target_type m_template;
+ TmpTransaction m_template;
Transaction m_created;
};
Modified: gnucash/trunk/src/gnc/QofEventWrapper.hpp
===================================================================
--- gnucash/trunk/src/gnc/QofEventWrapper.hpp 2010-03-21 21:37:50 UTC (rev 18946)
+++ gnucash/trunk/src/gnc/QofEventWrapper.hpp 2010-03-21 21:38:33 UTC (rev 18947)
@@ -28,10 +28,15 @@
extern "C"
{
#include "qof.h"
+#include "engine/gnc-engine.h"
+#include "engine/Account.h"
+#include "engine/Transaction.h"
+#include "engine/gnc-commodity.h"
}
#include <QString>
#include <QDebug>
+#include <stdexcept>
namespace gnc
{
@@ -45,6 +50,45 @@
return obj->e_type;
}
+namespace detail
+{
+ template<class ValuePtrT> inline const char* getQofTypeT()
+ {
+ Q_ASSERT(false);
+ // This would need a BOOST_STATIC_ASSERT(sizeof(T)==0) to
+ // trigger a compile-time warning in case this function is
+ // erroneously instantiated.
+ // This template must not be instantiated. Instead, for each
+ // templated usage there must exist a specialization which
+ // will return the correct QOF_ID string!
+ throw std::runtime_error("Should not have been instantiated");
+ }
+ template<> inline const char* getQofTypeT< ::Transaction*>()
+ {
+ return GNC_ID_TRANS;
+ }
+ template<> inline const char* getQofTypeT< ::Account*>()
+ {
+ return GNC_ID_ACCOUNT;
+ }
+ template<> inline const char* getQofTypeT< ::QofBook*>()
+ {
+ return QOF_ID_BOOK;
+ }
+ template<> inline const char* getQofTypeT< ::QofSession*>()
+ {
+ return QOF_ID_SESSION;
+ }
+ template<> inline const char* getQofTypeT< ::gnc_commodity*>()
+ {
+ return GNC_ID_COMMODITY;
+ }
+ template<> inline const char* getQofTypeT< ::Split*>()
+ {
+ return GNC_ID_SPLIT;
+ }
+}
+
/** Template wrapper class for objects which want to receive
* notifications from the qof_event system in any of their member
* functions.
@@ -57,11 +101,10 @@
{
public:
QofEventWrapper(ReceiverT& receiver,
- SlotFunc recvSlot,
- const char* qof_type)
+ SlotFunc recvSlot)
: m_receiver(receiver)
, m_receiveFunc(recvSlot)
- , m_qof_type(qof_type)
+ , m_qof_type(gnc::detail::getQofTypeT<ValuePtrT>())
{
m_handler_id = qof_event_register_handler(QofEventWrapper::event_handler, this);
}
Modified: gnucash/trunk/src/gnc/SplitListModel.cpp
===================================================================
--- gnucash/trunk/src/gnc/SplitListModel.cpp 2010-03-21 21:37:50 UTC (rev 18946)
+++ gnucash/trunk/src/gnc/SplitListModel.cpp 2010-03-21 21:38:33 UTC (rev 18947)
@@ -43,8 +43,8 @@
, m_account(acc)
, m_list()
, m_undoStack(undoStack)
- , m_eventWrapper(*this, &SplitListModel::transactionEvent, GNC_ID_TRANS)
- , m_eventWrapperAccount(*this, &SplitListModel::accountEvent, GNC_ID_ACCOUNT)
+ , m_eventWrapper(*this, &SplitListModel::transactionEvent)
+ , m_eventWrapperAccount(*this, &SplitListModel::accountEvent)
, m_enableNewTransaction(true)
{
recreateCache();
@@ -646,7 +646,7 @@
void SplitListModel::transactionEvent( ::Transaction* trans, QofEventId event_type)
{
- qDebug() << "transactionEvent, id=" << qofEventToString(event_type);
+ qDebug() << "SplitListModel::transactionEvent, id=" << qofEventToString(event_type);
switch (event_type)
{
case QOF_EVENT_MODIFY:
@@ -666,7 +666,7 @@
{
if (acc != m_account.get())
return;
- qDebug() << "accountEvent, id=" << qofEventToString(event_type);
+ qDebug() << "SplitListModel::accountEvent, id=" << qofEventToString(event_type);
switch (event_type)
{
Modified: gnucash/trunk/src/gnc/SplitListView.cpp
===================================================================
--- gnucash/trunk/src/gnc/SplitListView.cpp 2010-03-21 21:37:50 UTC (rev 18946)
+++ gnucash/trunk/src/gnc/SplitListView.cpp 2010-03-21 21:38:33 UTC (rev 18947)
@@ -22,6 +22,7 @@
#include "SplitListView.hpp"
+#include "engine/gnc-event.h" // for GNC_EVENT_ITEM_ADDED
#include "gnc/Account.hpp"
#include "gnc/SplitListModel.hpp"
#include "gnc/AccountSelectionDelegate.hpp"
@@ -35,6 +36,9 @@
SplitListView::SplitListView(Account account, QUndoStack* undoStack, QWidget* parent)
: base_class(parent)
+ , m_account(account)
+ , m_eventWrapperAccount(*this, &SplitListView::accountEvent)
+ , m_eventWrapperBook(*this, &SplitListView::bookEvent)
{
// Create a model that is used in this view
SplitListModel *smodel = new SplitListModel(account, undoStack, this);
@@ -80,4 +84,36 @@
}
}
+void SplitListView::accountEvent( ::Account* v, QofEventId event_type)
+{
+ if (v != m_account.get())
+ return;
+ //qDebug() << "SplitListView::accountEvent, id=" << qofEventToString(event_type);
+ switch (event_type)
+ {
+ case QOF_EVENT_DESTROY:
+ // This account seems to be getting deleted - better close
+ // this view.
+ deleteLater();
+ break;
+ default:
+ break;
+ }
}
+
+void SplitListView::bookEvent( ::QofBook* v, QofEventId event_type)
+{
+ qDebug() << "SplitListView::bookEvent, id=" << qofEventToString(event_type);
+
+ switch (event_type)
+ {
+ case QOF_EVENT_DESTROY:
+ // The book is being deleted - better close this view ASAP!
+ deleteLater();
+ break;
+ default:
+ break;
+ }
+}
+
+}
Modified: gnucash/trunk/src/gnc/SplitListView.hpp
===================================================================
--- gnucash/trunk/src/gnc/SplitListView.hpp 2010-03-21 21:37:50 UTC (rev 18946)
+++ gnucash/trunk/src/gnc/SplitListView.hpp 2010-03-21 21:38:33 UTC (rev 18947)
@@ -25,6 +25,7 @@
#include "gnc/Account.hpp"
#include "gnc/SplitListModel.hpp"
+#include "gnc/QofEventWrapper.hpp"
#include <QtGui/QTableView>
#include <QtGui/QAbstractItemDelegate>
@@ -46,6 +47,13 @@
public slots:
void closeEditor(QWidget* editor, QAbstractItemDelegate::EndEditHint hint);
+ void accountEvent( ::Account* v, QofEventId event_type);
+ void bookEvent( ::QofBook* v, QofEventId event_type);
+
+private:
+ Account m_account;
+ QofEventWrapper<SplitListView, ::Account*> m_eventWrapperAccount;
+ QofEventWrapper<SplitListView, ::QofBook*> m_eventWrapperBook;
};
}
More information about the gnucash-changes
mailing list