Skip to content

Commit

Permalink
C++ experiment: Add first simple model/view widget for the loaded acc…
Browse files Browse the repository at this point in the history
…ount list.

git-svn-id: svn+ssh://svn.gnucash.org/repo/gnucash/trunk@18804 57a11ea4-9604-0410-9ed3-97b8803252fd
  • Loading branch information
cstim committed Mar 3, 2010
1 parent 3b41838 commit dbb5d0d
Show file tree
Hide file tree
Showing 7 changed files with 110 additions and 21 deletions.
12 changes: 12 additions & 0 deletions src/gnc/Account.hpp
Expand Up @@ -2,6 +2,7 @@
#define GNC_ACCOUNT_HPP

// gnucash includes
#include "config.h"
extern "C"
{
#include "qof.h"
Expand All @@ -10,6 +11,8 @@ extern "C"

#include "gnc/WeakPointer.hpp"

#include <QAbstractItemModel>

namespace gnc
{

Expand All @@ -20,6 +23,15 @@ class Account : public WeakPointer< ::Account >
Account(element_type* ptr = 0)
: base_class(ptr)
{ }
std::string getName() const { return xaccAccountGetName(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()); }
Account nth_child (gint num) const { return gnc_account_nth_child(get(), num); }

};

} // END namespace gnc
Expand Down
56 changes: 56 additions & 0 deletions src/gnc/AccountItemModel.hpp
@@ -0,0 +1,56 @@
#ifndef GNC_ACCOUNTITEMMODEL_HPP
#define GNC_ACCOUNTITEMMODEL_HPP

#include "gnc/Account.hpp"

#include <QAbstractItemModel>
#include <QAbstractListModel>

namespace gnc
{

class AccountItemModel : public QAbstractListModel
{
Q_OBJECT
public:
AccountItemModel(Account rootaccount, QObject *parent = 0)
: QAbstractListModel(parent)
, m_root(rootaccount)
{
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(); }
QVariant data(const QModelIndex& index, int role) const
{
if (!index.isValid())
return QVariant();
if (index.row() > rowCount(index))
return QVariant();
if (role == Qt::DisplayRole)
return QString::fromStdString(Account(m_acclist.at(index.row())).getName());
else
return QVariant();
}
QVariant headerData(int section, Qt::Orientation orientation, int role) const
{
if (role != Qt::DisplayRole)
return QVariant();
if (orientation == Qt::Horizontal)
return QString("Account Name");
else
return QString("#%1").arg(1 + section);
}
private:
Account m_root;
QList< ::Account*> m_acclist;
};

} // END namespace gnc

#endif
1 change: 1 addition & 0 deletions src/gnc/CMakeLists.txt
Expand Up @@ -17,6 +17,7 @@ SET (gnc_SOURCES
)

SET (gnc_QOBJECT_HEADERS
AccountItemModel.hpp
mainwindow.hpp
)
SET (gnc_HEADERS ${gnc_QOBJECT_HEADERS}
Expand Down
2 changes: 2 additions & 0 deletions src/gnc/WeakPointer.hpp
@@ -1,6 +1,8 @@
#ifndef GNC_WEAKPOINTER_HPP
#define GNC_WEAKPOINTER_HPP

#include <string>

namespace gnc
{

Expand Down
54 changes: 35 additions & 19 deletions src/gnc/mainwindow.cpp
@@ -1,7 +1,8 @@
#include <QtGui/QToolBar>
#include <QtGui/QMessageBox>
#include <QtGui/QFileDialog>
#include <QtCore/QSettings>
#include <QtGui/QCloseEvent>
#include <QtGui/QFileDialog>
#include <QtGui/QMessageBox>
#include <QtGui/QToolBar>

#include "config.h"
#include "mainwindow.hpp"
Expand All @@ -19,6 +20,7 @@ extern "C"
}

#include "gnc/Account.hpp"
#include "gnc/AccountItemModel.hpp"
#include "gnc/Book.hpp"

namespace gnc
Expand All @@ -38,6 +40,7 @@ static QofLogModule log_module = GNC_MOD_GUI;

MainWindow::MainWindow()
: ui(new Ui::MainWindow)
, m_accountItemModel(NULL)
{
ui->setupUi(this);

Expand All @@ -47,8 +50,8 @@ MainWindow::MainWindow()

readSettings();

connect(ui->textEdit->document(), SIGNAL(contentsChanged()),
this, SLOT(documentWasModified()));
// connect(ui->labelMain, SIGNAL(linkActivated(const QString&)),
// this, SLOT(documentWasModified()));

setWindowIcon(QIcon(":/pixmaps/gnucash-icon-32x32.png"));

Expand Down Expand Up @@ -101,7 +104,7 @@ void MainWindow::about()

void MainWindow::documentWasModified()
{
setWindowModified(ui->textEdit->document()->isModified());
// setWindowModified(ui->textEdit->document()->isModified());
}

void MainWindow::createActions()
Expand All @@ -118,19 +121,19 @@ void MainWindow::createActions()
connect(ui->actionSave_as, SIGNAL(triggered()), this, SLOT(saveAs()));
connect(ui->actionExit, SIGNAL(triggered()), this, SLOT(close()));

connect(ui->actionCut, SIGNAL(triggered()), ui->textEdit, SLOT(cut()));
connect(ui->actionCopy, SIGNAL(triggered()), ui->textEdit, SLOT(copy()));
connect(ui->actionPaste, SIGNAL(triggered()), ui->textEdit, SLOT(paste()));
// connect(ui->actionCut, SIGNAL(triggered()), ui->textEdit, SLOT(cut()));
// connect(ui->actionCopy, SIGNAL(triggered()), ui->textEdit, SLOT(copy()));
// connect(ui->actionPaste, SIGNAL(triggered()), ui->textEdit, SLOT(paste()));

connect(ui->actionAbout, SIGNAL(triggered()), this, SLOT(about()));
connect(ui->actionAbout_Qt, SIGNAL(triggered()), qApp, SLOT(aboutQt()));

ui->actionCut->setEnabled(false);
ui->actionCopy->setEnabled(false);
connect(ui->textEdit, SIGNAL(copyAvailable(bool)),
ui->actionCut, SLOT(setEnabled(bool)));
connect(ui->textEdit, SIGNAL(copyAvailable(bool)),
ui->actionCopy, SLOT(setEnabled(bool))); // why doesn't this work?!?
// connect(ui->textEdit, SIGNAL(copyAvailable(bool)),
// ui->actionCut, SLOT(setEnabled(bool)));
// connect(ui->textEdit, SIGNAL(copyAvailable(bool)),
// ui->actionCopy, SLOT(setEnabled(bool)));
}

void MainWindow::createToolBars()
Expand Down Expand Up @@ -169,7 +172,7 @@ void MainWindow::writeSettings()

bool MainWindow::maybeSave()
{
if (ui->textEdit->document()->isModified())
if (false)//ui->textEdit->document()->isModified())
{
QMessageBox::StandardButton ret;
ret = QMessageBox::warning(this, tr("Application"),
Expand All @@ -187,7 +190,7 @@ bool MainWindow::maybeSave()
void MainWindow::setCurrentFile(const QString &fileName)
{
curFile = fileName;
ui->textEdit->document()->setModified(false);
// ui->textEdit->document()->setModified(false);
setWindowModified(false);

QString shownName;
Expand Down Expand Up @@ -393,15 +396,28 @@ void MainWindow::loadFile(const QString &fileName)
/* close up the old file session (if any) */
m_session.reset(new_session);

::Account * new_root = m_session.get_book().get_root_account().get();
if (we_are_in_error)
new_root = NULL;

qof_event_resume ();

/* Call this after re-enabling events. */
gnc_book_opened (m_session);

// ////////////////////////////////////////////////////////////
// Some display about this file

Account root (m_session.get_book().get_root_account());
if (root)
{
m_accountItemModel = new AccountItemModel(root, this);
ui->tableView->setModel(m_accountItemModel);
ui->tabWidget->setCurrentIndex(1); //setCurrentWidget(ui->tableView);
}
else
{
//ui->labelMain->setText(tr("No root account"));
}

// ////////////////////////////////////////////////////////////

QApplication::restoreOverrideCursor();

setCurrentFile(fileName);
Expand Down
2 changes: 2 additions & 0 deletions src/gnc/mainwindow.hpp
Expand Up @@ -4,6 +4,7 @@
#include <QMainWindow>
#include <QSharedPointer>
#include "gnc/Session.hpp"
#include "gnc/AccountItemModel.hpp"

class QAction;
class QMenu;
Expand Down Expand Up @@ -58,6 +59,7 @@ private slots:
QToolBar *editToolBar;

Session m_session;
AccountItemModel *m_accountItemModel;
};

} // END namespace gnc
Expand Down
4 changes: 2 additions & 2 deletions src/gnc/mainwindow.ui
Expand Up @@ -63,14 +63,14 @@ p, li { white-space: pre-wrap; }
</widget>
<widget class="QWidget" name="tab_2">
<attribute name="title">
<string>Example Text Editor</string>
<string>Account Name List View</string>
</attribute>
<layout class="QHBoxLayout" name="horizontalLayout_3">
<property name="margin">
<number>0</number>
</property>
<item>
<widget class="QPlainTextEdit" name="textEdit"/>
<widget class="QTableView" name="tableView"/>
</item>
</layout>
</widget>
Expand Down

0 comments on commit dbb5d0d

Please sign in to comment.