From aa47adb7916923d70276af208c38f2ac79ff35e9 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Gr=C3=A9gory=20Soutad=C3=A9?= Date: Thu, 20 Oct 2011 17:29:22 +0200 Subject: [PATCH] Add FormulaDelegate --- src/view/AccountPanel.cpp | 8 ++- src/view/grid/FloatDelegate.cpp | 6 +- src/view/grid/FormulaDelegate.cpp | 93 +++++++++++++++++++++++++++++++ src/view/grid/FormulaDelegate.hpp | 44 +++++++++++++++ src/view/grid/GridAccount.cpp | 39 +++++++------ 5 files changed, 167 insertions(+), 23 deletions(-) create mode 100644 src/view/grid/FormulaDelegate.cpp create mode 100644 src/view/grid/FormulaDelegate.hpp diff --git a/src/view/AccountPanel.cpp b/src/view/AccountPanel.cpp index d393de9..431587f 100644 --- a/src/view/AccountPanel.cpp +++ b/src/view/AccountPanel.cpp @@ -24,6 +24,7 @@ #include #include "AccountPanel.hpp" +#include "grid/FloatDelegate.hpp" enum {ACCOUNT_NUMBER, ACCOUNT_NAME, ACCOUNT_INIT, ACCOUNT_CUR, ACCOUNT_FINAL, NUMBER_COLS_ACCOUNTS}; enum {CUR_CREDIT, CUR_DEBIT, TOTAL_CREDIT, TOTAL_DEBIT, BALANCE, STATS_ROW, CATS_STATS, NON_FIX}; @@ -441,8 +442,11 @@ void AccountPanel::InitAccountsGrid(User* user, int month, int year) if (_accountsGrid->rowCount()) _accountsGrid->clear(); - _accountsGrid->setRowCount(user->_accounts.size()); + FloatDelegate* floatEditor = new FloatDelegate(_accountsGrid); + _accountsGrid->setItemDelegateForColumn(ACCOUNT_INIT, floatEditor); + _accountsGrid->setRowCount(user->_accounts.size()); + for (i=0, it = user->_accounts.begin(); it != user->_accounts.end(); i++, it++, curLine++) { if (it->shared) @@ -452,8 +456,6 @@ void AccountPanel::InitAccountsGrid(User* user, int month, int year) _accountsGrid->setItem(curLine, ACCOUNT_NAME, new QTableWidgetItem(it->name)); value = _kiss->GetAccountAmount(it->id, month, year); - // _accountsGrid->SetCellEditor(curLine, ACCOUNT_INIT, new wxGridCellFloatEditor(-1, 2)); - // _accountsGrid->setItem(curLine, ACCOUNT_INIT, wxString::Format(wxT("%.2lf"), value)); _accountsGrid->setItem(curLine, ACCOUNT_INIT, new QTableWidgetItem(v.sprintf("%.2lf", value))); _accountsGrid->setItem(curLine, ACCOUNT_CUR, new QTableWidgetItem("")); diff --git a/src/view/grid/FloatDelegate.cpp b/src/view/grid/FloatDelegate.cpp index e4b1cd9..76e49cb 100644 --- a/src/view/grid/FloatDelegate.cpp +++ b/src/view/grid/FloatDelegate.cpp @@ -31,8 +31,10 @@ void FloatDelegate::setModelData(QWidget *editor, QAbstractItemModel *model, { QLineEdit *line = qobject_cast(editor); QString s ; - double value = line->text().toDouble(); - model->setData(index, qVariantFromValue(s.sprintf("%.2lf", value))); + bool ok; + double value = line->text().toDouble(&ok); + if (ok) + model->setData(index, qVariantFromValue(s.sprintf("%.2lf", value))); } void FloatDelegate::setEditorData(QWidget *editor, diff --git a/src/view/grid/FormulaDelegate.cpp b/src/view/grid/FormulaDelegate.cpp new file mode 100644 index 0000000..84ae1df --- /dev/null +++ b/src/view/grid/FormulaDelegate.cpp @@ -0,0 +1,93 @@ +/* + Copyright 2010-2011 Grégory Soutadé + + This file is part of KissCount. + + KissCount 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 3 of the License, or + (at your option) any later version. + + KissCount 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 KissCount. If not, see . +*/ + +#include +#include + +#include "FormulaDelegate.hpp" +#include +#include "../wxUI.hpp" +#include + +QWidget * FormulaDelegate::createEditor (QWidget * parent, const QStyleOptionViewItem & option, const QModelIndex & index ) const +{ + return new QLineEdit(parent); +} + +void FormulaDelegate::newFormula(QString f) +{ + _formula = f; +} + +void FormulaDelegate::setModelData(QWidget *editor, QAbstractItemModel *model, + const QModelIndex &index) const +{ + QLineEdit *line = qobject_cast(editor); + QString s ; + double res = 0.0; + QString value = line->text(); + struct ParseExp::parse_opt opt, *r; + const char* c; + char* str, *str2; + bool ok; + + value = value.trimmed(); + if (value.startsWith("=")) + { + value = value.replace(",", "."); + c = value.toStdString().c_str(); + str2 = str = new char[strlen(c)]; + strcpy(str, c+1); + memset(&opt, 0, sizeof(opt)); + r = &opt; + try { + ParseExp::ParseExp(&str, r, false); + } + catch(...) + { + QMessageBox::critical(0, _("Error"), _("Invalid formula !")); + delete[] str2; + return; + } + + const_cast(this)->newFormula(value); // hum hum... + delete[] str2; + res = ParseExp::EvaluateExpr(&opt, true); + model->setData(index, qVariantFromValue(s.sprintf("%.2lf", res))); + } + else + { + res = value.toDouble(&ok); + if (ok) + model->setData(index, qVariantFromValue(s.sprintf("%.2lf", res))); + } + +} + +void FormulaDelegate::setEditorData(QWidget *editor, + const QModelIndex &index) const +{ + QLineEdit* line = qobject_cast(editor); + QString s = qVariantValue(index.data()); + + if (_formula.startsWith("=")) + line->setText(_formula); + else + line->setText(s); +} diff --git a/src/view/grid/FormulaDelegate.hpp b/src/view/grid/FormulaDelegate.hpp new file mode 100644 index 0000000..23834b7 --- /dev/null +++ b/src/view/grid/FormulaDelegate.hpp @@ -0,0 +1,44 @@ +/* + Copyright 2010-2011 Grégory Soutadé + + This file is part of KissCount. + + KissCount 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 3 of the License, or + (at your option) any later version. + + KissCount 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 KissCount. If not, see . +*/ + +#ifndef FORMULADELEGATE_H +#define FORMULADELEGATE_H + +#include +#include + +class FormulaDelegate : public QItemDelegate +{ + Q_OBJECT; + +public: + FormulaDelegate(QWidget *parent = 0) : QItemDelegate(parent) {} + + QWidget * createEditor ( QWidget * parent, const QStyleOptionViewItem & option, const QModelIndex & index ) const; + void setEditorData(QWidget *editor, const QModelIndex &index) const; + void setModelData(QWidget *editor, QAbstractItemModel *model, + const QModelIndex &index) const; + +private: + void newFormula(QString f); + + QString _formula; +}; + +#endif diff --git a/src/view/grid/GridAccount.cpp b/src/view/grid/GridAccount.cpp index b04e345..2043263 100644 --- a/src/view/grid/GridAccount.cpp +++ b/src/view/grid/GridAccount.cpp @@ -17,6 +17,7 @@ along with KissCount. If not, see . */ +#include #include #include #include @@ -25,6 +26,7 @@ #include "ChoiceDelegate.hpp" #include "DateDelegate.hpp" #include "FloatDelegate.hpp" +#include "FormulaDelegate.hpp" #define SET_ROW_COLOR(row, backcolor, forecolor) for(int i=0; i::iterator accountIt; std::vector::iterator categoryIt; QTableWidgetItem* item; + QLabel* label; setColumnCount(NUMBER_COLS_OPS); setRowCount(1); @@ -80,8 +81,15 @@ GridAccount::GridAccount(KissCount* kiss, QWidget *parent, item->setTextAlignment(Qt::AlignHCenter|Qt::AlignVCenter); setItem(0, i, item); } - //SetCellRenderer(0, OP_DELETE, new wxGridCellBitmapRenderer(deleteBitmap)); - //SetCellRenderer(0, CHECKED, new wxGridCellBitmapRenderer(checkedBitmap)); + label = new QLabel(); + label->setPixmap(QPixmap(DELETE_ICON)); + label->setAlignment(Qt::AlignHCenter | Qt::AlignVCenter); + setCellWidget(0, OP_DELETE, label); + + label = new QLabel(); + label->setPixmap(QPixmap(CHECKED_ICON)); + label->setAlignment(Qt::AlignHCenter | Qt::AlignVCenter); + setCellWidget(0, CHECKED, label); _accounts = new QString[user->GetAccountsNumber()]; for (i=0, @@ -213,9 +221,13 @@ void GridAccount::LoadOperations(std::vector* operations, int month, DateDelegate* dateEditor = new DateDelegate(this, month+1, year); setItemDelegateForColumn(OP_DATE, dateEditor); - FloatDelegate* floatEditor = new FloatDelegate(this); - setItemDelegateForColumn(DEBIT, floatEditor); - setItemDelegateForColumn(CREDIT, floatEditor); + // FloatDelegate* floatEditor = new FloatDelegate(this); + // setItemDelegateForColumn(DEBIT, floatEditor); + // setItemDelegateForColumn(CREDIT, floatEditor); + + FormulaDelegate* formulaEditor = new FormulaDelegate(this); + setItemDelegateForColumn(DEBIT, formulaEditor); + setItemDelegateForColumn(CREDIT, formulaEditor); if (_canAddOperation) { @@ -320,16 +332,13 @@ void GridAccount::InsertOperation(User* user, Operation& op, int line, bool fix, QCheckBox* checkBox; // // First is header - // if (op) - _displayedOperations.insert(_displayedOperations.begin()+line, op); + // if (op.id) + _displayedOperations.insert(_displayedOperations.begin()+line, op); if (!user->_accounts.size()) return; insertRow(line); - // SetCellEditor(line, DEBIT, new wxGridCellFloatEditor(wxID_ANY, 2)); - // SetCellEditor(line, CREDIT, new wxGridCellFloatEditor(wxID_ANY, 2)); - if (fix) { item = new QTableWidgetItem(_("Fix")); @@ -345,7 +354,6 @@ void GridAccount::InsertOperation(User* user, Operation& op, int line, bool fix, { cat = user->GetCategory(op.category); - //SetCellEditor(line, OP_DATE, new CalendarEditor(op.day, month, year)); description = op.description; UNESCAPE_CHARS(description); setItem(line, DESCRIPTION, new QTableWidgetItem(description)); @@ -406,17 +414,12 @@ void GridAccount::InsertOperation(User* user, Operation& op, int line, bool fix, } else { - //SetCellEditor(line, OP_DATE, new CalendarEditor(0, month, year)); if (curDate.month() == month && curDate.year() == year) { if (!fix) setItem(line, OP_DATE, new QTableWidgetItem(v.sprintf("%02d/%02d/%d", curDate.day(), month+1, year))); - //SetCellEditor(line, OP_DATE, new CalendarEditor(curDate.GetDay()-1, month, year)); } - else - ; - //SetCellEditor(line, OP_DATE, new CalendarEditor(0, month, year)); if (fix) {