From c5cec6c9d67c0121ccef3707c98eb9b70adca15d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Gr=C3=A9gory=20Soutad=C3=A9?= Date: Sat, 22 Oct 2011 10:55:34 +0200 Subject: [PATCH] Add meta support in GridAccount Add TabDelegate --- src/view/grid/GridAccount.cpp | 165 ++++++++++++++++++---------------- src/view/grid/GridAccount.hpp | 3 + src/view/grid/TabDelegate.cpp | 49 ++++++++++ src/view/grid/TabDelegate.hpp | 43 +++++++++ 4 files changed, 185 insertions(+), 75 deletions(-) create mode 100644 src/view/grid/TabDelegate.cpp create mode 100644 src/view/grid/TabDelegate.hpp diff --git a/src/view/grid/GridAccount.cpp b/src/view/grid/GridAccount.cpp index a5c4a1b..9dabd6b 100644 --- a/src/view/grid/GridAccount.cpp +++ b/src/view/grid/GridAccount.cpp @@ -27,6 +27,7 @@ #include "DateDelegate.hpp" #include "FloatDelegate.hpp" #include "FormulaDelegate.hpp" +#include "TabDelegate.hpp" #define SET_ROW_COLOR(row, backcolor, forecolor) for(int i=0; i* operations, int month, if (rowCount() > 1) clear(); + TabDelegate* descriptionEditor = new TabDelegate(this, &_displayedOperations); + setItemDelegateForColumn(DESCRIPTION, descriptionEditor); + ChoiceDelegate* categoryEditor = new ChoiceDelegate(this, _categories, user->GetCategoriesNumber()); setItemDelegateForColumn(CATEGORY, categoryEditor); ChoiceDelegate* accountEditor = new ChoiceDelegate(this, _accounts, user->GetAccountsNumber()); @@ -353,7 +359,10 @@ void GridAccount::InsertOperation(User* user, Operation& op, int line, bool fix, description = op.description; UNESCAPE_CHARS(description); - setItem(line, DESCRIPTION, new QTableWidgetItem(description)); + if (op.parent) + setItem(line, DESCRIPTION, new QTableWidgetItem(" " + description)); + else + setItem(line, DESCRIPTION, new QTableWidgetItem(description)); item = new QTableWidgetItem(); setItem(line, OP_DATE, new QTableWidgetItem(v.sprintf("%02d/%02d/%d", op.day+1, month+1, year))); if (op.amount < 0) @@ -443,6 +452,9 @@ void GridAccount::InsertOperation(User* user, Operation& op, int line, bool fix, { int height = rowHeight(TREE); QPushButton* button = new QPushButton("+"); + _signalMapper.setMapping(button, op.id); + connect(button, SIGNAL(clicked()), &_signalMapper, SLOT(map())); + //button->setSizePolicy(QSizePolicy::Minimum, QSizePolicy::Minimum); //button->resize(button->minimumSize()); button->setMaximumSize(QSize(height, height)); @@ -543,57 +555,54 @@ void GridAccount::InsertIntoGrid(Operation& op) int GridAccount::RemoveMeta(Operation op, int line, bool removeRoot, bool deleteOp) { - // std::vector::iterator it, it2; - // wxGridCellTreeButtonRenderer* treeRenderer; - // int i, deletedOperations = 0; - // Operation op2; - - // treeRenderer = (wxGridCellTreeButtonRenderer*) GetCellRenderer(line, TREE); + std::vector::iterator it, it2; + int i, deletedOperations = 0; + Operation op2; + QPushButton* button = qobject_cast (cellWidget(line, TREE)); - // for(i=0; i<(int)op.childs.size(); i++) - // { - // op2 = GetOperation(op.childs[i]); - // if (op2.meta) - // RemoveMeta(op2, line+1, true, deleteOp); - // else - // { - // if (treeRenderer->IsCollapsed()) - // { - // DeleteRows(line+1, 1); - // deletedOperations++; - // if (op2.fix_cost) _fixCosts--; - // _displayedOperations.erase(_displayedOperations.begin()+line+1); - // } + for(i=0; i<(int)op.childs.size(); i++) + { + op2 = GetOperation(op.childs[i]); + if (op2.meta) + RemoveMeta(op2, line+1, true, deleteOp); + else + { + if (button->text() == "-") + { + removeRow(line+1); + deletedOperations++; + if (op2.fix_cost) _fixCosts--; + _displayedOperations.erase(_displayedOperations.begin()+line+1); + } - // if (deleteOp) - // { - // DeleteOperation(op2); - // if (_databaseSynchronization) - // _kiss->DeleteOperation(op2); - // } - // } - // } + if (deleteOp) + { + DeleteOperation(op2); + if (_databaseSynchronization) + _kiss->DeleteOperation(op2); + } + } + } - // op.childs.clear(); + op.childs.clear(); - // if (removeRoot) - // { - // DeleteRows(line, 1); - // _displayedOperations.erase(_displayedOperations.begin()+line); - // if (op.fix_cost) _fixCosts--; - // if (deleteOp) - // { - // DeleteOperation(op); - // if (_databaseSynchronization) - // _kiss->DeleteOperation(op); - // } - // deletedOperations++; - // } + if (removeRoot) + { + removeRow(line); + button->disconnect(&_signalMapper, SLOT(map())); + removeCellWidget(line, TREE); + _displayedOperations.erase(_displayedOperations.begin()+line); + if (op.fix_cost) _fixCosts--; + if (deleteOp) + { + DeleteOperation(op); + if (_databaseSynchronization) + _kiss->DeleteOperation(op); + } + deletedOperations++; + } - // treeRenderer->DecRef(); - - // return deletedOperations; - return 0; + return deletedOperations; } void GridAccount::CheckMeta(Operation& op, int line, bool check) @@ -637,6 +646,39 @@ void GridAccount::CheckMeta(Operation& op, int line, bool check) // treeRenderer->DecRef(); } +void GridAccount::OnMetaClicked(int id) +{ + QPushButton* button = qobject_cast (_signalMapper.mapping(id)); + std::vector::iterator it; + std::vector::iterator it2; + int i, row; + Operation op, op2; + User* user = _kiss->GetUser(); + + it = std::find(_displayedOperations.begin(), _displayedOperations.end(), id); + + if (it == _displayedOperations.end()) return ; + + op = *it; + + row = it - _displayedOperations.begin(); + + if (button->text() == "+") + { + for (i=1, it2=op.childs.begin(); it2!=op.childs.end(); it2++, i++) + { + op2 = GetOperation(*it2); + InsertOperationWithWeek(user, op2, row+i, op2.fix_cost, op2.month, op2.year); + } + button->setText("-"); + } + else + { + RemoveMeta(op, row, false, false); + button->setText("+"); + } +} + void GridAccount::OnOperationModified() { // User* user = _kiss->GetUser(); @@ -662,33 +704,6 @@ void GridAccount::OnOperationModified() // inModification = true ; - // if (col == TREE) - // { - // treeRenderer = (wxGridCellTreeButtonRenderer*) GetCellRenderer(row, col); - - // op = _displayedOperations[row]; - - // if (!treeRenderer->IsCollapsed()) - // { - // for (i=1, it=op.childs.begin(); it!=op.childs.end(); it++, i++) - // { - // op2 = GetOperation(*it); - // InsertOperationWithWeek(user, op2, row+i, op2.fix_cost, op2.month, op2.year); - // } - // } - // else - // { - // RemoveMeta(op, row, false, false); - // } - - // treeRenderer->DecRef(); - - // ComputeWeeks(); - // inModification = false; - // _parent->Fit(); - // return; - // } - // if (col == DEBIT) // SetCellValue(row, CREDIT, ""); // else if (col == CREDIT) diff --git a/src/view/grid/GridAccount.hpp b/src/view/grid/GridAccount.hpp index 297f3a0..19ac670 100644 --- a/src/view/grid/GridAccount.hpp +++ b/src/view/grid/GridAccount.hpp @@ -36,6 +36,7 @@ // #include "wxGridCellTabStringRenderer.hpp" #include +#include class KissCount; @@ -75,6 +76,7 @@ public: private slots: void OnOperationModified(); + void OnMetaClicked(int id); private: QWidget* _parent; @@ -86,6 +88,7 @@ private: std::vector* _operations; bool _loadOperations; int _curMonth, _curYear; + QSignalMapper _signalMapper; void SetWeek(int week, int line); void ResetWeeks(); diff --git a/src/view/grid/TabDelegate.cpp b/src/view/grid/TabDelegate.cpp new file mode 100644 index 0000000..29d55e8 --- /dev/null +++ b/src/view/grid/TabDelegate.cpp @@ -0,0 +1,49 @@ +/* + 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 "TabDelegate.hpp" + +QWidget * TabDelegate::createEditor (QWidget * parent, const QStyleOptionViewItem & option, const QModelIndex & index ) const +{ + return new QLineEdit(parent); +} + +void TabDelegate::setModelData(QWidget *editor, QAbstractItemModel *model, + const QModelIndex &index) const +{ + QLineEdit *line = qobject_cast(editor); + QString value = line->text().trimmed(); + + if ((*_operations)[index.row()].parent) + model->setData(index, qVariantFromValue(" " + value)); + else + model->setData(index, qVariantFromValue(value)); +} + +void TabDelegate::setEditorData(QWidget *editor, + const QModelIndex &index) const +{ + QLineEdit* line = qobject_cast(editor); + QString s = qVariantValue(index.data()); + + line->setText(s.trimmed()); +} diff --git a/src/view/grid/TabDelegate.hpp b/src/view/grid/TabDelegate.hpp new file mode 100644 index 0000000..6341042 --- /dev/null +++ b/src/view/grid/TabDelegate.hpp @@ -0,0 +1,43 @@ +/* + 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 TABDELEGATE_H +#define TABDELEGATE_H + +#include +#include +#include + +class TabDelegate : public QItemDelegate +{ + Q_OBJECT; + +public: + TabDelegate(QWidget *parent = 0, std::vector* operations=0) : QItemDelegate(parent), _operations(operations) {} + + 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: + std::vector* _operations; +}; + +#endif