From 9c52ca53626a88d7f6b18d6419b36a63f02ad093 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Gr=C3=A9gory=20Soutad=C3=A9?= Date: Sun, 12 Sep 2010 18:10:40 +0200 Subject: [PATCH] Some Grid code moved to GridAccount --- src/view/AccountPanel.cpp | 57 +--------------------- src/view/SearchPanel.cpp | 11 +---- src/view/grid/GridAccount.cpp | 90 +++++++++++++++++++++++++++++++---- src/view/grid/GridAccount.h | 7 ++- 4 files changed, 89 insertions(+), 76 deletions(-) diff --git a/src/view/AccountPanel.cpp b/src/view/AccountPanel.cpp index 7f4570f..cea0843 100644 --- a/src/view/AccountPanel.cpp +++ b/src/view/AccountPanel.cpp @@ -291,9 +291,7 @@ void AccountPanel::LoadYear(int year, bool showMonth) void AccountPanel::ShowMonth(int month, int year) { std::vector operations; - std::vector::iterator it; _fixCosts = 0; - int curLine = 0; User* user = _kiss->GetUser(); DEFAULT_FONT(font); std::vector::iterator categoryIt; @@ -337,24 +335,7 @@ void AccountPanel::ShowMonth(int month, int year) // Operations are ordered _curOperations = &((*user->_operations[year])[month]); - it = _curOperations->begin(); - - for (;it != _curOperations->end() && it->fix_cost; it++) - InsertOperation(user, &(*it), ++curLine, true); - - InsertOperation(user, NULL, ++curLine, true); - _grid->_fixCosts = _fixCosts--; - - for (; it != _curOperations->end(); it++) - InsertOperation(user, &(*it), ++curLine, false); - - InsertOperation(user, NULL, ++curLine, false); - - _grid->AutoSizeColumn(CATEGORY, false); - _grid->AutoSizeColumn(DATE, false); - _grid->AutoSizeColumn(ACCOUNT, false); - _grid->AutoSizeColumn(DELETE, false); - _grid->AutoSizeColumn(CHECKED, false); + _grid->LoadOperations(_curOperations, true, true, _curMonth, _curYear); InitAccountsGrid(user, month, year); @@ -377,40 +358,6 @@ void AccountPanel::ShowMonth(int month, int year) // SetMinSize(GetSize()); } -void AccountPanel::InsertOperation(User* user, Operation* op, int line, bool fix) -{ - std::vector::iterator it; - int curLine, curWeek, week, i; - - _grid->InsertOperation(user, op, line, fix, _curMonth, _curYear); - - if (fix) - _fixCosts++; - - if (op && !fix) - { - for (it = _curOperations->begin(), curLine=1; - it->fix_cost && it != _curOperations->end(); - it++, curLine++) ; - - if (it == _curOperations->end()) return; - - curLine++; - curWeek = wxDateTime(it->day+1, (wxDateTime::Month)it->month, it->year).GetWeekOfMonth(); - for (i=1, it++; it != _curOperations->end(); it++, curLine++) - { - week = wxDateTime(it->day+1, (wxDateTime::Month)it->month, it->year).GetWeekOfMonth(); - if (week != curWeek) - { - _grid->SetWeek(i++, curLine); - curWeek = week; - } - } - } - - _wxUI->Layout(); -} - void AccountPanel::InitAccountsGrid(User* user, int month, int year) { std::vector::iterator it; @@ -828,7 +775,7 @@ void AccountPanel::OnOperationModified(wxGridEvent& event) i++; // For header if (!fix_op) i++; - InsertOperation(user, &new_op, i, fix_op); + _grid->InsertOperationWithWeek(user, &new_op, i, fix_op, _curMonth, _curYear); if (fix_op) _grid->_fixCosts = _fixCosts+1; } diff --git a/src/view/SearchPanel.cpp b/src/view/SearchPanel.cpp index d086e7b..b698d13 100644 --- a/src/view/SearchPanel.cpp +++ b/src/view/SearchPanel.cpp @@ -225,16 +225,7 @@ void SearchPanel::OnButtonSearch(wxCommandEvent& event) return; } - for(i=1, it = _operations->begin(); it != _operations->end(); it++, i++) - { - _grid->InsertOperation(user, &(*it), i, false, 0, 0); - } - - _grid->AutoSizeColumn(CATEGORY, false); - _grid->AutoSizeColumn(DATE, false); - _grid->AutoSizeColumn(ACCOUNT, false); - _grid->AutoSizeColumn(DELETE, false); - _grid->AutoSizeColumn(CHECKED, false); + _grid->LoadOperations(_operations, false, false, 0, 0); _wxUI->Layout(); } diff --git a/src/view/grid/GridAccount.cpp b/src/view/grid/GridAccount.cpp index 3db9199..83cf690 100644 --- a/src/view/grid/GridAccount.cpp +++ b/src/view/grid/GridAccount.cpp @@ -116,7 +116,77 @@ void GridAccount::SetWeek(int week, int line) { } } -void GridAccount::InsertOperation(User* user, Operation* op, int line, bool fix, int curMonth, int curYear) +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; + + _operations = operations; + _canAddOperation = canAddOperation; + + _fixCosts = 1; + + it = _operations->begin(); + + for (;it != _operations->end() && it->fix_cost; it++) + if (setWeek) + InsertOperationWithWeek(user, &(*it), ++curLine, true, it->month, it->year); + else + InsertOperation(user, &(*it), ++curLine, true, it->month, it->year); + + if (canAddOperation) + InsertOperation(user, NULL, ++curLine, true, month, year); + + for (; it != _operations->end(); it++) + if (setWeek) + InsertOperationWithWeek(user, &(*it), ++curLine, false, it->month, it->year); + else + InsertOperation(user, &(*it), ++curLine, false, it->month, it->year); + + if (canAddOperation) + InsertOperation(user, NULL, ++curLine, false, month, year); + + AutoSizeColumn(CATEGORY, false); + AutoSizeColumn(DATE, false); + AutoSizeColumn(ACCOUNT, false); + AutoSizeColumn(DELETE, false); + AutoSizeColumn(CHECKED, false); +} + +void GridAccount::InsertOperationWithWeek(User* user, Operation* op, int line, bool fix, int month, int year) +{ + std::vector::iterator it; + int curLine, curWeek, week, i; + + InsertOperation(user, op, line, fix, month, year); + + if (fix) + _fixCosts++; + + if (op && !fix) + { + for (it = _operations->begin(), curLine=1; + it->fix_cost && it != _operations->end(); + it++, curLine++) ; + + if (it == _operations->end()) return; + + curLine++; + curWeek = wxDateTime(it->day+1, (wxDateTime::Month)it->month, it->year).GetWeekOfMonth(); + for (i=1, it++; it != _operations->end(); it++, curLine++) + { + week = wxDateTime(it->day+1, (wxDateTime::Month)it->month, it->year).GetWeekOfMonth(); + if (week != curWeek) + { + SetWeek(i++, curLine); + curWeek = week; + } + } + } +} + +void GridAccount::InsertOperation(User* user, Operation* op, int line, bool fix, int month, int year) { std::vector::iterator it; int r, g, b; @@ -149,18 +219,18 @@ void GridAccount::InsertOperation(User* user, Operation* op, int line, bool fix, { cat = user->GetCategory(op->category); - SetCellEditor(line, DATE, new CalendarEditor(op->day, op->month, op->year)); + SetCellEditor(line, DATE, new CalendarEditor(op->day, month, year)); description = op->description; UNESCAPE_CHARS(description); SetCellValue(line, DESCRIPTION, description); - SetCellValue(line, DATE, wxString::Format(wxT("%02d/%02d/%d"), op->day+1, op->month+1, op->year)); + 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)); SetCellValue(line, ACCOUNT, user->GetAccountName(op->account)); if (!fix) - SetCellValue(line, CATEGORY, user->GetCategoryName(op->category)); + SetCellValue(line, CATEGORY, cat.name); SetCellRenderer(line, DELETE, new wxGridCellBoolRenderer ()); SetCellEditor(line, DELETE, new wxGridCellBoolEditor ()); SetCellRenderer(line, CHECKED, new wxGridCellBoolRenderer ()); @@ -186,16 +256,16 @@ void GridAccount::InsertOperation(User* user, Operation* op, int line, bool fix, } else { - SetCellEditor(line, DATE, new CalendarEditor(0, curMonth, curYear)); + SetCellEditor(line, DATE, new CalendarEditor(0, month, year)); if (!fix && - curDate.GetMonth() == curMonth && - curDate.GetYear() == curYear) + curDate.GetMonth() == month && + curDate.GetYear() == year) { - SetCellValue(line, DATE, wxString::Format(wxT("%02d/%02d/%d"), curDate.GetDay(), curMonth+1, curYear)); - SetCellEditor(line, DATE, new CalendarEditor(curDate.GetDay()-1, curMonth, curYear)); + 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, curMonth, curYear)); + SetCellEditor(line, DATE, new CalendarEditor(0, month, year)); if (fix) { diff --git a/src/view/grid/GridAccount.h b/src/view/grid/GridAccount.h index 907019a..ddfe6cf 100644 --- a/src/view/grid/GridAccount.h +++ b/src/view/grid/GridAccount.h @@ -41,7 +41,10 @@ public: wxPen GetColGridLinePen (int col); wxPen GetRowGridLinePen (int row); void SetWeek(int week, int line); - void InsertOperation(User* user, Operation* op, int line, bool fix, int curMonth, int curYear); + + void LoadOperations(std::vector* operations, bool canAddOperation, bool setWeek, int month, int year); + void InsertOperationWithWeek(User* user, Operation* op, int line, bool fix, int month, int year) ; + void InsertOperation(User* user, Operation* op, int line, bool fix, int month, int year) ; void OnCellLeftClick(wxGridEvent& evt); @@ -50,6 +53,8 @@ public: private: KissCount* _kiss; wxString* _categories, *_accounts; + std::vector* _operations; + bool _canAddOperation; DECLARE_EVENT_TABLE(); };