From f73c80fc6a96f13070c6f7257e6bf4a90680490b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Gr=C3=A9gory=20Soutad=C3=A9?= Date: Mon, 24 May 2010 20:14:15 +0200 Subject: [PATCH] wip --- controller/KissCount.cpp | 3 +- controller/KissCount.h | 4 +-- init.sql | 23 ++++++------ model/Account.cpp | 7 ---- model/Account.h | 21 ----------- model/Database.cpp | 36 +++++++++++-------- model/Preferences.h | 2 +- model/User.cpp | 20 +++++++---- model/User.h | 17 +++++++-- model/model.h | 2 +- view/AccountPanel.cpp | 77 ++++++++++++++++++++++++++++++++-------- view/AccountPanel.h | 1 + 12 files changed, 129 insertions(+), 84 deletions(-) delete mode 100644 model/Account.cpp delete mode 100644 model/Account.h diff --git a/controller/KissCount.cpp b/controller/KissCount.cpp index c2dd26f..2e56dbc 100644 --- a/controller/KissCount.cpp +++ b/controller/KissCount.cpp @@ -43,7 +43,8 @@ void KissCount::LoadUser(wxString user) { if (_user) delete _user; _user = _db->LoadUser(user) ; - _wxUI->LoadUser(); + if (_user) + _wxUI->LoadUser(); } void KissCount::LoadYear(int year, bool force) diff --git a/controller/KissCount.h b/controller/KissCount.h index 549e9c6..5fcecb3 100644 --- a/controller/KissCount.h +++ b/controller/KissCount.h @@ -5,9 +5,7 @@ #include #include -#include -#include -#include +#include #include class wxUI; diff --git a/init.sql b/init.sql index 5d1a225..ceab0d5 100644 --- a/init.sql +++ b/init.sql @@ -4,18 +4,19 @@ CREATE TABLE account_amount(id INTEGER PRIMARY KEY, account REFERENCES account(i CREATE TABLE operation(id INTEGER PRIMARY KEY, user REFERENCES user(id), account REFERENCES account(id), year INTEGER, month INTEGER, day INTEGER, amount INTEGER, description VARCHAR(255), category REFERENCES preference(id), fix_cost CHAR(1)); CREATE TABLE preference(id INTEGER PRIMARY KEY, user REFERENCES user(id), type VARCHAR(255), name VARCHAR(255), value VARCHAR(255)); CREATE TABLE default_preference(id INTEGER PRIMARY KEY, type VARCHAR(255), name VARCHAR(255), value VARCHAR(255)); -INSERT INTO default_preference ("type", "value") VALUES ("category", "Fixe"); -INSERT INTO default_preference ("type", "value") VALUES ("category", "Courses"); -INSERT INTO default_preference ("type", "value") VALUES ("category", "Loisirs"); -INSERT INTO default_preference ("type", "value") VALUES ("category", "Frais de fonctionnement"); -INSERT INTO default_preference ("type", "value") VALUES ("category", "Exceptionnel"); +INSERT INTO default_preference ("type", "name", "value") VALUES ("category", "name", "Fixe"); +INSERT INTO default_preference ("type", "name", "value") VALUES ("category", "name", "Courses"); +INSERT INTO default_preference ("type", "name", "value") VALUES ("category", "name", "Loisirs"); +INSERT INTO default_preference ("type", "name", "value") VALUES ("category", "name", "Frais de fonctionnement"); +INSERT INTO default_preference ("type", "name", "value") VALUES ("category", "name", "Exceptionnel"); -- No password INSERT INTO user ("id", "name", "password") VALUES ("0", "Greg", "da39a3ee5e6b4b0d3255bfef95601890afd80709"); -INSERT INTO account ("id", "user", "name", "number", "shared", "default_account") VALUES ("0", "0", "Compte courant", "000" , "0", "1"); +INSERT INTO account ("id", "user", "name", "number", "shared", "default_account") VALUES ("0", "0", "Compte Courant", "000" , "0", "1"); +INSERT INTO account_amount("id", "account", "year", "month", "amount") VALUES("0", "0", "2010", "0", "1000"); INSERT INTO operation ("id", "user", "account", "year", "month", "day", "amount", "description", "category", "fix_cost") VALUES ("0", "0", "0", "2010", "0", "0", "1234", "Opé 1", "0", "1"); INSERT INTO operation ("id", "user", "account", "year", "month", "day", "amount", "description", "category", "fix_cost") VALUES ("1", "0", "0", "2010", "0", "1", "5678", "Opé 2", "1", "0"); -INSERT INTO preference ("type", "value") VALUES ("category", "Fixe"); -INSERT INTO preference ("type", "value") VALUES ("category", "Courses"); -INSERT INTO preference ("type", "value") VALUES ("category", "Loisirs"); -INSERT INTO preference ("type", "value") VALUES ("category", "Frais de fonctionnement"); -INSERT INTO preference ("type", "value") VALUES ("category", "Exceptionnel"); +INSERT INTO preference ("user", "type", "name", "value") VALUES ("0", "category", "name", "Fixe"); +INSERT INTO preference ("user", "type", "name", "value") VALUES ("0", "category", "name", "Courses"); +INSERT INTO preference ("user", "type", "name", "value") VALUES ("0", "category", "name", "Loisirs"); +INSERT INTO preference ("user", "type", "name", "value") VALUES ("0", "category", "name", "Frais de fonctionnement"); +INSERT INTO preference ("user", "type", "name", "value") VALUES ("0", "category", "name", "Exceptionnel"); diff --git a/model/Account.cpp b/model/Account.cpp deleted file mode 100644 index b235d8d..0000000 --- a/model/Account.cpp +++ /dev/null @@ -1,7 +0,0 @@ -#include "Account.h" - - -Account::~Account() -{ - -} diff --git a/model/Account.h b/model/Account.h deleted file mode 100644 index 149fada..0000000 --- a/model/Account.h +++ /dev/null @@ -1,21 +0,0 @@ -#ifndef ACCOUNT_H -#define ACCOUNT_H - -#include -#include -#include - -class Account -{ -public: - ~Account(); - - wxString _id; - wxString _name; - wxString _number; - int _amount; - bool _shared; - bool _default; -}; - -#endif diff --git a/model/Database.cpp b/model/Database.cpp index b364004..3633d7a 100644 --- a/model/Database.cpp +++ b/model/Database.cpp @@ -131,8 +131,8 @@ User* Database::LoadUser(wxString name) wxSQLite3ResultSet set; wxString req; User* user; - Account* account; - std::list::iterator it; + struct Account account; + std::map::iterator it; req = _("SELECT * FROM user WHERE name='") + name + _("'"); @@ -155,25 +155,23 @@ User* Database::LoadUser(wxString name) while (set.NextRow()) { - account = new Account(); - account->_id = set.GetAsString(_("id")); - account->_name = set.GetAsString(_("name")); - account->_number = set.GetAsString(_("number")); - account->_amount = set.GetInt(_("amount")); - account->_shared = set.GetBool(_("shared")); - account->_default = set.GetBool(_("default_account")); - user->_accounts.push_back(account); + account.id = set.GetAsString(_("id")); + account.name = set.GetAsString(_("name")); + account.number = set.GetAsString(_("number")); + account.shared = set.GetBool(_("shared")); + account._default = set.GetBool(_("default_account")); + user->_accounts[account.id] = account; } set.Finalize(); if (!user->_accounts.empty()) { it = user->_accounts.begin(); - req = _("SELECT DISTINCT year FROM operation WHERE account IN('") + (*it)->_id; + req = _("SELECT DISTINCT year FROM account_amount WHERE account IN('") + it->first; it++; for (;it != user->_accounts.end(); it++) { - req += _("', '") + (*it)->_id ; + req += _("', '") + it->first ; } req += _("') ORDER BY year ASC"); @@ -186,6 +184,14 @@ User* Database::LoadUser(wxString name) set.Finalize(); } + req = _("SELECT id, value FROM preference WHERE type='category' AND name='name' AND user='") + user->_id + _("' ORDER BY value ASC"); + EXECUTE_SQL_QUERY_WITH_CODE(req, set, NULL, {delete user;}, {delete user;}); + + while (set.NextRow()) + user->_preferences._categories[set.GetAsString(_("id"))] = set.GetAsString(_("value")); + + set.Finalize(); + return user; } @@ -193,17 +199,17 @@ void Database::LoadYear(User* user, int year) { wxSQLite3ResultSet set; wxString req; - std::list::iterator it; + std::map::iterator it; if (user->_operations[year] == NULL) user->_operations[year] = new std::map >(); it = user->_accounts.begin(); - req = _("SELECT * FROM operation WHERE account IN('") + (*it)->_id; + req = _("SELECT * FROM operation WHERE account IN('") + it->first; it++; for (;it != user->_accounts.end(); it++) { - req += _("', '") + (*it)->_id ; + req += _("', '") + it->first ; } req += _("') ORDER BY fix_cost DESC, year,month,day ASC"); diff --git a/model/Preferences.h b/model/Preferences.h index f5a7330..ba2070b 100644 --- a/model/Preferences.h +++ b/model/Preferences.h @@ -8,7 +8,7 @@ class Preferences { public: std::map _colors; - std::list _categories; + std::map _categories; }; #endif diff --git a/model/User.cpp b/model/User.cpp index 77f167a..b32615c 100644 --- a/model/User.cpp +++ b/model/User.cpp @@ -5,12 +5,6 @@ User::~User() { std::map >* >::iterator it; - while (!_accounts.empty()) - { - delete _accounts.front(); - _accounts.pop_front(); - } - for (it = _operations.begin(); it != _operations.end(); it++) { if (_operations[it->first]) @@ -19,3 +13,17 @@ User::~User() } } } + +wxString User::GetCategoryName(wxString catId) +{ + if (_preferences._categories.find(catId) == _preferences._categories.end()) + return _("Unknown") ; + return _preferences._categories[catId]; +} + +wxString User::GetAccountName(wxString accountId) +{ + if (_accounts.find(accountId) == _accounts.end()) + return _("Unknown") ; + return _accounts[accountId].name; +} diff --git a/model/User.h b/model/User.h index c45ebce..b803b7c 100644 --- a/model/User.h +++ b/model/User.h @@ -2,8 +2,7 @@ #define USER_H #include - -#include "Account.h" +#include #include "Preferences.h" struct operation { @@ -18,6 +17,15 @@ struct operation { wxString account; } ; +struct Account { + wxString id; + wxString name; + wxString number; + int amount; + bool shared; + bool _default; +}; + class User { public: @@ -26,9 +34,12 @@ public: wxString _id; wxString _name; wxString _password; - std::list _accounts; + std::map _accounts; std::map >* > _operations; Preferences _preferences; + + wxString GetCategoryName(wxString catId); + wxString GetAccountName(wxString accountId); }; #endif diff --git a/model/model.h b/model/model.h index e1ac579..43aae4a 100644 --- a/model/model.h +++ b/model/model.h @@ -2,7 +2,7 @@ #define MODEL_H #include "User.h" -#include "Account.h" #include "Preferences.h" +#include "Database.h" #endif diff --git a/view/AccountPanel.cpp b/view/AccountPanel.cpp index e28caf4..e4d0abd 100644 --- a/view/AccountPanel.cpp +++ b/view/AccountPanel.cpp @@ -6,17 +6,42 @@ AccountPanel::AccountPanel(KissCount* kiss, wxUI *parent) : wxPanel(&(*parent)), { wxBoxSizer *hbox = new wxBoxSizer(wxHORIZONTAL); wxChartPanel* chart ; - + double data[] = {1.0, 2.0, 3.0} ; + wxString cats[] = {_("cat1"), _("cat2"), _("cat3")}; + wxColour colours[] = {OWN_CYAN, OWN_YELLOW, OWN_GREEN} ; + double value; SetSizer(hbox); _pie = new PiePlot(); + CategorySimpleDataset* set = new CategorySimpleDataset(cats, 3); + value = 1.0; + //set->AddSerie(new CategorySerie(_("cat1"), &value, 1)) ; + set->AddSerie(_("série 1"), data, 3); + // value = 2.0; + // set->AddSerie(new CategorySerie(_("cat2"), &value, 1)) ; + // value = 3.0; + // set->AddSerie(new CategorySerie(_("cat3"), &value, 1)) ; + // set->SetRenderer(new XYLineRenderer()); + // set serie names to be displayed on legend + //dataset->SetSerieName(0, wxT("Serie 0")); + //dataset->SetSerieName(1, wxT("Serie 1")); + + _pie->SetDataset(set); + _pie->SetColorScheme(new ColorScheme(colours, 3)); + + // set legend + _pie->SetLegend(new Legend(wxCENTER, wxTOP)); + _grid = new GridAccount(this, -1); _grid->CreateGrid(1, NUMBER_COLS); _grid->SetColLabelSize(0); _grid->SetRowLabelSize(0); - chart = new wxChartPanel(this); chart->SetChart(new Chart(_pie, _("Cost repartition"))); + chart->Fit(); + chart->Layout(); + chart->SetMinSize(// chart->GetSize() + wxSize(200,200)); hbox->Add(&_tree, 0); hbox->Add(_grid, 0); hbox->Add(chart, 0); @@ -30,7 +55,7 @@ AccountPanel::AccountPanel(KissCount* kiss, wxUI *parent) : wxPanel(&(*parent)), void AccountPanel::ChangeUser() { User* user = _kiss->GetUser(); - int curYear; + int curYear = -1; std::map >* >::iterator it; wxDateTime curDate; @@ -42,7 +67,8 @@ void AccountPanel::ChangeUser() _tree.AddRoot(wxString::Format(wxT("%d"), it->first)); } - LoadYear(curYear); + if (curYear != -1) + LoadYear(curYear); } void AccountPanel::LoadYear(int year) @@ -99,9 +125,10 @@ void AccountPanel::ShowMonth(int year, int month) int curLine = 0; User* user = _kiss->GetUser(); wxFont font(DEFAULT_FONT_SIZE, wxFONTFAMILY_DEFAULT, wxFONTSTYLE_NORMAL, wxFONTWEIGHT_NORMAL, false, DEFAULT_FONT); - std::list::iterator categoryIt; - wxGridCellChoiceEditor* categoryEditor; - wxString* categories; + std::map::iterator categoryIt; + std::map::iterator accountIt; + wxGridCellChoiceEditor* categoryEditor, *accountEditor; + wxString* categories, *accounts; int i; _wxUI->SetTitle(user->_name + _(" - ") + months[month]); @@ -132,15 +159,21 @@ void AccountPanel::ShowMonth(int year, int month) // SetReadOnly(row, col, bool) categories = new wxString[user->_preferences._categories.size()+1]; - categories[0] = _("unknown"); + categories[0] = _("Unknown"); for (i=1, categoryIt = user->_preferences._categories.begin(); categoryIt != user->_preferences._categories.end(); - categoryIt++, - i++) - categories[i] = *categoryIt; - - categoryEditor = new wxGridCellChoiceEditor(user->_preferences._categories.size()+1, categories, false); + categoryIt++, i++) + categories[i] = user->_preferences._categories[categoryIt->first]; + + accounts = new wxString[user->_accounts.size()+1]; + accounts[0] = _("Unknown"); + for (i=1, + accountIt = user->_accounts.begin(); + accountIt != user->_accounts.end(); + accountIt++, i++) + accounts[i] = user->_accounts[accountIt->first].name; + it = operations.begin(); @@ -174,7 +207,10 @@ enum {DESCRIPTION, DATE, DEBIT, CREDIT, CATEGORY, ACCOUNT, VIEW, NUMBER_COLS}; _grid->SetCellEditor(curLine, CREDIT, new wxGridCellNumberEditor()); categoryEditor = new wxGridCellChoiceEditor(user->_preferences._categories.size()+1, categories, false); _grid->SetCellEditor(curLine, CATEGORY, categoryEditor); - _grid->SetCellValue(curLine, ACCOUNT, it->account); + _grid->SetCellValue(curLine, CATEGORY, user->GetCategoryName(it->category)); + accountEditor = new wxGridCellChoiceEditor(user->_accounts.size()+1, accounts, false); + _grid->SetCellEditor(curLine, ACCOUNT, accountEditor); + _grid->SetCellValue(curLine, ACCOUNT, user->GetAccountName(it->account)); it++; } @@ -185,6 +221,8 @@ enum {DESCRIPTION, DATE, DEBIT, CREDIT, CATEGORY, ACCOUNT, VIEW, NUMBER_COLS}; _grid->SetCellEditor(curLine, CATEGORY, categoryEditor); _grid->SetCellEditor(curLine, DEBIT, new wxGridCellNumberEditor()); _grid->SetCellEditor(curLine, CREDIT, new wxGridCellNumberEditor()); + accountEditor = new wxGridCellChoiceEditor(user->_accounts.size()+1, accounts, false); + _grid->SetCellEditor(curLine, ACCOUNT, accountEditor); _grid->_fixCosts = ++fixCosts; @@ -204,7 +242,10 @@ enum {DESCRIPTION, DATE, DEBIT, CREDIT, CATEGORY, ACCOUNT, VIEW, NUMBER_COLS}; _grid->SetCellValue(curLine, CATEGORY, it->category); categoryEditor = new wxGridCellChoiceEditor(user->_preferences._categories.size()+1, categories, false); _grid->SetCellEditor(curLine, CATEGORY, categoryEditor); - _grid->SetCellValue(curLine, ACCOUNT, it->account); + _grid->SetCellValue(curLine, CATEGORY, user->GetCategoryName(it->category)); + accountEditor = new wxGridCellChoiceEditor(user->_accounts.size()+1, accounts, false); + _grid->SetCellEditor(curLine, ACCOUNT, accountEditor); + _grid->SetCellValue(curLine, ACCOUNT, user->GetAccountName(it->account)); it++; } @@ -215,8 +256,14 @@ enum {DESCRIPTION, DATE, DEBIT, CREDIT, CATEGORY, ACCOUNT, VIEW, NUMBER_COLS}; _grid->SetCellEditor(curLine, CATEGORY, categoryEditor); _grid->SetCellEditor(curLine, DEBIT, new wxGridCellNumberEditor()); _grid->SetCellEditor(curLine, CREDIT, new wxGridCellNumberEditor()); + accountEditor = new wxGridCellChoiceEditor(user->_accounts.size()+1, accounts, false); + _grid->SetCellEditor(curLine, ACCOUNT, accountEditor); delete[] categories; + delete[] accounts; + + _grid->AutoSizeColumn(CATEGORY, false); + _grid->AutoSizeColumn(ACCOUNT, false); Fit(); SetMinSize(GetSize()); diff --git a/view/AccountPanel.h b/view/AccountPanel.h index d87f1a1..504a60c 100644 --- a/view/AccountPanel.h +++ b/view/AccountPanel.h @@ -20,6 +20,7 @@ enum {DESCRIPTION, DATE, DEBIT, CREDIT, CATEGORY, ACCOUNT, VIEW, NUMBER_COLS}; #include "wxUI.h" #include #include "GridAccount.h" +#include class wxUI; class KissCount;