Add FormulaDelegate

This commit is contained in:
Grégory Soutadé 2011-10-20 17:29:22 +02:00
parent 5bea328b8d
commit aa47adb791
5 changed files with 167 additions and 23 deletions

View File

@ -24,6 +24,7 @@
#include <QGroupBox>
#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(""));

View File

@ -31,8 +31,10 @@ void FloatDelegate::setModelData(QWidget *editor, QAbstractItemModel *model,
{
QLineEdit *line = qobject_cast<QLineEdit *>(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,

View File

@ -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 <http://www.gnu.org/licenses/>.
*/
#include <QtGui>
#include <QString>
#include "FormulaDelegate.hpp"
#include <ParseExp.hpp>
#include "../wxUI.hpp"
#include <iostream>
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<QLineEdit *>(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<FormulaDelegate*>(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<QLineEdit *>(editor);
QString s = qVariantValue<QString>(index.data());
if (_formula.startsWith("="))
line->setText(_formula);
else
line->setText(s);
}

View File

@ -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 <http://www.gnu.org/licenses/>.
*/
#ifndef FORMULADELEGATE_H
#define FORMULADELEGATE_H
#include <QItemDelegate>
#include <QString>
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

View File

@ -17,6 +17,7 @@
along with KissCount. If not, see <http://www.gnu.org/licenses/>.
*/
#include <QtGui>
#include <QHeaderView>
#include <QComboBox>
#include <QCheckBox>
@ -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<NUMBER_COLS_OPS; i++) \
{ \
@ -49,8 +51,6 @@ GridAccount::GridAccount(KissCount* kiss, QWidget *parent,
_databaseSynchronization(synchronizeWithDatabase), _loadOperations(false),
_curMonth(0), _curYear(0)
{
//wxBitmap deleteBitmap(wxT(DELETE_ICON), wxBITMAP_TYPE_PNG);
//wxBitmap checkedBitmap(wxT(CHECKED_ICON), wxBITMAP_TYPE_PNG);
//DEFAULT_FONT(font);
QFont font;
int i;
@ -58,6 +58,7 @@ GridAccount::GridAccount(KissCount* kiss, QWidget *parent,
std::vector<Account>::iterator accountIt;
std::vector<Category>::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<Operation>* 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)
{