r18814 - gnucash/trunk/src/gnc - C++ experiment: Extend the AccountModel into a table with name and description.

Christian Stimming cstim at code.gnucash.org
Thu Mar 4 12:48:12 EST 2010


Author: cstim
Date: 2010-03-04 12:48:12 -0500 (Thu, 04 Mar 2010)
New Revision: 18814
Trac: http://svn.gnucash.org/trac/changeset/18814

Modified:
   gnucash/trunk/src/gnc/Account.hpp
   gnucash/trunk/src/gnc/AccountItemModel.hpp
   gnucash/trunk/src/gnc/Session.cpp
   gnucash/trunk/src/gnc/Session.hpp
   gnucash/trunk/src/gnc/mainwindow.cpp
   gnucash/trunk/src/gnc/mainwindow.ui
Log:
C++ experiment: Extend the AccountModel into a table with name and description.

Use QString everywhere as well.

Modified: gnucash/trunk/src/gnc/Account.hpp
===================================================================
--- gnucash/trunk/src/gnc/Account.hpp	2010-03-04 17:47:51 UTC (rev 18813)
+++ gnucash/trunk/src/gnc/Account.hpp	2010-03-04 17:48:12 UTC (rev 18814)
@@ -11,7 +11,8 @@
 
 #include "gnc/WeakPointer.hpp"
 
-#include <QAbstractItemModel>
+#include <QString>
+#include <QList>
 
 namespace gnc
 {
@@ -23,7 +24,9 @@
     Account(element_type* ptr = 0)
             : base_class(ptr)
     { }
-    std::string getName() const { return xaccAccountGetName(get()); }
+    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()); }
@@ -32,6 +35,18 @@
     GList * get_descendants () const { return gnc_account_get_descendants (get()); }
     Account nth_child (gint num) const { return gnc_account_nth_child(get(), num); }
 
+    typedef QList< ::Account*> AccountQList;
+    static AccountQList fromGList(GList* glist)
+    {
+        AccountQList result;
+        GList* list = glist;
+        while (list)
+        {
+            result.append(reinterpret_cast< ::Account*>(list->data));
+            list = g_list_next(list);
+        }
+        return result;
+    }
 };
 
 } // END namespace gnc

Modified: gnucash/trunk/src/gnc/AccountItemModel.hpp
===================================================================
--- gnucash/trunk/src/gnc/AccountItemModel.hpp	2010-03-04 17:47:51 UTC (rev 18813)
+++ gnucash/trunk/src/gnc/AccountItemModel.hpp	2010-03-04 17:48:12 UTC (rev 18814)
@@ -4,28 +4,24 @@
 #include "gnc/Account.hpp"
 
 #include <QAbstractItemModel>
-#include <QAbstractListModel>
+#include <QAbstractTableModel>
 
 namespace gnc
 {
 
-class AccountItemModel : public QAbstractListModel
+class AccountItemModel : public QAbstractTableModel
 {
     Q_OBJECT
 public:
     AccountItemModel(Account rootaccount, QObject *parent = 0)
-            : QAbstractListModel(parent)
+            : QAbstractTableModel(parent)
             , m_root(rootaccount)
+            , m_acclist(Account::fromGList(m_root.get_descendants()))
     {
-        GList* list = m_root.get_descendants();
-        while (list)
-        {
-            m_acclist.append(reinterpret_cast< ::Account*>(list->data));
-            list = g_list_next(list);
-        }
     }
 
     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())
@@ -33,7 +29,19 @@
         if (index.row() > rowCount(index))
             return QVariant();
         if (role == Qt::DisplayRole)
-            return QString::fromStdString(Account(m_acclist.at(index.row())).getName());
+        {
+            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();
     }
@@ -42,13 +50,25 @@
         if (role != Qt::DisplayRole)
             return QVariant();
         if (orientation == Qt::Horizontal)
-            return QString("Account Name");
+        {
+            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);
+            return QString("%1").arg(1 + section);
     }
 private:
     Account m_root;
-    QList< ::Account*> m_acclist;
+    Account::AccountQList m_acclist;
 };
 
 } // END namespace gnc

Modified: gnucash/trunk/src/gnc/Session.cpp
===================================================================
--- gnucash/trunk/src/gnc/Session.cpp	2010-03-04 17:47:51 UTC (rev 18813)
+++ gnucash/trunk/src/gnc/Session.cpp	2010-03-04 17:48:12 UTC (rev 18814)
@@ -16,9 +16,9 @@
 }
 
 
-#define TYPE_TO_STR(tstr, desc) tstr : return std::make_pair<std::string,std::string>(#tstr, desc)
+#define TYPE_TO_STR(tstr, desc) tstr : return std::make_pair<QString,QString>(QString::fromUtf8(#tstr), QString::fromUtf8(desc))
 
-std::pair<std::string, std::string> errorToStringPair(QofBackendError err)
+std::pair<QString, QString> errorToStringPair(QofBackendError err)
 {
     switch (err)
     {

Modified: gnucash/trunk/src/gnc/Session.hpp
===================================================================
--- gnucash/trunk/src/gnc/Session.hpp	2010-03-04 17:47:51 UTC (rev 18813)
+++ gnucash/trunk/src/gnc/Session.hpp	2010-03-04 17:48:12 UTC (rev 18814)
@@ -12,7 +12,7 @@
 
 #include "gnc/ScopedPointer.hpp"
 #include <boost/noncopyable.hpp>
-#include <string>
+#include <QString>
 
 namespace gnc
 {
@@ -62,20 +62,20 @@
     {
         return qof_session_pop_error(get());
     }
-    std::string get_error_message() const
+    QString get_error_message() const
     {
-        return qof_session_get_error_message(get());
+        return QString::fromUtf8(qof_session_get_error_message(get()));
     }
     Book get_book () const;
 
-    std::string get_file_path () const
+    QString get_file_path () const
     {
-        return qof_session_get_file_path(get());
+        return QString::fromUtf8(qof_session_get_file_path(get()));
     }
 
-    std::string get_url() const
+    QString get_url() const
     {
-        return qof_session_get_url(get());
+        return QString::fromUtf8(qof_session_get_url(get()));
     }
 
     bool save_in_progress() const
@@ -100,7 +100,7 @@
 
 };
 
-std::pair<std::string, std::string> errorToStringPair(QofBackendError err);
+std::pair<QString, QString> errorToStringPair(QofBackendError err);
 
 } // END namespace gnc
 

Modified: gnucash/trunk/src/gnc/mainwindow.cpp
===================================================================
--- gnucash/trunk/src/gnc/mainwindow.cpp	2010-03-04 17:47:51 UTC (rev 18813)
+++ gnucash/trunk/src/gnc/mainwindow.cpp	2010-03-04 17:48:12 UTC (rev 18814)
@@ -28,11 +28,11 @@
 
 inline QString errorToString(QofBackendError err)
 {
-    return QString::fromStdString(errorToStringPair(err).first);
+    return errorToStringPair(err).first;
 }
 inline QString errorToDescription(QofBackendError err)
 {
-    return QString::fromStdString(errorToStringPair(err).second);
+    return errorToStringPair(err).second;
 }
 
 /* This static indicates the debugging module that this .o belongs to.  */

Modified: gnucash/trunk/src/gnc/mainwindow.ui
===================================================================
--- gnucash/trunk/src/gnc/mainwindow.ui	2010-03-04 17:47:51 UTC (rev 18813)
+++ gnucash/trunk/src/gnc/mainwindow.ui	2010-03-04 17:48:12 UTC (rev 18814)
@@ -63,7 +63,7 @@
       </widget>
       <widget class="QWidget" name="tab_2">
        <attribute name="title">
-        <string>Account Name List View</string>
+        <string>Account List</string>
        </attribute>
        <layout class="QHBoxLayout" name="horizontalLayout_3">
         <property name="margin">



More information about the gnucash-changes mailing list