From 6a8ce2164a8889a8699c5f82e8f1ca6ce166a379 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Gr=C3=A9gory=20Soutad=C3=A9?= Date: Tue, 22 Jun 2010 12:29:36 +0200 Subject: [PATCH] Move categories from map to vector --- controller/KissCount.cpp | 30 ++++++++----------- controller/KissCount.h | 6 ++-- model/Database.cpp | 62 ++++++++++++++++++++++++++++++++------- model/Database.h | 4 +-- model/Preferences.cpp | 1 + model/Preferences.h | 12 ++++++-- model/User.cpp | 31 +++++++++++++++----- model/User.h | 2 ++ view/AccountPanel.cpp | 38 ++++++------------------ view/PreferencesPanel.cpp | 38 ++++++++++-------------- view/PreferencesPanel.h | 1 - 11 files changed, 128 insertions(+), 97 deletions(-) diff --git a/controller/KissCount.cpp b/controller/KissCount.cpp index 2466319..97880ef 100644 --- a/controller/KissCount.cpp +++ b/controller/KissCount.cpp @@ -117,29 +117,23 @@ void KissCount::DeleteAccount(struct Account ac) _user->_accounts.erase(_user->_accounts.begin()+i); } -void KissCount::AddCategory(wxString name, wxColour colour) +wxString KissCount::AddCategory(struct category category) +{ + return _db->AddCategory(_user, category); +} + +void KissCount::UpdateCategory(wxString oldName, struct category category) { wxString color; color = _("#") ; - color += wxString::Format(_("%02X"), colour.Red()); - color += wxString::Format(_("%02X"), colour.Green()); - color += wxString::Format(_("%02X"), colour.Blue()); + color += wxString::Format(_("%02X"), category.color.Red()); + color += wxString::Format(_("%02X"), category.color.Green()); + color += wxString::Format(_("%02X"), category.color.Blue()); - _db->AddCategory(_user, name, color); + _db->UpdateCategory(_user, oldName, category.name, color); } -void KissCount::UpdateCategory(wxString oldName, wxString name, wxColour colour) +void KissCount::DeleteCategory(struct category category) { - wxString color; - color = _("#") ; - color += wxString::Format(_("%02X"), colour.Red()); - color += wxString::Format(_("%02X"), colour.Green()); - color += wxString::Format(_("%02X"), colour.Blue()); - - _db->UpdateCategory(_user, oldName, name, color); -} - -void KissCount::DeleteCategory(wxString name) -{ - _db->DeleteCategory(_user, name); + _db->DeleteCategory(_user, category); } diff --git a/controller/KissCount.h b/controller/KissCount.h index d2f5f15..2480f38 100644 --- a/controller/KissCount.h +++ b/controller/KissCount.h @@ -32,9 +32,9 @@ class KissCount void UpdateAccount(struct Account ac); void DeleteAccount(struct Account ac); - void AddCategory(wxString name, wxColour colour); - void UpdateCategory(wxString oldName, wxString name, wxColour colour); - void DeleteCategory(wxString name); + wxString AddCategory(struct category category); + void UpdateCategory(wxString oldName, struct category category); + void DeleteCategory(struct category category); private: wxUI* _wxUI; diff --git a/model/Database.cpp b/model/Database.cpp index 284f8c7..5169369 100644 --- a/model/Database.cpp +++ b/model/Database.cpp @@ -148,6 +148,8 @@ User* Database::LoadUser(wxString name) wxString req; User* user; struct Account account; + struct category category; + std::vector::iterator it; req = _("SELECT * FROM user WHERE name='") + name + _("'"); @@ -204,7 +206,14 @@ User* Database::LoadUser(wxString name) EXECUTE_SQL_QUERY_WITH_CODE(req, set, NULL, {delete user;}, {delete user;}); while (set.NextRow()) - user->_preferences._categories[set.GetAsString(_("id"))] = set.GetAsString(_("value")); + { + category.id = set.GetAsString(_("id")); + category.name = set.GetAsString(_("value")); + if (category.name != _("Fixe")) + user->_preferences._categories.push_back(category); + else + user->_preferences._categories.insert(user->_preferences._categories.begin(), category); + } set.Finalize(); @@ -212,7 +221,15 @@ User* Database::LoadUser(wxString name) EXECUTE_SQL_QUERY_WITH_CODE(req, set, NULL, {delete user;}, {delete user;}); while (set.NextRow()) - user->_preferences._colors[set.GetAsString(_("name"))] = wxColour(set.GetAsString(_("value"))); + { + std::vector::iterator it; + for (it=user->_preferences._categories.begin(); it !=user->_preferences._categories.end(); it++) + if (it->name == set.GetAsString(_("name"))) + { + it->color = wxColour(set.GetAsString(_("value"))); + break; + } + } set.Finalize(); @@ -437,27 +454,50 @@ void Database::DeleteAccount(struct Account ac) EXECUTE_SQL_UPDATE(req, ); } -void Database::AddCategory(User* user, wxString name, wxString color) +wxString Database::AddCategory(User* user, struct category category) { - wxString req; + wxString req, res; + wxSQLite3ResultSet set; + wxString color; + + color = _("#") ; + color += wxString::Format(_("%02X"), category.color.Red()); + color += wxString::Format(_("%02X"), category.color.Green()); + color += wxString::Format(_("%02X"), category.color.Blue()); req = _("INSERT INTO preference ('user', 'type', 'name', 'value') VALUES ('") ; req += user->_id + _("'"); req += _(", 'category'"); req += _(", 'name'"); - req += _(", '") + name + _("'"); + req += _(", '") + category.name + _("'"); req += _(")"); - EXECUTE_SQL_UPDATE(req, ); + EXECUTE_SQL_UPDATE(req, _("0")); req = _("INSERT INTO preference ('user', 'type', 'name', 'value') VALUES ('") ; req += user->_id + _("'"); req += _(", 'category_color'"); - req += _(", '") + name + _("'"); + req += _(", '") + category.name + _("'"); req += _(", '") + color + _("'"); req += _(")"); - EXECUTE_SQL_UPDATE(req, ); + EXECUTE_SQL_UPDATE(req, _("0")); + + req = _("SELECT id FROM preference WHERE user='") + user->_id + _("'") ; + req += _(" AND type='category'"); + req += _(" AND name='name'"); + req += _(" AND value='") + category.name + _("'"); + + EXECUTE_SQL_QUERY(req , set, _("0")); + + if (set.NextRow()) + res = set.GetAsString(_("id")); + else + res = _("0"); + + set.Finalize(); + + return res; } void Database::UpdateCategory(User* user, wxString oldName, wxString name, wxString color) @@ -497,19 +537,19 @@ void Database::UpdateCategory(User* user, wxString oldName, wxString name, wxStr } } -void Database::DeleteCategory(User* user, wxString name) +void Database::DeleteCategory(User* user, struct category category) { wxString req; req = _("DELETE FROM preference WHERE user='") + user->_id + _("'"); req += _(" AND type='category'"); req += _(" AND name='name'"); - req += _(" AND value='") + name + _("'"); + req += _(" AND value='") + category.name + _("'"); EXECUTE_SQL_UPDATE(req, ); req = _("DELETE FROM preference WHERE user='") + user->_id + _("'"); req += _(" AND type='category_color'"); - req += _(" AND name='") + name + _("'"); + req += _(" AND name='") + category.name + _("'"); EXECUTE_SQL_UPDATE(req, ); } diff --git a/model/Database.h b/model/Database.h index b630c01..fcf1727 100644 --- a/model/Database.h +++ b/model/Database.h @@ -34,9 +34,9 @@ class Database void UpdateAccount(struct Account ac); void DeleteAccount(struct Account ac); - void AddCategory(User* user, wxString name, wxString color); + wxString AddCategory(User* user, struct category category); void UpdateCategory(User* user, wxString oldName, wxString name, wxString color); - void DeleteCategory(User* user, wxString name); + void DeleteCategory(User* user, struct category category); private: wxSQLite3Database _db; diff --git a/model/Preferences.cpp b/model/Preferences.cpp index 6ef05cf..9092b96 100644 --- a/model/Preferences.cpp +++ b/model/Preferences.cpp @@ -1 +1,2 @@ #include "Preferences.h" + diff --git a/model/Preferences.h b/model/Preferences.h index ba2070b..9abcaa0 100644 --- a/model/Preferences.h +++ b/model/Preferences.h @@ -2,13 +2,19 @@ #define PREFERENCES_H #include -#include +#include + +struct category +{ + wxString id; + wxString name; + wxColour color; +}; class Preferences { public: - std::map _colors; - std::map _categories; + std::vector _categories; }; #endif diff --git a/model/User.cpp b/model/User.cpp index c28119b..31fa164 100644 --- a/model/User.cpp +++ b/model/User.cpp @@ -14,19 +14,36 @@ User::~User() } } +struct category User::GetCategory(wxString catId) +{ + struct category cat; + std::vector::iterator it; + + for (it=_preferences._categories.begin(); it !=_preferences._categories.end(); it++) + if (it->id == catId) + return *it; + + cat.id = _("0"); + return cat; + +} + wxString User::GetCategoryName(wxString catId) { - if (_preferences._categories.find(catId) == _preferences._categories.end()) - return _("Unknown") ; - return _preferences._categories[catId]; + std::vector::iterator it; + for (it=_preferences._categories.begin(); it !=_preferences._categories.end(); it++) + if (it->id == catId) + return it->name; + + return _("Unknown") ; } wxString User::GetCategoryId(wxString catName) { - std::map::iterator it; - for (it=_preferences._categories.begin(); it !=_preferences._categories.end(); it++) - if (it->second == catName) - return it->first; + std::vector::iterator it; + for (it=_preferences._categories.begin(); it !=_preferences._categories.end(); it++) + if (it->name == catName) + return it->id; return _("0") ; } diff --git a/model/User.h b/model/User.h index 800b110..3487eee 100644 --- a/model/User.h +++ b/model/User.h @@ -1,6 +1,7 @@ #ifndef USER_H #define USER_H +#include #include #include #include "Preferences.h" @@ -38,6 +39,7 @@ public: std::map >* > _operations; Preferences _preferences; + struct category GetCategory(wxString catId); wxString GetCategoryName(wxString catId); wxString GetCategoryId(wxString catName); wxString GetAccountName(wxString accountId); diff --git a/view/AccountPanel.cpp b/view/AccountPanel.cpp index a6807da..e69d3a3 100644 --- a/view/AccountPanel.cpp +++ b/view/AccountPanel.cpp @@ -22,7 +22,7 @@ AccountPanel::AccountPanel(KissCount* kiss, wxUI *parent) : wxPanel(&(*parent)), DEFAULT_FONT(font); User* user = _kiss->GetUser(); std::vector::iterator accountIt; - std::map::iterator it; + std::vector::iterator categoryIt; wxColour categoryColors[] = {wxColour(0x00, 0x45, 0x86), wxColour(0xFF, 0x3E, 0x0E), wxColour(0xFF, 0xD3, 0x20), @@ -49,10 +49,12 @@ AccountPanel::AccountPanel(KissCount* kiss, wxUI *parent) : wxPanel(&(*parent)), _accounts[i] = accountIt->name; _categories = new wxString[user->GetCategoriesNumber()] ; - for(i=0, it = user->_preferences._categories.begin(); it != user->_preferences._categories.end(); it++, i++) + for(i=0, categoryIt = user->_preferences._categories.begin(); + categoryIt != user->_preferences._categories.end(); + categoryIt++, i++) { - _categories[i] = it->second ; - _categoriesIndexes[it->second] = i; + _categories[i] = categoryIt->name ; + _categoriesIndexes[categoryIt->name] = i; } _dataset = new CategorySimpleDataset(_categories, user->GetCategoriesNumber()); @@ -232,7 +234,7 @@ void AccountPanel::ShowMonth(int year, int month) int curLine = 0; User* user = _kiss->GetUser(); DEFAULT_FONT(font); - std::map::iterator categoryIt; + std::vector::iterator categoryIt; //wxGridCellChoiceEditor* categoryEditor, *accountEditor; int i; wxBitmap bitmap(_(DELETE_ICON)); @@ -259,30 +261,9 @@ void AccountPanel::ShowMonth(int year, int month) _grid->SetCellAlignment(0, i, wxALIGN_CENTRE, wxALIGN_CENTRE); } _grid->SetCellRenderer(0, DELETE, new wxGridCellBitmapRenderer(bitmap)); - // SetCellBackgroundColour (int row, int col, const wxColour &colour); - // SetCellFont (int row, int col, const wxFont &font); - // SetCellValue (int row, int col, const wxString &s); - // GetColSize (int col) const ; - // SetColSize (int col, int width); - // AppendRows (int numRows=1, bool updateLabels=true); - // InsertRows (int pos=0, int numRows=1, bool updateLabels=true); - // SetReadOnly(row, col, bool) it = _curOperations->begin(); - /* -struct operation { - wxString id; - unsigned int day; - unsigned int month; - unsigned int year; - int amount; - wxString description; - wxString category; - bool fix_cost; -enum {DESCRIPTION, DATE, DEBIT, CREDIT, CATEGORY, ACCOUNT, DELETE, NUMBER_COLS_OPS}; -} ; - */ for (;it->fix_cost && it != _curOperations->end(); it++) InsertOperation(user, &(*it), ++curLine, true); @@ -321,7 +302,6 @@ void AccountPanel::InsertOperation(User* user, operation* op, int line, bool fix _grid->SetCellEditor(line, CREDIT, new wxGridCellFloatEditor(-1, 2)); wxGridCellChoiceEditor* accountEditor = new wxGridCellChoiceEditor(user->GetAccountsNumber(), _accounts, false); _grid->SetCellEditor(line, ACCOUNT, accountEditor); - // Remove Fix category wxGridCellChoiceEditor* categoryEditor = new wxGridCellChoiceEditor(user->GetCategoriesNumber()-1, _categories+1, false); _grid->SetCellEditor(line, CATEGORY, categoryEditor); @@ -349,7 +329,7 @@ void AccountPanel::InsertOperation(User* user, operation* op, int line, bool fix _grid->SetCellRenderer(line, CHECKED, new wxGridCellBoolRenderer ()); _grid->SetCellEditor(line, CHECKED, new wxGridCellBoolEditor ()); - color = user->_preferences._colors[user->GetCategoryName(op->category)]; + color = user->GetCategory(op->category).color; if (op->checked) { @@ -587,7 +567,7 @@ void AccountPanel::OnOperationModified(wxGridEvent& event) if (col == CHECKED || col == CATEGORY) { - color = user->_preferences._colors[user->GetCategoryName(new_op.category)]; + color = user->GetCategory(new_op.category).color; if (new_op.checked) { diff --git a/view/PreferencesPanel.cpp b/view/PreferencesPanel.cpp index eef4435..1073bd8 100644 --- a/view/PreferencesPanel.cpp +++ b/view/PreferencesPanel.cpp @@ -130,7 +130,7 @@ void PreferencesPanel::InitAccounts(User* user) void PreferencesPanel::InitCategories(User* user) { - std::map::iterator it; + std::vector::iterator it; int curLine = 0; DEFAULT_FONT(font); @@ -147,8 +147,8 @@ void PreferencesPanel::InitCategories(User* user) { _categoriesGrid->AppendRows(); - _categoriesGrid->SetCellValue(curLine, CATEGORY_NAME, it->second); - SET_ROW_COLOR(curLine, user->_preferences._colors[it->second]); + _categoriesGrid->SetCellValue(curLine, CATEGORY_NAME, it->name); + SET_ROW_COLOR(curLine, it->color); if (curLine) { _categoriesGrid->SetCellRenderer(curLine, CATEGORY_DELETE, new wxGridCellBoolRenderer ()); @@ -158,8 +158,6 @@ void PreferencesPanel::InitCategories(User* user) _categoriesGrid->SetCellAlignment(curLine, CATEGORY_COLOR, wxALIGN_CENTRE, wxALIGN_CENTRE); _categoriesGrid->SetCellAlignment(curLine, CATEGORY_FONT, wxALIGN_CENTRE, wxALIGN_CENTRE); _categoriesGrid->SetCellAlignment(curLine, CATEGORY_DELETE, wxALIGN_CENTRE, wxALIGN_CENTRE); - - _categoriesIndexes[curLine] = it->second; } _categoriesGrid->SetReadOnly(0, CATEGORY_DELETE, true); @@ -172,16 +170,6 @@ void PreferencesPanel::InitCategories(User* user) SET_ROW_COLOR(curLine, OWN_GREEN); } -/* -struct Account { - wxString id; - wxString name; - wxString number; - bool shared; - bool _default; -}; - - */ void PreferencesPanel::OnAccountModified(wxGridEvent& event) { int op_complete = 2; @@ -323,11 +311,12 @@ void PreferencesPanel::OnAccountModified(wxGridEvent& event) void PreferencesPanel::OnCategoryModified(wxGridEvent& event) { int op_complete = 1; - wxString value, categoryName ; + wxString value; User* user = _kiss->GetUser(); int row = event.GetRow(); int col = event.GetCol(); static bool inModification = false ; + struct category new_cat; if (inModification) return; @@ -336,32 +325,34 @@ void PreferencesPanel::OnCategoryModified(wxGridEvent& event) value = _categoriesGrid->GetCellValue(row, CATEGORY_NAME); if (value != _("")) { - categoryName = value; + new_cat.name = value; op_complete--; } + new_cat.color = _categoriesGrid->GetCellBackgroundColour(row, col); + // Categories modification if (user->GetCategoriesNumber() && row < user->GetCategoriesNumber()) { + new_cat.id = user->_preferences._categories[row].id; if (col == CATEGORY_DELETE) { - wxMessageDialog dialog(_wxUI, _("Are you sure want to delete : \n")+categoryName, _("KissCount"), wxYES_NO); + wxMessageDialog dialog(_wxUI, _("Are you sure want to delete : \n")+new_cat.name, _("KissCount"), wxYES_NO); if (dialog.ShowModal() == wxID_NO) { _categoriesGrid->SetCellValue(row, col, _("0")); } else { - _categoriesIndexes.erase(row); _categoriesGrid->DeleteRows(row, 1); - _kiss->DeleteCategory(categoryName); + _kiss->DeleteCategory(user->_preferences._categories[row]); } _wxUI->Layout(); inModification = false; return; } - _kiss->UpdateCategory(_categoriesIndexes[row], categoryName, _categoriesGrid->GetCellBackgroundColour(row, col)); + _kiss->UpdateCategory(user->_preferences._categories[row].name, new_cat); } // New category else @@ -372,8 +363,7 @@ void PreferencesPanel::OnCategoryModified(wxGridEvent& event) return ; } - _categoriesIndexes[row] = categoryName; - _kiss->AddCategory(categoryName, _categoriesGrid->GetCellBackgroundColour(row, col)); + _kiss->AddCategory(new_cat); _categoriesGrid->SetReadOnly(row, CATEGORY_COLOR, false); _categoriesGrid->SetReadOnly(row, CATEGORY_FONT, false); _categoriesGrid->SetReadOnly(row, CATEGORY_DELETE, false); @@ -390,6 +380,8 @@ void PreferencesPanel::OnCategoryModified(wxGridEvent& event) _categoriesGrid->SetReadOnly(row, CATEGORY_FONT, true); _categoriesGrid->SetReadOnly(row, CATEGORY_DELETE, true); SET_ROW_COLOR(row, OWN_GREEN); + + _wxUI->Layout(); } return; diff --git a/view/PreferencesPanel.h b/view/PreferencesPanel.h index 3570dac..bc9186c 100644 --- a/view/PreferencesPanel.h +++ b/view/PreferencesPanel.h @@ -30,7 +30,6 @@ private: wxUI* _wxUI; wxGrid* _accountsGrid; wxGrid* _categoriesGrid; - std::map _categoriesIndexes; void InitAccounts(User* user); void InitCategories(User* user);