r18854 - gnucash/trunk/src/gnc - Cutecash: Add a tree view of the accounts.

Christian Stimming cstim at code.gnucash.org
Sat Mar 6 09:32:05 EST 2010


Author: cstim
Date: 2010-03-06 09:32:05 -0500 (Sat, 06 Mar 2010)
New Revision: 18854
Trac: http://svn.gnucash.org/trac/changeset/18854

Added:
   gnucash/trunk/src/gnc/AccountItemModel.cpp
Modified:
   gnucash/trunk/src/gnc/Account.hpp
   gnucash/trunk/src/gnc/AccountItemModel.hpp
   gnucash/trunk/src/gnc/CMakeLists.txt
   gnucash/trunk/src/gnc/mainwindow.cpp
   gnucash/trunk/src/gnc/mainwindow.hpp
   gnucash/trunk/src/gnc/mainwindow.ui
Log:
Cutecash: Add a tree view of the accounts.

Modified: gnucash/trunk/src/gnc/Account.hpp
===================================================================
--- gnucash/trunk/src/gnc/Account.hpp	2010-03-06 11:09:24 UTC (rev 18853)
+++ gnucash/trunk/src/gnc/Account.hpp	2010-03-06 14:32:05 UTC (rev 18854)
@@ -49,14 +49,32 @@
     QString getName() const { return QString::fromUtf8(xaccAccountGetName(get())); }
     QString getCode() const { return QString::fromUtf8(xaccAccountGetCode(get())); }
     QString getDescription() const { return QString::fromUtf8(xaccAccountGetDescription(get())); }
+
     Account get_parent() const { return gnc_account_get_parent(get()); }
     Account get_root() { return gnc_account_get_root(get()); }
     bool is_root() const { return gnc_account_is_root(get()); }
     gint n_children() const { return gnc_account_n_children(get()); }
     GList *get_children() const { return gnc_account_get_children(get()); }
-    GList * get_descendants () const { return gnc_account_get_descendants (get()); }
+    GList *get_descendants () const { return gnc_account_get_descendants (get()); }
     Account nth_child (gint num) const { return gnc_account_nth_child(get(), num); }
 
+
+    /** Return the index of this account in the children's list of its
+     * parent account.
+     */
+    gint child_index () const
+    {
+        Account parent(get_parent());
+        if (parent.get())
+            return gnc_account_child_index(parent.get(), get());
+        else
+            return 0;
+    }
+
+    gint get_current_depth () const { return gnc_account_get_current_depth(get()); }
+    gint get_tree_depth () const { return gnc_account_get_tree_depth(get()); }
+
+
     typedef QList< ::Account*> AccountQList;
     static AccountQList fromGList(GList* glist)
     {

Added: gnucash/trunk/src/gnc/AccountItemModel.cpp
===================================================================
--- gnucash/trunk/src/gnc/AccountItemModel.cpp	                        (rev 0)
+++ gnucash/trunk/src/gnc/AccountItemModel.cpp	2010-03-06 14:32:05 UTC (rev 18854)
@@ -0,0 +1,158 @@
+/*
+ * AccountItemModel.hpp
+ * Copyright (C) 2010 Christian Stimming
+ *
+ * This program is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU General Public License as
+ * published by the Free Software Foundation; either version 2 of
+ * the License, or (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program; if not, contact:
+ *
+ * Free Software Foundation           Voice:  +1-617-542-5942
+ * 51 Franklin Street, Fifth Floor    Fax:    +1-617-542-2652
+ * Boston, MA  02110-1301,  USA       gnu at gnu.org
+ */
+
+#include "AccountItemModel.hpp"
+#include <QDebug>
+
+namespace gnc
+{
+
+AccountTreeModel::AccountTreeModel(Account rootaccount, QObject *parent)
+    : QAbstractItemModel(parent)
+    , m_root(rootaccount)
+{
+}
+
+QModelIndex AccountTreeModel::index(int row, int column,
+                                    const QModelIndex &parent) const
+{
+    //qDebug() << "index(), " << row << column << parent;
+    if (!hasIndex(row, column, parent))
+        return QModelIndex();
+
+    Account parentItem;
+
+    if (!parent.isValid())
+        parentItem = m_root;
+    else
+        parentItem.reset(static_cast< ::Account*>(parent.internalPointer()));
+
+    Account childItem = parentItem.nth_child(row);
+    if (childItem.get())
+    {
+        //qDebug() << "returning" << childItem.getName();
+        return createIndex(row, column, childItem.get());
+    }
+    else
+        return QModelIndex();
+}
+
+QModelIndex AccountTreeModel::parent(const QModelIndex &index) const
+{
+    //qDebug() << "parent()" << index;
+    if (!index.isValid())
+        return QModelIndex();
+
+    Account childItem(static_cast< ::Account*>(index.internalPointer()));
+    Account parentItem(childItem.get_parent());
+
+    if (parentItem.get() == m_root.get())
+        return QModelIndex();
+
+    return createIndex(parentItem.child_index(), 0, parentItem.get());
+}
+
+int AccountTreeModel::rowCount(const QModelIndex& parent) const
+{
+    //qDebug() << "rowCount()" << parent;
+    if (parent.column() > 0)
+        return 0;
+    // FIXME: Doesn't this just mean the nonzero columns don't have a
+    // tree? In that case it would be correct.
+
+    Account parentItem;
+    if (!parent.isValid())
+        parentItem = m_root;
+    else
+        parentItem.reset(static_cast< ::Account*>(parent.internalPointer()));
+
+    //qDebug() << "Returning " << parentItem.n_children();
+    return parentItem.n_children();
+}
+
+int AccountTreeModel::columnCount(const QModelIndex& parent) const
+{
+    //qDebug() << "columnCount()" << parent;
+//     if (!parent.isValid())
+//         return 0;
+//     else
+    return 3; // Fixed number for now
+}
+
+QVariant AccountTreeModel::data(const QModelIndex& index, int role) const
+{
+    //qDebug() << "data(), " << index;
+    if (!index.isValid())
+        return QVariant();
+    if (role == Qt::DisplayRole)
+    {
+        Account account(static_cast< ::Account*>(index.internalPointer()));
+        switch (index.column())
+        {
+            case 0:
+                return account.getName();
+            case 1:
+                return account.getCode();
+            case 2:
+                return account.getDescription();
+            default:
+                return QVariant();
+        }
+    }
+    else
+        return QVariant();
+}
+
+Qt::ItemFlags AccountTreeModel::flags(const QModelIndex &index) const
+{
+    //qDebug() << "flags()" << index;
+    if (!index.isValid())
+        return 0;
+
+    // Ensure read-only access only
+    return Qt::ItemIsEnabled | Qt::ItemIsSelectable;
+}
+
+QVariant AccountTreeModel::headerData(int section, Qt::Orientation orientation, int role) const
+{
+    //qDebug() << "headerData()" << section;
+    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);
+}
+
+} // END namespace gnc

Modified: gnucash/trunk/src/gnc/AccountItemModel.hpp
===================================================================
--- gnucash/trunk/src/gnc/AccountItemModel.hpp	2010-03-06 11:09:24 UTC (rev 18853)
+++ gnucash/trunk/src/gnc/AccountItemModel.hpp	2010-03-06 14:32:05 UTC (rev 18854)
@@ -93,6 +93,31 @@
     Account::AccountQList m_acclist;
 };
 
+
+
+
+class AccountTreeModel : public QAbstractItemModel
+{
+    Q_OBJECT
+public:
+    AccountTreeModel(Account rootaccount, QObject *parent = 0);
+
+    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;
+
+private:
+    Account m_root;
+    Account::AccountQList m_acclist;
+};
+
+
 } // END namespace gnc
 
 #endif

Modified: gnucash/trunk/src/gnc/CMakeLists.txt
===================================================================
--- gnucash/trunk/src/gnc/CMakeLists.txt	2010-03-06 11:09:24 UTC (rev 18853)
+++ gnucash/trunk/src/gnc/CMakeLists.txt	2010-03-06 14:32:05 UTC (rev 18854)
@@ -10,6 +10,7 @@
 )
 
 SET (gnc_SOURCES
+  AccountItemModel.cpp
   Book.cpp
   Session.cpp
   main.cpp

Modified: gnucash/trunk/src/gnc/mainwindow.cpp
===================================================================
--- gnucash/trunk/src/gnc/mainwindow.cpp	2010-03-06 11:09:24 UTC (rev 18853)
+++ gnucash/trunk/src/gnc/mainwindow.cpp	2010-03-06 14:32:05 UTC (rev 18854)
@@ -455,7 +455,11 @@
     {
         m_accountItemModel = new AccountItemModel(root, this);
         ui->tableView->setModel(m_accountItemModel);
-        ui->tabWidget->setCurrentIndex(1); //setCurrentWidget(ui->tableView);
+
+        m_accountTreeModel = new AccountTreeModel(root, this);
+        ui->treeView->setModel(m_accountTreeModel);
+
+        ui->tabWidget->setCurrentWidget(ui->treeViewTab);
     }
     else
     {

Modified: gnucash/trunk/src/gnc/mainwindow.hpp
===================================================================
--- gnucash/trunk/src/gnc/mainwindow.hpp	2010-03-06 11:09:24 UTC (rev 18853)
+++ gnucash/trunk/src/gnc/mainwindow.hpp	2010-03-06 14:32:05 UTC (rev 18854)
@@ -84,6 +84,7 @@
 
     Session m_session;
     AccountItemModel *m_accountItemModel;
+    AccountTreeModel *m_accountTreeModel;
 };
 
 } // END namespace gnc

Modified: gnucash/trunk/src/gnc/mainwindow.ui
===================================================================
--- gnucash/trunk/src/gnc/mainwindow.ui	2010-03-06 11:09:24 UTC (rev 18853)
+++ gnucash/trunk/src/gnc/mainwindow.ui	2010-03-06 14:32:05 UTC (rev 18854)
@@ -36,7 +36,7 @@
       <property name="currentIndex">
        <number>0</number>
       </property>
-      <widget class="QWidget" name="tab_1">
+      <widget class="QWidget" name="textBrowserTab">
        <attribute name="title">
         <string>Example Welcome Page</string>
        </attribute>
@@ -53,7 +53,11 @@
 &lt;/style&gt;&lt;/head&gt;&lt;body style=&quot; font-family:'Sans'; font-size:10pt; font-weight:400; font-style:normal;&quot;&gt;
 &lt;p style=&quot; margin-top:18px; margin-bottom:12px; margin-left:0px; margin-right:0px; -qt-block-indent:0; text-indent:0px;&quot;&gt;&lt;span style=&quot; font-family:'MS Shell Dlg 2'; font-size:12pt; font-weight:600;&quot;&gt;Cutecash&lt;/span&gt;&lt;/p&gt;
 &lt;p style=&quot; margin-top:18px; margin-bottom:12px; margin-left:0px; margin-right:0px; -qt-block-indent:0; text-indent:0px;&quot;&gt;&lt;span style=&quot; font-family:'MS Shell Dlg 2'; font-size:12pt; font-weight:600;&quot;&gt;Free Finance Software. Easy to develop, easy to use.&lt;/span&gt;&lt;/p&gt;
-&lt;p style=&quot; margin-top:12px; margin-bottom:12px; margin-left:0px; margin-right:0px; -qt-block-indent:0; text-indent:0px;&quot;&gt;&lt;span style=&quot; font-family:'MS Shell Dlg 2'; font-size:8pt;&quot;&gt;Currently this is only a proof-of-concept for developers: You can load an existing gnucash XML file, and it will show the list of accounts as a flat table in a QTableView. The fun part is how easy it was to add this display of all accounts, so it will probably take only another 1-2 hours until the account list is a tree to be viewed in a QTreeView. And a QTableView with the splits of an account can't be far...&lt;/span&gt;&lt;/p&gt;
+&lt;p style=&quot; margin-top:12px; margin-bottom:12px; margin-left:0px; margin-right:0px; -qt-block-indent:0; text-indent:0px;&quot;&gt;&lt;span style=&quot; font-family:'MS Shell Dlg 2'; font-size:8pt;&quot;&gt;Currently this is just a proof-of-concept for developers:&lt;/span&gt;&lt;/p&gt;
+&lt;ol style=&quot;-qt-list-indent: 1;&quot;&gt;&lt;li style=&quot; margin-top:12px; margin-bottom:0px; margin-left:0px; margin-right:0px; -qt-block-indent:0; text-indent:0px;&quot;&gt;Load an existing gnucash XML file&lt;/li&gt;
+&lt;li style=&quot; margin-top:0px; margin-bottom:0px; margin-left:0px; margin-right:0px; -qt-block-indent:0; text-indent:0px;&quot;&gt;It will show the account tree in a QTreeView&lt;/li&gt;
+&lt;li style=&quot; margin-top:0px; margin-bottom:12px; margin-left:0px; margin-right:0px; -qt-block-indent:0; text-indent:0px;&quot;&gt;Additionally the accounts will be shown in a flat list in a QTableView&lt;/li&gt;&lt;/ol&gt;
+&lt;p style=&quot; margin-top:12px; margin-bottom:12px; margin-left:0px; margin-right:0px; -qt-block-indent:0; text-indent:0px;&quot;&gt;&lt;span style=&quot; font-family:'MS Shell Dlg 2'; font-size:8pt;&quot;&gt;The fun part is how easy it was to add this display of all accounts. And a QTableView with the splits of an account can't be far...&lt;/span&gt;&lt;/p&gt;
 &lt;p style=&quot; margin-top:16px; margin-bottom:12px; margin-left:0px; margin-right:0px; -qt-block-indent:0; text-indent:0px;&quot;&gt;&lt;span style=&quot; font-family:'MS Shell Dlg 2'; font-size:8pt; font-weight:600;&quot;&gt;Images&lt;/span&gt;&lt;/p&gt;
 &lt;p style=&quot; margin-top:12px; margin-bottom:12px; margin-left:0px; margin-right:0px; -qt-block-indent:0; text-indent:0px;&quot;&gt;&lt;img src=&quot;:/pixmaps/gnucash_splash.png&quot; /&gt;&lt;/p&gt;
 &lt;p style=&quot; margin-top:12px; margin-bottom:12px; margin-left:0px; margin-right:0px; -qt-block-indent:0; text-indent:0px;&quot;&gt;&lt;span style=&quot; font-family:'MS Shell Dlg 2'; font-size:8pt;&quot;&gt;We can also embed images here in this compiled-in HTML page, as you can see above. Eventually, this page should explain what this software can do for the new user. Maybe a &lt;/span&gt;&lt;a href=&quot;gnc:test&quot;&gt;&lt;span style=&quot; text-decoration: underline; color:#0000ff;&quot;&gt;hyperlink &lt;/span&gt;&lt;/a&gt;&lt;span style=&quot; font-family:'MS Shell Dlg 2'; font-size:8pt;&quot;&gt;here?&lt;/span&gt;&lt;/p&gt;&lt;/body&gt;&lt;/html&gt;</string>
@@ -65,7 +69,7 @@
         </item>
        </layout>
       </widget>
-      <widget class="QWidget" name="tab_2">
+      <widget class="QWidget" name="tableViewTab">
        <attribute name="title">
         <string>Account List</string>
        </attribute>
@@ -78,6 +82,19 @@
         </item>
        </layout>
       </widget>
+      <widget class="QWidget" name="treeViewTab">
+       <attribute name="title">
+        <string>Account Tree</string>
+       </attribute>
+       <layout class="QHBoxLayout" name="horizontalLayout_4">
+        <property name="margin">
+         <number>0</number>
+        </property>
+        <item>
+         <widget class="QTreeView" name="treeView"/>
+        </item>
+       </layout>
+      </widget>
      </widget>
     </item>
    </layout>



More information about the gnucash-changes mailing list