From 083054b3769d2c1221c5c12aa40eed2fe9b5a3e4 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Gr=C3=A9gory=20Soutad=C3=A9?= Date: Mon, 21 Jun 2010 10:53:43 +0200 Subject: [PATCH] PreferencesPanel in work --- controller/KissCount.cpp | 16 +++ controller/KissCount.h | 3 + model/Account.cpp | 0 model/Account.h | 0 model/Database.cpp | 52 +++++++- model/Database.h | 6 +- model/Preferences.cpp | 0 model/User.h | 1 - view/AccountPanel.cpp | 17 ++- view/AccountPanel.h | 6 - view/CalendarEditor.cpp | 0 view/CalendarEditor.h | 0 view/GridAccount.h | 7 +- view/PreferencesPanel.cpp | 259 ++++++++++++++++++++++++++++++++++++-- view/PreferencesPanel.h | 11 +- 15 files changed, 346 insertions(+), 32 deletions(-) mode change 100644 => 100755 model/Account.cpp mode change 100644 => 100755 model/Account.h mode change 100755 => 100644 model/Preferences.cpp mode change 100755 => 100644 view/CalendarEditor.cpp mode change 100755 => 100644 view/CalendarEditor.h diff --git a/controller/KissCount.cpp b/controller/KissCount.cpp index e0f9c9c..10109cc 100644 --- a/controller/KissCount.cpp +++ b/controller/KissCount.cpp @@ -88,3 +88,19 @@ void KissCount::SetAccountAmount(int month, int year, wxString accountId, double { _db->SetAccountAmount(month, year, accountId, amount); } + +void KissCount::InsertAccount(struct Account ac) +{ + _db->InsertAccount(_user, ac); +} + +void KissCount::UpdateAccount(struct Account ac) +{ + _db->UpdateAccount(ac); +} + +void KissCount::DeleteAccount(struct Account ac) +{ + _db->DeleteAccount(ac); + _user->_accounts.erase(ac.id); +} diff --git a/controller/KissCount.h b/controller/KissCount.h index 37f0a3a..b765085 100644 --- a/controller/KissCount.h +++ b/controller/KissCount.h @@ -25,6 +25,9 @@ class KissCount void AddOperation(struct operation op); void DeleteOperation(struct operation op); void SetAccountAmount(int month, int year, wxString accountId, double value); + void InsertAccount(struct Account ac); + void UpdateAccount(struct Account ac); + void DeleteAccount(struct Account ac); private: wxUI* _wxUI; diff --git a/model/Account.cpp b/model/Account.cpp old mode 100644 new mode 100755 diff --git a/model/Account.h b/model/Account.h old mode 100644 new mode 100755 diff --git a/model/Database.cpp b/model/Database.cpp index 8151210..3432727 100644 --- a/model/Database.cpp +++ b/model/Database.cpp @@ -274,7 +274,10 @@ double Database::GetAccountAmount(wxString id, int month, int year) if (set.NextRow()) res = set.GetDouble(_("amount")); else - res = 0.0; + { + SetAccountAmount(month, year, id, 0.0); + res = 0.0; + } set.Finalize(); @@ -343,3 +346,50 @@ void Database::SetAccountAmount(int month, int year, wxString accountId, double EXECUTE_SQL_UPDATE(req, ); } + +void Database::InsertAccount(User* user, struct Account ac) +{ + wxString req; + req = _("INSERT INTO account ('user', 'name', 'number', 'shared', 'default_account') VALUES ('") ; + req += user->_id + _("'"); + req += _(", '") + ac.name + _("'"); + req += _(", '") + ac.number + _("'"); + if (ac.shared) + req += _(", '1'") ; + else + req += _(", '0'") ; + if (ac._default) + req += _(", '1'") ; + else + req += _(", '0'") ; + req += _(")"); + + EXECUTE_SQL_UPDATE(req, ); +} + +void Database::UpdateAccount(struct Account ac) +{ + wxString req; + req = _("UPDATE account SET ") ; + req += _("name='") + ac.name + _("'"); + req += _(", number='") + ac.number + _("'"); + if (ac.shared) + req += _(", shared='1'"); + else + req += _(", shared='0'"); + if (ac._default) + req += _(", default_account='1'"); + else + req += _(", default_account='0'"); + req += _(" WHERE id='") + ac.id + _("'"); + + EXECUTE_SQL_UPDATE(req, ); +} + +void Database::DeleteAccount(struct Account ac) +{ + wxString req; + req = _("DELETE FROM account WHERE id='") + ac.id + _("'"); + + EXECUTE_SQL_UPDATE(req, ); +} diff --git a/model/Database.h b/model/Database.h index 56d08b6..186657d 100644 --- a/model/Database.h +++ b/model/Database.h @@ -23,13 +23,17 @@ class Database User* LoadUser(wxString name); void LoadYear(User* user, int year); - double GetAccountAmount(wxString id, int month, int year); void UpdateOperation(struct operation op); void AddOperation(User* user, struct operation op); void DeleteOperation(struct operation op); + double GetAccountAmount(wxString id, int month, int year); void SetAccountAmount(int month, int year, wxString accountId, double amount); + void InsertAccount(User* user, struct Account ac); + void UpdateAccount(struct Account ac); + void DeleteAccount(struct Account ac); + private: wxSQLite3Database _db; diff --git a/model/Preferences.cpp b/model/Preferences.cpp old mode 100755 new mode 100644 diff --git a/model/User.h b/model/User.h index 1d09709..ac88a7c 100644 --- a/model/User.h +++ b/model/User.h @@ -22,7 +22,6 @@ struct Account { wxString id; wxString name; wxString number; - int amount; bool shared; bool _default; }; diff --git a/view/AccountPanel.cpp b/view/AccountPanel.cpp index 12706b9..0758af5 100644 --- a/view/AccountPanel.cpp +++ b/view/AccountPanel.cpp @@ -1,5 +1,10 @@ #include "AccountPanel.h" +enum {DESCRIPTION, DATE, DEBIT, CREDIT, CATEGORY, ACCOUNT, DELETE, CHECKED, NUMBER_COLS_OPS}; +enum {ACCOUNT_NUMBER, ACCOUNT_NAME, ACCOUNT_INIT, ACCOUNT_CUR, ACCOUNT_FINAL, NUMBER_COLS_ACCOUNTS}; +enum {CUR_CREDIT, CUR_DEBIT, TOTAL_CREDIT, TOTAL_DEBIT, REMAINS, STATS_ROW, CATS_STATS}; +enum {CALENDAR_TREE_ID=10, OPS_GRID_ID, ACCOUNTS_GRID_ID}; + static wxString colsName[] = {_("Description"), _("Date"), _("Debit"), _("Credit"), _("Category"), _("Account"), _(""), _("")}; BEGIN_EVENT_TABLE(AccountPanel, wxPanel) @@ -363,9 +368,11 @@ void AccountPanel::InsertOperation(User* user, operation* op, int line, bool fix if (fix) SET_ROW_COLOR(line, OWN_YELLOW) else - SET_ROW_COLOR(line, OWN_GREEN) + SET_ROW_COLOR(line, OWN_GREEN); + _grid->SetReadOnly(line, CHECKED, true); + _grid->SetReadOnly(line, DELETE, true); } - + _grid->SetCellAlignment(line, DEBIT, wxALIGN_RIGHT, wxALIGN_CENTRE); _grid->SetCellAlignment(line, CREDIT, wxALIGN_RIGHT, wxALIGN_CENTRE); _grid->SetCellAlignment(line, DELETE, wxALIGN_CENTRE, wxALIGN_CENTRE); @@ -390,8 +397,9 @@ void AccountPanel::InsertOperation(User* user, operation* op, int line, bool fix curWeek = week; } } - } + + _wxUI->Layout(); } void AccountPanel::InitAccountsGrid(User* user, int month, int year) @@ -574,7 +582,7 @@ void AccountPanel::OnOperationModified(wxGridEvent& event) } value = _grid->GetCellValue(event.GetRow(), CHECKED); - if (value != _("")) + if (value != _("") && value != _("0")) new_op.checked = true; else new_op.checked = false; @@ -601,6 +609,7 @@ void AccountPanel::OnOperationModified(wxGridEvent& event) if (dialog.ShowModal() == wxID_NO) { _grid->SetCellValue(event.GetRow(), event.GetCol(), _("0")); + inModification = false; return; } } diff --git a/view/AccountPanel.h b/view/AccountPanel.h index 320e32e..1b506eb 100644 --- a/view/AccountPanel.h +++ b/view/AccountPanel.h @@ -17,12 +17,6 @@ #define DEFAULT_FONT_SIZE 12 #define DEFAULT_FONT(font_name) wxFont font_name(DEFAULT_FONT_SIZE, wxFONTFAMILY_DEFAULT, wxFONTSTYLE_NORMAL, wxFONTWEIGHT_NORMAL, false, DEFAULT_FONT_NAME); -enum {DESCRIPTION, DATE, DEBIT, CREDIT, CATEGORY, ACCOUNT, DELETE, CHECKED, NUMBER_COLS_OPS}; -enum {ACCOUNT_NUMBER, ACCOUNT_NAME, ACCOUNT_INIT, ACCOUNT_CUR, ACCOUNT_FINAL, NUMBER_COLS_ACCOUNTS}; -enum {CUR_CREDIT, CUR_DEBIT, TOTAL_CREDIT, TOTAL_DEBIT, REMAINS, STATS_ROW, CATS_STATS}; - -enum {CALENDAR_TREE_ID=10, OPS_GRID_ID, ACCOUNTS_GRID_ID}; - #define DELETE_ICON "ressources/process-stop.png" #include diff --git a/view/CalendarEditor.cpp b/view/CalendarEditor.cpp old mode 100755 new mode 100644 diff --git a/view/CalendarEditor.h b/view/CalendarEditor.h old mode 100755 new mode 100644 diff --git a/view/GridAccount.h b/view/GridAccount.h index 9a483f9..e1e89d2 100644 --- a/view/GridAccount.h +++ b/view/GridAccount.h @@ -17,11 +17,8 @@ public: row == _week2 || row == _week3) return wxPen(*wxBLACK, 1, wxSOLID); - - if (row <= _fixCosts) - return wxPen(OWN_YELLOW, 1, wxSOLID); - - return wxPen(OWN_GREEN, 1, wxSOLID); + + return GetCellBackgroundColour(row, 0); } void SetWeek(int week, int line) { diff --git a/view/PreferencesPanel.cpp b/view/PreferencesPanel.cpp index bb2360f..53b126a 100644 --- a/view/PreferencesPanel.cpp +++ b/view/PreferencesPanel.cpp @@ -1,5 +1,14 @@ #include "PreferencesPanel.h" +enum {ACCOUNT_NAME, ACCOUNT_NUMBER, ACCOUNT_SHARED, ACCOUNT_DEFAULT, ACCOUNT_DELETE, NUMBER_COLS_ACCOUNT}; +enum {CATEGORY_NAME, CATEGORY_COLOR, CATEGORY_FONT, CATEGORY_DELETE, NUMBER_COLS_CATEGORY}; + +enum {CATEGORIES_GRID_ID=20, ACCOUNTS_GRID_ID}; + +BEGIN_EVENT_TABLE(PreferencesPanel, wxPanel) + EVT_GRID_CMD_CELL_CHANGE(CATEGORIES_GRID_ID, PreferencesPanel::OnCategoryModified) + EVT_GRID_CMD_CELL_CHANGE(ACCOUNTS_GRID_ID, PreferencesPanel::OnAccountModified) +END_EVENT_TABLE() PreferencesPanel::PreferencesPanel(KissCount* kiss, wxUI *parent) : wxPanel(&(*parent)), _kiss(kiss), _wxUI(parent) { @@ -43,11 +52,11 @@ PreferencesPanel::PreferencesPanel(KissCount* kiss, wxUI *parent) : wxPanel(&(*p // Account staticBoxSizer = new wxStaticBoxSizer (staticAccount, wxVERTICAL); - _accountGrid = new wxGrid(this, -1); + _accountsGrid = new wxGrid(this, ACCOUNTS_GRID_ID); - LoadAccounts(user); + InitAccounts(user); - staticBoxSizer->Add(_accountGrid); + staticBoxSizer->Add(_accountsGrid); vbox->Add(staticBoxSizer); vbox->Add(-1, 20); @@ -55,10 +64,12 @@ PreferencesPanel::PreferencesPanel(KissCount* kiss, wxUI *parent) : wxPanel(&(*p // Categories staticBoxSizer = new wxStaticBoxSizer (staticCategories, wxVERTICAL); - _categoriesGrid = new wxGrid(this, -1); + _categoriesGrid = new wxGrid(this, CATEGORIES_GRID_ID); staticBoxSizer->Add(_categoriesGrid); + InitCategories(user); + vbox->Add(staticBoxSizer); Fit(); @@ -67,13 +78,237 @@ PreferencesPanel::PreferencesPanel(KissCount* kiss, wxUI *parent) : wxPanel(&(*p Hide(); } -void PreferencesPanel::LoadAccounts(User* user) +void PreferencesPanel::InitAccounts(User* user) +{ + std::map::iterator it; + int curLine = 0; + Account account ; + DEFAULT_FONT(font); + + _accountsGrid->CreateGrid(0, NUMBER_COLS_ACCOUNT); + _accountsGrid->SetRowLabelSize(0); + _accountsGrid->SetColLabelValue(ACCOUNT_NAME, _("Name")); + _accountsGrid->SetColLabelValue(ACCOUNT_NUMBER, _("Number")); + _accountsGrid->SetColLabelValue(ACCOUNT_SHARED, _("Shared")); + _accountsGrid->SetColLabelValue(ACCOUNT_DEFAULT, _("Default")); + _accountsGrid->SetColLabelValue(ACCOUNT_DELETE, _("Delete")); + _accountsGrid->SetDefaultCellFont(font); + + for (it = user->_accounts.begin(); it != user->_accounts.end(); it++, curLine++) + { + _accountsGrid->AppendRows(); + account = user->_accounts[it->first]; + + _accountsGrid->SetCellValue(curLine, ACCOUNT_NAME, account.name); + _accountsGrid->SetCellValue(curLine, ACCOUNT_NUMBER, account.number); + + _accountsGrid->SetCellRenderer(curLine, ACCOUNT_SHARED, new wxGridCellBoolRenderer ()); + _accountsGrid->SetCellEditor(curLine, ACCOUNT_SHARED, new wxGridCellBoolEditor ()); + _accountsGrid->SetCellRenderer(curLine, ACCOUNT_DEFAULT, new wxGridCellBoolRenderer ()); + _accountsGrid->SetCellEditor(curLine, ACCOUNT_DEFAULT, new wxGridCellBoolEditor ()); + _accountsGrid->SetCellRenderer(curLine, ACCOUNT_DELETE, new wxGridCellBoolRenderer ()); + _accountsGrid->SetCellEditor(curLine, ACCOUNT_DELETE, new wxGridCellBoolEditor ()); + _accountsGrid->SetCellValue(curLine, ACCOUNT_SHARED, (account.shared)?_("1"):_("0")); + _accountsGrid->SetCellValue(curLine, ACCOUNT_DEFAULT, (account._default)?_("1"):_("0")); + + _accountsGrid->SetCellAlignment(curLine, ACCOUNT_SHARED, wxALIGN_CENTRE, wxALIGN_CENTRE); + _accountsGrid->SetCellAlignment(curLine, ACCOUNT_DEFAULT, wxALIGN_CENTRE, wxALIGN_CENTRE); + _accountsGrid->SetCellAlignment(curLine, ACCOUNT_DELETE, wxALIGN_CENTRE, wxALIGN_CENTRE); + _accountsIndexes[curLine] = account.id; + } + + _accountsGrid->AutoSizeColumns(true); + _accountsGrid->AppendRows(); + + _accountsGrid->SetReadOnly(curLine, ACCOUNT_SHARED, true); + _accountsGrid->SetReadOnly(curLine, ACCOUNT_DEFAULT, true); + _accountsGrid->SetReadOnly(curLine, ACCOUNT_DELETE, true); +} + +#define SET_ROW_COLOR(row, color) for(int i=0; iSetCellBackgroundColour(row, i, color);\ + } + +void PreferencesPanel::InitCategories(User* user) +{ + std::map::iterator it; + int curLine = 0; + DEFAULT_FONT(font); + + _categoriesGrid->CreateGrid(0, NUMBER_COLS_CATEGORY); + _categoriesGrid->SetRowLabelSize(0); + _categoriesGrid->SetDefaultCellFont(font); + + _categoriesGrid->SetColLabelValue(CATEGORY_NAME, _("Name")); + _categoriesGrid->SetColLabelValue(CATEGORY_COLOR, _("Color")); + _categoriesGrid->SetColLabelValue(CATEGORY_FONT, _("Font")); + _categoriesGrid->SetColLabelValue(CATEGORY_DELETE, _("Delete")); + + for (it=user->_preferences._categories.begin(); it!=user->_preferences._categories.end(); it++, curLine++) + { + _categoriesGrid->AppendRows(); + + _categoriesGrid->SetCellValue(curLine, CATEGORY_NAME, it->second); + SET_ROW_COLOR(curLine, user->_preferences._colors[it->second]); + if (curLine) + { + _categoriesGrid->SetCellRenderer(curLine, CATEGORY_DELETE, new wxGridCellBoolRenderer ()); + _categoriesGrid->SetCellEditor(curLine, CATEGORY_DELETE, new wxGridCellBoolEditor ()); + } + + _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); + } + + _categoriesGrid->SetReadOnly(0, CATEGORY_DELETE, true); + _categoriesGrid->AutoSizeColumns(true); + _categoriesGrid->AppendRows(); + 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; + wxString value ; + struct Account new_account, account; + User* user = _kiss->GetUser(); + int row = event.GetRow(); + int col = event.GetCol(); + static bool inModification = false ; + int i; + + if (inModification) return; + + inModification = true; + + value = _accountsGrid->GetCellValue(row, ACCOUNT_NAME); + if (value != _("")) + { + new_account.name = value; + op_complete--; + } + + value = _accountsGrid->GetCellValue(row, ACCOUNT_NUMBER); + if (value != _("")) + { + new_account.number = value; + op_complete--; + } + + value = _accountsGrid->GetCellValue(row, ACCOUNT_SHARED); + if (value != _("") && value != _("0")) + new_account.shared = true; + else + new_account.shared = false; + + value = _accountsGrid->GetCellValue(row, ACCOUNT_DEFAULT); + if (value != _("") && value != _("0")) + new_account._default = true; + else + new_account._default = false; + + if (col == ACCOUNT_DEFAULT) + { + new_account.id = _accountsIndexes[row]; + + for (i=0; iGetAccountsNumber(); i++) + { + if (i != col) + { + account = user->_accounts[_accountsIndexes[i]]; + if (account._default) + { + account._default = false; + _kiss->UpdateAccount(account); + user->_accounts[_accountsIndexes[i]] = account; + _accountsGrid->SetCellValue(i, ACCOUNT_DEFAULT, _("")); + break; + } + } + + _kiss->UpdateAccount(new_account); + } + } + + // Account modification + if (user->GetAccountsNumber() && row < user->GetAccountsNumber()) + { + new_account.id = _accountsIndexes[row]; + + if (col == ACCOUNT_DELETE) + { + wxMessageDialog dialog(_wxUI, _("Are you sure want to delete : \n")+new_account.name, _("KissCount"), wxYES_NO); + if (dialog.ShowModal() == wxID_NO) + { + _accountsGrid->SetCellValue(row, col, _("0")); + } + else + { + _accountsGrid->DeleteRows(row, 1); + _kiss->DeleteAccount(new_account); + } + + _wxUI->Layout(); + inModification = false; + return; + } + _kiss->UpdateAccount(new_account); + } + // New account + else + { + if (op_complete) + { + inModification = false; + return ; + } + + _accountsGrid->SetCellRenderer(row, ACCOUNT_SHARED, new wxGridCellBoolRenderer ()); + _accountsGrid->SetCellEditor(row, ACCOUNT_SHARED, new wxGridCellBoolEditor ()); + _accountsGrid->SetCellRenderer(row, ACCOUNT_DEFAULT, new wxGridCellBoolRenderer ()); + _accountsGrid->SetCellEditor(row, ACCOUNT_DEFAULT, new wxGridCellBoolEditor ()); + _accountsGrid->SetCellRenderer(row, ACCOUNT_DELETE, new wxGridCellBoolRenderer ()); + _accountsGrid->SetCellEditor(row, ACCOUNT_DELETE, new wxGridCellBoolEditor ()); + _accountsGrid->SetCellAlignment(row, ACCOUNT_SHARED, wxALIGN_CENTRE, wxALIGN_CENTRE); + _accountsGrid->SetCellAlignment(row, ACCOUNT_DEFAULT, wxALIGN_CENTRE, wxALIGN_CENTRE); + _accountsGrid->SetCellAlignment(row, ACCOUNT_DELETE, wxALIGN_CENTRE, wxALIGN_CENTRE); + + _accountsGrid->SetReadOnly(row, ACCOUNT_SHARED, false); + _accountsGrid->SetReadOnly(row, ACCOUNT_DEFAULT, false); + _accountsGrid->SetReadOnly(row, ACCOUNT_DELETE, false); + + if (!user->GetAccountsNumber()) + { + new_account._default = true; + _accountsGrid->SetCellValue(row, ACCOUNT_DEFAULT, _("1")); + } + + _accountsGrid->AutoSizeColumns(true); + _accountsGrid->AppendRows(); + + _accountsGrid->SetReadOnly(row+1, ACCOUNT_SHARED, true); + _accountsGrid->SetReadOnly(row+1, ACCOUNT_DEFAULT, true); + _accountsGrid->SetReadOnly(row+1, ACCOUNT_DELETE, true); + _wxUI->Layout(); + + _kiss->InsertAccount(new_account); + } + + inModification = false; +} + +void PreferencesPanel::OnCategoryModified(wxGridEvent& event) { - _accountGrid->CreateGrid(0, 5); - _accountGrid->SetRowLabelSize(0); - _accountGrid->SetColLabelValue(0, _("Name")); - _accountGrid->SetColLabelValue(1, _("Number")); - _accountGrid->SetColLabelValue(2, _("Shared")); - _accountGrid->SetColLabelValue(3, _("Default")); - _accountGrid->SetColLabelValue(4, _("")); } diff --git a/view/PreferencesPanel.h b/view/PreferencesPanel.h index 1d05ec6..6d8ad10 100644 --- a/view/PreferencesPanel.h +++ b/view/PreferencesPanel.h @@ -22,13 +22,20 @@ public: PreferencesPanel(KissCount* kiss, wxUI *parent); void ChangeUser(); + void OnAccountModified(wxGridEvent& event); + void OnCategoryModified(wxGridEvent& event); + private: KissCount* _kiss; wxUI* _wxUI; - wxGrid* _accountGrid; + wxGrid* _accountsGrid; wxGrid* _categoriesGrid; + std::map _categoriesIndexes, _accountsIndexes; - void LoadAccounts(User* user); + void InitAccounts(User* user); + void InitCategories(User* user); + + DECLARE_EVENT_TABLE(); }; #endif