/* Copyright 2010 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 "GridAccount.h" #define SET_ROW_COLOR(row, backcolor, forecolor) for(int i=0; iGetUser(); std::vector::iterator accountIt; std::vector::iterator categoryIt; CreateGrid(1, NUMBER_COLS_OPS); SetColLabelSize(0); SetRowLabelSize(0); SetColSize (DESCRIPTION, GetColSize(DESCRIPTION)*3); SetDefaultCellFont(font); font.SetWeight(wxFONTWEIGHT_BOLD); wxString colsName[] = {wxT(""), _("Description"), _("Date"), _("Debit"), _("Credit"), _("Category"), _("Account"), wxT(""), wxT("")}; for(i=0; iGetAccountsNumber()]; for (i=0, accountIt = user->_accounts.begin(); accountIt != user->_accounts.end(); accountIt++, i++) _accounts[i] = accountIt->name; _categories = new wxString[user->GetCategoriesNumber()] ; for(i=0, categoryIt = user->_categories.begin(); categoryIt != user->_categories.end(); categoryIt++, i++) { _categories[i] = categoryIt->name ; } Connect(id, wxEVT_GRID_CELL_CHANGE, wxGridEventHandler(GridAccount::OnOperationModified), NULL, this); AutoSizeColumn(TREE, false); AutoSizeColumn(CATEGORY, false); AutoSizeColumn(DATE, false); AutoSizeColumn(ACCOUNT, false); AutoSizeColumn(DELETE, false); AutoSizeColumn(CHECKED, false); } GridAccount::~GridAccount() { delete[] _categories; delete[] _accounts; } wxPen GridAccount::GetColGridLinePen (int col) {return wxPen(*wxBLACK, 1, wxSOLID);} wxPen GridAccount::GetRowGridLinePen (int row) { if (_canAddOperation) { if (row == 0 || row == _fixCosts || row == _week1 || row == _week2 || row == _week3 || row == _week4) return wxPen(*wxBLACK, 1, wxSOLID); } return GetCellBackgroundColour(row, 0); } void GridAccount::ResetWeeks() { _week1 = _week2 = _week3 = _week4 = 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; } } Operation& GridAccount::GetOperation(const wxString& id) { std::vector::iterator it; for(it=_operations->begin(); it!=_operations->end(); it++) if (it->id == id) return *it; // I'm aware about the warning, but the method may not fail } void GridAccount::UpdateOperation(Operation& op) { std::vector::iterator it; int i; for(i=0; i < (int)_operations->size(); i++) if ((*_operations)[i].id == op.id) { _kiss->UpdateOperation(op); (*_operations)[i] = op; break; } } int GridAccount::GetDisplayedRow(const wxString& id) { for(int i=0; i<(int)_displayedOperations.size(); i++) if (_displayedOperations[i].id == id) return i; return -1; } void GridAccount::LoadOperations(std::vector* operations, bool canAddOperation, bool setWeek, int month, int year) { std::vector::iterator it; User* user = _kiss->GetUser(); int curLine = 0; Operation NULLop; NULLop.id = wxT(""); _loadOperations = true; _operations = operations; _canAddOperation = canAddOperation; _curMonth = month; _curYear = year; _displayedOperations.clear(); _displayedOperations.push_back(NULLop); // Header _fixCosts = 0; it = _operations->begin(); if (_canAddOperation) { for (;it != _operations->end() && it->fix_cost; it++) { if (it->parent.Length()) 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.Length()) 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.Length()) continue; InsertOperation(user, *it, ++curLine, true, it->month, it->year); } } AutoSizeColumn(TREE, false); AutoSizeColumn(CATEGORY, false); AutoSizeColumn(DATE, false); AutoSizeColumn(ACCOUNT, false); AutoSizeColumn(DELETE, false); AutoSizeColumn(CHECKED, false); _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.Length() && !it->fix_cost) break; } if (it == _displayedOperations.end()) return; ResetWeeks(); curWeek = wxDateTime(it->day+1, (wxDateTime::Month)it->month, it->year).GetWeekOfMonth(); it++; for (i=1; it != _displayedOperations.end(); it++, curLine++) { if (!it->id.Length() || it->parent.Length()) continue; week = wxDateTime(it->day+1, (wxDateTime::Month)it->month, it->year).GetWeekOfMonth(); 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.Length() && !fix) ComputeWeeks(); } 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; double amount; wxColour color; wxDateTime curDate; wxString description; wxFont font; Category cat ; Operation op2; curDate.SetToCurrent(); // // First is header // if (op) _displayedOperations.insert(_displayedOperations.begin()+line, op); if (!user->_accounts.size()) return; InsertRows(line, 1); if (op.id.Length() && op.meta) { SetCellRenderer(line, TREE, new wxGridCellTreeButtonRenderer()); SetCellEditor(line, TREE, new wxGridCellTreeButtonEditor()); SetReadOnly(line, DATE, true); SetReadOnly(line, CREDIT, true); SetReadOnly(line, DEBIT, true); SetReadOnly(line, CATEGORY, true); SetReadOnly(line, ACCOUNT, true); } else SetReadOnly(line, TREE, true); SetCellEditor(line, DEBIT, new wxGridCellFloatEditor(wxID_ANY, 2)); SetCellEditor(line, CREDIT, new wxGridCellFloatEditor(wxID_ANY, 2)); wxGridCellChoiceEditor* accountEditor = new wxGridCellChoiceEditor(user->GetAccountsNumber(), _accounts, false); SetCellEditor(line, ACCOUNT, accountEditor); wxGridCellChoiceEditor* categoryEditor = new wxGridCellChoiceEditor(user->GetCategoriesNumber()-1, _categories+1, false); SetCellEditor(line, CATEGORY, categoryEditor); if (fix) { SetCellValue(line, CATEGORY, _("Fix")); SetReadOnly(line, CATEGORY); _fixCosts++; } SetCellEditor(line, DEBIT, new wxGridCellFormulaEditor(op.formula)); SetCellEditor(line, CREDIT, new wxGridCellFormulaEditor(op.formula)); if (op.id.Length()) { cat = user->GetCategory(op.category); SetCellEditor(line, DATE, new CalendarEditor(op.day, month, year)); description = op.description; UNESCAPE_CHARS(description); SetCellValue(line, DESCRIPTION, description); SetCellRenderer(line, DESCRIPTION, new wxGridCellTabStringRenderer ()); SetCellValue(line, DATE, wxString::Format(wxT("%02d/%02d/%d"), op.day+1, month+1, year)); if (op.amount < 0) SetCellValue(line, DEBIT, wxString::Format(wxT("%.2lf"), -op.amount)); else SetCellValue(line, CREDIT, wxString::Format(wxT("%.2lf"), op.amount)); if (!op.meta) SetCellValue(line, ACCOUNT, user->GetAccountName(op.account)); if (!fix && !op.meta) SetCellValue(line, CATEGORY, cat.name); SetCellRenderer(line, DELETE, new wxGridCellBoolRenderer ()); SetCellEditor(line, DELETE, new wxGridCellBoolEditor ()); SetCellRenderer(line, CHECKED, new wxGridCellBoolRenderer ()); SetCellEditor(line, CHECKED, new wxGridCellFastBoolEditor ()); if (op.category.Length()) color = cat.backcolor; else color = 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.Set(r, g, b, color.Alpha()); SetCellValue(line, CHECKED, wxT("1")); } SET_ROW_COLOR(line, color, cat.forecolor); if (op.category.Length() && cat.font.Length()) { font = user->GetCategoryFont(cat.id); SET_ROW_FONT(line, font); } if (op.meta && !op.amount) { amount = _kiss->MetaPositiveAmount(op.id); SetCellValue(line, DEBIT, wxString::Format(wxT("%.2lf"), amount)); SetCellValue(line, CREDIT, wxString::Format(wxT("%.2lf"), amount)); } if (!_loadOperations) { if (op.meta) AutoSizeColumn(TREE, false); AutoSizeColumn(CATEGORY, false); AutoSizeColumn(ACCOUNT, false); } } else { SetCellEditor(line, DATE, new CalendarEditor(0, month, year)); if (curDate.GetMonth() == month && curDate.GetYear() == year) { if (!fix) SetCellValue(line, DATE, wxString::Format(wxT("%02d/%02d/%d"), curDate.GetDay(), month+1, year)); SetCellEditor(line, DATE, new CalendarEditor(curDate.GetDay()-1, month, year)); } else SetCellEditor(line, DATE, new CalendarEditor(0, month, year)); if (fix) { SET_ROW_COLOR(line, OWN_YELLOW, *wxBLACK); } else { SET_ROW_COLOR(line, OWN_GREEN, *wxBLACK); } SetReadOnly(line, CHECKED, true); SetReadOnly(line, DELETE, true); } SetCellAlignment(line, DATE, wxALIGN_CENTRE, wxALIGN_CENTRE); SetCellAlignment(line, DEBIT, wxALIGN_RIGHT, wxALIGN_CENTRE); SetCellAlignment(line, CREDIT, wxALIGN_RIGHT, wxALIGN_CENTRE); SetCellAlignment(line, DELETE, wxALIGN_CENTRE, wxALIGN_CENTRE); SetCellAlignment(line, CHECKED, wxALIGN_CENTRE, wxALIGN_CENTRE); AutoSizeRow(line); Layout(); SetMinSize(GetMinSize()); } // From http://nomadsync.cvs.sourceforge.net/nomadsync/nomadsync/src/ void GridAccount::OnCellLeftClick(wxGridEvent& evt) { if (evt.GetCol() != TREE && evt.GetCol() != DELETE && evt.GetCol() != CHECKED) { evt.Skip() ; return;} // This forces the cell to go into edit mode directly //m_waitForSlowClick = TRUE; SetGridCursor(evt.GetRow(), evt.GetCol()); // Store the click co-ordinates in the editor if possible // if an editor has created a ClientData area, we presume it's // a wxPoint and we store the click co-ordinates wxGridCellEditor* pEditor = GetCellEditor(evt.GetRow(), evt.GetCol()); wxPoint* pClickPoint = (wxPoint*)pEditor->GetClientData(); if (pClickPoint) { *pClickPoint = ClientToScreen(evt.GetPosition()); #ifndef __WINDOWS__ EnableCellEditControl(true); #endif } // hack to prevent selection from being lost when click combobox if (/*evt.GetCol() == 0 && */IsInSelection(evt.GetRow(), evt.GetCol())) { //m_selTemp = m_selection; m_selection = NULL; } pEditor->DecRef(); evt.Skip(); } void GridAccount::DeleteOperation(const Operation& op) { std::vector::iterator it; for (it=_operations->begin(); it!=_operations->end(); it++) if (it->id == op.id) { _operations->erase(it); break; } } void GridAccount::InsertIntoGrid(Operation& op) { int i, a, start; User* user = _kiss->GetUser(); Operation parent; // No previous fix operations if (op.fix_cost && !_displayedOperations[1].id.Length()) i = 1; else { if (op.parent.Length()) start = GetDisplayedRow(op.parent); else start = 0; for(i=start; i<(int)_displayedOperations.size(); i++) { if (!_displayedOperations[i].id.Length()) continue; if (_displayedOperations[i].parent.Length()) continue; if ((_displayedOperations)[i].fix_cost && !op.fix_cost) continue; if (!(_displayedOperations)[i].fix_cost && op.fix_cost) break; if (user->_preferences[wxT("operation_order")] == wxT("ASC")) { if ((_displayedOperations)[i].day > op.day) break; } else { if ((_displayedOperations)[i].day < op.day) break; } } if (op.parent.Length()) { parent = GetOperation(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 (a < 0) a = 0; _operations->insert(_operations->begin()+a, op); InsertOperationWithWeek(user, (*_operations)[a], i, op.fix_cost, _curMonth, _curYear); } void GridAccount::RemoveMeta(Operation op, int line, bool removeRoot, bool deleteOp) { std::vector::iterator it, it2; wxGridCellTreeButtonRenderer* treeRenderer; int i; Operation op2; treeRenderer = (wxGridCellTreeButtonRenderer*) GetCellRenderer(line, TREE); for(i=0; i<(int)op.childs.size(); i++) { op2 = GetOperation(op.childs[i]); if (op2.meta) RemoveMeta(op2, line+1, true, deleteOp); else { if (treeRenderer->IsCollapsed()) { DeleteRows(line+1, 1); if (op2.fix_cost) _fixCosts--; _displayedOperations.erase(_displayedOperations.begin()+line+1); } if (deleteOp) { DeleteOperation(op2); _kiss->DeleteOperation(op2); } } } op.childs.clear(); if (removeRoot) { DeleteRows(line, 1); _displayedOperations.erase(_displayedOperations.begin()+line); if (op.fix_cost) _fixCosts--; if (deleteOp) { DeleteOperation(op); _kiss->DeleteOperation(op); } } treeRenderer->DecRef(); } void GridAccount::CheckMeta(Operation& op, int line, bool check) { std::vector::iterator it, it2; wxGridCellTreeButtonRenderer* treeRenderer; int i; Operation op2; wxColour color ; unsigned char r, g, b; User* user = _kiss->GetUser(); treeRenderer = (wxGridCellTreeButtonRenderer*) GetCellRenderer(line, TREE); for(i=0; i<(int)op.childs.size(); i++) { op2 = GetOperation(op.childs[i]); op2.checked = check; UpdateOperation(op2); if (op2.meta) CheckMeta(op2, line+1, check); if (treeRenderer->IsCollapsed()) { SetCellValue(line+i+1, CHECKED, check ? wxT("1") : wxT("0")); 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.Set(r, g, b, color.Alpha()); } SET_ROW_COLOR(line+i+1, color, user->GetCategory(op2.category).forecolor); } } treeRenderer->DecRef(); } void GridAccount::OnOperationModified(wxGridEvent& event) { User* user = _kiss->GetUser(); int row = event.GetRow(); int col = event.GetCol(); Operation new_op, cur_op, op_tmp, op_tmp2; int op_complete = 6, i, last_day; wxString value ; wxDateTime date; bool need_insertion = false, fix_op=false; static bool inModification = false ; wxColour color ; unsigned char r, g, b; wxGridCellTreeButtonRenderer* treeRenderer; std::vector::iterator it; Operation op, op2; double amount; wxFont font; Category cat ; // Avoid recursives calls if (inModification) return; inModification = true ; if (col == TREE) { treeRenderer = (wxGridCellTreeButtonRenderer*) GetCellRenderer(row, col); op = _displayedOperations[row]; if (!treeRenderer->IsCollapsed()) { for (i=1, it=op.childs.begin(); it!=op.childs.end(); it++, i++) { op2 = GetOperation(*it); InsertOperationWithWeek(user, op2, row+i, op2.fix_cost, _curMonth, _curYear); } } else { RemoveMeta(op, row, false, false); } treeRenderer->DecRef(); ComputeWeeks(); inModification = false; return; } if (col == DEBIT) SetCellValue(row, CREDIT, wxT("")); else if (col == CREDIT) SetCellValue(row, DEBIT, wxT("")); value = GetCellValue(row, DESCRIPTION); if (value.Length()) { new_op.description = value; op_complete--; } value = GetCellValue(row, DATE); if (value.Length()) { date.ParseFormat(value, wxT("%d/%m/%Y")); new_op.day = date.GetDay()-1; new_op.month = date.GetMonth(); new_op.year = date.GetYear(); op_complete--; } value = GetCellValue(row, DEBIT); if (value.Length()) { value.ToDouble(&new_op.amount); if (new_op.amount < 0) { new_op.amount *= -1.0; SetCellValue(row, DEBIT, wxString::Format(wxT("%.2lf"), new_op.amount)); } if (new_op.amount != 0.0) new_op.amount *= -1.0; op_complete--; wxGridCellFormulaEditor* pEditor = (wxGridCellFormulaEditor*) GetCellEditor(row, DEBIT); new_op.formula = pEditor->GetFormula(); pEditor->DecRef(); } value = GetCellValue(row, CREDIT); if (value.Length()) { value.ToDouble(&new_op.amount); if (new_op.amount < 0) { new_op.amount *= -1.0; SetCellValue(row, CREDIT, wxString::Format(wxT("%.2lf"), new_op.amount)); } op_complete--; wxGridCellFormulaEditor* pEditor = (wxGridCellFormulaEditor*) GetCellEditor(row, CREDIT); new_op.formula = pEditor->GetFormula(); pEditor->DecRef(); } value = GetCellValue(row, CATEGORY); if (value.Length()) { new_op.category = user->GetCategoryId(value); op_complete--; } value = GetCellValue(row, ACCOUNT); if (value.Length()) { new_op.account = user->GetAccountId(value); op_complete--; } value = GetCellValue(row, CHECKED); if (value.Length() && value != wxT("0")) new_op.checked = true; else new_op.checked = false; op_complete--; if (col == DESCRIPTION && (!GetCellValue(row, CATEGORY).Length() || !GetCellValue(row, ACCOUNT).Length())) { new_op.fix_cost = row <= _fixCosts; if (_kiss->SearchPreviousOperation(&op_tmp, new_op, _curMonth, _curYear)) { new_op.category = op_tmp.category; new_op.account = op_tmp.account; SetCellValue(row, CATEGORY, user->GetCategoryName(new_op.category)); SetCellValue(row, ACCOUNT, user->GetAccountName(new_op.account)); op_complete -= 2; } } cur_op = (_displayedOperations)[row] ; if (col == CHECKED || col == CATEGORY) { 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.Set(r, g, b, color.Alpha()); } SET_ROW_COLOR(row, color, user->GetCategory(new_op.category).forecolor); SET_ROW_FONT(row, user->GetCategoryFont(new_op.category)); if (col == CHECKED) { cur_op.checked = new_op.checked; UpdateOperation(cur_op); inModification = false; if (cur_op.meta) CheckMeta(cur_op, row, cur_op.checked); else { if (cur_op.parent.Length()) { op2 = GetOperation(cur_op.parent); UpdateMeta(op2); int row2 = GetDisplayedRow(op2.id); SetCellValue(row2, CHECKED, op2.checked ? wxT("1") : wxT("0")); color = user->GetCategory(op2.category).backcolor; if (op2.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.Set(r, g, b, color.Alpha()); } SET_ROW_COLOR(row2, color, user->GetCategory(op2.category).forecolor); } } event.Skip(); return; } } if (col == DELETE) { wxMessageDialog dialog(this, _("Are you sure want to delete : \n")+new_op.description, wxT("KissCount"), wxYES_NO); if (dialog.ShowModal() == wxID_NO) { SetCellValue(row, col, wxT("0")); inModification = false; return; } } // Modify a fix operation if (row < _fixCosts) { if (col == DELETE) { if (cur_op.parent.Length()) user->UnGroup(_displayedOperations[row]); if (cur_op.meta) RemoveMeta(_displayedOperations[row], row, true, true); else { if (cur_op.parent.Length()) { op_tmp = GetOperation(cur_op.parent); for (int a=0; a<(int)op_tmp.childs.size(); a++) if (op_tmp.childs[a] == op.id) { op2.childs.erase(op_tmp.childs.begin()+a); break; } } DeleteRows(row, 1); DeleteOperation(cur_op); _kiss->DeleteOperation(cur_op); _displayedOperations.erase(_displayedOperations.begin()+row); if (cur_op.parent.Length() && op_tmp.childs.size() < 2) { if (op_tmp.childs.size() == 1) { op_tmp2 = GetOperation(op_tmp.childs[0]); op_tmp2.parent = wxT(""); UpdateOperation(op_tmp2); row = GetDisplayedRow(op_tmp2.id); _displayedOperations[row] = op_tmp2; } row = GetDisplayedRow(cur_op.parent); DeleteRows(row, 1); DeleteOperation(op_tmp); _kiss->DeleteOperation(op_tmp); _displayedOperations.erase(_displayedOperations.begin()+row); _fixCosts--; } _fixCosts--; ComputeWeeks(); } inModification = false ; event.Skip(); return ; } new_op.id = cur_op.id; new_op.fix_cost = true; new_op.transfert = cur_op.transfert; new_op.meta = cur_op.meta; new_op.parent = cur_op.parent; new_op.childs = cur_op.childs; if (cur_op.day != new_op.day) { need_insertion = true; DeleteRows(row, 1); DeleteOperation(cur_op); _displayedOperations.erase(_displayedOperations.begin()+row); _fixCosts--; } else { UpdateOperation(new_op); (_displayedOperations)[row] = new_op; } fix_op = true; } // Add a fixCost else if (row == _fixCosts) { if (op_complete) { inModification = false ; return ; } need_insertion = true; fix_op = true; new_op.fix_cost = true; new_op.meta = false; for(i=0; iAddOperation(new_op); } // Modify an operation else if (row < (int)(_displayedOperations.size()-1)) { new_op.id = cur_op.id; new_op.fix_cost = false; new_op.transfert = cur_op.transfert; new_op.meta = cur_op.meta; new_op.parent = cur_op.parent; new_op.childs = cur_op.childs; if (col == DELETE) { if (cur_op.parent.Length()) user->UnGroup(_displayedOperations[row]); if (cur_op.meta) RemoveMeta(_displayedOperations[row], row, true, true); else { if (cur_op.parent.Length()) { op_tmp = GetOperation(cur_op.parent); for (int a=0; a<(int)op_tmp.childs.size(); a++) if (op_tmp.childs[a] == op.id) { op2.childs.erase(op_tmp.childs.begin()+a); break; } } DeleteRows(row, 1); DeleteOperation(cur_op); _displayedOperations.erase(_displayedOperations.begin()+row); _kiss->DeleteOperation(cur_op); if (cur_op.parent.Length() && op_tmp.childs.size() <= 1) { if (op_tmp.childs.size() == 1) { op_tmp2 = GetOperation(op_tmp.childs[0]); op_tmp2.parent = wxT(""); UpdateOperation(op_tmp2); row = GetDisplayedRow(op_tmp2.id); _displayedOperations[row] = op_tmp2; } row = GetDisplayedRow(cur_op.parent); DeleteRows(row, 1); DeleteOperation(op_tmp); _kiss->DeleteOperation(op_tmp); _displayedOperations.erase(_displayedOperations.begin()+row); } ComputeWeeks(); } inModification = false ; event.Skip(); return ; } if (cur_op.day != new_op.day) { need_insertion = true; DeleteRows(row, 1); DeleteOperation(cur_op); _displayedOperations.erase(_displayedOperations.begin()+row); } else { UpdateOperation(new_op); (_displayedOperations)[row] = new_op; } } // Add an operation else { if (op_complete) { inModification = false ; return ; } need_insertion = true; fix_op = false; new_op.fix_cost = false; new_op.meta = false; for(i=0; iAddOperation(new_op); } if (user->GetAccount(new_op.account).blocked && new_op.amount < 0) wxMessageBox(_("You made a debit on a blocked account"), _("Warning"), wxICON_WARNING | wxOK); if (need_insertion) { InsertIntoGrid(new_op); UpdateOperation(new_op); } if (new_op.parent.Length()) { row = GetDisplayedRow(new_op.parent); new_op = _displayedOperations[row]; last_day = new_op.day; UpdateMeta(new_op); _displayedOperations[row] = new_op; cat = user->GetCategory(new_op.category); if (new_op.category.Length()) color = cat.backcolor; else color = OWN_GREEN; 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.Set(r, g, b, color.Alpha()); SetCellValue(row, CHECKED, wxT("1")); } else SetCellValue(row, CHECKED, wxT("0")); SET_ROW_COLOR(row, color, cat.forecolor); if (new_op.category.Length() && cat.font.Length()) { font = user->GetCategoryFont(cat.id); SET_ROW_FONT(row, font); } SetCellValue(row, DATE, wxString::Format(wxT("%02d/%02d/%d"), new_op.day+1, _curMonth+1, _curYear)); if (!_displayedOperations[row].amount) { amount = _kiss->MetaPositiveAmount(new_op.id); SetCellValue(row, DEBIT, wxString::Format(wxT("%.2lf"), amount)); SetCellValue(row, CREDIT, wxString::Format(wxT("%.2lf"), amount)); } else { if (_displayedOperations[row].amount < 0) { SetCellValue(row, DEBIT, wxString::Format(wxT("%.2lf"), -new_op.amount)); SetCellValue(row, CREDIT, wxT("")); } else { SetCellValue(row, DEBIT, wxT("")); SetCellValue(row, CREDIT, wxString::Format(wxT("%.2lf"), new_op.amount)); } } // 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); } treeRenderer = (wxGridCellTreeButtonRenderer*) GetCellRenderer(row, 0); treeRenderer->Invert(); treeRenderer->DecRef(); } } inModification = false ; event.Skip(); } void GridAccount::UpdateMeta(Operation& meta) { std::vector::iterator it; Operation op ; wxString category = wxT(""); bool updateCat = false ; if (!meta.childs.size()) return ; meta.category = wxT(""); meta.checked = true; meta.amount = 0; 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; } meta.checked &= op.checked; if (!meta.description.Length() && op.description.Length()) meta.description = op.description; if (!category.Length()) { if (op.category.Length()) { category = op.category; updateCat = true; } } else { if (op.category.Length() && op.category != category) updateCat = false; } op.parent = meta.id; } if (updateCat) meta.category = category; meta.amount = _kiss->MetaAmount(meta.id); UpdateOperation(meta); } void GridAccount::GetSelectedOperations(std::vector* rows) { std::vector::iterator it; rows->clear(); // Blocks. We always expect btl and bbr to have the same size, since their // entries are supposed to correspond. const wxGridCellCoordsArray& btl(GetSelectionBlockTopLeft()); const wxGridCellCoordsArray& bbr(GetSelectionBlockBottomRight()); size_t blockCount = btl.size(); if (blockCount == bbr.size()) { for (size_t i = 0; i < blockCount; ++i) { const wxGridCellCoords& tl = btl[i]; const wxGridCellCoords& br = bbr[i]; for (int row = tl.GetRow(); row <= br.GetRow(); ++row) { for (it=rows->begin(); it!=rows->end(); it++) if (*it == row) break; if (it != rows->end()) continue; rows->push_back(row); } } } // Singly selected cells. const wxGridCellCoordsArray& cells(GetSelectedCells()); for (size_t i = 0; i < cells.size(); ++i) { const wxGridCellCoords& c = cells[i]; for (it=rows->begin(); it!=rows->end(); it++) if (*it == c.GetRow()) break; if (it != rows->end()) continue; rows->push_back(c.GetRow()); } } void GridAccount::Group() { std::vector selected, rows; std::vector::iterator it; std::vector ops; std::vector::iterator it2; std::vector::iterator it3; wxString parent = wxT(""); Operation op, op2; int fix = -1, i, a, row; GetSelectedOperations(&selected); for (size_t i = 0; i < selected.size(); ++i) { op = _displayedOperations[selected[i]] ; if (op.id.Length()) { if (!parent.Length()) { if (op.parent.Length()) { parent = op.parent; continue; } else if(op.meta) parent = op.id; } else { if ((parent.Length() && op.parent.Length() && op.parent != parent)) { wxMessageBox(_("Cannot group these operations"), _("Error"), wxICON_ERROR | wxOK); return ; } } if (fix != -1 && ((!fix && op.fix_cost) || (fix && !op.fix_cost))) { wxMessageBox(_("Cannot group these operations"), _("Error"), wxICON_ERROR | wxOK); return ; } if (fix == -1) fix = op.fix_cost ? 1 : 0; ops.push_back(op); rows.push_back(selected[i]); } } if (!ops.size()) return; if (!parent.Length()) { if (rows.size() < 2) return; op.parent = wxT(""); op.day = ops[0].day; op.month = ops[0].month; op.year = ops[0].year; op.amount = 0; op.description = wxT(""); op.category = wxT(""); op.fix_cost = fix; op.account = wxT(""); op.checked = false; op.transfert = wxT(""); op.formula = wxT(""); op.meta = true; op.childs.clear(); op.id = _kiss->AddOperation(op); } else { if (rows.size() < 1) return; row = GetDisplayedRow(parent); op = _displayedOperations[row]; //if (op.id.Length()) return; } std::sort(rows.begin(), rows.end()); for(i=0; i<(int)rows.size(); i++) { if (ops[i].meta) RemoveMeta(ops[i], rows[i], true, false); else { if (ops[i].fix_cost) _fixCosts--; DeleteRows(rows[i], 1); _displayedOperations.erase(_displayedOperations.begin()+rows[i]); for(a=i+1; a<(int)rows.size(); a++) if (rows[a] >= rows[i]) rows[a]--; } } 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 || 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); } UpdateMeta(op); InsertIntoGrid(op); UpdateOperation(op); } void GridAccount::UnGroup() { std::vector rows, selected; std::vector::iterator it; std::vector ops; std::vector ops2; std::vector::iterator it2; std::vector::iterator it3; wxString parent = wxT(""); Operation op, op2; int fix = -1, i, a, line; GetSelectedOperations(&selected); for (size_t i = 0; i < selected.size(); ++i) { op = _displayedOperations[selected[i]] ; if (op.id.Length()) { if ((parent.Length() && op.parent != parent) || (!op.parent.Length() && !op.meta)) { wxMessageBox(_("Cannot ungroup these operations"), _("Error"), wxICON_ERROR | wxOK); return ; } if (fix != -1 && ((!fix && op.fix_cost) || (fix && !op.fix_cost))) { wxMessageBox(_("Cannot ungroup these operations"), _("Error"), wxICON_ERROR | wxOK); return ; } if (fix == -1) fix = op.fix_cost ? 1 : 0; if(op.meta) { parent = op.id; continue; } if (!parent.Length() && op.parent.Length()) parent = op.parent; ops.push_back(op); rows.push_back(selected[i]); } } if (!ops.size() && !parent.Length()) return; 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 = wxT(""); UpdateOperation(op2); InsertIntoGrid(op2); } _kiss->DeleteOperation(op); DeleteOperation(op); } else { if (!parent.Length()) return; line = GetDisplayedRow(parent); op2 = _displayedOperations[line]; for(i=0; i<(int)ops.size(); i++) { op = ops[i]; op.parent = wxT(""); UpdateOperation(op); line = GetDisplayedRow(op.id); DeleteRows(line, 1); _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); UpdateOperation(op2); } line = GetDisplayedRow(parent); _displayedOperations[line] = op2; if (op2.childs.size() < 2) { ops.clear(); // Sorry ... goto removeLastGroup; } UpdateOperation(op2); } ComputeWeeks(); }