diff --git a/src/controller/KissCount.cpp b/src/controller/KissCount.cpp index d1a1715..2e773da 100644 --- a/src/controller/KissCount.cpp +++ b/src/controller/KissCount.cpp @@ -75,6 +75,7 @@ void KissCount::LoadYear(int year, bool force) if (_user->_operations[year] != NULL) { delete _user->_operations[year]; + _user->_operations[year] = NULL; } _db->LoadYear(_user, year); diff --git a/src/view/SearchPanel.cpp b/src/view/SearchPanel.cpp index c97e5a1..e7ce80f 100644 --- a/src/view/SearchPanel.cpp +++ b/src/view/SearchPanel.cpp @@ -23,9 +23,10 @@ enum {SEARCH_ID, GRID_ID, CALENDAR_FROM_ID, CALENDAR_TO_ID}; BEGIN_EVENT_TABLE(SearchPanel, wxPanel) EVT_BUTTON(SEARCH_ID, SearchPanel::OnButtonSearch) -EVT_SHOW(SearchPanel::OnShow) EVT_CALENDAR_SEL_CHANGED(CALENDAR_FROM_ID, SearchPanel::OnCalendarFromChange) EVT_CALENDAR_SEL_CHANGED(CALENDAR_TO_ID, SearchPanel::OnCalendarToChange) +EVT_GRID_CMD_CELL_CHANGE(GRID_ID, SearchPanel::OnOperationModified) +EVT_SHOW(SearchPanel::OnShow) END_EVENT_TABLE() #define SET_ROW_COLOR(row, color) for(int i=0; iSetValue(true); } + +void SearchPanel::OnOperationModified(wxGridEvent& event) +{ + User* user = _kiss->GetUser(); + int row = event.GetRow()-1; + int col = event.GetCol(); + Operation new_op, cur_op; + int op_complete = 6; + wxString value ; + wxDateTime date; + static bool inModification = false ; + wxColour color ; + unsigned char r, g, b; + + // Avoid recursives calls + if (inModification) return; + + inModification = true ; + + if (event.GetCol() == DEBIT) + _grid->SetCellValue(event.GetRow(), CREDIT, wxT("")); + else if (event.GetCol() == CREDIT) + _grid->SetCellValue(event.GetRow(), DEBIT, wxT("")); + + value = _grid->GetCellValue(event.GetRow(), DESCRIPTION); + if (value != wxT("")) + { + new_op.description = value; + op_complete--; + } + + value = _grid->GetCellValue(event.GetRow(), DATE); + if (value != wxT("")) + { + 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 = _grid->GetCellValue(event.GetRow(), DEBIT); + if (value != wxT("")) + { + value.ToDouble(&new_op.amount); + new_op.amount *= -1.0; + op_complete--; + } + + value = _grid->GetCellValue(event.GetRow(), CREDIT); + if (value != wxT("")) + { + value.ToDouble(&new_op.amount); + op_complete--; + } + + value = _grid->GetCellValue(event.GetRow(), CATEGORY); + if (value != wxT("")) + { + new_op.category = user->GetCategoryId(value); + op_complete--; + } + + value = _grid->GetCellValue(event.GetRow(), ACCOUNT); + if (value != wxT("")) + { + new_op.account = user->GetAccountId(value); + op_complete--; + } + + value = _grid->GetCellValue(event.GetRow(), CHECKED); + if (value != wxT("") && value != wxT("0")) + new_op.checked = true; + else + new_op.checked = false; + op_complete--; + + if (col == CHECKED || col == CATEGORY) + { + color = user->GetCategory(new_op.category).color; + + 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(event.GetRow(), color); + } + + if (col == DELETE) + { + wxMessageDialog dialog(_wxUI, _("Are you sure want to delete : \n")+new_op.description, wxT("KissCount"), wxYES_NO); + if (dialog.ShowModal() == wxID_NO) + { + _grid->SetCellValue(event.GetRow(), event.GetCol(), wxT("0")); + _wxUI->NeedReload(); + inModification = false; + return; + } + } + + // Modify an operation + cur_op = (*_operations)[row] ; + new_op.id = cur_op.id; + new_op.fix_cost = false; + + if (col == DELETE) + { + _grid->DeleteRows(event.GetRow(), 1); + _kiss->DeleteOperation(cur_op); + _operations->erase(_operations->begin()+row); + _wxUI->NeedReload(); + inModification = false ; + return ; + } + + (*_operations)[row] = new_op; + _kiss->UpdateOperation(new_op); + + _wxUI->NeedReload(); + + inModification = false ; +} diff --git a/src/view/SearchPanel.h b/src/view/SearchPanel.h index 393a8e6..dfcfc37 100644 --- a/src/view/SearchPanel.h +++ b/src/view/SearchPanel.h @@ -45,6 +45,7 @@ class SearchPanel: public wxScrolledWindow ~SearchPanel(); void OnButtonSearch(wxCommandEvent& event); + void OnOperationModified(wxGridEvent& event); void OnShow(wxShowEvent& event); void OnCalendarFromChange(wxCalendarEvent& event); void OnCalendarToChange(wxCalendarEvent& event); diff --git a/src/view/wxUI.cpp b/src/view/wxUI.cpp index 4fc1982..9a01fff 100644 --- a/src/view/wxUI.cpp +++ b/src/view/wxUI.cpp @@ -195,7 +195,10 @@ void wxUI::ShowPanel(wxPanel* panel) _accountPanel = new AccountPanel(_kiss, this); if (year != -1) - _accountPanel->ShowMonth(month, year); + { + _kiss->LoadYear(year, true); + _accountPanel->ShowMonth(month, year); + } _preferencesPanel = new PreferencesPanel(_kiss, this); _searchPanel = new SearchPanel(_kiss, this);