Rework GridAccount to be simpler, more modulable and more resitant to bugs

This commit is contained in:
Grégory Soutadé 2012-12-27 17:56:20 +01:00
parent 6a90c340d3
commit ba24fd4a91
3 changed files with 299 additions and 403 deletions

6
TODO
View File

@ -29,13 +29,9 @@ Undo/redo
=============================================================== ===============================================================
BUGS BUGS
* When we broke a transfert into a meta operation and re create it,
it's not taken in account by UpdateStats
* If a sub operation is found using SearchPanel but not its parent * If a sub operation is found using SearchPanel but not its parent
it will not be displayed. In this case we must load whole meta. it will not be displayed. In this case we must load whole meta.
This bug can't be resolved without use of hashtable because of This bug can't be resolved without use of hashtable because of
complexity in searching this issue. complexity in searching this issue.
* When changing date in a sub operation (set date > main date), * After manipulating some meta operation, it withdrawn
meta amount is set to 0

View File

@ -177,13 +177,13 @@ void GridAccount::UpdateOperation(Operation& op)
} }
} }
int GridAccount::GetDisplayedRow(int id) int GridAccount::GetDisplayedRow(int id) throw (OperationNotFound)
{ {
std::vector<Operation>::iterator it = std::find(_displayedOperations.begin(), _displayedOperations.end(), id); std::vector<Operation>::iterator it = std::find(_displayedOperations.begin(), _displayedOperations.end(), id);
if (it != _displayedOperations.end()) return it-_displayedOperations.begin(); if (it != _displayedOperations.end()) return it-_displayedOperations.begin();
return -1; throw OperationNotFound();
} }
void GridAccount::ClearGrid() void GridAccount::ClearGrid()
@ -433,6 +433,7 @@ void GridAccount::InsertOperation(User* user, Operation& op, int line, bool fix,
} }
else else
{ {
// NULL Op
item = new QTableWidgetItem(""); item = new QTableWidgetItem("");
setItem(line, DESCRIPTION, item); setItem(line, DESCRIPTION, item);
if (fix) if (fix)
@ -498,16 +499,74 @@ void GridAccount::InsertOperation(User* user, Operation& op, int line, bool fix,
_inModification = false; _inModification = false;
} }
void GridAccount::DeleteOperation(const Operation& op) void GridAccount::DeleteOperation(const Operation& op) throw (OperationNotFound)
{ {
std::vector<Operation>::iterator it = std::find(_operations->begin(), _operations->end(), op.id); std::vector<Operation>::iterator it = std::find(_operations->begin(), _operations->end(), op.id);
std::vector<int>::iterator it2;
Operation parent;
if (it != _operations->end()) _operations->erase(it); User* user = _kiss->GetUser();
if (it == _operations->end())
throw OperationNotFound();
if (it->parent)
user->UnGroup(*it);
if (it != _operations->end())
{
if (_databaseSynchronization)
_kiss->DeleteOperation(*it);
if (it->parent)
{
parent = GetOperation(it->parent);
it2 = std::find(parent.childs.begin(), parent.childs.end(), it->id);
if (it2 != parent.childs.end())
parent.childs.erase(it2);
}
_operations->erase(it);
}
} }
void GridAccount::InsertIntoGrid(Operation& op) void GridAccount::RemoveRow(const Operation& op, int line, bool deleteOp)
{ {
int i, a, start; QPushButton* button = qobject_cast<QPushButton*> (cellWidget(line, TREE));
if (button)
button->disconnect(&_treeSignalMapper, SLOT(map()));
QCheckBox* checkBox = qobject_cast<QCheckBox*> (cellWidget(line, CHECKED));
if (checkBox)
checkBox->disconnect(&_checkSignalMapper, SLOT(map()));
checkBox = qobject_cast<QCheckBox*> (cellWidget(line, OP_DELETE));
if (checkBox)
checkBox->disconnect(&_deleteSignalMapper, SLOT(map()));
removeRow(line);
_displayedOperations.erase(_displayedOperations.begin()+line);
if (op.fix_cost) _fixCosts--;
if (deleteOp)
DeleteOperation(op);
}
bool GridAccount::CheckDay(User* user, const Operation& op, int& i)
{
if (user->_preferences["operation_order"] == "ASC")
{
if ((_displayedOperations)[i].day > op.day)
return true;
}
else
{
if ((_displayedOperations)[i].day < op.day)
return true;
}
return false;
}
int GridAccount::InsertIntoGrid(Operation& op)
{
int i, a, start=0;
User* user = _kiss->GetUser(); User* user = _kiss->GetUser();
Operation parent; Operation parent;
@ -520,61 +579,81 @@ void GridAccount::InsertIntoGrid(Operation& op)
else else
{ {
if (op.parent) if (op.parent)
start = GetDisplayedRow(op.parent); {
else start = GetDisplayedRow(op.parent)+1;
start = 0;
for(i=start; i<(int)_displayedOperations.size(); i++) for(i=start; i<(int)_displayedOperations.size(); i++)
{ {
if (!_displayedOperations[i].id) continue; if (!_displayedOperations[i].parent) break;
if (_displayedOperations[i].parent) continue; if (_displayedOperations[i].id == 0) break;
if ((_displayedOperations)[i].fix_cost && !op.fix_cost) continue; if (CheckDay(user, op, i)) break;
if (!(_displayedOperations)[i].fix_cost && op.fix_cost) break; }
if (user->_preferences["operation_order"] == "ASC")
{
if ((_displayedOperations)[i].day > op.day)
break;
} }
else else
{ {
if ((_displayedOperations)[i].day < op.day) if (op.fix_cost)
break;
}
}
if (op.parent)
{ {
if ((i-start) > (int)(parent.childs.size())) for(i=1; i<(int)_fixCosts; i++)
i = start + parent.childs.size(); {
if (parent.day >= op.day) if (_displayedOperations[i].parent) continue;
i = start + 1; if (_displayedOperations[i].id == 0) break;
if (CheckDay(user, op, i)) break;
}
}
else
{
for(i=_fixCosts+1; i<(int)_displayedOperations.size()-1; i++)
{
if (_displayedOperations[i].parent) continue;
if (_displayedOperations[i].fix_cost) continue;
if (_displayedOperations[i].id == 0) break;
if (CheckDay(user, op, i)) break;
}
}
} }
else if (i == (int)_displayedOperations.size() ||
i == _fixCosts)
i--;
else if (!(_displayedOperations)[i].fix_cost && op.fix_cost)
i --;
} }
for (a=0; a<(int)_operations->size(); a++) for (a=0; a<(int)_operations->size(); a++)
{ {
if ((*_operations)[a].fix_cost && !op.fix_cost) continue; if ((*_operations)[a].fix_cost && !op.fix_cost) continue;
if (!(*_operations)[a].fix_cost && op.fix_cost) if (!(*_operations)[a].fix_cost && op.fix_cost) break;
{ if ((*_operations)[a].day > op.day) break;
a--;
break;
}
if ((*_operations)[a].day > op.day)
{
a--;
break;
}
} }
if (a < 0) a = 0;
_operations->insert(_operations->begin()+a, op); _operations->insert(_operations->begin()+a, op);
InsertOperationWithWeek(user, (*_operations)[a], i, op.fix_cost, _curMonth, _curYear); InsertOperationWithWeek(user, op, i, op.fix_cost, _curMonth, _curYear);
return i;
}
void GridAccount::CheckOperation(Operation& op, int line, bool check, bool force)
{
QColor color;
int r,g,b;
User* user = _kiss->GetUser();
if (!force)
{
if (op.checked == check) return;
op.checked = check;
UpdateOperation(op);
QCheckBox* checkBox = qobject_cast<QCheckBox*>(cellWidget(line, CHECKED));
if (checkBox)
checkBox->setCheckState(check ? Qt::Checked : Qt::Unchecked);
}
color = user->GetCategory(op.category).backcolor;
if (check)
{
r = ((color.red()*1.5) >= 0xFF) ? 0xFF : color.red()*1.5 ;
g = ((color.green()*1.5) >= 0xFF) ? 0xFF : color.green()*1.5 ;
b = ((color.blue()*1.5) >= 0xFF) ? 0xFF : color.blue()*1.5 ;
color.setRgb(r, g, b);
}
SET_ROW_COLOR(line, color, user->GetCategory(op.category).forecolor);
} }
int GridAccount::RemoveMeta(Operation op, int line, bool removeRoot, bool deleteOp) int GridAccount::RemoveMeta(Operation op, int line, bool removeRoot, bool deleteOp)
@ -588,22 +667,13 @@ int GridAccount::RemoveMeta(Operation op, int line, bool removeRoot, bool delete
{ {
op2 = GetOperation(op.childs[i]); op2 = GetOperation(op.childs[i]);
if (op2.meta) if (op2.meta)
RemoveMeta(op2, line+1, true, deleteOp); deletedOperations += RemoveMeta(op2, line+1, true, deleteOp);
else else
{ {
if (button->text() == "-") if (button->text() == "-")
{ {
removeRow(line+1); RemoveRow(op2, line+1, deleteOp);
deletedOperations++; deletedOperations++;
if (op2.fix_cost) _fixCosts--;
_displayedOperations.erase(_displayedOperations.begin()+line+1);
}
if (deleteOp)
{
DeleteOperation(op2);
if (_databaseSynchronization)
_kiss->DeleteOperation(op2);
} }
} }
} }
@ -612,20 +682,7 @@ int GridAccount::RemoveMeta(Operation op, int line, bool removeRoot, bool delete
if (removeRoot) if (removeRoot)
{ {
button->disconnect(&_treeSignalMapper, SLOT(map())); RemoveRow(op, line, deleteOp);
QCheckBox* checkBox = qobject_cast<QCheckBox*> (cellWidget(line, CHECKED));
checkBox->disconnect(&_checkSignalMapper, SLOT(map()));
checkBox = qobject_cast<QCheckBox*> (cellWidget(line, OP_DELETE));
checkBox->disconnect(&_deleteSignalMapper, SLOT(map()));
removeRow(line);
_displayedOperations.erase(_displayedOperations.begin()+line);
if (op.fix_cost) _fixCosts--;
if (deleteOp)
{
DeleteOperation(op);
if (_databaseSynchronization)
_kiss->DeleteOperation(op);
}
deletedOperations++; deletedOperations++;
} }
@ -634,41 +691,53 @@ int GridAccount::RemoveMeta(Operation op, int line, bool removeRoot, bool delete
void GridAccount::CheckMeta(Operation& op, int line, bool check) void GridAccount::CheckMeta(Operation& op, int line, bool check)
{ {
int i, new_line; int i;
Operation op2; Operation op2;
QColor color ;
unsigned char r, g, b;
User* user = _kiss->GetUser();
QPushButton* button = qobject_cast<QPushButton*>(cellWidget(line, TREE)); CheckOperation(op, line, check, false);
if (IsMetaOpened(op.id))
{
for(i=0; i<(int)op.childs.size(); i++) for(i=0; i<(int)op.childs.size(); i++)
{ {
op2 = GetOperation(op.childs[i]); op2 = GetOperation(op.childs[i]);
op2.checked = check;
UpdateOperation(op2);
if (op2.meta) if (op2.meta)
CheckMeta(op2, line+1, check); CheckMeta(op2, line+i+1, check);
else
if (button->text() == "-") CheckOperation(op2, line+i+1, check, false);
}
}
else
{ {
QCheckBox* checkBox = qobject_cast<QCheckBox*>(cellWidget(line+i+1, CHECKED)); for(i=0; i<(int)op.childs.size(); i++)
checkBox->setCheckState(check ? Qt::Checked : Qt::Unchecked);
color = user->GetCategory(op2.category).backcolor;
if (check)
{ {
r = ((color.red()*1.5) >= 0xFF) ? 0xFF : color.red()*1.5 ; op2 = GetOperation(op.childs[i]);
g = ((color.green()*1.5) >= 0xFF) ? 0xFF : color.green()*1.5 ;
b = ((color.blue()*1.5) >= 0xFF) ? 0xFF : color.blue()*1.5 ; if (op2.meta)
color.setRgb(r, g, b); CheckMeta(op2, line+i+1, check);
else
{
op2.checked = check;
UpdateOperation(op2);
}
}
}
} }
new_line = line+i+1; bool GridAccount::IsMetaOpened(int id)
SET_ROW_COLOR(new_line, color, user->GetCategory(op2.category).forecolor); {
} int row = GetDisplayedRow(id);
QPushButton* button = qobject_cast<QPushButton*> (cellWidget(row, TREE));
return (button->text() == QString("-"));
} }
void GridAccount::OpenMeta(const Operation& meta)
{
if (!IsMetaOpened(meta.id))
OnMetaClicked(meta.id);
} }
void GridAccount::OnMetaClicked(int id) void GridAccount::OnMetaClicked(int id)
@ -688,12 +757,12 @@ void GridAccount::OnMetaClicked(int id)
row = it - _displayedOperations.begin(); row = it - _displayedOperations.begin();
if (button->text() == "+") if (button->text() == QString("+"))
{ {
for (i=1, it2=op.childs.begin(); it2!=op.childs.end(); it2++, i++) for (i=1, it2=op.childs.begin(); it2!=op.childs.end(); it2++, i++)
{ {
op2 = GetOperation(*it2); op2 = GetOperation(*it2);
InsertOperationWithWeek(user, op2, row+i, op2.fix_cost, op2.month, op2.year); InsertOperation(user, op2, row+i, op2.fix_cost, op2.month, op2.year);
} }
button->setText("-"); button->setText("-");
} }
@ -702,21 +771,20 @@ void GridAccount::OnMetaClicked(int id)
RemoveMeta(op, row, false, false); RemoveMeta(op, row, false, false);
button->setText("+"); button->setText("+");
} }
ComputeWeeks();
} }
void GridAccount::OnCheckClicked(int id) void GridAccount::OnCheckClicked(int id)
{ {
std::vector<Operation>::iterator it; std::vector<Operation>::iterator it;
std::vector<int>::iterator it2;
int row; int row;
Operation op2; Operation op2, parent;
QColor color; bool fullCheck = true;
User* user = _kiss->GetUser();
unsigned char r, g, b;
if (_inModification || _loadOperations) return; if (_inModification || _loadOperations) return;
QCheckBox* checkBox = qobject_cast<QCheckBox*> (_checkSignalMapper.mapping(id));
it = std::find(_displayedOperations.begin(), _displayedOperations.end(), id); it = std::find(_displayedOperations.begin(), _displayedOperations.end(), id);
if (it == _displayedOperations.end()) return ; if (it == _displayedOperations.end()) return ;
@ -724,45 +792,26 @@ void GridAccount::OnCheckClicked(int id)
_inModification = true; _inModification = true;
row = it-_displayedOperations.begin(); row = it-_displayedOperations.begin();
_displayedOperations[row].checked = (checkBox->checkState() == Qt::Checked);
color = user->GetCategory(it->category).backcolor;
if (it->checked)
{
r = ((color.red()*1.5) >= 0xFF) ? 0xFF : color.red()*1.5 ;
g = ((color.green()*1.5) >= 0xFF) ? 0xFF : color.green()*1.5 ;
b = ((color.blue()*1.5) >= 0xFF) ? 0xFF : color.blue()*1.5 ;
color.setRgb(r, g, b);
}
SET_ROW_COLOR(row, color, user->GetCategory(it->category).forecolor);
SET_ROW_FONT(row, user->GetCategoryFont(it->category));
UpdateOperation(*it);
if (it->meta) if (it->meta)
CheckMeta(*it, row, it->checked); CheckMeta(*it, row, !it->checked);
else else
{ {
CheckOperation(*it, row, !it->checked, false);
if (it->parent) if (it->parent)
{ {
op2 = GetOperation(it->parent); parent = GetOperation(it->parent);
UpdateMeta(op2); for(it2=parent.childs.begin(); it2!=parent.childs.end(); it2++)
int row2 = GetDisplayedRow(op2.id);
QCheckBox* checkBox = qobject_cast<QCheckBox*>(cellWidget(row2, CHECKED));
checkBox->setCheckState(op2.checked ? Qt::Checked : Qt::Unchecked);
color = user->GetCategory(op2.category).backcolor;
if (op2.checked)
{ {
r = ((color.red()*1.5) >= 0xFF) ? 0xFF : color.red()*1.5 ; op2 = GetOperation(*it2);
g = ((color.green()*1.5) >= 0xFF) ? 0xFF : color.green()*1.5 ; if (!op2.checked)
b = ((color.blue()*1.5) >= 0xFF) ? 0xFF : color.blue()*1.5 ; {
color.setRgb(r, g, b); fullCheck = false;
break;
} }
}
SET_ROW_COLOR(row2, color, user->GetCategory(op2.category).forecolor); CheckOperation(parent, GetDisplayedRow(parent.id), fullCheck, false);
} }
} }
@ -773,12 +822,9 @@ void GridAccount::OnCheckClicked(int id)
void GridAccount::OnDeleteClicked(int id) void GridAccount::OnDeleteClicked(int id)
{ {
std::vector<Operation>::iterator it; std::vector<Operation>::iterator it;
std::vector<int>::iterator it2; int row, parentRow;
int row; Operation op, op2, parent;
User* user = _kiss->GetUser(); User* user = _kiss->GetUser();
Operation op, op_tmp, op_tmp2;
QColor color;
unsigned char r, g, b;
if (_inModification || _loadOperations) return; if (_inModification || _loadOperations) return;
@ -802,74 +848,36 @@ void GridAccount::OnDeleteClicked(int id)
row = it-_displayedOperations.begin(); row = it-_displayedOperations.begin();
if (op.parent)
user->UnGroup(_displayedOperations[row]);
if (op.meta) if (op.meta)
RemoveMeta(_displayedOperations[row], row, true, true); RemoveMeta(op, row, true, true);
else else
{ {
if (op.parent) RemoveRow(op, row, true);
{
op_tmp = GetOperation(op.parent);
it2 = std::find(op_tmp.childs.begin(), op_tmp.childs.end(), op.id);
if (it2 != op_tmp.childs.end())
op_tmp.childs.erase(it2);
}
removeRow(row);
DeleteOperation(*it);
if (_databaseSynchronization)
_kiss->DeleteOperation(*it);
_displayedOperations.erase(_displayedOperations.begin()+row);
if (op.parent) if (op.parent)
{ {
if (op_tmp.childs.size() < 2) user->UnGroup(op);
{
if (op_tmp.childs.size() == 1)
{
op_tmp2 = GetOperation(op_tmp.childs[0]);
op_tmp2.parent = 0;
UpdateOperation(op_tmp2);
row = GetDisplayedRow(op_tmp2.id);
_displayedOperations[row] = op_tmp2;
}
row = GetDisplayedRow(op.parent); parent = GetOperation(op.parent);
removeRow(row); parentRow = GetDisplayedRow(parent.id);
DeleteOperation(op_tmp);
if (_databaseSynchronization)
_kiss->DeleteOperation(op_tmp);
_displayedOperations.erase(_displayedOperations.begin()+row);
if (op.fix_cost)
_fixCosts--;
setItem(row, DESCRIPTION, new QTableWidgetItem(op.description)); // Remove tabulation
color = user->GetCategory(op.category).backcolor;
if (op.checked) // One child remains
if (parent.childs.size() == 1)
{ {
r = ((color.red()*1.5) >= 0xFF) ? 0xFF : color.red()*1.5 ; op = GetOperation(parent.childs[0]);
g = ((color.green()*1.5) >= 0xFF) ? 0xFF : color.green()*1.5 ; user->UnGroup(op);
b = ((color.blue()*1.5) >= 0xFF) ? 0xFF : color.blue()*1.5 ; // Remove parent
color.setRgb(r, g, b); RemoveRow(parent, parentRow, true);
} // Remove child
op.parent = 0;
SET_ROW_COLOR(row, color, user->GetCategory(op.category).forecolor); UpdateOperation(op);
SET_ROW_FONT(row, user->GetCategoryFont(op.category)); RemoveRow(op, parentRow, false);
InsertIntoGrid(op);
} }
else else
{ UpdateMeta(parent);
UpdateMeta(op_tmp);
row = GetDisplayedRow(op_tmp.id);
RemoveMeta(op_tmp, row, true, false);
InsertIntoGrid(op_tmp);
} }
} }
if (op.fix_cost)
_fixCosts--;
ComputeWeeks();
}
_kiss->UpdateStats(); _kiss->UpdateStats();
_inModification = false; _inModification = false;
@ -879,19 +887,17 @@ void GridAccount::OnOperationModified(int row, int col)
{ {
User* user = _kiss->GetUser(); User* user = _kiss->GetUser();
Operation new_op, cur_op, op_tmp, op_tmp2; Operation new_op, cur_op, op_tmp, op_tmp2;
int op_complete = 6, i, last_day; int op_complete = 6;
QString value, v ; QString value, v ;
QDate date; QDate date;
bool need_insertion = false, transfertCompleted = false; bool need_insertion = false, transfertCompleted = false;
QColor color ;
unsigned char r, g, b;
std::vector<int>::iterator it; std::vector<int>::iterator it;
Operation op, op2; std::vector<Operation>::iterator it2;
int amount; Operation op, op2, parent;
QFont font; QFont font;
Category cat ; Category cat ;
bool fix_cost; bool fix_cost;
QDate curDate = QDate::currentDate(); Operation NULLop;
// Avoid recursives calls // Avoid recursives calls
if (_inModification || _loadOperations) return; if (_inModification || _loadOperations) return;
@ -1005,19 +1011,6 @@ void GridAccount::OnOperationModified(int row, int col)
new_op.checked = false; new_op.checked = false;
op_complete--; op_complete--;
color = user->GetCategory(new_op.category).backcolor;
if (new_op.checked)
{
r = ((color.red()*1.5) >= 0xFF) ? 0xFF : color.red()*1.5 ;
g = ((color.green()*1.5) >= 0xFF) ? 0xFF : color.green()*1.5 ;
b = ((color.blue()*1.5) >= 0xFF) ? 0xFF : color.blue()*1.5 ;
color.setRgb(r, g, b);
}
SET_ROW_COLOR(row, color, user->GetCategory(new_op.category).forecolor);
SET_ROW_FONT(row, user->GetCategoryFont(new_op.category));
fix_cost = (row <= _fixCosts); fix_cost = (row <= _fixCosts);
// Modify an operation // Modify an operation
@ -1033,20 +1026,32 @@ void GridAccount::OnOperationModified(int row, int col)
new_op.childs = cur_op.childs; new_op.childs = cur_op.childs;
new_op._virtual = cur_op._virtual; new_op._virtual = cur_op._virtual;
UpdateOperation(new_op);
if (cur_op.day != new_op.day) if (cur_op.day != new_op.day)
{ {
// Remove from _operation without DeleteOperation to avoid commit into database
it2 = std::find(_operations->begin(), _operations->end(), new_op.id);
if (it2 != _operations->end())
_operations->erase(it2);
need_insertion = true; need_insertion = true;
removeRow(row); RemoveRow(new_op, row, false);
if (fix_cost)
_fixCosts--;
} }
else
{
(_displayedOperations)[row] = new_op; (_displayedOperations)[row] = new_op;
UpdateOperation(new_op); cat = user->GetCategory(new_op.category);
CheckOperation(new_op, row, new_op.checked, true);
SET_ROW_FONT(row, user->GetCategoryFont(cat.id));
}
} }
// Add an operation // Add an operation
else else
{ {
cat = user->GetCategory(new_op.category);
CheckOperation(new_op, row, new_op.checked, true);
SET_ROW_FONT(row, user->GetCategoryFont(cat.id));
if (op_complete) { if (op_complete) {
_inModification = false ; _inModification = false ;
return ; return ;
@ -1057,49 +1062,9 @@ void GridAccount::OnOperationModified(int row, int col)
new_op._virtual = false; new_op._virtual = false;
new_op.parent = 0; new_op.parent = 0;
for(i=0; i<NUMBER_COLS_OPS; i++) RemoveRow(new_op, row, false);
{ NULLop.id = 0;
if (fix_cost && i == CATEGORY) continue; InsertOperation(user, NULLop, row, new_op.fix_cost, _curMonth, _curYear);
setItem(row, i, new QTableWidgetItem(""));
}
this->item(row, DEBIT)->setTextAlignment(Qt::AlignRight|Qt::AlignVCenter);
this->item(row, CREDIT)->setTextAlignment(Qt::AlignRight|Qt::AlignVCenter);
this->item(row, OP_DELETE)->setTextAlignment(Qt::AlignHCenter|Qt::AlignVCenter);
this->item(row, CHECKED)->setTextAlignment(Qt::AlignHCenter|Qt::AlignVCenter);
if (!fix_cost)
{
int day;
if (curDate.year() == _curYear)
{
if (curDate.month() > (_curMonth+1))
day = QDate(_curYear, _curMonth+1, 1).daysInMonth();
else if (curDate.month() < (_curMonth+1))
day = 1;
else
day = curDate.day();
}
else if (curDate.year() > _curYear)
day = QDate(_curYear, _curMonth+1, 1).daysInMonth();
else
day = 1;
setItem(row, OP_DATE, new QTableWidgetItem(_kiss->FormatDate(day, _curMonth+1, _curYear)));
this->item(row, OP_DATE)->setTextAlignment(Qt::AlignHCenter|Qt::AlignVCenter);
}
DEFAULT_FONT(font);
if (fix_cost)
{
SET_ROW_COLOR(row, view::OWN_YELLOW, Qt::black);
}
else
{
SET_ROW_COLOR(row, view::OWN_GREEN, Qt::black);
}
SET_ROW_FONT(row, font);
new_op.id = _kiss->AddOperation(new_op); new_op.id = _kiss->AddOperation(new_op);
@ -1115,101 +1080,8 @@ void GridAccount::OnOperationModified(int row, int col)
if (new_op.parent) if (new_op.parent)
{ {
row = GetDisplayedRow(new_op.parent); parent = GetOperation(new_op.parent);
UpdateMeta(parent);
last_day = new_op.day;
new_op = _displayedOperations[row];
it = std::find(new_op.childs.begin(), new_op.childs.end(), cur_op.id);
new_op.childs.erase(it);
i = 0;
for(it = new_op.childs.begin(); it != new_op.childs.end(); it++)
{
op2 = GetOperation(*it);
if ((int)op2.day > last_day) break;
i++;
}
new_op.childs.insert(new_op.childs.begin()+i, cur_op.id);
last_day = new_op.day;
UpdateMeta(new_op);
_displayedOperations[row] = new_op;
cat = user->GetCategory(new_op.category);
if (new_op.category)
color = cat.backcolor;
else
color = view::OWN_GREEN;
QCheckBox* checkBox = qobject_cast<QCheckBox*> (_checkSignalMapper.mapping(new_op.id));
if (new_op.checked)
{
r = ((color.red()*1.5) >= 0xFF) ? 0xFF : color.red()*1.5 ;
g = ((color.green()*1.5) >= 0xFF) ? 0xFF : color.green()*1.5 ;
b = ((color.blue()*1.5) >= 0xFF) ? 0xFF : color.blue()*1.5 ;
color.setRgb(r, g, b);
checkBox->setCheckState(Qt::Checked);
}
else
checkBox->setCheckState(Qt::Unchecked);
setItem(row, OP_DATE, new QTableWidgetItem(_kiss->FormatDate(new_op.day+1, _curMonth+1, _curYear)));
if (!_displayedOperations[row].amount)
{
amount = _kiss->MetaPositiveAmount(new_op.id);
setItem(row, DEBIT, new QTableWidgetItem(v.sprintf("%.2lf", (double)amount/100)));
setItem(row, CREDIT, new QTableWidgetItem(v.sprintf("%.2lf", (double)amount/100)));
}
else
{
if (_displayedOperations[row].amount < 0)
{
setItem(row, DEBIT, new QTableWidgetItem(v.sprintf("%.2lf", (double)-new_op.amount/100)));
setItem(row, CREDIT, new QTableWidgetItem(""));
}
else
{
setItem(row, DEBIT, new QTableWidgetItem(""));
setItem(row, CREDIT, new QTableWidgetItem(v.sprintf("%.2lf", (double)new_op.amount/100)));
}
}
this->item(row, OP_DATE)->setTextAlignment(Qt::AlignHCenter|Qt::AlignVCenter);
this->item(row, DEBIT)->setTextAlignment(Qt::AlignRight|Qt::AlignVCenter);
this->item(row, CREDIT)->setTextAlignment(Qt::AlignRight|Qt::AlignVCenter);
SET_ROW_COLOR(row, color, cat.forecolor);
if (new_op.category && cat.font.length())
{
SET_ROW_FONT(row, user->GetCategoryFont(cat.id));
}
// Move updated meta
if ((int)new_op.day != last_day)
{
int i;
RemoveMeta(new_op, row, true, false);
InsertIntoGrid(new_op);
row = GetDisplayedRow(new_op.id);
for (i=1, it=new_op.childs.begin(); it!=new_op.childs.end(); it++, i++)
{
op2 = GetOperation(*it);
InsertOperationWithWeek(user, op2, row+i, op2.fix_cost, _curMonth, _curYear);
}
QPushButton* button = qobject_cast<QPushButton*> (cellWidget(row, TREE));
if (button->text() == "+")
button->setText("-");
else
button->setText("+");
}
} }
_kiss->UpdateStats(); _kiss->UpdateStats();
@ -1219,20 +1091,24 @@ void GridAccount::OnOperationModified(int row, int col)
void GridAccount::UpdateMeta(Operation& meta) void GridAccount::UpdateMeta(Operation& meta)
{ {
std::vector<int>::iterator it; std::vector<int>::iterator it;
std::vector<Operation>::iterator it2;
Operation op ; Operation op ;
int category = 0; int category = 0;
bool updateCat = false ; bool updateCat = false ;
bool openMeta;
if (!meta.childs.size()) return ; if (!meta.childs.size()) return ;
openMeta = IsMetaOpened(meta.id);
meta.category = 0; meta.category = 0;
meta.checked = true;
meta.amount = 0; meta.amount = 0;
op = GetOperation(meta.childs[0]); op = GetOperation(meta.childs[0]);
meta.year = op.year; meta.year = op.year;
meta.month = op.month; meta.month = op.month;
meta.day = op.day; meta.day = op.day;
meta.description = "";
for(it=meta.childs.begin(); it!=meta.childs.end(); it++) for(it=meta.childs.begin(); it!=meta.childs.end(); it++)
{ {
@ -1243,7 +1119,6 @@ void GridAccount::UpdateMeta(Operation& meta)
meta.month = op.month; meta.month = op.month;
meta.day = op.day; meta.day = op.day;
} }
meta.checked &= op.checked;
if (!meta.description.length() && op.description.length()) if (!meta.description.length() && op.description.length())
meta.description = op.description; meta.description = op.description;
if (!category) if (!category)
@ -1267,8 +1142,17 @@ void GridAccount::UpdateMeta(Operation& meta)
meta.amount = _kiss->MetaAmount(meta.id); meta.amount = _kiss->MetaAmount(meta.id);
if (_databaseSynchronization) UpdateOperation(meta);
_kiss->UpdateOperation(meta);
RemoveMeta(meta, GetDisplayedRow(meta.id), true, false);
it2 = std::find(_operations->begin(), _operations->end(), meta.id);
if (it2 != _operations->end())
_operations->erase(it2);
InsertIntoGrid(meta);
if (openMeta)
OpenMeta(meta);
} }
void GridAccount::Group() void GridAccount::Group()
@ -1281,8 +1165,9 @@ void GridAccount::Group()
int parent = 0, deletedRows; int parent = 0, deletedRows;
Operation op, op2; Operation op, op2;
int fix = -1, i, a, row; int fix = -1, i, a, row;
User* user = _kiss->GetUser();
QModelIndexList selected = selectedIndexes(); QModelIndexList selected = selectedIndexes();
bool fullCheck = true;
for (int i = 0; i < selected.size(); ++i) for (int i = 0; i < selected.size(); ++i)
{ {
@ -1366,9 +1251,7 @@ void GridAccount::Group()
deletedRows = RemoveMeta(ops[i], rows[i], true, false); deletedRows = RemoveMeta(ops[i], rows[i], true, false);
else else
{ {
if (ops[i].fix_cost) _fixCosts--; RemoveRow(ops[i], rows[i], false);
removeRow(rows[i]);
_displayedOperations.erase(_displayedOperations.begin()+rows[i]);
deletedRows = 1; deletedRows = 1;
} }
for(a=i+1; a<(int)rows.size(); a++) for(a=i+1; a<(int)rows.size(); a++)
@ -1383,10 +1266,20 @@ void GridAccount::Group()
for (i=0, it3=op.childs.begin(); it3!=op.childs.end(); it3++, i++) for (i=0, it3=op.childs.begin(); it3!=op.childs.end(); it3++, i++)
{ {
op2 = GetOperation(*it3); op2 = GetOperation(*it3);
if (*it3 == it2->id || if (*it3 == it2->id)
op2.day > it2->day) {
if (user->_preferences["operation_order"] == "ASC")
{
if (op2.day > it2->day)
break; break;
} }
else
{
if (op2.day < it2->day)
break;
}
}
}
if (i) i--; if (i) i--;
@ -1399,11 +1292,16 @@ void GridAccount::Group()
it2->parent = op.id; it2->parent = op.id;
UpdateOperation(*it2); UpdateOperation(*it2);
if (!it2->checked)
fullCheck = false;
} }
row = InsertIntoGrid(op);
UpdateMeta(op); UpdateMeta(op);
InsertIntoGrid(op); if (fullCheck)
CheckMeta(op, row, true);
} }
void GridAccount::GetSelectedOperations(std::vector<int>* rows) void GridAccount::GetSelectedOperations(std::vector<int>* rows)
@ -1433,7 +1331,7 @@ void GridAccount::UnGroup()
std::vector<int>::iterator it3; std::vector<int>::iterator it3;
int parent = 0; int parent = 0;
Operation op, op2; Operation op, op2;
int fix = -1, i, a, line; int fix = -1, i, line;
QModelIndexList selected = selectedIndexes(); QModelIndexList selected = selectedIndexes();
@ -1475,6 +1373,8 @@ void GridAccount::UnGroup()
if (!ops.size() && !parent) return; if (!ops.size() && !parent) return;
_inModification = true;
removeLastGroup: removeLastGroup:
// Only one meta is selected // Only one meta is selected
if (!ops.size()) if (!ops.size())
@ -1492,7 +1392,6 @@ removeLastGroup:
InsertIntoGrid(op2); InsertIntoGrid(op2);
} }
_kiss->DeleteOperation(op);
DeleteOperation(op); DeleteOperation(op);
} }
else else
@ -1508,19 +1407,14 @@ removeLastGroup:
op.parent = 0; op.parent = 0;
UpdateOperation(op); UpdateOperation(op);
line = GetDisplayedRow(op.id); line = GetDisplayedRow(op.id);
removeRow(line); RemoveRow(op, line, false);
_displayedOperations.erase(_displayedOperations.begin()+line); InsertIntoGrid(op);
InsertIntoGrid(GetOperation(op.id)); it = std::find(op2.childs.begin(), op2.childs.end(), op.id);
if (op.fix_cost) _fixCosts--; if (it != op2.childs.end())
for (a=0; a<(int)op2.childs.size(); a++) op2.childs.erase(it);
if (op2.childs[a] == op.id)
{
op2.childs.erase(op2.childs.begin()+a);
break;
}
UpdateMeta(op2);
} }
UpdateMeta(op2);
line = GetDisplayedRow(parent); line = GetDisplayedRow(parent);
_displayedOperations[line] = op2; _displayedOperations[line] = op2;
@ -1534,7 +1428,7 @@ removeLastGroup:
UpdateOperation(op2); UpdateOperation(op2);
} }
ComputeWeeks(); _inModification = false;
} }
void GridAccount::MassUpdate(std::vector<int>& rows, bool do_childs, updateOperationFunc func, void** params) void GridAccount::MassUpdate(std::vector<int>& rows, bool do_childs, updateOperationFunc func, void** params)

View File

@ -91,14 +91,20 @@ private:
void ResetWeeks(); void ResetWeeks();
void ComputeWeeks(); void ComputeWeeks();
void InsertIntoGrid(Operation& op); bool CheckDay(User* user, const Operation& op, int& i);
void DeleteOperation(const Operation& op);
bool IsMetaOpened(int id);
void OpenMeta(const Operation& meta);
int InsertIntoGrid(Operation& op);
void DeleteOperation(const Operation& op) throw (OperationNotFound);
void RemoveRow(const Operation& op, int line, bool deleteOp);
void CheckOperation(Operation& op, int line, bool check, bool force);
void UpdateMeta(Operation& op); void UpdateMeta(Operation& op);
int RemoveMeta(Operation op, int line, bool removeRoot, bool deleteOp); int RemoveMeta(Operation op, int line, bool removeRoot, bool deleteOp);
void CheckMeta(Operation& op, int line, bool check); void CheckMeta(Operation& op, int line, bool check);
Operation& GetOperation(int id) throw(OperationNotFound); Operation& GetOperation(int id) throw(OperationNotFound);
void UpdateOperation(Operation& op); void UpdateOperation(Operation& op);
int GetDisplayedRow(int id); int GetDisplayedRow(int id) throw (OperationNotFound);
}; };
#endif #endif