/* Copyright 2010-2016 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 "GridAccount.hpp" #include "TableViewDelegate.hpp" #include "ChoiceDelegate.hpp" #include "DateDelegate.hpp" #include "FloatDelegate.hpp" #include "FormulaDelegate.hpp" #include "TabDelegate.hpp" #include "TransfertDialog.hpp" #define SET_ROW_COLOR(row, backcolor, forecolor) for(int i=0; iitem(row, i)) setItem(row, i, new QTableWidgetItem("")); \ this->item(row, i)->setBackground(backcolor); \ this->item(row, i)->setForeground(forecolor); \ } #define SET_ROW_FONT(row, font) for(int i=0; iitem(row, i)) setItem(row, i, new QTableWidgetItem("")); \ this->item(row, i)->setFont(font); \ } #define SET_READ_ONLY(item) item->setFlags(item->flags() & ~Qt::ItemIsEditable); GridAccount::GridAccount(KissCount* kiss, KissPanel *parent, bool canAddOperation, bool setWeek, bool synchronizeWithDatabase) : QTableWidget(parent), _fixCosts(0), _week1(0), _week2(0), _week3(0), _week4(0), _week5(0), _canAddOperation(canAddOperation), _parent(parent), _kiss(kiss), _setWeek(setWeek), _databaseSynchronization(synchronizeWithDatabase), _accounts(0), _loadOperations(false), _curMonth(0), _curYear(0), _treeSignalMapper(this), _checkSignalMapper(this), _deleteSignalMapper(this), _inModification(false), _completer(0), _transfertCompletionIndex(0), _ctrlT(0), _ctrlR(0), _suppr(0) { DEFAULT_FONT(font); int i; User* user = _kiss->GetUser(); std::vector::iterator categoryIt; std::vector::iterator tagIt; QTableWidgetItem* item; QLabel* label; setColumnCount(NUMBER_COLS_OPS); setRowCount(1); verticalHeader()->setHidden(true); horizontalHeader()->setHidden(true); setShowGrid(false); setColumnWidth (DESCRIPTION, columnWidth(DESCRIPTION)*3); setFont(font); font.setBold(true); QString colsName[] = {"", _("Description"), _("Date"), _("Debit"), _("Credit"), _("Category"), _("Tag"), _("Account"), "", ""}; for(i=0; isetBackground(view::OWN_CYAN); item->setFont(font); SET_READ_ONLY(item); item->setTextAlignment(Qt::AlignHCenter|Qt::AlignVCenter); setItem(0, i, item); } label = new QLabel(); label->setPixmap(QPixmap(CHECKED_ICON)); label->setAlignment(Qt::AlignHCenter | Qt::AlignVCenter); setCellWidget(0, CHECKED, label); _categories = new QString[user->GetCategoriesNumber()-1] ; for(i=0, categoryIt = user->_categories.begin()+1; categoryIt != user->_categories.end(); categoryIt++, i++) { _categories[i] = _(categoryIt->name.toStdString().c_str()) ; } _tags = new QString[user->GetTagsNumber()+1] ; _tags[0] = _("No Tag"); for(i=1, tagIt = user->_tags.begin(); tagIt != user->_tags.end(); tagIt++, i++) { _tags[i] = _(tagIt->name.toStdString().c_str()) ; } resizeColumnToContents(TREE); resizeColumnToContents(CATEGORY); resizeColumnToContents(TAG); resizeColumnToContents(OP_DATE); resizeColumnToContents(ACCOUNT); resizeColumnToContents(CHECKED); connect(&_treeSignalMapper, SIGNAL(mapped(int)), this, SLOT(OnMetaClicked(int))); connect(&_checkSignalMapper, SIGNAL(mapped(int)), this, SLOT(OnCheckClicked(int))); setItemDelegate(new TableViewDelegate(this)); connect(this, SIGNAL(cellChanged(int, int)), this, SLOT(OnOperationModified(int, int))); if (canAddOperation) { _ctrlT = new QShortcut(QKeySequence(Qt::CTRL + Qt::Key_T), this, SLOT(OnCtrlT())); _ctrlR = new QShortcut(QKeySequence(Qt::CTRL + Qt::Key_R), this, SLOT(OnCtrlR())); _suppr = new QShortcut(QKeySequence(Qt::Key_Delete), this, SLOT(OnSuppr())); } } GridAccount::~GridAccount() { delete[] _categories; delete[] _tags; if (_accounts) delete[] _accounts; if (_completer) delete _completer; if (_ctrlT) delete _ctrlT; if (_ctrlR) delete _ctrlR; if (_suppr) delete _suppr; } void GridAccount::ResetWeeks() { _week1 = _week2 = _week3 = _week4 = _week5 = 0; } void GridAccount::SetWeek(int week, int line) { switch (week) { case 1: _week1 = line; break; case 2: _week2 = line; break; case 3: _week3 = line; break; case 4: _week4 = line; break; case 5: _week5 = line; break; } } Operation& GridAccount::GetOperation(int id) /*throw (OperationNotFound)*/ { std::vector::iterator it = std::find(_operations->begin(), _operations->end(), id); if (it != _operations->end()) return *it; throw OperationNotFound(); } void GridAccount::UpdateOperation(Operation& op) { std::vector::iterator it = std::find(_operations->begin(), _operations->end(), op.id); if (it != _operations->end()) { if (_databaseSynchronization) { _kiss->UpdateOperation(op); _parent->NeedReload(); } *it = op; } } int GridAccount::GetDisplayedRow(int id) /*throw (OperationNotFound)*/ { std::vector::iterator it = std::find(_displayedOperations.begin(), _displayedOperations.end(), id); if (it != _displayedOperations.end()) return it-_displayedOperations.begin(); throw OperationNotFound(); } void GridAccount::ClearGrid() { std::vector operations; LoadOperations(&operations, 0, 0); } void GridAccount::LoadOperations(std::vector* operations, int month, int year) { std::vector::iterator it; User* user = _kiss->GetUser(); std::vector::iterator accountIt; int curLine = 0, i; Operation NULLop; 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; _operations = operations; _curMonth = month; _curYear = year; _displayedOperations.clear(); _displayedOperations.push_back(NULLop); // Header _fixCosts = 0; it = _operations->begin(); if (rowCount() > 1) setRowCount(1); if (_completer) { delete _completer; _completer = 0; } if (_canAddOperation) { _kiss->GetHistory(month, year, list); _completer = new QCompleter(list); _completer->setCaseSensitivity(Qt::CaseInsensitive); //_completer->setCompletionMode(QCompleter::InlineCompletion); } _nbAccounts = 0; for (accountIt = user->_accounts.begin(); accountIt != user->_accounts.end(); accountIt++) { if (!accountIt->hidden && accountIt->validAt(month, year)) _nbAccounts++; } if (_accounts) delete[] _accounts; _accounts = new QString[_nbAccounts]; for (i=0, accountIt = user->_accounts.begin(); accountIt != user->_accounts.end(); accountIt++, i++) { if (!accountIt->hidden && accountIt->validAt(month, year)) _accounts[i] = accountIt->name; else i--; } TabDelegate* descriptionEditor = new TabDelegate(this, &_displayedOperations, _completer); setItemDelegateForColumn(DESCRIPTION, descriptionEditor); ChoiceDelegate* categoryEditor = new ChoiceDelegate(this, _categories, user->GetCategoriesNumber()-1); setItemDelegateForColumn(CATEGORY, categoryEditor); ChoiceDelegate* tagEditor = new ChoiceDelegate(this, _tags, user->GetTagsNumber()+1); setItemDelegateForColumn(TAG, tagEditor); ChoiceDelegate* accountEditor = new ChoiceDelegate(this, _accounts, _nbAccounts); setItemDelegateForColumn(ACCOUNT, accountEditor); DateDelegate* dateEditor = new DateDelegate(this, month+1, year, _kiss->GetDateLocalFormat()); setItemDelegateForColumn(OP_DATE, dateEditor); FormulaDelegate* formulaEditor = new FormulaDelegate(this, &_displayedOperations); setItemDelegateForColumn(DEBIT, formulaEditor); setItemDelegateForColumn(CREDIT, formulaEditor); if (_canAddOperation) { for (;it != _operations->end() && it->fix_cost; it++) { if (it->parent) continue; if (_setWeek) InsertOperationWithWeek(user, *it, ++curLine, true, it->month, it->year); else InsertOperation(user, *it, ++curLine, true, it->month, it->year); } InsertOperation(user, NULLop, ++curLine, true, month, year); for (; it != _operations->end(); it++) { if (it->parent) continue; if (_setWeek) InsertOperationWithWeek(user, *it, ++curLine, false, it->month, it->year); else InsertOperation(user, *it, ++curLine, false, it->month, it->year); } InsertOperation(user, NULLop, ++curLine, false, month, year); } else { for (;it != _operations->end(); it++) { if (it->parent) continue; InsertOperation(user, *it, ++curLine, it->fix_cost, it->month, it->year); } } resizeColumnToContents(TREE); resizeColumnToContents(TAG); resizeColumnToContents(CATEGORY); resizeColumnToContents(OP_DATE); resizeColumnToContents(ACCOUNT); resizeColumnToContents(CHECKED); resizeRowsToContents(); _loadOperations = false; } void GridAccount::ComputeWeeks() { std::vector::iterator it; int curLine, curWeek, week, i; if (!_canAddOperation) return; for (it = _displayedOperations.begin(), curLine=0; it != _displayedOperations.end(); it++, curLine++) { if (it->id && !it->fix_cost) break; } if (it == _displayedOperations.end()) return; ResetWeeks(); curWeek = QDate(it->year, it->month+1, it->day+1).weekNumber(); it++; for (i=1; it != _displayedOperations.end(); it++, curLine++) { if (!it->id || it->parent) continue; week = QDate(it->year, it->month+1, it->day+1).weekNumber(); if (week != curWeek) { SetWeek(i++, curLine); curWeek = week; } } } void GridAccount::InsertOperationWithWeek(User* user, Operation& op, int line, bool fix, int month, int year) { InsertOperation(user, op, line, fix, month, year); if (op.id && !fix) ComputeWeeks(); } void GridAccount::InsertCenteredWidget(int line, int column, QWidget* widget, int margins) { QVBoxLayout* layout = new QVBoxLayout; layout->setAlignment(Qt::AlignHCenter|Qt::AlignVCenter); layout->setSizeConstraint(QLayout::SetMinimumSize); layout->setContentsMargins(margins, margins, margins, margins); layout->addWidget(widget); QWidget* fakeWidget = new QWidget; fakeWidget->setLayout(layout); setCellWidget(line, column, fakeWidget); } QWidget* GridAccount::GetCenteredWidget(int line, int column) { QWidget* fakeWidget= cellWidget(line, column); if (!fakeWidget) return 0; QLayout* layout = fakeWidget->layout(); QLayoutItem* layoutItem = layout->itemAt(0); if (!layoutItem) return 0; return layoutItem->widget(); } void GridAccount::InsertOperation(User* user, Operation& op, int line, bool fix, int month, int year) { std::vector::iterator it; std::vector::iterator it2; int r, g, b; int amount; QColor color; QDate curDate = QDate::currentDate(); QString description, v; DEFAULT_FONT(font); Category cat ; Tag tag; Operation op2; QTableWidgetItem* item; QCheckBox* checkBox; // // First is header // if (op.id) _displayedOperations.insert(_displayedOperations.begin()+line, op); if (!user->_accounts.size()) return; _inModification = true; insertRow(line); if (fix) { _fixCosts++; } if (op.id) { cat = user->GetCategory(op.category); tag = user->GetTag(op.tag); description = op.description; UNESCAPE_CHARS(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(_kiss->FormatDate(op.day+1, month+1, year))); if (op.amount < 0) { setItem(line, DEBIT, new QTableWidgetItem(v.sprintf("%.2lf", (double)-op.amount/100))); } else setItem(line, CREDIT, new QTableWidgetItem(v.sprintf("%.2lf", (double)op.amount/100))); if (!op.meta) setItem(line, ACCOUNT, new QTableWidgetItem(user->GetAccountName(op.account))); if (!op.meta) setItem(line, CATEGORY, new QTableWidgetItem(_(cat.name.toStdString().c_str()))); if (tag.id) setItem(line, TAG, new QTableWidgetItem(_(tag.name.toStdString().c_str()))); else setItem(line, TAG, new QTableWidgetItem("")); checkBox = new QCheckBox(); checkBox->setCheckState((op.checked) ? Qt::Checked : Qt::Unchecked); InsertCenteredWidget(line, CHECKED, checkBox); _checkSignalMapper.setMapping(checkBox, op.id); connect(checkBox, SIGNAL(stateChanged(int)), &_checkSignalMapper, SLOT(map())); if (op.meta && !op.amount) { amount = _kiss->MetaPositiveAmount(op.id); setItem(line, DEBIT, new QTableWidgetItem(v.sprintf("%.2lf", (double)amount/100))); setItem(line, CREDIT, new QTableWidgetItem(v.sprintf("%.2lf", (double)amount/100))); } if (line <= _fixCosts) cat = user->GetCategory(1); if (op.category) color = cat.backcolor; else { color = (op.fix_cost) ? view::OWN_YELLOW : view::OWN_GREEN; } 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, color.alpha()); } SET_ROW_COLOR(line, color, cat.forecolor); SET_ROW_FONT(line, user->GetCategoryFont(cat.id)); if (!_loadOperations) { if (op.meta) resizeColumnToContents(TREE); resizeColumnToContents(CATEGORY); resizeColumnToContents(TAG); resizeColumnToContents(OP_DATE); resizeColumnToContents(ACCOUNT); } } else { // NULL Op item = new QTableWidgetItem(""); setItem(line, DESCRIPTION, item); if (fix) { SET_ROW_COLOR(line, view::OWN_YELLOW, Qt::black); } else { int day; if (curDate.year() == year) { if (curDate.month() > (month+1)) day = QDate(year, month+1, 1).daysInMonth(); else if (curDate.month() < (month+1)) day = 1; else day = curDate.day(); } else if (curDate.year() > year) day = QDate(year, month+1, 1).daysInMonth(); else day = 1; setItem(line, OP_DATE, new QTableWidgetItem(_kiss->FormatDate(day, month+1, year))); SET_ROW_COLOR(line, view::OWN_GREEN, Qt::black); } SET_READ_ONLY(this->item(line, CHECKED)); SET_ROW_FONT(line, user->GetCategoryFont(0)); } this->item(line, OP_DATE)->setTextAlignment(Qt::AlignHCenter|Qt::AlignVCenter); this->item(line, DEBIT)->setTextAlignment(Qt::AlignRight|Qt::AlignVCenter); this->item(line, CREDIT)->setTextAlignment(Qt::AlignRight|Qt::AlignVCenter); if (op.id && op.meta) { int height = rowHeight(TREE); QPushButton* button = new QPushButton("+"); _treeSignalMapper.setMapping(button, op.id); connect(button, SIGNAL(clicked()), &_treeSignalMapper, SLOT(map())); button->setMaximumSize(QSize(height, height)); InsertCenteredWidget(line, TREE, button); SET_READ_ONLY(this->item(line, OP_DATE)); SET_READ_ONLY(this->item(line, CREDIT)); SET_READ_ONLY(this->item(line, DEBIT)); SET_READ_ONLY(this->item(line, CATEGORY)); SET_READ_ONLY(this->item(line, TAG)); SET_READ_ONLY(this->item(line, ACCOUNT)); } else SET_READ_ONLY(this->item(line, TREE)); resizeRowToContents(line); layout(); _inModification = false; } void GridAccount::DeleteOperation(const Operation& op) /*throw (OperationNotFound)*/ { std::vector::iterator it = std::find(_operations->begin(), _operations->end(), op.id); std::vector::iterator it2; Operation parent; 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); _parent->NeedReload(); } 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::RemoveRow(const Operation& op, int line, bool deleteOp) { QPushButton* button = qobject_cast (GetCenteredWidget(line, TREE)); if (button) button->disconnect(&_treeSignalMapper, SLOT(map())); QCheckBox* checkBox = qobject_cast (GetCenteredWidget(line, CHECKED)); if (checkBox) checkBox->disconnect(&_checkSignalMapper, 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; if (op.parent) parent = GetOperation(op.parent); // No previous fix operations if (op.fix_cost && !_displayedOperations[1].id) i = 1; else { if (op.parent) { start = GetDisplayedRow(op.parent)+1; for(i=start; i<(int)_displayedOperations.size(); i++) { 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 { 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; } } } } 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) break; if ((*_operations)[a].day > op.day) break; } _operations->insert(_operations->begin()+a, op); 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(); Category cat; if (!force) { if (op.checked == check) return; op.checked = check; UpdateOperation(op); QCheckBox* checkBox = qobject_cast(GetCenteredWidget(line, CHECKED)); if (checkBox) checkBox->setCheckState(check ? Qt::Checked : Qt::Unchecked); } if (line <= _fixCosts) cat = user->GetCategory(1); else cat = user->GetCategory(op.category); color = cat.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) { std::vector::iterator it, it2; int i, deletedOperations = 0; Operation op2; QPushButton* button = qobject_cast (GetCenteredWidget(line, TREE)); for(i=0; i<(int)op.childs.size(); i++) { op2 = GetOperation(op.childs[i]); if (op2.meta) deletedOperations += RemoveMeta(op2, line+1, true, deleteOp); else { if (button->text() == "-") { RemoveRow(op2, line+1, deleteOp); deletedOperations++; } else { if (deleteOp) DeleteOperation(op2); } } } op.childs.clear(); if (removeRoot) { RemoveRow(op, line, deleteOp); deletedOperations++; } return deletedOperations; } void GridAccount::CheckMeta(Operation& op, int line, bool check) { int i; Operation op2; CheckOperation(op, line, check, false); if (IsMetaOpened(op.id)) { for(i=0; i<(int)op.childs.size(); i++) { op2 = GetOperation(op.childs[i]); 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 (GetCenteredWidget(row, TREE)); return (button->text() == QString("-")); } void GridAccount::OpenMeta(const Operation& meta) { if (!IsMetaOpened(meta.id)) OnMetaClicked(meta.id); } void GridAccount::OnMetaClicked(int id) { QPushButton* button = qobject_cast (_treeSignalMapper.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() == QString("+")) { for (i=1, it2=op.childs.begin(); it2!=op.childs.end(); it2++, i++) { op2 = GetOperation(*it2); InsertOperation(user, op2, row+i, op2.fix_cost, op2.month, op2.year); } button->setText("-"); } else { RemoveMeta(op, row, false, false); button->setText("+"); } ComputeWeeks(); } void GridAccount::OnCheckClicked(int id) { std::vector::iterator it; std::vector::iterator it2; int row; Operation op2, parent; bool fullCheck = true; if (_inModification || _loadOperations) return; it = std::find(_displayedOperations.begin(), _displayedOperations.end(), id); if (it == _displayedOperations.end()) return ; _inModification = true; row = it-_displayedOperations.begin(); if (it->meta) CheckMeta(*it, row, !it->checked); else { CheckOperation(*it, row, !it->checked, false); if (it->parent) { parent = GetOperation(it->parent); for(it2=parent.childs.begin(); it2!=parent.childs.end(); it2++) { op2 = GetOperation(*it2); if (!op2.checked) { fullCheck = false; break; } } CheckOperation(parent, GetDisplayedRow(parent.id), fullCheck, false); } } _inModification = false; _kiss->UpdateStats(); } void GridAccount::OnDeleteClicked(int id) { std::vector::iterator it; int row, parentRow; Operation op, op2, parent; User* user = _kiss->GetUser(); if (_inModification || _loadOperations) return; it = std::find(_displayedOperations.begin(), _displayedOperations.end(), id); if (it == _displayedOperations.end()) return ; if (QMessageBox::question(0, "KissCount", _("Are you sure want to delete : \n")+it->description, QMessageBox::Yes|QMessageBox::No) == QMessageBox::No) return; op = *it; // Make a copy _inModification = true; row = it-_displayedOperations.begin(); if (op.meta) RemoveMeta(op, row, true, true); else { RemoveRow(op, row, true); if (op.parent) { user->UnGroup(op); parent = GetOperation(op.parent); parentRow = GetDisplayedRow(parent.id); // One child remains if (parent.childs.size() == 1) { 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(parent); } } ComputeWeeks(); _kiss->UpdateStats(); _inModification = false; } void GridAccount::OnOperationModified(int row, int col) { User* user = _kiss->GetUser(); Operation new_op, cur_op, op_tmp, op_tmp2; int op_complete = 6, new_op_id; QString value, v ; QDate date; bool need_insertion = false, transfertCompleted = false; std::vector::iterator it; std::vector::iterator it2; Operation op, op2, parent; QFont font; Category cat ; Tag tag; bool fix_cost; Operation NULLop; Account account; // Avoid recursives calls if (_inModification || _loadOperations) return; _inModification = true ; cur_op = (_displayedOperations)[row] ; new_op.id = 0; new_op.parent = 0; new_op.day = 0; new_op.month = 0; new_op.year = 0; new_op.amount = 0.0; new_op.description = ""; new_op.category = 0; new_op.tag = 0; new_op.fix_cost = false; new_op.account = 0; new_op.checked = 0; new_op.transfert = 0; new_op.formula = ""; new_op.meta = false; new_op._virtual = false; if (col == DEBIT) setItem(row, CREDIT, new QTableWidgetItem("")); else if (col == CREDIT) setItem(row, DEBIT, new QTableWidgetItem("")); value = item(row, DESCRIPTION)->text(); if (value.length()) { new_op.description = value.trimmed(); op_complete--; } value = item(row, OP_DATE)->text(); if (value.length()) { date = QDate::fromString(value, _kiss->GetDateLocalFormat()); new_op.day = date.day()-1; new_op.month = date.month()-1; new_op.year = date.year(); op_complete--; } if (!cur_op.meta && col == DESCRIPTION && (!item(row, CATEGORY)->text().length() || !item(row, ACCOUNT)->text().length())) { new_op.fix_cost = (row <= _fixCosts); if (_kiss->SearchPreviousOperation(&op_tmp, new_op, _curMonth, _curYear, _canAddOperation, _transfertCompletionIndex)) { if (!item(row, CATEGORY)->text().length()) setItem(row, CATEGORY, new QTableWidgetItem(_(user->GetCategoryName(op_tmp.category).toStdString().c_str()))); if (!item(row, TAG)->text().length() && op_tmp.tag) setItem(row, TAG, new QTableWidgetItem(_(user->GetTagName(op_tmp.tag).toStdString().c_str()))); if (!item(row, ACCOUNT)->text().length()) setItem(row, ACCOUNT, new QTableWidgetItem(user->GetAccountName(op_tmp.account))); col = CATEGORY; new_op.fix_cost = (new_op.category == user->GetCategoryId("Fix")); if (op_tmp.transfert != 0) transfertCompleted = true; /* Non null value --> set amount */ if (op_tmp.amount > 0) setItem(row, CREDIT, new QTableWidgetItem(value.sprintf("%.2lf", (double)op_tmp.amount/100))); else if (op_tmp.amount < 0) setItem(row, DEBIT, new QTableWidgetItem(value.sprintf("%.2lf", (double)-op_tmp.amount/100))); } } value = item(row, DEBIT)->text(); if (value.length()) { new_op.amount = value.replace(".", "").toInt(); if (new_op.amount < 0) { new_op.amount *= -1.0; setItem(row, DEBIT, new QTableWidgetItem(value.sprintf("%.2lf", (double)new_op.amount/100))); } if (new_op.amount != 0.0) new_op.amount *= -1.0; op_complete--; new_op.formula = _displayedOperations[row].formula; } else { value = item(row, CREDIT)->text(); if (value.length()) { new_op.amount = value.replace(".", "").toInt(); if (new_op.amount < 0) { new_op.amount *= -1.0; setItem(row, DEBIT, new QTableWidgetItem(value.sprintf("%.2lf", (double)new_op.amount/100))); } op_complete--; new_op.formula = _displayedOperations[row].formula; } else // Don't add operation if amount not set op_complete += 100; } value = item(row, CATEGORY)->text(); if (value.length()) { new_op.category = user->GetCategoryId(value); op_complete--; } value = item(row, TAG)->text(); if (value.length()) { try { new_op.tag = user->GetTagId(value); } catch (User::TagNotFound e) { setItem(row, TAG, new QTableWidgetItem("")); } } value = item(row, ACCOUNT)->text(); if (value.length()) { new_op.account = user->GetAccountId(value); account = user->GetAccount(new_op.account); op_complete--; } if (cellWidget(row, CHECKED)) new_op.checked = _displayedOperations[row].checked; else new_op.checked = false; op_complete--; fix_cost = (row <= _fixCosts); // Modify an operation if (!_canAddOperation || (row < _fixCosts || (row > _fixCosts && row < (int)(_displayedOperations.size()-1)))) { new_op.id = cur_op.id; new_op.fix_cost = fix_cost; new_op.transfert = cur_op.transfert; new_op.meta = cur_op.meta; new_op.parent = cur_op.parent; new_op.childs = cur_op.childs; new_op._virtual = account._virtual; UpdateOperation(new_op); 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; RemoveRow(new_op, row, false); } else { (_displayedOperations)[row] = new_op; cat = user->GetCategory(new_op.category); CheckOperation(new_op, row, new_op.checked, true); if (row <= _fixCosts) { SET_ROW_FONT(row, user->GetCategoryFont(0)); } else { SET_ROW_FONT(row, user->GetCategoryFont(cat.id)); } } } // 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 > 0) { _inModification = false ; return ; } need_insertion = true; new_op.fix_cost = fix_cost; new_op.meta = false; new_op._virtual = account._virtual; new_op.parent = 0; new_op_id = _kiss->AddOperation(new_op); _parent->NeedReload(); if (!new_op_id) { _inModification = false ; return; } RemoveRow(new_op, row, false); NULLop.id = 0; InsertOperation(user, NULLop, row, new_op.fix_cost, _curMonth, _curYear); new_op.id = new_op_id; if (transfertCompleted) _transfertCompletionIndex = (_transfertCompletionIndex + 1) % 2; } if (!new_op.meta && account.blocked && new_op.amount < 0) QMessageBox::warning(0, _("Warning"), _("You made a debit on a blocked account")); if (need_insertion) InsertIntoGrid(new_op); else { resizeColumnToContents(CATEGORY); resizeColumnToContents(TAG); } if (new_op.parent) { parent = GetOperation(new_op.parent); UpdateMeta(parent); } _kiss->UpdateStats(); _inModification = false ; } void GridAccount::UpdateMeta(Operation& meta) { std::vector::iterator it; std::vector::iterator it2; Operation op ; int category = 0; bool updateCat = false ; int tag = 0; bool updateTag = false ; bool openMeta; if (!meta.childs.size()) return ; openMeta = IsMetaOpened(meta.id); meta.category = 0; meta.amount = 0; op = GetOperation(meta.childs[0]); meta.year = op.year; meta.month = op.month; meta.day = op.day; for(it=meta.childs.begin(); it!=meta.childs.end(); it++) { op = GetOperation(*it); if (op.year <= meta.year && op.month <= meta.month && op.day < meta.day) { meta.year = op.year; meta.month = op.month; meta.day = op.day; } if (meta.description.length() == 0) meta.description = op.description; if (!category) { if (op.category) { category = op.category; updateCat = true; } } else { if (op.category && op.category != category) updateCat = false; } if (!tag) { if (op.tag) { tag = op.tag; updateCat = true; } } else { if (op.tag && op.tag != tag) updateCat = false; } op.parent = meta.id; } if (updateCat) meta.category = category; if (updateTag) meta.tag = tag; meta.amount = _kiss->MetaAmount(meta.id); 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() { std::vector rows; std::vector::iterator it; std::vector ops; std::vector::iterator it2; std::vector::iterator it3; 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) { row = selected[i].row(); it = std::find(rows.begin(), rows.end(), row); if (it != rows.end()) continue; op = _displayedOperations[row] ; if (op.id) { if (!parent) { if (op.parent) parent = op.parent; else if(op.meta) parent = op.id; } else { if ((parent && op.parent && op.parent != parent)) { QMessageBox::critical(0, _("Error"), _("Cannot group these operations")); return ; } } if (op.parent) { row = GetDisplayedRow(op.parent); op = _displayedOperations[row] ; } if (fix != -1 && ((!fix && op.fix_cost) || (fix && !op.fix_cost))) { QMessageBox::critical(0, _("Error"), _("Cannot group these operations")); return ; } if (fix == -1) fix = op.fix_cost ? 1 : 0; ops.push_back(op); rows.push_back(row); } } if (!ops.size()) return; if (!parent) { if (rows.size() < 2) return; op.parent = 0; op.day = ops[0].day; op.month = ops[0].month; op.year = ops[0].year; op.amount = 0; op.description = ""; op.category = 0; op.tag = 0; op.fix_cost = fix; op.account = 0; op.checked = false; op.transfert = 0; op.formula = ""; op.meta = true; op.childs.clear(); op.id = _kiss->AddOperation(op); _parent->NeedReload(); } else { if (rows.size() < 1) return; row = GetDisplayedRow(parent); op = _displayedOperations[row]; //if (op.id) return; } for(i=0; i<(int)rows.size(); i++) { if (ops[i].meta) deletedRows = RemoveMeta(ops[i], rows[i], true, false); else { RemoveRow(ops[i], rows[i], false); deletedRows = 1; } for(a=i+1; a<(int)rows.size(); a++) if (rows[a] >= rows[i]) rows[a] -= deletedRows; } for(it2=ops.begin(); it2!=ops.end(); it2++) { if (it2->id == parent) continue; for (i=0, it3=op.childs.begin(); it3!=op.childs.end(); it3++, i++) { op2 = GetOperation(*it3); 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--; if (it3 == op.childs.end()) op.childs.push_back(it2->id); else if (*it3 == it2->id) continue; else op.childs.insert(op.childs.begin()+i, it2->id); it2->parent = op.id; UpdateOperation(*it2); if (!it2->checked) fullCheck = false; } row = InsertIntoGrid(op); if (fullCheck) { op.checked = true; CheckOperation(op, row, true, false); } else { op.checked = false; CheckOperation(op, row, false, false); } UpdateMeta(op); } void GridAccount::GetSelectedOperations(std::vector* rows) { Operation op; rows->clear(); QModelIndexList selected = selectedIndexes(); for (int i = 0; i < selected.size(); ++i) { op = _displayedOperations[selected[i].row()] ; if (op.id) rows->push_back(selected[i].row()); } } void GridAccount::UnGroup() { std::vector rows; std::vector::iterator it; std::vector ops; std::vector ops2; std::vector::iterator it2; std::vector::iterator it3; int parent = 0; Operation op, op2; int fix = -1, i, line; QModelIndexList selected = selectedIndexes(); for (int i = 0; i < selected.size(); ++i) { it = std::find(rows.begin(), rows.end(), selected[i].row()); if (it != rows.end()) continue; op = _displayedOperations[selected[i].row()] ; if (op.id) { if ((parent && op.parent != parent) || (!op.parent && !op.meta)) { QMessageBox::critical(0, _("Error"), _("Cannot ungroup these operations")); return ; } if (fix != -1 && ((!fix && op.fix_cost) || (fix && !op.fix_cost))) { QMessageBox::critical(0, _("Error"), _("Cannot ungroup these operations")); return ; } if (fix == -1) fix = op.fix_cost ? 1 : 0; if(op.meta) { parent = op.id; continue; } if (!parent && op.parent) parent = op.parent; ops.push_back(op); rows.push_back(selected[i].row()); } } if (!ops.size() && !parent) return; _inModification = true; removeLastGroup: // Only one meta is selected if (!ops.size()) { line = GetDisplayedRow(parent); op = _displayedOperations[line]; ops2 = op.childs; RemoveMeta(op, line, true, false); for(i=0; i<(int)ops2.size(); i++) { op2 = GetOperation(ops2[i]); op2.parent = 0; UpdateOperation(op2); InsertIntoGrid(op2); } DeleteOperation(op); } else { if (!parent) return; line = GetDisplayedRow(parent); op2 = _displayedOperations[line]; for(i=0; i<(int)ops.size(); i++) { op = ops[i]; op.parent = 0; UpdateOperation(op); line = GetDisplayedRow(op.id); 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; if (op2.childs.size() < 2) { ops.clear(); // Sorry ... goto removeLastGroup; } UpdateOperation(op2); } _inModification = false; } void GridAccount::MassUpdate(std::vector& rows, bool do_childs, updateOperationFunc func, void** params) { int i, b; std::vector::iterator it; Operation op, op2; _kiss->setOverrideCursor(QCursor(Qt::WaitCursor)); _parent->setEnabled(false); _parent->repaint(); if (rows.size()) { for(i=0; i<(int)rows.size(); i++) { op = _displayedOperations[rows[i]]; func (&op, params); UpdateOperation(op); if (op.meta && do_childs) { for(b=0; b<(int)op.childs.size(); b++) { op2 = GetOperation(op.childs[b]); func (&op2, params); UpdateOperation(op2); } } } } else { for(it=_operations->begin(); it!=_operations->end(); it++) { func (&(*it), params); if (_databaseSynchronization) { _kiss->UpdateOperation(*it); _parent->NeedReload(); } if (it->meta && do_childs) { for(b=0; b<(int)it->childs.size(); b++) { op2 = GetOperation(it->childs[b]); func (&op2, params); UpdateOperation(op2); } } } } LoadOperations(_operations, 0, 0); layout(); _parent->setEnabled(true); _kiss->setOverrideCursor(QCursor(Qt::ArrowCursor)); } void GridAccount::OnCtrlT(void) { Operation op, op2; QModelIndexList selected = selectedIndexes(); int account, idx; bool groupOperations; User* user = _kiss->GetUser(); std::vector::iterator it; if (selected.size() > 1 || !selected.size()) return; op = _displayedOperations[selected[0].row()] ; if (op.parent || op.meta || op.transfert) return; TransfertDialog g(_kiss, user, op, &account, &groupOperations); g.setModal(true); if (g.exec()) { op2 = op; op2.account = account; op2.amount *= -1; op2.id = _kiss->AddOperation(op2); InsertOperationWithWeek(user, op2, selected[0].row()+1, op.fix_cost, op.month, op.year); for (idx = 0, it = _operations->begin(); it != _operations->end(); it++, idx++) { if (it->id == op.id) { _operations->insert(_operations->begin()+idx+1, op2); break; } } if (groupOperations) { setRangeSelected(QTableWidgetSelectionRange(selected[0].row(), 0, selected[0].row()+1, 0), true); Group(); } _kiss->UpdateStats(); } } void GridAccount::OnCtrlR(void) { QModelIndexList selected = selectedIndexes(); std::vector rows; std::vector::iterator it; int row; for (int i = 0; i < selected.size(); ++i) { row = selected[i].row(); it = std::find(rows.begin(), rows.end(), row); if (it != rows.end()) continue; OnCheckClicked(_displayedOperations[row].id); rows.push_back(row); } } void GridAccount::OnSuppr(void) { QModelIndexList selected = selectedIndexes(); std::vector rows; std::vector::iterator it; int row; for (int i = 0; i < selected.size(); ++i) { row = selected[i].row(); it = std::find(rows.begin(), rows.end(), row); if (it != rows.end()) continue; OnDeleteClicked(_displayedOperations[row].id); rows.push_back(row); } }