diff --git a/src/view/UsersDialog.cpp b/src/view/UsersDialog.cpp index f4f1dbf..ce60891 100644 --- a/src/view/UsersDialog.cpp +++ b/src/view/UsersDialog.cpp @@ -86,6 +86,8 @@ void UsersDialog::OnOK(wxCommandEvent& event) if (!_kiss->IsValidUser(_users->GetStringSelection(), _password->GetLineText(0))) { wxMessageBox(_("Invalid password"), _("Error"), wxICON_ERROR | wxOK); + _password->Clear(); + _password->SetFocus(); } else { diff --git a/src/view/grid/GridAccount.cpp b/src/view/grid/GridAccount.cpp index 5faa510..3e5cc30 100644 --- a/src/view/grid/GridAccount.cpp +++ b/src/view/grid/GridAccount.cpp @@ -110,6 +110,11 @@ wxPen GridAccount::GetRowGridLinePen (int row) { 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; @@ -130,7 +135,7 @@ void GridAccount::LoadOperations(std::vector* operations, bool canAdd _curMonth = month; _curYear = year; _displayedOperations.clear(); - _displayedOperations.insert(_displayedOperations.begin()+0, NULL); + _displayedOperations.push_back(NULL); // Header _fixCosts = 1; it = _operations->begin(); @@ -162,7 +167,7 @@ void GridAccount::LoadOperations(std::vector* operations, bool canAdd void GridAccount::InsertOperationWithWeek(User* user, Operation* op, int line, bool fix, int month, int year) { - std::vector::iterator it; + std::vector::iterator it; int curLine, curWeek, week, i; InsertOperation(user, op, line, fix, month, year); @@ -172,17 +177,23 @@ void GridAccount::InsertOperationWithWeek(User* user, Operation* op, int line, b 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++) + for (it = _displayedOperations.begin(), curLine=0; + it != _displayedOperations.end(); + it++, curLine++) { - week = wxDateTime(it->day+1, (wxDateTime::Month)it->month, it->year).GetWeekOfMonth(); + if (*it && !(*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) continue; + week = wxDateTime((*it)->day+1, (wxDateTime::Month)(*it)->month, (*it)->year).GetWeekOfMonth(); if (week != curWeek) { SetWeek(i++, curLine); @@ -204,9 +215,11 @@ void GridAccount::InsertOperation(User* user, Operation* op, int line, bool fix, curDate.SetToCurrent(); + // // First is header + // if (op) _displayedOperations.insert(_displayedOperations.begin()+line, op); - if (!op && !user->_accounts.size()) return; + if (!user->_accounts.size()) return; InsertRows(line, 1); @@ -329,10 +342,22 @@ void GridAccount::OnCellLeftClick(wxGridEvent& evt) 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::OnOperationModified(wxGridEvent& event) { User* user = _kiss->GetUser(); - int row = event.GetRow()-1; + int row = event.GetRow(); int col = event.GetCol(); Operation new_op, cur_op, op_tmp; int op_complete = 6, i; @@ -348,19 +373,19 @@ void GridAccount::OnOperationModified(wxGridEvent& event) inModification = true ; - if (event.GetCol() == DEBIT) - SetCellValue(event.GetRow(), CREDIT, wxT("")); - else if (event.GetCol() == CREDIT) - SetCellValue(event.GetRow(), DEBIT, wxT("")); + if (col == DEBIT) + SetCellValue(row, CREDIT, wxT("")); + else if (col == CREDIT) + SetCellValue(row, DEBIT, wxT("")); - value = GetCellValue(event.GetRow(), DESCRIPTION); + value = GetCellValue(row, DESCRIPTION); if (value.Length()) { new_op.description = value; op_complete--; } - value = GetCellValue(event.GetRow(), DATE); + value = GetCellValue(row, DATE); if (value.Length()) { date.ParseFormat(value, wxT("%d/%m/%Y")); @@ -370,7 +395,7 @@ void GridAccount::OnOperationModified(wxGridEvent& event) op_complete--; } - value = GetCellValue(event.GetRow(), DEBIT); + value = GetCellValue(row, DEBIT); if (value.Length()) { value.ToDouble(&new_op.amount); @@ -378,44 +403,44 @@ void GridAccount::OnOperationModified(wxGridEvent& event) op_complete--; } - value = GetCellValue(event.GetRow(), CREDIT); + value = GetCellValue(row, CREDIT); if (value.Length()) { value.ToDouble(&new_op.amount); op_complete--; } - value = GetCellValue(event.GetRow(), CATEGORY); + value = GetCellValue(row, CATEGORY); if (value.Length()) { new_op.category = user->GetCategoryId(value); op_complete--; } - value = GetCellValue(event.GetRow(), ACCOUNT); + value = GetCellValue(row, ACCOUNT); if (value.Length()) { new_op.account = user->GetAccountId(value); op_complete--; } - value = GetCellValue(event.GetRow(), CHECKED); + value = GetCellValue(row, CHECKED); if (value.Length() && value != wxT("0")) new_op.checked = true; else new_op.checked = false; op_complete--; - if (event.GetCol() == DESCRIPTION && - (!GetCellValue(event.GetRow(), CATEGORY).Length() || - !GetCellValue(event.GetRow(), ACCOUNT).Length())) + if (col == DESCRIPTION && + (!GetCellValue(row, CATEGORY).Length() || + !GetCellValue(row, ACCOUNT).Length())) { if (_kiss->SearchPreviousOperation(&op_tmp, new_op.description, _curMonth, _curYear)) { new_op.category = op_tmp.category; new_op.account = op_tmp.account; - SetCellValue(event.GetRow(), CATEGORY, user->GetCategoryName(new_op.category)); - SetCellValue(event.GetRow(), ACCOUNT, user->GetAccountName(new_op.account)); + SetCellValue(row, CATEGORY, user->GetCategoryName(new_op.category)); + SetCellValue(row, ACCOUNT, user->GetAccountName(new_op.account)); op_complete -= 2; } } @@ -432,8 +457,8 @@ void GridAccount::OnOperationModified(wxGridEvent& event) color.Set(r, g, b, color.Alpha()); } - SET_ROW_COLOR(event.GetRow(), color, user->GetCategory(new_op.category).forecolor); - SET_ROW_FONT(event.GetRow(), user->GetCategoryFont(new_op.category)); + SET_ROW_COLOR(row, color, user->GetCategory(new_op.category).forecolor); + SET_ROW_FONT(row, user->GetCategoryFont(new_op.category)); } if (col == DELETE) @@ -441,7 +466,7 @@ void GridAccount::OnOperationModified(wxGridEvent& event) wxMessageDialog dialog(this, _("Are you sure want to delete : \n")+new_op.description, wxT("KissCount"), wxYES_NO); if (dialog.ShowModal() == wxID_NO) { - SetCellValue(event.GetRow(), event.GetCol(), wxT("0")); + SetCellValue(row, col, wxT("0")); inModification = false; return; } @@ -450,13 +475,14 @@ void GridAccount::OnOperationModified(wxGridEvent& event) // Modify a fix operation if (row < _fixCosts) { - cur_op = (*_operations)[row] ; + cur_op = *(_displayedOperations)[row] ; if (col == DELETE) { - DeleteRows(event.GetRow(), 1); - _operations->erase(_operations->begin()+row); - _kiss->DeleteOperation(cur_op); + DeleteRows(row, 1); + DeleteOperation(cur_op); + _kiss->DeleteOperation(cur_op); + _displayedOperations.erase(_displayedOperations.begin()+row); _fixCosts = _fixCosts--; inModification = false ; event.Skip(); @@ -470,15 +496,16 @@ void GridAccount::OnOperationModified(wxGridEvent& event) if (cur_op.day != new_op.day) { need_insertion = true; - DeleteRows(event.GetRow(), 1); - _operations->erase(_operations->begin()+row); + DeleteRows(row, 1); + DeleteOperation(cur_op); + _displayedOperations.erase(_displayedOperations.begin()+row); _fixCosts--; _kiss->UpdateOperation(new_op); } else { _kiss->UpdateOperation(new_op); - (*_operations)[row] = new_op; + *(_displayedOperations)[row] = new_op; } fix_op = true; @@ -497,7 +524,7 @@ void GridAccount::OnOperationModified(wxGridEvent& event) for(i=0; iAddOperation(new_op); } // Modify an operation - else if (row <= user->GetOperationsNumber(_curMonth, _curYear)) + else if (row < (int)(_displayedOperations.size()-1)) { - row--; - cur_op = (*_operations)[row] ; + cur_op = *(_displayedOperations)[row] ; new_op.id = cur_op.id; new_op.fix_cost = false; new_op.transfert = cur_op.transfert; if (col == DELETE) { - DeleteRows(event.GetRow(), 1); - _operations->erase(_operations->begin()+row); + DeleteRows(row, 1); + DeleteOperation(cur_op); + _displayedOperations.erase(_displayedOperations.begin()+row); _kiss->DeleteOperation(cur_op); inModification = false ; event.Skip(); @@ -529,20 +556,20 @@ void GridAccount::OnOperationModified(wxGridEvent& event) if (cur_op.day != new_op.day) { need_insertion = true; - DeleteRows(event.GetRow(), 1); - _operations->erase(_operations->begin()+row); + DeleteRows(row, 1); + DeleteOperation(cur_op); + _displayedOperations.erase(_displayedOperations.begin()+row); _kiss->UpdateOperation(new_op); } else { _kiss->UpdateOperation(new_op); - (*_operations)[row] = new_op; + *(_displayedOperations)[row] = new_op; } } // Add an operation else { - row--; if (op_complete) { inModification = false ; return ; @@ -553,41 +580,42 @@ void GridAccount::OnOperationModified(wxGridEvent& event) for(i=0; iAddOperation(new_op); } if (need_insertion) { - for(i=0; i<(int)_operations->size(); i++) + for(i=0; i<(int)_displayedOperations.size(); i++) { - if ((*_operations)[i].fix_cost && !fix_op) continue; - if (!(*_operations)[i].fix_cost && fix_op) break; + if (_displayedOperations[i] == NULL) continue; + if ((_displayedOperations)[i]->fix_cost && !fix_op) continue; + if (!(_displayedOperations)[i]->fix_cost && fix_op) break; if (user->_preferences[wxT("operation_order")] == wxT("ASC")) { - if ((*_operations)[i].day > new_op.day) + if ((_displayedOperations)[i]->day > new_op.day) break; } else { - if ((*_operations)[i].day < new_op.day) + if ((_displayedOperations)[i]->day < new_op.day) break; } } - _operations->insert(_operations->begin()+i ,new_op); + if (i == (int)_displayedOperations.size()) i--; - i++; // For header - if (!fix_op) i++; - InsertOperationWithWeek(user, &new_op, i, fix_op, _curMonth, _curYear); + _operations->push_back(new_op); + + InsertOperationWithWeek(user, &((*_operations)[_operations->size()-1]), i, fix_op, _curMonth, _curYear); if (fix_op) _fixCosts = _fixCosts+1; } diff --git a/src/view/grid/GridAccount.h b/src/view/grid/GridAccount.h index 061734e..2c3c7c1 100644 --- a/src/view/grid/GridAccount.h +++ b/src/view/grid/GridAccount.h @@ -40,7 +40,6 @@ public: wxPen GetColGridLinePen (int col); wxPen GetRowGridLinePen (int row); - void SetWeek(int week, int line); 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) ; @@ -59,6 +58,10 @@ private: int _curMonth, _curYear; std::vector _displayedOperations; + void SetWeek(int week, int line); + void ResetWeeks(); + void DeleteOperation(const Operation& op); + DECLARE_EVENT_TABLE(); }; #endif