C++ experiment: Extend the AccountModel into a table with name and description.

Use QString everywhere as well.

git-svn-id: svn+ssh://svn.gnucash.org/repo/gnucash/trunk@18814 57a11ea4-9604-0410-9ed3-97b8803252fd
This commit is contained in:
Christian Stimming 2010-03-04 17:48:12 +00:00
parent 4708f169ea
commit 6087cfd688
6 changed files with 63 additions and 28 deletions

View File

@ -11,7 +11,8 @@ extern "C"
#include "gnc/WeakPointer.hpp"
#include <QAbstractItemModel>
#include <QString>
#include <QList>
namespace gnc
{
@ -23,7 +24,9 @@ public:
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 @@ public:
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

View File

@ -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 @@ public:
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 @@ public:
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

View File

@ -16,9 +16,9 @@ Book Session::get_book () const
}
#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)
{

View File

@ -12,7 +12,7 @@ extern "C"
#include "gnc/ScopedPointer.hpp"
#include <boost/noncopyable.hpp>
#include <string>
#include <QString>
namespace gnc
{
@ -62,20 +62,20 @@ public:
{
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 @@ public:
};
std::pair<std::string, std::string> errorToStringPair(QofBackendError err);
std::pair<QString, QString> errorToStringPair(QofBackendError err);
} // END namespace gnc

View File

@ -28,11 +28,11 @@ namespace gnc
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. */

View File

@ -63,7 +63,7 @@ p, li { white-space: pre-wrap; }
</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">