r18862 - gnucash/trunk/src/gnc - Implement the account list data model as a specialization of the account tree model.

Christian Stimming cstim at code.gnucash.org
Sun Mar 7 06:21:26 EST 2010


Author: cstim
Date: 2010-03-07 06:21:26 -0500 (Sun, 07 Mar 2010)
New Revision: 18862
Trac: http://svn.gnucash.org/trac/changeset/18862

Modified:
   gnucash/trunk/src/gnc/AccountItemModel.cpp
   gnucash/trunk/src/gnc/AccountItemModel.hpp
   gnucash/trunk/src/gnc/mainwindow.cpp
   gnucash/trunk/src/gnc/mainwindow.hpp
Log:
Implement the account list data model as a specialization of the account tree model.

This is helpful in order to understand Qt's Model/View structure, so both
(list and tree) are still available.

Modified: gnucash/trunk/src/gnc/AccountItemModel.cpp
===================================================================
--- gnucash/trunk/src/gnc/AccountItemModel.cpp	2010-03-06 21:42:39 UTC (rev 18861)
+++ gnucash/trunk/src/gnc/AccountItemModel.cpp	2010-03-07 11:21:26 UTC (rev 18862)
@@ -155,4 +155,26 @@
         return QString("%1").arg(1 + section);
 }
 
+// ////////////////////////////////////////////////////////////
+
+
+QModelIndex AccountListModel::index(int row, int column,
+                                    const QModelIndex &parent) const
+{
+    //qDebug() << "index(), " << row << column << parent;
+    if (!hasIndex(row, column, parent)
+        || row >= m_acclist.size())
+        return QModelIndex();
+
+    Account childItem = m_acclist.at(row);
+    if (childItem.get())
+    {
+        //qDebug() << "returning" << childItem.getName();
+        return createIndex(row, column, childItem.get());
+    }
+    else
+        return QModelIndex();
+}
+
+
 } // END namespace gnc

Modified: gnucash/trunk/src/gnc/AccountItemModel.hpp
===================================================================
--- gnucash/trunk/src/gnc/AccountItemModel.hpp	2010-03-06 21:42:39 UTC (rev 18861)
+++ gnucash/trunk/src/gnc/AccountItemModel.hpp	2010-03-07 11:21:26 UTC (rev 18862)
@@ -26,98 +26,70 @@
 #include "gnc/Account.hpp"
 
 #include <QAbstractItemModel>
-#include <QAbstractTableModel>
 
 namespace gnc
 {
 
-class AccountItemModel : public QAbstractTableModel
+/** This is the data model for an account tree.
+ *
+ * It was written following the "Simple Tree Model Example" in the qt4
+ * documentation. In particular, the trick is that each returned
+ * QModelIndex of our model implementation which is valid will have a
+ * ::Account* pointer in its QModelIndex::internalPointer() member,
+ * which can then be used in the data() method and the other
+ * methods. This trick works quite nice because our C structures
+ * ::Account* already have all the methods to iterate the tree back
+ * and forth, and our C++ wrapper gnc::Account doesn't add any
+ * functionality except for some nicer method names.
+ */
+class AccountTreeModel : public QAbstractItemModel
 {
     Q_OBJECT
 public:
-    AccountItemModel(Account rootaccount, QObject *parent = 0)
-            : QAbstractTableModel(parent)
-            , m_root(rootaccount)
-            , m_acclist(Account::fromGList(m_root.get_descendants()))
-    {
-    }
+    AccountTreeModel(Account rootaccount, QObject *parent = 0);
 
-    int rowCount(const QModelIndex& parent = QModelIndex()) const { return m_acclist.size(); }
-    int columnCount(const QModelIndex& parent = QModelIndex()) const { return 3; }
-    QVariant data(const QModelIndex& index, int role) const
-    {
-        if (!index.isValid())
-            return QVariant();
-        if (index.row() > rowCount(index))
-            return QVariant();
-        if (role == Qt::DisplayRole)
-        {
-            switch (index.column())
-            {
-            case 0:
-                return Account(m_acclist.at(index.row())).getName();
-            case 1:
-                return Account(m_acclist.at(index.row())).getCode();
-            case 2:
-                return Account(m_acclist.at(index.row())).getDescription();
-            default:
-                return QVariant();
-            }
-        }
-        else
-            return QVariant();
-    }
-    QVariant headerData(int section, Qt::Orientation orientation, int role) const
-    {
-        if (role != Qt::DisplayRole)
-            return QVariant();
-        if (orientation == Qt::Horizontal)
-        {
-            switch (section)
-            {
-            case 0:
-                return QString("Name");
-            case 1:
-                return QString("Code");
-            case 2:
-                return QString("Description");
-            default:
-                return QVariant();
-            }
-        }
-        else
-            return QString("%1").arg(1 + section);
-    }
-private:
+    int rowCount(const QModelIndex& parent = QModelIndex()) const;
+    int columnCount(const QModelIndex& parent = QModelIndex()) const;
+    QModelIndex parent(const QModelIndex &index) const;
+    QModelIndex index(int row, int column,
+                      const QModelIndex &parent = QModelIndex()) const;
+    Qt::ItemFlags flags(const QModelIndex &index) const;
+
+    QVariant data(const QModelIndex& index, int role) const;
+    QVariant headerData(int section, Qt::Orientation orientation, int role) const;
+
+protected:
     Account m_root;
-    Account::AccountQList m_acclist;
 };
 
 
 
-
-class AccountTreeModel : public QAbstractItemModel
+/** Specialization of the account tree model for when all accounts
+ * should be viewed as a flat list instead of a tree. Only the index()
+ * and parent() functions had to be overridden - quite easy. */
+class AccountListModel : public AccountTreeModel
 {
     Q_OBJECT
 public:
-    AccountTreeModel(Account rootaccount, QObject *parent = 0);
+    AccountListModel(Account rootaccount, QObject *parent = 0)
+        : AccountTreeModel(rootaccount, parent)
+        , m_acclist(Account::fromGList(rootaccount.get_descendants()))
+    {
+    }
 
-    int rowCount(const QModelIndex& parent = QModelIndex()) const;
-    int columnCount(const QModelIndex& parent = QModelIndex()) const;
-    QModelIndex parent(const QModelIndex &index) const;
+    int rowCount(const QModelIndex& parent = QModelIndex()) const { return m_acclist.size(); }
+
     QModelIndex index(int row, int column,
                       const QModelIndex &parent = QModelIndex()) const;
-    Qt::ItemFlags flags(const QModelIndex &index) const;
 
-    QVariant data(const QModelIndex& index, int role) const;
-    QVariant headerData(int section, Qt::Orientation orientation, int role) const;
+    QModelIndex parent(const QModelIndex &index) const { return QModelIndex(); }
 
 private:
-    Account m_root;
     Account::AccountQList m_acclist;
 };
 
 
+
 } // END namespace gnc
 
 #endif

Modified: gnucash/trunk/src/gnc/mainwindow.cpp
===================================================================
--- gnucash/trunk/src/gnc/mainwindow.cpp	2010-03-06 21:42:39 UTC (rev 18861)
+++ gnucash/trunk/src/gnc/mainwindow.cpp	2010-03-07 11:21:26 UTC (rev 18862)
@@ -62,7 +62,6 @@
 
 MainWindow::MainWindow()
         : ui(new Ui::MainWindow)
-        , m_accountItemModel(NULL)
 {
     ui->setupUi(this);
 
@@ -453,8 +452,8 @@
     Account root (m_session.get_book().get_root_account());
     if (root)
     {
-        m_accountItemModel = new AccountItemModel(root, this);
-        ui->tableView->setModel(m_accountItemModel);
+        m_accountListModel = new AccountListModel(root, this);
+        ui->tableView->setModel(m_accountListModel);
 
         m_accountTreeModel = new AccountTreeModel(root, this);
         ui->treeView->setModel(m_accountTreeModel);

Modified: gnucash/trunk/src/gnc/mainwindow.hpp
===================================================================
--- gnucash/trunk/src/gnc/mainwindow.hpp	2010-03-06 21:42:39 UTC (rev 18861)
+++ gnucash/trunk/src/gnc/mainwindow.hpp	2010-03-07 11:21:26 UTC (rev 18862)
@@ -83,7 +83,7 @@
     QToolBar *editToolBar;
 
     Session m_session;
-    AccountItemModel *m_accountItemModel;
+    AccountListModel *m_accountListModel;
     AccountTreeModel *m_accountTreeModel;
 };
 



More information about the gnucash-changes mailing list