PreferencesPanel in work

This commit is contained in:
Grégory Soutadé 2010-06-21 10:53:43 +02:00
parent 36c01b0c56
commit 083054b376
15 changed files with 346 additions and 32 deletions

View File

@ -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);
}

View File

@ -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;

0
model/Account.cpp Normal file → Executable file
View File

0
model/Account.h Normal file → Executable file
View File

View File

@ -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, );
}

View File

@ -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;

0
model/Preferences.cpp Executable file → Normal file
View File

View File

@ -22,7 +22,6 @@ struct Account {
wxString id;
wxString name;
wxString number;
int amount;
bool shared;
bool _default;
};

View File

@ -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;
}
}

View File

@ -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 <controller/KissCount.h>

0
view/CalendarEditor.cpp Executable file → Normal file
View File

0
view/CalendarEditor.h Executable file → Normal file
View File

View File

@ -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) {

View File

@ -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<wxString, Account>::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; i<NUMBER_COLS_CATEGORY; i++) \
{\
_categoriesGrid->SetCellBackgroundColour(row, i, color);\
}
void PreferencesPanel::InitCategories(User* user)
{
std::map<wxString, wxString>::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; i<user->GetAccountsNumber(); 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, _(""));
}

View File

@ -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<int, wxString> _categoriesIndexes, _accountsIndexes;
void LoadAccounts(User* user);
void InitAccounts(User* user);
void InitCategories(User* user);
DECLARE_EVENT_TABLE();
};
#endif