Add meta support in GridAccount

Add TabDelegate
This commit is contained in:
Grégory Soutadé 2011-10-22 10:55:34 +02:00
parent 2243f0a1cf
commit c5cec6c9d6
4 changed files with 185 additions and 75 deletions

View File

@ -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<NUMBER_COLS_OPS; i++) \
{ \
@ -49,7 +50,7 @@ GridAccount::GridAccount(KissCount* kiss, QWidget *parent,
_week2(0), _week3(0), _week4(0), _parent(parent), _kiss(kiss),
_canAddOperation(canAddOperation), _setWeek(setWeek),
_databaseSynchronization(synchronizeWithDatabase), _loadOperations(false),
_curMonth(0), _curYear(0)
_curMonth(0), _curYear(0), _signalMapper(this)
{
//DEFAULT_FONT(font);
QFont font;
@ -113,6 +114,8 @@ GridAccount::GridAccount(KissCount* kiss, QWidget *parent,
resizeColumnToContents(ACCOUNT);
resizeColumnToContents(OP_DELETE);
resizeColumnToContents(CHECKED);
connect(&_signalMapper, SIGNAL(mapped(int)), this, SLOT(OnMetaClicked(int)));
}
GridAccount::~GridAccount()
@ -213,6 +216,9 @@ void GridAccount::LoadOperations(std::vector<Operation>* 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<Operation*>::iterator it, it2;
// wxGridCellTreeButtonRenderer* treeRenderer;
// int i, deletedOperations = 0;
// Operation op2;
// treeRenderer = (wxGridCellTreeButtonRenderer*) GetCellRenderer(line, TREE);
std::vector<Operation*>::iterator it, it2;
int i, deletedOperations = 0;
Operation op2;
QPushButton* button = qobject_cast<QPushButton*> (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<QPushButton*> (_signalMapper.mapping(id));
std::vector<Operation>::iterator it;
std::vector<int>::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)

View File

@ -36,6 +36,7 @@
// #include "wxGridCellTabStringRenderer.hpp"
#include <QTableWidget>
#include <QSignalMapper>
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<Operation>* _operations;
bool _loadOperations;
int _curMonth, _curYear;
QSignalMapper _signalMapper;
void SetWeek(int week, int line);
void ResetWeeks();

View File

@ -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 <http://www.gnu.org/licenses/>.
*/
#include <QtGui>
#include <QString>
#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<QLineEdit *>(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<QLineEdit *>(editor);
QString s = qVariantValue<QString>(index.data());
line->setText(s.trimmed());
}

View File

@ -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 <http://www.gnu.org/licenses/>.
*/
#ifndef TABDELEGATE_H
#define TABDELEGATE_H
#include <QItemDelegate>
#include <QString>
#include <model/model.hpp>
class TabDelegate : public QItemDelegate
{
Q_OBJECT;
public:
TabDelegate(QWidget *parent = 0, std::vector<Operation>* 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<Operation>* _operations;
};
#endif