diff --git a/src/view/grid/GridAccount.cpp b/src/view/grid/GridAccount.cpp index e730473..c0935ce 100644 --- a/src/view/grid/GridAccount.cpp +++ b/src/view/grid/GridAccount.cpp @@ -255,7 +255,8 @@ void GridAccount::InsertOperation(User* user, Operation& op, int line, bool fix, { std::vector::iterator it; std::vector::iterator it2; - int r, g, b, amount; + int r, g, b; + double amount; wxColour color; wxDateTime curDate; wxString description; @@ -314,6 +315,10 @@ void GridAccount::InsertOperation(User* user, Operation& op, int line, bool fix, SetCellValue(line, DEBIT, wxString::Format(wxT("%.2lf"), -op.amount)); else SetCellValue(line, CREDIT, wxString::Format(wxT("%.2lf"), op.amount)); + + SetCellEditor(line, DEBIT, new wxGridCellFormulaEditor(op.formula)); + SetCellEditor(line, CREDIT, new wxGridCellFormulaEditor(op.formula)); + if (!op.meta) SetCellValue(line, ACCOUNT, user->GetAccountName(op.account)); if (!fix && !op.meta) @@ -633,6 +638,9 @@ void GridAccount::OnOperationModified(wxGridEvent& event) value.ToDouble(&new_op.amount); new_op.amount *= -1.0; op_complete--; + wxGridCellFormulaEditor* pEditor = (wxGridCellFormulaEditor*) GetCellEditor(row, col); + new_op.formula = pEditor->GetFormula(); + pEditor->DecRef(); } value = GetCellValue(row, CREDIT); @@ -640,6 +648,9 @@ void GridAccount::OnOperationModified(wxGridEvent& event) { value.ToDouble(&new_op.amount); op_complete--; + wxGridCellFormulaEditor* pEditor = (wxGridCellFormulaEditor*) GetCellEditor(row, col); + new_op.formula = pEditor->GetFormula(); + pEditor->DecRef(); } value = GetCellValue(row, CATEGORY); @@ -750,10 +761,30 @@ void GridAccount::OnOperationModified(wxGridEvent& event) 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() <= 1) + { + row = GetDisplayedRow(cur_op.parent); + DeleteRows(row, 1); + DeleteOperation(op_tmp); + _kiss->DeleteOperation(op_tmp); + _displayedOperations.erase(_displayedOperations.begin()+row); + } } _fixCosts = _fixCosts--; inModification = false ; @@ -767,7 +798,7 @@ void GridAccount::OnOperationModified(wxGridEvent& event) 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; @@ -829,10 +860,30 @@ void GridAccount::OnOperationModified(wxGridEvent& event) 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) + { + row = GetDisplayedRow(cur_op.parent); + DeleteRows(row, 1); + DeleteOperation(op_tmp); + _kiss->DeleteOperation(op_tmp); + _displayedOperations.erase(_displayedOperations.begin()+row); + } } inModification = false ; event.Skip(); @@ -1214,7 +1265,7 @@ removeLastGroup: line = GetDisplayedRow(op.id); DeleteRows(line, 1); _displayedOperations.erase(_displayedOperations.begin()+line); - InsertIntoGrid(GetOperation(op.id)); // Don't use temp variable + InsertIntoGrid(GetOperation(op.id)); if (op.fix_cost) _fixCosts--; for (a=0; a<(int)op2.childs.size(); a++) if (op2.childs[a] == op.id) diff --git a/src/view/grid/GridAccount.h b/src/view/grid/GridAccount.h index 7ac9a59..c7eb5dc 100644 --- a/src/view/grid/GridAccount.h +++ b/src/view/grid/GridAccount.h @@ -30,6 +30,7 @@ #include "wxGridCellFastBoolEditor.h" #include "wxGridCellTreeButtonRenderer.h" #include "wxGridCellTreeButtonEditor.h" +#include "wxGridCellFormulaEditor.h" class KissCount; @@ -56,13 +57,14 @@ public: int _fixCosts; int _week1, _week2, _week3, _week4; + std::vector _displayedOperations; + private: KissCount* _kiss; wxString* _categories, *_accounts; std::vector* _operations; bool _canAddOperation; int _curMonth, _curYear; - std::vector _displayedOperations; void SetWeek(int week, int line); void ResetWeeks(); diff --git a/src/view/grid/wxGridCellFormulaEditor.cpp b/src/view/grid/wxGridCellFormulaEditor.cpp new file mode 100644 index 0000000..7e0ca2d --- /dev/null +++ b/src/view/grid/wxGridCellFormulaEditor.cpp @@ -0,0 +1,70 @@ +/* + 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 "wxGridCellFormulaEditor.h" + +wxGridCellFormulaEditor::wxGridCellFormulaEditor(const wxString& formula) : _formula(formula) +{} + +void wxGridCellFormulaEditor::BeginEdit (int row, int col, wxGrid *grid) { + static bool inModification = false; + Operation op; + + if (inModification) return ; + + inModification = true; + + op = ((GridAccount*) grid)->_displayedOperations[row]; + + if (op.formula.Length()) + { + grid->SetCellValue(row, col, op.formula); + _formula = op.formula; + } + + wxGridCellTextEditor::BeginEdit(row, col, grid); + + inModification = false; +} + +bool wxGridCellFormulaEditor::EndEdit (int row, int col, wxGrid *grid/*, const wxString &oldval, wxString *newval*/) +{ + wxString res = GetValue(); + Operation op; + bool ret; + + res = res.Trim(); + + if (res.StartsWith(wxT("="))) + _formula = res; + else + _formula = wxT(""); + + ret = wxGridCellTextEditor::EndEdit(row, col, grid); + + if (_formula.Length()) + grid->SetCellValue(row, col, wxT("0")); + + return ret; +} + +wxString wxGridCellFormulaEditor::GetFormula() +{ + return _formula; +} diff --git a/src/view/grid/wxGridCellFormulaEditor.h b/src/view/grid/wxGridCellFormulaEditor.h new file mode 100644 index 0000000..8817bf6 --- /dev/null +++ b/src/view/grid/wxGridCellFormulaEditor.h @@ -0,0 +1,56 @@ +/* + 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 . +*/ + +#ifndef WXGRIDCELLFORMULAEDITOR_H +#define WXGRIDCELLFORMULAEDITOR_H + +#include +#include +#include +#include "GridAccount.h" + +class wxGridCellFormulaEditor : public wxGridCellTextEditor +{ +public: + wxGridCellFormulaEditor(const wxString& formula); + /* ~wxGridCellFormulEeditor(); */ + + /* wxGridCellEditor* Clone () const; */ + + void BeginEdit (int row, int col, wxGrid *grid); + /* void Create (wxWindow *parent, wxWindowID id, wxEvtHandler *evtHandler); */ + bool EndEdit (int row, int col, wxGrid *grid/*, const wxString &oldval, wxString *newval*/); + /* void ApplyEdit (int row, int col, wxGrid *grid); */ + /* void Reset () ; */ + /* wxString GetValue() const ; */ + /* void SetSize (const wxRect &rect); */ + /* void Show (bool show, wxGridCellAttr *attr); */ + + wxString GetFormula(); + +private: + wxString _formula; + wxButton* _button; + int _row, _col; + wxGrid* _grid; + + /* DECLARE_EVENT_TABLE(); */ +}; + +#endif