From 477d155c3f4e44093572dcf5a54cbf787b22ed17 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Gr=C3=A9gory=20Soutad=C3=A9?= Date: Thu, 7 Oct 2010 20:24:35 +0200 Subject: [PATCH] * Group/UnGroup seems to work --- src/view/AccountPanel.cpp | 2 +- src/view/grid/GridAccount.cpp | 183 +++++++++++++++++++++++++++++++--- src/view/grid/GridAccount.h | 1 + 3 files changed, 170 insertions(+), 16 deletions(-) diff --git a/src/view/AccountPanel.cpp b/src/view/AccountPanel.cpp index 5f25fc8..4a2fe73 100644 --- a/src/view/AccountPanel.cpp +++ b/src/view/AccountPanel.cpp @@ -124,7 +124,7 @@ AccountPanel::AccountPanel(KissCount* kiss, wxUI *parent) : wxScrolledWindow(&(* _tree.SetIndent(5); wxButton* buttonGroup = new wxButton(this, GROUP_ID, _("Group")); - wxButton* buttonUnGroup = new wxButton(this, GROUP_ID, _("UnGroup")); + wxButton* buttonUnGroup = new wxButton(this, UNGROUP_ID, _("UnGroup")); vbox3->Add(&_tree, 0); vbox3->Add(-1, 30); diff --git a/src/view/grid/GridAccount.cpp b/src/view/grid/GridAccount.cpp index a9f4cd0..e730473 100644 --- a/src/view/grid/GridAccount.cpp +++ b/src/view/grid/GridAccount.cpp @@ -138,6 +138,7 @@ Operation& GridAccount::GetOperation(const wxString& id) 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) @@ -442,6 +443,7 @@ void GridAccount::InsertIntoGrid(Operation& op) for(i=0; 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")) @@ -930,9 +932,57 @@ void GridAccount::UpdateMeta(Operation& op) UpdateOperation(op); } + +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 rows; + std::vector selected, rows; std::vector::iterator it; std::vector ops; std::vector::iterator it2; @@ -940,20 +990,12 @@ void GridAccount::Group() wxString parent = wxT(""); Operation op, op2; int fix = -1, i; - - // Singly selected cells. - const wxGridCellCoordsArray& cells(GetSelectedCells()); - for (size_t i = 0; i < cells.size(); ++i) + + GetSelectedOperations(&selected); + + for (size_t i = 0; i < selected.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; - - op = _displayedOperations[c.GetRow()] ; + op = _displayedOperations[selected[i]] ; if (op.id.Length()) { @@ -986,10 +1028,12 @@ void GridAccount::Group() fix = op.fix_cost ? 1 : 0; ops.push_back(op); - rows.push_back(c.GetRow()); + rows.push_back(selected[i]); } } + if (!ops.size()) return; + if (!parent.Length()) { if (rows.size() < 2) return; @@ -1080,7 +1124,116 @@ void GridAccount::Group() InsertIntoGrid(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(""); + _kiss->UpdateOperation(op2); + if (op2.fix_cost) _fixCosts--; + InsertIntoGrid(op2); + } + + _kiss->DeleteOperation(op); + DeleteOperation(op); + if (op.fix_cost) _fixCosts--; + } + 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(""); + _kiss->UpdateOperation(op); + line = GetDisplayedRow(op.id); + DeleteRows(line, 1); + _displayedOperations.erase(_displayedOperations.begin()+line); + InsertIntoGrid(GetOperation(op.id)); // Don't use temp variable + 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; + } + } + + line = GetDisplayedRow(parent); + _displayedOperations[line] = op2; + + if (op2.childs.size() < 2) + { + ops.clear(); + // Sorry ... + goto removeLastGroup; + } + + _kiss->UpdateOperation(op2); + } } diff --git a/src/view/grid/GridAccount.h b/src/view/grid/GridAccount.h index 118a59b..7ac9a59 100644 --- a/src/view/grid/GridAccount.h +++ b/src/view/grid/GridAccount.h @@ -75,6 +75,7 @@ private: Operation& GetOperation(const wxString& id); void UpdateOperation(Operation& op); int GetDisplayedRow(const wxString& id); + void GetSelectedOperations(std::vector* rows); DECLARE_EVENT_TABLE(); };