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
* 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
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
complexity in searching this issue.
* When changing date in a sub operation (set date > main date),
meta amount is set to 0
* After manipulating some meta operation, it withdrawn

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);
if (it != _displayedOperations.end()) return it-_displayedOperations.begin();
return -1;
throw OperationNotFound();
}
void GridAccount::ClearGrid()
@ -347,7 +347,7 @@ void GridAccount::InsertOperation(User* user, Operation& op, int line, bool fix,
// // First is header
// if (op.id)
_displayedOperations.insert(_displayedOperations.begin()+line, op);
_displayedOperations.insert(_displayedOperations.begin()+line, op);
if (!user->_accounts.size()) return;
@ -433,6 +433,7 @@ void GridAccount::InsertOperation(User* user, Operation& op, int line, bool fix,
}
else
{
// NULL Op
item = new QTableWidgetItem("");
setItem(line, DESCRIPTION, item);
if (fix)
@ -498,16 +499,74 @@ void GridAccount::InsertOperation(User* user, Operation& op, int line, bool fix,
_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<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();
Operation parent;
@ -520,61 +579,81 @@ void GridAccount::InsertIntoGrid(Operation& op)
else
{
if (op.parent)
start = GetDisplayedRow(op.parent);
else
start = 0;
for(i=start; i<(int)_displayedOperations.size(); i++)
{
if (!_displayedOperations[i].id) continue;
if (_displayedOperations[i].parent) continue;
if ((_displayedOperations)[i].fix_cost && !op.fix_cost) continue;
if (!(_displayedOperations)[i].fix_cost && op.fix_cost) break;
if (user->_preferences["operation_order"] == "ASC")
start = GetDisplayedRow(op.parent)+1;
for(i=start; i<(int)_displayedOperations.size(); i++)
{
if ((_displayedOperations)[i].day > op.day)
break;
if (!_displayedOperations[i].parent) break;
if (_displayedOperations[i].id == 0) break;
if (CheckDay(user, op, i)) break;
}
}
else
{
if (op.fix_cost)
{
for(i=1; i<(int)_fixCosts; i++)
{
if (_displayedOperations[i].parent) continue;
if (_displayedOperations[i].id == 0) break;
if (CheckDay(user, op, i)) break;
}
}
else
{
if ((_displayedOperations)[i].day < op.day)
break;
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;
}
}
}
if (op.parent)
{
if ((i-start) > (int)(parent.childs.size()))
i = start + parent.childs.size();
if (parent.day >= op.day)
i = start + 1;
}
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++)
{
if ((*_operations)[a].fix_cost && !op.fix_cost) continue;
if (!(*_operations)[a].fix_cost && op.fix_cost)
{
a--;
break;
}
if ((*_operations)[a].day > op.day)
{
a--;
break;
}
if (!(*_operations)[a].fix_cost && op.fix_cost) break;
if ((*_operations)[a].day > op.day) break;
}
if (a < 0) a = 0;
_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)
@ -588,23 +667,14 @@ int GridAccount::RemoveMeta(Operation op, int line, bool removeRoot, bool delete
{
op2 = GetOperation(op.childs[i]);
if (op2.meta)
RemoveMeta(op2, line+1, true, deleteOp);
deletedOperations += 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);
}
{
RemoveRow(op2, line+1, deleteOp);
deletedOperations++;
}
}
}
@ -612,21 +682,8 @@ int GridAccount::RemoveMeta(Operation op, int line, bool removeRoot, bool delete
if (removeRoot)
{
button->disconnect(&_treeSignalMapper, SLOT(map()));
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++;
RemoveRow(op, line, deleteOp);
deletedOperations++;
}
return 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)
{
int i, new_line;
int i;
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);
for(i=0; i<(int)op.childs.size(); i++)
if (IsMetaOpened(op.id))
{
op2 = GetOperation(op.childs[i]);
op2.checked = check;
UpdateOperation(op2);
for(i=0; i<(int)op.childs.size(); i++)
{
op2 = GetOperation(op.childs[i]);
if (op2.meta)
CheckMeta(op2, line+1, check);
if (button->text() == "-")
{
QCheckBox* checkBox = qobject_cast<QCheckBox*>(cellWidget(line+i+1, CHECKED));
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 ;
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);
}
new_line = line+i+1;
SET_ROW_COLOR(new_line, color, user->GetCategory(op2.category).forecolor);
}
if (op2.meta)
CheckMeta(op2, line+i+1, check);
else
CheckOperation(op2, line+i+1, check, false);
}
}
else
{
for(i=0; i<(int)op.childs.size(); i++)
{
op2 = GetOperation(op.childs[i]);
if (op2.meta)
CheckMeta(op2, line+i+1, check);
else
{
op2.checked = check;
UpdateOperation(op2);
}
}
}
}
bool GridAccount::IsMetaOpened(int id)
{
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)
@ -688,12 +757,12 @@ void GridAccount::OnMetaClicked(int id)
row = it - _displayedOperations.begin();
if (button->text() == "+")
if (button->text() == QString("+"))
{
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);
InsertOperation(user, op2, row+i, op2.fix_cost, op2.month, op2.year);
}
button->setText("-");
}
@ -702,21 +771,20 @@ void GridAccount::OnMetaClicked(int id)
RemoveMeta(op, row, false, false);
button->setText("+");
}
ComputeWeeks();
}
void GridAccount::OnCheckClicked(int id)
{
std::vector<Operation>::iterator it;
std::vector<int>::iterator it2;
int row;
Operation op2;
QColor color;
User* user = _kiss->GetUser();
unsigned char r, g, b;
Operation op2, parent;
bool fullCheck = true;
if (_inModification || _loadOperations) return;
QCheckBox* checkBox = qobject_cast<QCheckBox*> (_checkSignalMapper.mapping(id));
it = std::find(_displayedOperations.begin(), _displayedOperations.end(), id);
if (it == _displayedOperations.end()) return ;
@ -724,45 +792,26 @@ void GridAccount::OnCheckClicked(int id)
_inModification = true;
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)
CheckMeta(*it, row, it->checked);
CheckMeta(*it, row, !it->checked);
else
{
CheckOperation(*it, row, !it->checked, false);
if (it->parent)
{
op2 = GetOperation(it->parent);
UpdateMeta(op2);
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)
parent = GetOperation(it->parent);
for(it2=parent.childs.begin(); it2!=parent.childs.end(); it2++)
{
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);
op2 = GetOperation(*it2);
if (!op2.checked)
{
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)
{
std::vector<Operation>::iterator it;
std::vector<int>::iterator it2;
int row;
int row, parentRow;
Operation op, op2, parent;
User* user = _kiss->GetUser();
Operation op, op_tmp, op_tmp2;
QColor color;
unsigned char r, g, b;
if (_inModification || _loadOperations) return;
@ -802,73 +848,35 @@ void GridAccount::OnDeleteClicked(int id)
row = it-_displayedOperations.begin();
if (op.parent)
user->UnGroup(_displayedOperations[row]);
if (op.meta)
RemoveMeta(_displayedOperations[row], row, true, true);
RemoveMeta(op, row, true, true);
else
{
if (op.parent)
{
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);
RemoveRow(op, row, true);
if (op.parent)
{
if (op_tmp.childs.size() < 2)
user->UnGroup(op);
parent = GetOperation(op.parent);
parentRow = GetDisplayedRow(parent.id);
// One child remains
if (parent.childs.size() == 1)
{
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);
removeRow(row);
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)
{
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(op.category).forecolor);
SET_ROW_FONT(row, user->GetCategoryFont(op.category));
op = GetOperation(parent.childs[0]);
user->UnGroup(op);
// Remove parent
RemoveRow(parent, parentRow, true);
// Remove child
op.parent = 0;
UpdateOperation(op);
RemoveRow(op, parentRow, false);
InsertIntoGrid(op);
}
else
{
UpdateMeta(op_tmp);
row = GetDisplayedRow(op_tmp.id);
RemoveMeta(op_tmp, row, true, false);
InsertIntoGrid(op_tmp);
}
UpdateMeta(parent);
}
if (op.fix_cost)
_fixCosts--;
ComputeWeeks();
}
_kiss->UpdateStats();
@ -879,19 +887,17 @@ void GridAccount::OnOperationModified(int row, int col)
{
User* user = _kiss->GetUser();
Operation new_op, cur_op, op_tmp, op_tmp2;
int op_complete = 6, i, last_day;
int op_complete = 6;
QString value, v ;
QDate date;
bool need_insertion = false, transfertCompleted = false;
QColor color ;
unsigned char r, g, b;
std::vector<int>::iterator it;
Operation op, op2;
int amount;
std::vector<Operation>::iterator it2;
Operation op, op2, parent;
QFont font;
Category cat ;
bool fix_cost;
QDate curDate = QDate::currentDate();
Operation NULLop;
// Avoid recursives calls
if (_inModification || _loadOperations) return;
@ -1005,19 +1011,6 @@ void GridAccount::OnOperationModified(int row, int col)
new_op.checked = false;
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);
// Modify an operation
@ -1033,20 +1026,32 @@ void GridAccount::OnOperationModified(int row, int col)
new_op.childs = cur_op.childs;
new_op._virtual = cur_op._virtual;
UpdateOperation(new_op);
if (cur_op.day != new_op.day)
{
need_insertion = true;
removeRow(row);
if (fix_cost)
_fixCosts--;
// 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;
RemoveRow(new_op, row, false);
}
else
{
(_displayedOperations)[row] = new_op;
cat = user->GetCategory(new_op.category);
CheckOperation(new_op, row, new_op.checked, true);
SET_ROW_FONT(row, user->GetCategoryFont(cat.id));
}
(_displayedOperations)[row] = new_op;
UpdateOperation(new_op);
}
// Add an operation
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) {
_inModification = false ;
return ;
@ -1057,49 +1062,9 @@ void GridAccount::OnOperationModified(int row, int col)
new_op._virtual = false;
new_op.parent = 0;
for(i=0; i<NUMBER_COLS_OPS; i++)
{
if (fix_cost && i == CATEGORY) continue;
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);
RemoveRow(new_op, row, false);
NULLop.id = 0;
InsertOperation(user, NULLop, row, new_op.fix_cost, _curMonth, _curYear);
new_op.id = _kiss->AddOperation(new_op);
@ -1115,101 +1080,8 @@ void GridAccount::OnOperationModified(int row, int col)
if (new_op.parent)
{
row = GetDisplayedRow(new_op.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("+");
}
parent = GetOperation(new_op.parent);
UpdateMeta(parent);
}
_kiss->UpdateStats();
@ -1219,20 +1091,24 @@ void GridAccount::OnOperationModified(int row, int col)
void GridAccount::UpdateMeta(Operation& meta)
{
std::vector<int>::iterator it;
std::vector<Operation>::iterator it2;
Operation op ;
int category = 0;
bool updateCat = false ;
bool openMeta;
if (!meta.childs.size()) return ;
openMeta = IsMetaOpened(meta.id);
meta.category = 0;
meta.checked = true;
meta.amount = 0;
op = GetOperation(meta.childs[0]);
meta.year = op.year;
meta.month = op.month;
meta.day = op.day;
meta.description = "";
for(it=meta.childs.begin(); it!=meta.childs.end(); it++)
{
@ -1243,7 +1119,6 @@ void GridAccount::UpdateMeta(Operation& meta)
meta.month = op.month;
meta.day = op.day;
}
meta.checked &= op.checked;
if (!meta.description.length() && op.description.length())
meta.description = op.description;
if (!category)
@ -1267,8 +1142,17 @@ void GridAccount::UpdateMeta(Operation& meta)
meta.amount = _kiss->MetaAmount(meta.id);
if (_databaseSynchronization)
_kiss->UpdateOperation(meta);
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()
@ -1281,8 +1165,9 @@ void GridAccount::Group()
int parent = 0, deletedRows;
Operation op, op2;
int fix = -1, i, a, row;
User* user = _kiss->GetUser();
QModelIndexList selected = selectedIndexes();
bool fullCheck = true;
for (int i = 0; i < selected.size(); ++i)
{
@ -1366,9 +1251,7 @@ void GridAccount::Group()
deletedRows = RemoveMeta(ops[i], rows[i], true, false);
else
{
if (ops[i].fix_cost) _fixCosts--;
removeRow(rows[i]);
_displayedOperations.erase(_displayedOperations.begin()+rows[i]);
RemoveRow(ops[i], rows[i], false);
deletedRows = 1;
}
for(a=i+1; a<(int)rows.size(); a++)
@ -1383,9 +1266,19 @@ void GridAccount::Group()
for (i=0, it3=op.childs.begin(); it3!=op.childs.end(); it3++, i++)
{
op2 = GetOperation(*it3);
if (*it3 == it2->id ||
op2.day > it2->day)
break;
if (*it3 == it2->id)
{
if (user->_preferences["operation_order"] == "ASC")
{
if (op2.day > it2->day)
break;
}
else
{
if (op2.day < it2->day)
break;
}
}
}
if (i) i--;
@ -1399,11 +1292,16 @@ void GridAccount::Group()
it2->parent = op.id;
UpdateOperation(*it2);
if (!it2->checked)
fullCheck = false;
}
row = InsertIntoGrid(op);
UpdateMeta(op);
InsertIntoGrid(op);
if (fullCheck)
CheckMeta(op, row, true);
}
void GridAccount::GetSelectedOperations(std::vector<int>* rows)
@ -1433,7 +1331,7 @@ void GridAccount::UnGroup()
std::vector<int>::iterator it3;
int parent = 0;
Operation op, op2;
int fix = -1, i, a, line;
int fix = -1, i, line;
QModelIndexList selected = selectedIndexes();
@ -1475,6 +1373,8 @@ void GridAccount::UnGroup()
if (!ops.size() && !parent) return;
_inModification = true;
removeLastGroup:
// Only one meta is selected
if (!ops.size())
@ -1492,7 +1392,6 @@ removeLastGroup:
InsertIntoGrid(op2);
}
_kiss->DeleteOperation(op);
DeleteOperation(op);
}
else
@ -1508,19 +1407,14 @@ removeLastGroup:
op.parent = 0;
UpdateOperation(op);
line = GetDisplayedRow(op.id);
removeRow(line);
_displayedOperations.erase(_displayedOperations.begin()+line);
InsertIntoGrid(GetOperation(op.id));
if (op.fix_cost) _fixCosts--;
for (a=0; a<(int)op2.childs.size(); a++)
if (op2.childs[a] == op.id)
{
op2.childs.erase(op2.childs.begin()+a);
break;
}
UpdateMeta(op2);
RemoveRow(op, line, false);
InsertIntoGrid(op);
it = std::find(op2.childs.begin(), op2.childs.end(), op.id);
if (it != op2.childs.end())
op2.childs.erase(it);
}
UpdateMeta(op2);
line = GetDisplayedRow(parent);
_displayedOperations[line] = op2;
@ -1534,7 +1428,7 @@ removeLastGroup:
UpdateOperation(op2);
}
ComputeWeeks();
_inModification = false;
}
void GridAccount::MassUpdate(std::vector<int>& rows, bool do_childs, updateOperationFunc func, void** params)

View File

@ -91,14 +91,20 @@ private:
void ResetWeeks();
void ComputeWeeks();
void InsertIntoGrid(Operation& op);
void DeleteOperation(const Operation& op);
bool CheckDay(User* user, const Operation& op, int& i);
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);
int RemoveMeta(Operation op, int line, bool removeRoot, bool deleteOp);
void CheckMeta(Operation& op, int line, bool check);
Operation& GetOperation(int id) throw(OperationNotFound);
void UpdateOperation(Operation& op);
int GetDisplayedRow(int id);
int GetDisplayedRow(int id) throw (OperationNotFound);
};
#endif