KissCount/view/PreferencesPanel.cpp

397 lines
13 KiB
C++
Raw Normal View History

2010-05-17 18:03:21 +02:00
#include "PreferencesPanel.h"
2010-06-21 10:53:43 +02:00
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()
2010-05-17 18:03:21 +02:00
PreferencesPanel::PreferencesPanel(KissCount* kiss, wxUI *parent) : wxPanel(&(*parent)), _kiss(kiss), _wxUI(parent)
{
wxBoxSizer *vbox = new wxBoxSizer(wxVERTICAL);
//wxBoxSizer *hbox = new wxBoxSizer(wxHORIZONTAL);
wxStaticBox* staticUser, *staticAccount, *staticCategories;
User* user = _kiss->GetUser();
wxGridBagSizer *gridBagSizer;
wxStaticText* label;
wxButton* buttonChangeName, *buttonChangePassword;
wxStaticBoxSizer * staticBoxSizer;
wxTextCtrl* name;
SetSizer(vbox);
staticUser = new wxStaticBox(this, -1, _("User"));
staticAccount = new wxStaticBox(this, -1, _("Accounts"));
staticCategories = new wxStaticBox(this, -1, _("Categories"));
// User
staticBoxSizer = new wxStaticBoxSizer (staticUser, wxVERTICAL);
gridBagSizer = new wxGridBagSizer(10, 10);
staticBoxSizer->Add(gridBagSizer);
label = new wxStaticText(this, -1, _("Name"));
gridBagSizer->Add(label, wxGBPosition(0, 0));
name = new wxTextCtrl(this, -1, user->_name);
gridBagSizer->Add(name, wxGBPosition(0, 1));
buttonChangeName = new wxButton(this, -1, _("Change Name"));
buttonChangePassword = new wxButton(this, -1, _("Change Password"));
gridBagSizer->Add(buttonChangeName, wxGBPosition(1, 0));
gridBagSizer->Add(buttonChangePassword, wxGBPosition(1, 1));
vbox->Add(staticBoxSizer);
vbox->Add(-1, 20);
// Account
staticBoxSizer = new wxStaticBoxSizer (staticAccount, wxVERTICAL);
2010-06-21 10:53:43 +02:00
_accountsGrid = new wxGrid(this, ACCOUNTS_GRID_ID);
2010-05-17 18:03:21 +02:00
2010-06-21 10:53:43 +02:00
InitAccounts(user);
2010-05-17 18:03:21 +02:00
2010-06-21 10:53:43 +02:00
staticBoxSizer->Add(_accountsGrid);
2010-05-17 18:03:21 +02:00
vbox->Add(staticBoxSizer);
vbox->Add(-1, 20);
// Categories
staticBoxSizer = new wxStaticBoxSizer (staticCategories, wxVERTICAL);
2010-06-21 10:53:43 +02:00
_categoriesGrid = new wxGrid(this, CATEGORIES_GRID_ID);
2010-05-17 18:03:21 +02:00
staticBoxSizer->Add(_categoriesGrid);
2010-06-21 10:53:43 +02:00
InitCategories(user);
2010-05-17 18:03:21 +02:00
vbox->Add(staticBoxSizer);
Fit();
SetMinSize(GetSize());
Hide();
}
2010-06-21 10:53:43 +02:00
void PreferencesPanel::InitAccounts(User* user)
{
2010-06-21 13:02:02 +02:00
std::vector<Account>::iterator it;
2010-06-21 10:53:43 +02:00
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();
2010-06-21 13:02:02 +02:00
_accountsGrid->SetCellValue(curLine, ACCOUNT_NAME, it->name);
_accountsGrid->SetCellValue(curLine, ACCOUNT_NUMBER, it->number);
2010-06-21 10:53:43 +02:00
_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 ());
2010-06-21 13:02:02 +02:00
_accountsGrid->SetCellValue(curLine, ACCOUNT_SHARED, (it->shared)?_("1"):_("0"));
_accountsGrid->SetCellValue(curLine, ACCOUNT_DEFAULT, (it->_default)?_("1"):_("0"));
2010-06-21 10:53:43 +02:00
_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);
}
_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);
2010-06-22 11:35:21 +02:00
_categoriesIndexes[curLine] = it->second;
2010-06-21 10:53:43 +02:00
}
_categoriesGrid->SetReadOnly(0, CATEGORY_DELETE, true);
_categoriesGrid->AutoSizeColumns(true);
2010-06-22 11:35:21 +02:00
2010-06-21 10:53:43 +02:00
_categoriesGrid->AppendRows();
2010-06-22 11:35:21 +02:00
_categoriesGrid->SetReadOnly(curLine, CATEGORY_COLOR, true);
_categoriesGrid->SetReadOnly(curLine, CATEGORY_FONT, true);
_categoriesGrid->SetReadOnly(curLine, CATEGORY_DELETE, true);
2010-06-21 10:53:43 +02:00
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)
{
2010-06-21 13:02:02 +02:00
new_account.id = user->_accounts[row].id;
2010-06-21 10:53:43 +02:00
for (i=0; i<user->GetAccountsNumber(); i++)
{
2010-06-21 13:02:02 +02:00
if (i != row)
2010-06-21 10:53:43 +02:00
{
2010-06-21 13:02:02 +02:00
account = user->_accounts[i];
2010-06-21 10:53:43 +02:00
if (account._default)
{
account._default = false;
_kiss->UpdateAccount(account);
_accountsGrid->SetCellValue(i, ACCOUNT_DEFAULT, _(""));
break;
}
}
_kiss->UpdateAccount(new_account);
}
}
// Account modification
if (user->GetAccountsNumber() && row < user->GetAccountsNumber())
{
2010-06-21 13:02:02 +02:00
new_account.id = user->_accounts[row].id;
2010-06-21 10:53:43 +02:00
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 ;
}
2010-06-21 13:02:02 +02:00
if (user->GetAccountId(new_account.name) != _("0"))
{
wxMessageDialog dialog(_wxUI, _("Account ")+new_account.name+_(" already exists"), _("Error"), wxOK);
dialog.ShowModal();
inModification = false;
return ;
}
2010-06-21 10:53:43 +02:00
_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();
2010-06-21 13:02:02 +02:00
_kiss->AddAccount(new_account);
2010-06-21 10:53:43 +02:00
}
inModification = false;
}
void PreferencesPanel::OnCategoryModified(wxGridEvent& event)
2010-05-17 18:03:21 +02:00
{
2010-06-22 11:35:21 +02:00
int op_complete = 1;
wxString value, categoryName ;
User* user = _kiss->GetUser();
int row = event.GetRow();
int col = event.GetCol();
static bool inModification = false ;
if (inModification) return;
inModification = true;
value = _categoriesGrid->GetCellValue(row, CATEGORY_NAME);
if (value != _(""))
{
categoryName = value;
op_complete--;
}
// Categories modification
if (user->GetCategoriesNumber() && row < user->GetCategoriesNumber())
{
if (col == CATEGORY_DELETE)
{
wxMessageDialog dialog(_wxUI, _("Are you sure want to delete : \n")+categoryName, _("KissCount"), wxYES_NO);
if (dialog.ShowModal() == wxID_NO)
{
_categoriesGrid->SetCellValue(row, col, _("0"));
}
else
{
_categoriesIndexes.erase(row);
_categoriesGrid->DeleteRows(row, 1);
_kiss->DeleteCategory(categoryName);
}
_wxUI->Layout();
inModification = false;
return;
}
_kiss->UpdateCategory(_categoriesIndexes[row], categoryName, _categoriesGrid->GetCellBackgroundColour(row, col));
}
// New category
else
{
if (op_complete)
{
inModification = false;
return ;
}
_categoriesIndexes[row] = categoryName;
_kiss->AddCategory(categoryName, _categoriesGrid->GetCellBackgroundColour(row, col));
_categoriesGrid->SetReadOnly(row, CATEGORY_COLOR, false);
_categoriesGrid->SetReadOnly(row, CATEGORY_FONT, false);
_categoriesGrid->SetReadOnly(row, CATEGORY_DELETE, false);
_categoriesGrid->SetCellRenderer(row, CATEGORY_DELETE, new wxGridCellBoolRenderer ());
_categoriesGrid->SetCellEditor(row, CATEGORY_DELETE, new wxGridCellBoolEditor ());
_categoriesGrid->SetCellAlignment(row, CATEGORY_COLOR, wxALIGN_CENTRE, wxALIGN_CENTRE);
_categoriesGrid->SetCellAlignment(row, CATEGORY_FONT, wxALIGN_CENTRE, wxALIGN_CENTRE);
_categoriesGrid->SetCellAlignment(row, CATEGORY_DELETE, wxALIGN_CENTRE, wxALIGN_CENTRE);
row++;
_categoriesGrid->AppendRows();
_categoriesGrid->SetReadOnly(row, CATEGORY_COLOR, true);
_categoriesGrid->SetReadOnly(row, CATEGORY_FONT, true);
_categoriesGrid->SetReadOnly(row, CATEGORY_DELETE, true);
SET_ROW_COLOR(row, OWN_GREEN);
}
return;
2010-05-17 18:03:21 +02:00
}