Add a constructor for Operation (to clear all fields)

Setup default date when insert an operation in GridAccount
This commit is contained in:
Grégory Soutadé 2020-04-19 16:47:35 +02:00
parent 196a0cc5ab
commit c57405a407
2 changed files with 69 additions and 58 deletions

View File

@ -42,6 +42,26 @@ struct Operation {
std::vector<int> childs; std::vector<int> childs;
int tag; int tag;
Operation()
{
id = 0;
parent = 0;
day = 0;
month = 0;
year = 0;
amount = 0.0;
description = "";
category = 0;
fix_cost = false;
account = 0;
checked = 0;
transfert = 0;
formula = "";
meta = false;
_virtual = false;
tag = 0;
}
bool operator == (int opId) bool operator == (int opId)
{ {
return id == opId; return id == opId;

View File

@ -45,15 +45,15 @@
#define SET_READ_ONLY(item) item->setFlags(item->flags() & ~Qt::ItemIsEditable); #define SET_READ_ONLY(item) item->setFlags(item->flags() & ~Qt::ItemIsEditable);
GridAccount::GridAccount(KissCount* kiss, KissPanel *parent, GridAccount::GridAccount(KissCount* kiss, KissPanel *parent,
bool canAddOperation, bool setWeek, bool synchronizeWithDatabase) bool canAddOperation, bool setWeek, bool synchronizeWithDatabase)
: QTableWidget(parent), _fixCosts(0), _week1(0), : QTableWidget(parent), _fixCosts(0), _week1(0),
_week2(0), _week3(0), _week4(0), _week5(0), _canAddOperation(canAddOperation), _week2(0), _week3(0), _week4(0), _week5(0), _canAddOperation(canAddOperation),
_parent(parent), _kiss(kiss), _setWeek(setWeek), _parent(parent), _kiss(kiss), _setWeek(setWeek),
_databaseSynchronization(synchronizeWithDatabase), _accounts(0), _databaseSynchronization(synchronizeWithDatabase), _accounts(0),
_loadOperations(false), _loadOperations(false),
_curMonth(0), _curYear(0), _treeSignalMapper(this), _checkSignalMapper(this), _curMonth(0), _curYear(0), _treeSignalMapper(this), _checkSignalMapper(this),
_deleteSignalMapper(this), _inModification(false), _completer(0), _deleteSignalMapper(this), _inModification(false), _completer(0),
_transfertCompletionIndex(0), _ctrlT(0), _ctrlR(0), _suppr(0) _transfertCompletionIndex(0), _lastCtrlH(0), _ctrlT(0), _ctrlR(0), _suppr(0)
{ {
DEFAULT_FONT(font); DEFAULT_FONT(font);
int i; int i;
@ -90,8 +90,8 @@ GridAccount::GridAccount(KissCount* kiss, KissPanel *parent,
setCellWidget(0, CHECKED, label); setCellWidget(0, CHECKED, label);
_categories = new QString[user->GetCategoriesNumber()-1] ; _categories = new QString[user->GetCategoriesNumber()-1] ;
for(i=0, categoryIt = user->_categories.begin()+1; for(i=0, categoryIt = user->_categories.begin()+1;
categoryIt != user->_categories.end(); categoryIt != user->_categories.end();
categoryIt++, i++) categoryIt++, i++)
{ {
_categories[i] = _(categoryIt->name.toStdString().c_str()) ; _categories[i] = _(categoryIt->name.toStdString().c_str()) ;
@ -99,8 +99,8 @@ GridAccount::GridAccount(KissCount* kiss, KissPanel *parent,
_tags = new QString[user->GetTagsNumber()+1] ; _tags = new QString[user->GetTagsNumber()+1] ;
_tags[0] = _("No Tag"); _tags[0] = _("No Tag");
for(i=1, tagIt = user->_tags.begin(); for(i=1, tagIt = user->_tags.begin();
tagIt != user->_tags.end(); tagIt != user->_tags.end();
tagIt++, i++) tagIt++, i++)
{ {
_tags[i] = _(tagIt->name.toStdString().c_str()) ; _tags[i] = _(tagIt->name.toStdString().c_str()) ;
@ -162,7 +162,7 @@ Operation& GridAccount::GetOperation(int id) /*throw (OperationNotFound)*/
std::vector<Operation>::iterator it = std::find(_operations->begin(), _operations->end(), id); std::vector<Operation>::iterator it = std::find(_operations->begin(), _operations->end(), id);
if (it != _operations->end()) return *it; if (it != _operations->end()) return *it;
throw OperationNotFound(); throw OperationNotFound();
} }
@ -203,23 +203,9 @@ void GridAccount::LoadOperations(std::vector<Operation>* operations, int month,
User* user = _kiss->GetUser(); User* user = _kiss->GetUser();
std::vector<Account>::iterator accountIt; std::vector<Account>::iterator accountIt;
int curLine = 0, i; int curLine = 0, i;
Operation NULLop; Operation NULLop = Operation();
QStringList list; QStringList list;
NULLop.id = 0;
NULLop.parent = 0;
NULLop.amount = 0.0;
NULLop.description = "";
NULLop.category = 0;
NULLop.tag = 0;
NULLop.fix_cost = false;
NULLop.account = 0;
NULLop.checked = 0;
NULLop.transfert = 0;
NULLop.formula = "";
NULLop.meta = false;
NULLop._virtual = false;
_loadOperations = true; _loadOperations = true;
_operations = operations; _operations = operations;
_curMonth = month; _curMonth = month;
@ -258,7 +244,7 @@ void GridAccount::LoadOperations(std::vector<Operation>* operations, int month,
if (_accounts) delete[] _accounts; if (_accounts) delete[] _accounts;
_accounts = new QString[_nbAccounts]; _accounts = new QString[_nbAccounts];
for (i=0, for (i=0,
accountIt = user->_accounts.begin(); accountIt = user->_accounts.begin();
accountIt != user->_accounts.end(); accountIt != user->_accounts.end();
accountIt++, i++) accountIt++, i++)
@ -344,7 +330,7 @@ void GridAccount::ComputeWeeks()
if (!_canAddOperation) return; if (!_canAddOperation) return;
for (it = _displayedOperations.begin(), curLine=0; for (it = _displayedOperations.begin(), curLine=0;
it != _displayedOperations.end(); it != _displayedOperations.end();
it++, curLine++) it++, curLine++)
{ {
if (it->id && !it->fix_cost) break; if (it->id && !it->fix_cost) break;
@ -463,7 +449,7 @@ void GridAccount::InsertOperation(User* user, Operation& op, int line, bool fix,
setItem(line, TAG, new QTableWidgetItem(_(tag.name.toStdString().c_str()))); setItem(line, TAG, new QTableWidgetItem(_(tag.name.toStdString().c_str())));
else else
setItem(line, TAG, new QTableWidgetItem("")); setItem(line, TAG, new QTableWidgetItem(""));
checkBox = new QCheckBox(); checkBox = new QCheckBox();
checkBox->setCheckState((op.checked) ? Qt::Checked : Qt::Unchecked); checkBox->setCheckState((op.checked) ? Qt::Checked : Qt::Unchecked);
InsertCenteredWidget(line, CHECKED, checkBox); InsertCenteredWidget(line, CHECKED, checkBox);
@ -480,7 +466,7 @@ void GridAccount::InsertOperation(User* user, Operation& op, int line, bool fix,
if (line <= _fixCosts) if (line <= _fixCosts)
cat = user->GetCategory(1); cat = user->GetCategory(1);
if (op.category) if (op.category)
color = cat.backcolor; color = cat.backcolor;
else else
@ -490,9 +476,9 @@ void GridAccount::InsertOperation(User* user, Operation& op, int line, bool fix,
if (op.checked) if (op.checked)
{ {
r = ((color.red()*1.5) >= 0xFF) ? 0xFF : color.red()*1.5 ; r = ((color.red()*1.5) >= 0xFF) ? 0xFF : color.red()*1.5 ;
g = ((color.green()*1.5) >= 0xFF) ? 0xFF : color.green()*1.5 ; g = ((color.green()*1.5) >= 0xFF) ? 0xFF : color.green()*1.5 ;
b = ((color.blue()*1.5) >= 0xFF) ? 0xFF : color.blue()*1.5 ; b = ((color.blue()*1.5) >= 0xFF) ? 0xFF : color.blue()*1.5 ;
color.setRgb(r, g, b, color.alpha()); color.setRgb(r, g, b, color.alpha());
} }
@ -514,6 +500,11 @@ void GridAccount::InsertOperation(User* user, Operation& op, int line, bool fix,
// NULL Op // NULL Op
item = new QTableWidgetItem(""); item = new QTableWidgetItem("");
setItem(line, DESCRIPTION, item); setItem(line, DESCRIPTION, item);
op.month = _curMonth;
op.year = _curYear;
op.day = 0;
if (fix) if (fix)
{ {
SET_ROW_COLOR(line, view::OWN_YELLOW, Qt::black); SET_ROW_COLOR(line, view::OWN_YELLOW, Qt::black);
@ -536,6 +527,7 @@ void GridAccount::InsertOperation(User* user, Operation& op, int line, bool fix,
else else
day = 1; day = 1;
op.day = day-1;
setItem(line, OP_DATE, new QTableWidgetItem(_kiss->FormatDate(day, month+1, year))); setItem(line, OP_DATE, new QTableWidgetItem(_kiss->FormatDate(day, month+1, year)));
SET_ROW_COLOR(line, view::OWN_GREEN, Qt::black); SET_ROW_COLOR(line, view::OWN_GREEN, Qt::black);
} }
@ -543,7 +535,7 @@ void GridAccount::InsertOperation(User* user, Operation& op, int line, bool fix,
SET_READ_ONLY(this->item(line, CHECKED)); SET_READ_ONLY(this->item(line, CHECKED));
SET_ROW_FONT(line, user->GetCategoryFont(0)); SET_ROW_FONT(line, user->GetCategoryFont(0));
} }
this->item(line, OP_DATE)->setTextAlignment(Qt::AlignHCenter|Qt::AlignVCenter); this->item(line, OP_DATE)->setTextAlignment(Qt::AlignHCenter|Qt::AlignVCenter);
this->item(line, DEBIT)->setTextAlignment(Qt::AlignRight|Qt::AlignVCenter); this->item(line, DEBIT)->setTextAlignment(Qt::AlignRight|Qt::AlignVCenter);
this->item(line, CREDIT)->setTextAlignment(Qt::AlignRight|Qt::AlignVCenter); this->item(line, CREDIT)->setTextAlignment(Qt::AlignRight|Qt::AlignVCenter);
@ -554,7 +546,7 @@ void GridAccount::InsertOperation(User* user, Operation& op, int line, bool fix,
QPushButton* button = new QPushButton("+"); QPushButton* button = new QPushButton("+");
_treeSignalMapper.setMapping(button, op.id); _treeSignalMapper.setMapping(button, op.id);
connect(button, SIGNAL(clicked()), &_treeSignalMapper, SLOT(map())); connect(button, SIGNAL(clicked()), &_treeSignalMapper, SLOT(map()));
button->setMaximumSize(QSize(height, height)); button->setMaximumSize(QSize(height, height));
InsertCenteredWidget(line, TREE, button); InsertCenteredWidget(line, TREE, button);
@ -600,7 +592,7 @@ void GridAccount::DeleteOperation(const Operation& op) /*throw (OperationNotFoun
{ {
parent = GetOperation(it->parent); parent = GetOperation(it->parent);
it2 = std::find(parent.childs.begin(), parent.childs.end(), it->id); it2 = std::find(parent.childs.begin(), parent.childs.end(), it->id);
if (it2 != parent.childs.end()) if (it2 != parent.childs.end())
parent.childs.erase(it2); parent.childs.erase(it2);
} }
@ -730,9 +722,9 @@ void GridAccount::CheckOperation(Operation& op, int line, bool check, bool force
if (check) if (check)
{ {
r = ((color.red()*1.5) >= 0xFF) ? 0xFF : color.red()*1.5 ; r = ((color.red()*1.5) >= 0xFF) ? 0xFF : color.red()*1.5 ;
g = ((color.green()*1.5) >= 0xFF) ? 0xFF : color.green()*1.5 ; g = ((color.green()*1.5) >= 0xFF) ? 0xFF : color.green()*1.5 ;
b = ((color.blue()*1.5) >= 0xFF) ? 0xFF : color.blue()*1.5 ; b = ((color.blue()*1.5) >= 0xFF) ? 0xFF : color.blue()*1.5 ;
color.setRgb(r, g, b); color.setRgb(r, g, b);
} }
SET_ROW_COLOR(line, color, user->GetCategory(op.category).forecolor); SET_ROW_COLOR(line, color, user->GetCategory(op.category).forecolor);
@ -762,7 +754,7 @@ int GridAccount::RemoveMeta(Operation op, int line, bool removeRoot, bool delete
if (deleteOp) if (deleteOp)
DeleteOperation(op2); DeleteOperation(op2);
} }
} }
} }
op.childs.clear(); op.childs.clear();
@ -980,14 +972,14 @@ void GridAccount::OnOperationModified(int row, int col)
Category cat ; Category cat ;
Tag tag; Tag tag;
bool fix_cost; bool fix_cost;
Operation NULLop; Operation NULLop = Operation();
Account account; Account account;
// Avoid recursives calls // Avoid recursives calls
if (_inModification || _loadOperations) return; if (_inModification || _loadOperations) return;
_inModification = true ; _inModification = true ;
cur_op = (_displayedOperations)[row] ; cur_op = (_displayedOperations)[row] ;
new_op.id = 0; new_op.id = 0;
@ -1028,7 +1020,7 @@ void GridAccount::OnOperationModified(int row, int col)
new_op.year = date.year(); new_op.year = date.year();
op_complete--; op_complete--;
} }
if (!cur_op.meta && col == DESCRIPTION && if (!cur_op.meta && col == DESCRIPTION &&
(!item(row, CATEGORY)->text().length() || (!item(row, CATEGORY)->text().length() ||
!item(row, ACCOUNT)->text().length())) !item(row, ACCOUNT)->text().length()))
@ -1127,7 +1119,7 @@ void GridAccount::OnOperationModified(int row, int col)
fix_cost = (row <= _fixCosts); fix_cost = (row <= _fixCosts);
// Modify an operation // Modify an operation
if (!_canAddOperation || (row < _fixCosts || if (!_canAddOperation || (row < _fixCosts ||
(row > _fixCosts && (row > _fixCosts &&
row < (int)(_displayedOperations.size()-1)))) row < (int)(_displayedOperations.size()-1))))
{ {
@ -1138,7 +1130,7 @@ void GridAccount::OnOperationModified(int row, int col)
new_op.parent = cur_op.parent; new_op.parent = cur_op.parent;
new_op.childs = cur_op.childs; new_op.childs = cur_op.childs;
new_op._virtual = account._virtual; new_op._virtual = account._virtual;
UpdateOperation(new_op); UpdateOperation(new_op);
if (cur_op.day != new_op.day) if (cur_op.day != new_op.day)
@ -1192,11 +1184,10 @@ void GridAccount::OnOperationModified(int row, int col)
} }
RemoveRow(new_op, row, false); RemoveRow(new_op, row, false);
NULLop.id = 0;
InsertOperation(user, NULLop, row, new_op.fix_cost, _curMonth, _curYear); InsertOperation(user, NULLop, row, new_op.fix_cost, _curMonth, _curYear);
new_op.id = new_op_id; new_op.id = new_op_id;
if (transfertCompleted) if (transfertCompleted)
_transfertCompletionIndex = (_transfertCompletionIndex + 1) % 2; _transfertCompletionIndex = (_transfertCompletionIndex + 1) % 2;
} }
@ -1312,7 +1303,7 @@ void GridAccount::Group()
std::vector<int>::iterator it; std::vector<int>::iterator it;
std::vector<Operation> ops; std::vector<Operation> ops;
std::vector<Operation>::iterator it2; std::vector<Operation>::iterator it2;
std::vector<int>::iterator it3; std::vector<int>::iterator it3;
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;
@ -1323,7 +1314,7 @@ void GridAccount::Group()
for (int i = 0; i < selected.size(); ++i) for (int i = 0; i < selected.size(); ++i)
{ {
row = selected[i].row(); row = selected[i].row();
it = std::find(rows.begin(), rows.end(), row); it = std::find(rows.begin(), rows.end(), row);
if (it != rows.end()) if (it != rows.end())
@ -1363,12 +1354,12 @@ void GridAccount::Group()
if (fix == -1) if (fix == -1)
fix = op.fix_cost ? 1 : 0; fix = op.fix_cost ? 1 : 0;
ops.push_back(op); ops.push_back(op);
rows.push_back(row); rows.push_back(row);
} }
} }
if (!ops.size()) return; if (!ops.size()) return;
if (!parent) if (!parent)
@ -1444,7 +1435,7 @@ void GridAccount::Group()
if (it3 == op.childs.end()) if (it3 == op.childs.end())
op.childs.push_back(it2->id); op.childs.push_back(it2->id);
else if (*it3 == it2->id) else if (*it3 == it2->id)
continue; continue;
else else
op.childs.insert(op.childs.begin()+i, it2->id); op.childs.insert(op.childs.begin()+i, it2->id);
@ -1496,7 +1487,7 @@ void GridAccount::UnGroup()
std::vector<Operation> ops; std::vector<Operation> ops;
std::vector<int> ops2; std::vector<int> ops2;
std::vector<Operation>::iterator it2; std::vector<Operation>::iterator it2;
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, line; int fix = -1, i, line;
@ -1529,13 +1520,13 @@ void GridAccount::UnGroup()
if (fix == -1) if (fix == -1)
fix = op.fix_cost ? 1 : 0; fix = op.fix_cost ? 1 : 0;
if(op.meta) if(op.meta)
{ {
parent = op.id; parent = op.id;
continue; continue;
} }
if (!parent && op.parent) if (!parent && op.parent)
parent = op.parent; parent = op.parent;
@ -1624,7 +1615,7 @@ void GridAccount::MassUpdate(std::vector<int>& rows, bool do_childs, updateOpera
UpdateOperation(op); UpdateOperation(op);
if (op.meta && do_childs) if (op.meta && do_childs)
{ {
for(b=0; b<(int)op.childs.size(); b++) for(b=0; b<(int)op.childs.size(); b++)
{ {
@ -1646,7 +1637,7 @@ void GridAccount::MassUpdate(std::vector<int>& rows, bool do_childs, updateOpera
_parent->NeedReload(); _parent->NeedReload();
} }
if (it->meta && do_childs) if (it->meta && do_childs)
{ {
for(b=0; b<(int)it->childs.size(); b++) for(b=0; b<(int)it->childs.size(); b++)
{ {
@ -1753,7 +1744,7 @@ void GridAccount::OnSuppr(void)
for (int i = 0; i < selected.size(); ++i) for (int i = 0; i < selected.size(); ++i)
{ {
row = selected[i].row(); row = selected[i].row();
if (row == _fixCosts || row == (int)_displayedOperations.size()-1) if (row == _fixCosts || row == (int)_displayedOperations.size()-1)
continue; continue;