/*
Copyright 2010-2012 Grégory Soutadé
This file is part of KissCount.
KissCount is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
KissCount is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with KissCount. If not, see .
*/
#include "PasswordDialog.hpp"
#include "PreferencesPanel.hpp"
#include "grid/StarDelegate.hpp"
enum {ACCOUNT_NAME, ACCOUNT_NUMBER, ACCOUNT_DEFAULT, ACCOUNT_VIRTUAL, ACCOUNT_BLOCKED, ACCOUNT_DELETE, NUMBER_COLS_ACCOUNT};
enum {CATEGORY_NAME, CATEGORY_BACKGROUND_COLOR, CATEGORY_FOREGROUND_COLOR, CATEGORY_FONT, CATEGORY_DELETE, NUMBER_COLS_CATEGORY};
PreferencesPanel::PreferencesPanel(KissCount* kiss, wxUI *parent) : KissPanel(kiss, parent), _sharedWith(0), _curAccountRow(-1), _defaultSignalMapper(this), _virtualSignalMapper(this), _blockedSignalMapper(this), _deleteAccountSignalMapper(this), _deleteCategorySignalMapper(this), _backgroundColorSignalMapper(this), _foregroundColorSignalMapper(this), _fontSignalMapper(this), _inModification(false)
{
QVBoxLayout *vbox = new QVBoxLayout;
QHBoxLayout *hbox1 = new QHBoxLayout;
QHBoxLayout *hbox2 = new QHBoxLayout;
//QHBoxLayout *hbox = new QHBoxLayout;
QGroupBox* staticUser, *staticAccount, *staticCategories, *staticLanguage, *staticOperationOrder, *staticSharedWith;
User* user = _kiss->GetUser();
QGridLayout *gridBagSizer;
QLabel* label;
QPushButton* buttonChangeName, *buttonChangePassword, *killMe;
QVBoxLayout * staticBoxSizer;
std::list users;
std::list::iterator it;
setLayout(vbox);
staticUser = new QGroupBox(_("User"));
staticAccount = new QGroupBox(_("Accounts"));
staticCategories = new QGroupBox(_("Categories"));
staticLanguage = new QGroupBox(_("Language"));
staticOperationOrder = new QGroupBox(_("Operation order"));
staticSharedWith = new QGroupBox(_("Shared with"));
// User
staticBoxSizer = new QVBoxLayout ();
staticUser->setLayout(staticBoxSizer);
gridBagSizer = new QGridLayout();
label = new QLabel(_("Name"));
gridBagSizer->addWidget(label, 0, 0);
_name = new QLineEdit(user->_name);
gridBagSizer->addWidget(_name, 0, 1);
buttonChangeName = new QPushButton(_("Change Name"));
buttonChangePassword = new QPushButton(_("Change Password"));
killMe = new QPushButton(_("Kill me"));
gridBagSizer->addWidget(buttonChangeName, 1, 0);
gridBagSizer->addWidget(buttonChangePassword, 1, 1);
gridBagSizer->addWidget(killMe, 1, 2);
staticBoxSizer->addLayout(gridBagSizer);
vbox->addWidget(staticUser);
connect(buttonChangeName, SIGNAL(clicked()), this, SLOT(OnChangeName()));
connect(buttonChangePassword, SIGNAL(clicked()), this, SLOT(OnChangePassword()));
connect(killMe, SIGNAL(clicked()), this, SLOT(OnKillMe()));
// Account
staticBoxSizer = new QVBoxLayout ();
staticAccount->setLayout(staticBoxSizer);
_accountsGrid = new QTableWidget();
StarDelegate* starDelegate = new StarDelegate(this, &user->_accounts);
_accountsGrid->setItemDelegateForColumn(ACCOUNT_NUMBER, starDelegate);
InitAccounts(user);
connect(_accountsGrid, SIGNAL(cellChanged(int, int)), this, SLOT(OnAccountModified(int, int)));
connect(_accountsGrid, SIGNAL(currentCellChanged(int, int, int, int)), this, SLOT(OnAccountCellChanged(int, int, int, int)));
staticBoxSizer->addWidget(_accountsGrid);
hbox1->addWidget(staticAccount);
staticBoxSizer = new QVBoxLayout ();
staticSharedWith->setLayout(staticBoxSizer);
_sharedWith = new QListWidget();
users = _kiss->GetUsers();
QListWidgetItem* item;
for(it=users.begin(); it!=users.end(); it++)
{
if (*it != user->_name)
{
item = new QListWidgetItem(*it);
item->setCheckState(Qt::Unchecked);
_sharedWith->addItem(item);
}
}
_sharedWith->setEnabled(false);
connect(_sharedWith, SIGNAL(itemChanged(QListWidgetItem *)), this, SLOT(OnSharedChange(QListWidgetItem *)));
staticBoxSizer->addWidget(_sharedWith);
hbox1->addWidget(staticSharedWith);
vbox->addLayout(hbox1);
// Categories
staticBoxSizer = new QVBoxLayout ();
staticCategories->setLayout(staticBoxSizer);
_categoriesGrid = new QTableWidget();
staticBoxSizer->addWidget(_categoriesGrid);
InitCategories(user);
connect(_categoriesGrid, SIGNAL(cellChanged(int, int)), this, SLOT(OnCategoryModified(int, int)));
vbox->addWidget(staticCategories);
// Operation Order
staticBoxSizer = new QVBoxLayout ();
staticOperationOrder->setLayout(staticBoxSizer);
_operationOrder = new QComboBox();
//_operationOrder->SetWindowStyle(wxCB_READONLY);
InitOperationOrder(user);
staticBoxSizer->addWidget(_operationOrder);
hbox2->addWidget(staticOperationOrder);
connect(_operationOrder, SIGNAL(currentIndexChanged(int)), this, SLOT(OnOperationOrderChange(int)));
// Language
staticBoxSizer = new QVBoxLayout ();
staticLanguage->setLayout(staticBoxSizer);
_language = new QListWidget();
//_language->SetWindowStyle(wxCB_READONLY);
InitLanguage(user);
staticBoxSizer->addWidget(_language);
hbox2->addWidget(staticLanguage);
vbox->addLayout(hbox2);
connect(&_defaultSignalMapper, SIGNAL(mapped(int)), this, SLOT(OnAccountDefaultClicked(int)));
connect(&_virtualSignalMapper, SIGNAL(mapped(int)), this, SLOT(OnAccountVirtualClicked(int)));
connect(&_blockedSignalMapper, SIGNAL(mapped(int)), this, SLOT(OnAccountBlockedClicked(int)));
connect(&_deleteAccountSignalMapper, SIGNAL(mapped(int)), this, SLOT(OnAccountDeleteClicked(int)));
connect(&_deleteCategorySignalMapper, SIGNAL(mapped(int)), this, SLOT(OnCategoryDeleteClicked(int)));
connect(&_backgroundColorSignalMapper, SIGNAL(mapped(int)), this, SLOT(OnBackgroundColorClicked(int)));
connect(&_foregroundColorSignalMapper, SIGNAL(mapped(int)), this, SLOT(OnForegroundClicked(int)));
connect(&_fontSignalMapper, SIGNAL(mapped(int)), this, SLOT(OnFontClicked(int)));
layout();
}
KissPanel* PreferencesPanel::CreatePanel()
{
return new PreferencesPanel(_kiss, _wxUI);
}
QPushButton* PreferencesPanel::GetButton()
{
if (!_KissButton)
{
_KissButton = new QPushButton(QIcon(PREFS_ICON), "", this);
_KissButton->setFixedSize(128, 128);
_KissButton->setIconSize(QSize(128, 128));
}
return _KissButton;
}
QString PreferencesPanel::GetToolTip()
{
return _("Preferences");
}
void PreferencesPanel::InitAccounts(User* user)
{
std::vector::iterator it;
int curLine = 0;
Account account ;
DEFAULT_FONT(font);
_accountsGrid->setColumnCount(NUMBER_COLS_ACCOUNT);
_accountsGrid->verticalHeader()->setHidden(true);
_accountsGrid->setFont(font);
_accountsGrid->setHorizontalHeaderItem(ACCOUNT_NAME, new QTableWidgetItem(_("Name")));
_accountsGrid->setHorizontalHeaderItem(ACCOUNT_NUMBER, new QTableWidgetItem(_("Number")));
_accountsGrid->setHorizontalHeaderItem(ACCOUNT_DEFAULT, new QTableWidgetItem(_("Default")));
_accountsGrid->setHorizontalHeaderItem(ACCOUNT_VIRTUAL, new QTableWidgetItem(_("Virtual")));
_accountsGrid->setHorizontalHeaderItem(ACCOUNT_BLOCKED, new QTableWidgetItem(_("Blocked")));
_accountsGrid->setHorizontalHeaderItem(ACCOUNT_DELETE, new QTableWidgetItem(_("Delete")));
font.setBold(true);
for(int i=0; ihorizontalHeaderItem(i)->setFont(font);
}
_accountsGrid->setRowCount(user->GetAccountsNumber()+1);
for (it = user->_accounts.begin(); it != user->_accounts.end(); it++, curLine++)
{
AddAccount(curLine, *it);
}
account.id = 0;
AddAccount(curLine, account);
}
#define SET_READ_ONLY(row, line) _accountsGrid->item(row, line)->setFlags(_accountsGrid->item(row, line)->flags() & ~Qt::ItemIsEditable);
void PreferencesPanel::AddAccount(int line, Account ac)
{
QCheckBox* checkBox;
if (ac.id != 0)
{
_accountsGrid->setItem(line, ACCOUNT_NAME, new QTableWidgetItem(ac.name));
if (ac.shared)
_accountsGrid->setItem(line, ACCOUNT_NUMBER, new QTableWidgetItem(ac.number + "*"));
else
_accountsGrid->setItem(line, ACCOUNT_NUMBER, new QTableWidgetItem(ac.number));
checkBox = new QCheckBox();
checkBox->setCheckState((ac._default) ? Qt::Checked : Qt::Unchecked);
_accountsGrid->setCellWidget(line, ACCOUNT_DEFAULT, checkBox);
_defaultSignalMapper.setMapping(checkBox, ac.id);
connect(checkBox, SIGNAL(stateChanged(int)), &_defaultSignalMapper, SLOT(map()));
checkBox = new QCheckBox();
checkBox->setCheckState((ac._virtual) ? Qt::Checked : Qt::Unchecked);
_accountsGrid->setCellWidget(line, ACCOUNT_VIRTUAL, checkBox);
_virtualSignalMapper.setMapping(checkBox, ac.id);
connect(checkBox, SIGNAL(stateChanged(int)), &_virtualSignalMapper, SLOT(map()));
checkBox = new QCheckBox();
checkBox->setCheckState((ac.blocked) ? Qt::Checked : Qt::Unchecked);
_accountsGrid->setCellWidget(line, ACCOUNT_BLOCKED, checkBox);
_blockedSignalMapper.setMapping(checkBox, ac.id);
connect(checkBox, SIGNAL(stateChanged(int)), &_blockedSignalMapper, SLOT(map()));
checkBox = new QCheckBox();
checkBox->setCheckState(Qt::Unchecked);
_accountsGrid->setCellWidget(line, ACCOUNT_DELETE, checkBox);
_deleteAccountSignalMapper.setMapping(checkBox, ac.id);
connect(checkBox, SIGNAL(stateChanged(int)), &_deleteAccountSignalMapper, SLOT(map()));
for(int i=0; iitem(line, i))
_accountsGrid->setItem(line, i, new QTableWidgetItem(""));
// _accountsGrid->SetCellAlignment(line, ACCOUNT_DEFAULT, wxALIGN_CENTRE, wxALIGN_CENTRE);
// _accountsGrid->SetCellAlignment(line, ACCOUNT_VIRTUAL, wxALIGN_CENTRE, wxALIGN_CENTRE);
// _accountsGrid->SetCellAlignment(line, ACCOUNT_BLOCKED, wxALIGN_CENTRE, wxALIGN_CENTRE);
// _accountsGrid->SetCellAlignment(line, ACCOUNT_DELETE, wxALIGN_CENTRE, wxALIGN_CENTRE);
if (!ac.is_owner)
{
SET_READ_ONLY(line, ACCOUNT_NAME);
SET_READ_ONLY(line, ACCOUNT_NUMBER);
SET_READ_ONLY(line, ACCOUNT_DEFAULT);
SET_READ_ONLY(line, ACCOUNT_VIRTUAL);
SET_READ_ONLY(line, ACCOUNT_BLOCKED);
}
else
{
SET_READ_ONLY(line, ACCOUNT_DEFAULT);
SET_READ_ONLY(line, ACCOUNT_VIRTUAL);
SET_READ_ONLY(line, ACCOUNT_BLOCKED);
SET_READ_ONLY(line, ACCOUNT_DELETE);
}
}
else
{
for(int i=0; iitem(line, i))
_accountsGrid->setItem(line, i, new QTableWidgetItem(""));
SET_READ_ONLY(line, ACCOUNT_DEFAULT);
SET_READ_ONLY(line, ACCOUNT_VIRTUAL);
SET_READ_ONLY(line, ACCOUNT_BLOCKED);
SET_READ_ONLY(line, ACCOUNT_DELETE);
}
_accountsGrid->resizeColumnsToContents();
}
#undef SET_READ_ONLY
#define SET_READ_ONLY(row, line) _categoriesGrid->item(row, line)->setFlags(_categoriesGrid->item(row, line)->flags() & ~Qt::ItemIsEditable);
#define SET_ROW_COLOR(row, backcolor, forecolor) \
for(int i=0; iitem(row, i)->setBackground(QBrush(backcolor)); \
_categoriesGrid->item(row, i)->setForeground(QBrush(forecolor)); \
}
#define SET_ROW_FONT(row, font) \
for(int i=0; iitem(row, i)->setFont(font); \
}
void PreferencesPanel::InitCategories(User* user)
{
std::vector::iterator it;
int curLine = 0;
DEFAULT_FONT(font);
Category cat;
_categoriesGrid->setColumnCount(NUMBER_COLS_CATEGORY);
_categoriesGrid->verticalHeader()->setHidden(true);
_categoriesGrid->setFont(font);
_categoriesGrid->setHorizontalHeaderItem(CATEGORY_NAME, new QTableWidgetItem(_("Name")));
_categoriesGrid->setHorizontalHeaderItem(CATEGORY_BACKGROUND_COLOR, new QTableWidgetItem(_("Background color")));
_categoriesGrid->setHorizontalHeaderItem(CATEGORY_FOREGROUND_COLOR, new QTableWidgetItem(_("Foreground color")));
_categoriesGrid->setHorizontalHeaderItem(CATEGORY_FONT, new QTableWidgetItem(_("Font")));
_categoriesGrid->setHorizontalHeaderItem(CATEGORY_DELETE, new QTableWidgetItem(_("Delete")));
_categoriesGrid->setRowCount(user->GetCategoriesNumber()+1);
font.setBold(true);
for(int i=0; ihorizontalHeaderItem(i)->setFont(font);
}
for (it=user->_categories.begin(); it!=user->_categories.end(); it++, curLine++)
{
AddCategory(curLine, *it);
}
for(int i=0; iitem(curLine, i))
_categoriesGrid->setItem(curLine, i, new QTableWidgetItem(""));
SET_READ_ONLY(curLine, CATEGORY_BACKGROUND_COLOR);
SET_READ_ONLY(curLine, CATEGORY_FOREGROUND_COLOR);
SET_READ_ONLY(curLine, CATEGORY_FONT);
SET_READ_ONLY(curLine, CATEGORY_DELETE);
cat.id = 0;
AddCategory(curLine, cat);
}
void PreferencesPanel::AddCategory(int line, Category cat)
{
User* user = _kiss->GetUser();
QCheckBox* checkBox;
QPushButton* button;
if (cat.id != 0)
{
_categoriesGrid->setItem(line, CATEGORY_NAME, new QTableWidgetItem(_(cat.name.toStdString().c_str())));
if (line)
{
checkBox = new QCheckBox();
checkBox->setCheckState(Qt::Unchecked);
_categoriesGrid->setCellWidget(line, CATEGORY_DELETE, checkBox);
_deleteCategorySignalMapper.setMapping(checkBox, cat.id);
connect(checkBox, SIGNAL(stateChanged(int)), &_deleteCategorySignalMapper, SLOT(map()));
}
button = new QPushButton("...");
_categoriesGrid->setCellWidget(line, CATEGORY_BACKGROUND_COLOR, button);
_backgroundColorSignalMapper.setMapping(button, cat.id);
connect(button, SIGNAL(clicked()), &_backgroundColorSignalMapper, SLOT(map()));
button = new QPushButton("...");
_categoriesGrid->setCellWidget(line, CATEGORY_FOREGROUND_COLOR, button);
_foregroundColorSignalMapper.setMapping(button, cat.id);
connect(button, SIGNAL(clicked()), &_foregroundColorSignalMapper, SLOT(map()));
button = new QPushButton("...");
_categoriesGrid->setCellWidget(line, CATEGORY_FONT, button);
_fontSignalMapper.setMapping(button, cat.id);
connect(button, SIGNAL(clicked()), &_fontSignalMapper, SLOT(map()));
for(int i=0; iitem(line, i) == NULL)
_categoriesGrid->setItem(line, i, new QTableWidgetItem(""));
SET_ROW_COLOR(line, cat.backcolor, cat.forecolor);
if (cat.font.size())
{
QFont font = user->GetCategoryFont(cat.id);
SET_ROW_FONT(line, font);
}
// _categoriesGrid->SetCellAlignment(line, CATEGORY_DELETE, wxALIGN_CENTRE, wxALIGN_CENTRE);
}
else
{
for(int i=0; iitem(line, i))
_categoriesGrid->setItem(line, i, new QTableWidgetItem(""));
SET_READ_ONLY(line, CATEGORY_BACKGROUND_COLOR);
SET_READ_ONLY(line, CATEGORY_FOREGROUND_COLOR);
SET_READ_ONLY(line, CATEGORY_FONT);
SET_READ_ONLY(line, CATEGORY_DELETE);
SET_ROW_COLOR(line, view::OWN_GREEN, Qt::black);
_categoriesGrid->resizeColumnsToContents();
}
}
void PreferencesPanel::InitLanguage(User* user)
{
int i, select=0;
QListWidgetItem* item;
for (i=0; isetIcon(QIcon(SupportedLanguages::languages[i].icon)),
_language->addItem(item);
if (SupportedLanguages::languages[i].language == _wxUI->_language)
select = i;
}
_language->setCurrentItem(_language->item(select));
}
void PreferencesPanel::InitOperationOrder(User* user)
{
_operationOrder->addItem(_("Ascending"));
_operationOrder->addItem(_("Descending"));
if (user->_preferences["operation_order"] == "ASC")
_operationOrder->setCurrentIndex(0);
else
_operationOrder->setCurrentIndex(1);
}
void PreferencesPanel::OnAccountDefaultClicked(int id)
{
User* user = _kiss->GetUser();
QCheckBox* checkBox = qobject_cast (_defaultSignalMapper.mapping(id));
std::vector::iterator it;
int i, row;
Account account;
if (_inModification) return;
it = std::find(user->_accounts.begin(), user->_accounts.end(), id);
if (it == user->_accounts.end()) return ;
row = it-user->_accounts.begin();
account = user->_accounts[row];
_inModification = true;
if (account._default)
{
checkBox->setCheckState(Qt::Checked);
_inModification = false;
return;
}
account._default = true;
_kiss->UpdateAccount(account);
for (i=0; iGetAccountsNumber(); i++)
{
if (i != row)
{
account = user->_accounts[i];
if (account._default)
{
account._default = false;
_kiss->UpdateAccount(account);
checkBox = qobject_cast(_accountsGrid->cellWidget(i, ACCOUNT_DEFAULT));
checkBox->setCheckState(Qt::Unchecked);
break;
}
}
}
account = user->_accounts[row];
_inModification = false;
}
void PreferencesPanel::OnAccountVirtualClicked(int id)
{
User* user = _kiss->GetUser();
QCheckBox* checkBox = qobject_cast (_virtualSignalMapper.mapping(id));
std::vector::iterator it;
Account account;
int row;
it = std::find(user->_accounts.begin(), user->_accounts.end(), id);
if (it == user->_accounts.end()) return ;
row = it-user->_accounts.begin();
account = user->_accounts[row];
account._virtual = (checkBox->checkState() == Qt::Checked);
_kiss->UpdateAccount(account);
}
void PreferencesPanel::OnAccountBlockedClicked(int id)
{
User* user = _kiss->GetUser();
QCheckBox* checkBox = qobject_cast (_blockedSignalMapper.mapping(id));
std::vector::iterator it;
Account account;
int row;
it = std::find(user->_accounts.begin(), user->_accounts.end(), id);
if (it == user->_accounts.end()) return ;
row = it-user->_accounts.begin();
account = user->_accounts[row];
account.blocked = (checkBox->checkState() == Qt::Checked);
_kiss->UpdateAccount(account);
}
void PreferencesPanel::OnAccountDeleteClicked(int id)
{
User* user = _kiss->GetUser();
std::vector::iterator it;
int i, row;
QStringList accounts;
QString res;
QCheckBox* checkBox = qobject_cast (_deleteAccountSignalMapper.mapping(id));
Account account;
bool ok;
if (user->GetAccountsNumber() == 1)
{
QMessageBox::critical(0, _("Error"), _("It must be at least one account !"));
checkBox->setCheckState(Qt::Unchecked);
return;
}
it = std::find(user->_accounts.begin(), user->_accounts.end(), id);
if (it == user->_accounts.end()) return ;
if (_inModification) return;
_inModification = true;
row = it-user->_accounts.begin();
account = user->_accounts[row];
accounts << _("None");
for(i=0; i < user->GetAccountsNumber(); i++)
if (user->_accounts[i].id != id)
accounts << user->_accounts[i].name;
res = QInputDialog::getItem(this, "KissCount", _("Wich account will replace this one ?"), accounts, 0, false, &ok);
if (!ok)
{
checkBox->setCheckState(Qt::Unchecked);
_inModification = false;
return;
}
else
{
i = accounts.indexOf(res);
_kiss->DeleteAccount(account, (!i) ? 0 : user->GetAccountId(accounts[i]));
_accountsGrid->removeRow(row);
}
if (user->_accounts.size() == 1)
{
user->_accounts[0]._default = true;
_kiss->UpdateAccount(user->_accounts[0]);
checkBox = qobject_cast (_defaultSignalMapper.mapping(id));
checkBox->setCheckState(Qt::Checked);
}
_inModification = false;
_wxUI->NeedReload();
}
void PreferencesPanel::OnCategoryDeleteClicked(int id)
{
QStringList categories;
int i, row;
QString res;
User* user = _kiss->GetUser();
bool ok;
QCheckBox* checkBox = qobject_cast (_deleteCategorySignalMapper.mapping(id));
Category category;
std::vector::iterator it;
if (_inModification) return;
it = std::find(user->_categories.begin(), user->_categories.end(), id);
if (it == user->_categories.end()) return ;
_inModification = true;
row = it-user->_categories.begin();
category = user->_categories[row];
categories << _("None");
for(i=0; i < user->GetCategoriesNumber(); i++)
if (user->_categories[i].id != id)
categories << _(user->_categories[i].name.toStdString().c_str());
res = QInputDialog::getItem(this, "KissCount", _("Wich category will replace this one ?"), categories, 0, false, &ok);
if (!ok)
{
checkBox->setCheckState(Qt::Unchecked);
_inModification = false;
return;
}
else
{
i = categories.indexOf(res);
_kiss->DeleteCategory(category, (!i) ? 0 : user->GetCategoryId(categories[i]));
_categoriesGrid->removeRow(row);
_wxUI->NeedReload();
}
_inModification = false;
}
void PreferencesPanel::OnBackgroundColorClicked(int id)
{
User* user = _kiss->GetUser();
QColor color;
std::vector::iterator it;
int row;
it = std::find(user->_categories.begin(), user->_categories.end(), id);
if (it == user->_categories.end()) return ;
row = it-user->_categories.begin();
_inModification = true ;
color = QColorDialog::getColor(user->_categories[row].backcolor);
if (color.isValid())
{
user->_categories[row].backcolor = color;
_kiss->UpdateCategory(user->_categories[row]);
SET_ROW_COLOR(row, user->_categories[row].backcolor, user->_categories[row].forecolor);
_wxUI->NeedReload();
}
_inModification = false ;
}
void PreferencesPanel::OnForegroundClicked(int id)
{
User* user = _kiss->GetUser();
QColor color;
std::vector::iterator it;
int row;
it = std::find(user->_categories.begin(), user->_categories.end(), id);
if (it == user->_categories.end()) return ;
row = it-user->_categories.begin();
_inModification = true ;
color = QColorDialog::getColor(user->_categories[row].forecolor);
if (color.isValid())
{
user->_categories[row].forecolor = color;
_kiss->UpdateCategory(user->_categories[row]);
SET_ROW_COLOR(row, user->_categories[row].backcolor, user->_categories[row].forecolor);
_wxUI->NeedReload();
}
_inModification = false ;
}
void PreferencesPanel::OnFontClicked(int id)
{
User* user = _kiss->GetUser();
bool ok;
QFont font;
std::vector::iterator it;
int row;
it = std::find(user->_categories.begin(), user->_categories.end(), id);
if (it == user->_categories.end()) return ;
row = it-user->_categories.begin();
_inModification = true ;
font = QFontDialog::getFont(&ok, user->_categories[row].font);
if (ok)
{
user->_categories[row].font = KissCount::CompactFont(font);
_kiss->UpdateCategory(user->_categories[row]);
SET_ROW_FONT(row, font);
_wxUI->NeedReload();
}
_inModification = false ;
}
void PreferencesPanel::OnAccountModified(int row, int col)
{
int op_complete = 1, new_id;
QString value ;
Account new_account, account;
User* user = _kiss->GetUser();
if (_inModification) return;
_inModification = true;
value = _accountsGrid->item(row, ACCOUNT_NAME)->text();
if (value.size())
{
new_account.name = value;
op_complete--;
}
value = _accountsGrid->item(row, ACCOUNT_NUMBER)->text();
if (value.size())
{
new_account.number = value;
op_complete--;
}
// Account modification
if (user->GetAccountsNumber() && row < user->GetAccountsNumber())
{
new_account.id = user->_accounts[row].id;
new_account.shared = user->_accounts[row].shared;
new_account.is_owner = user->_accounts[row].is_owner;
if (col == ACCOUNT_NAME)
{
new_id = user->GetAccountId(new_account.name);
if (new_id != 0 && new_id != new_account.id)
{
QMessageBox::critical(0, _("Error"), _("Account ")+new_account.name+_(" already exists"));
_accountsGrid->setItem(row, ACCOUNT_NAME, new QTableWidgetItem(user->_accounts[row].name));
_inModification = false;
return ;
}
}
_kiss->UpdateAccount(new_account);
}
// New account
else
{
if (op_complete)
{
_inModification = false;
return ;
}
if (user->GetAccountId(new_account.name) != 0)
{
QMessageBox::critical(0, _("Error"), _("Account ")+new_account.name+_(" already exists"));
_inModification = false;
return ;
}
new_account._default = false;
new_account.shared = false;
new_account.blocked = false;
new_account.is_owner = true;
new_account._virtual = false;
AddAccount(row, new_account);
_kiss->AddAccount(new_account);
new_account.id = 0;
_accountsGrid->setRowCount(row+2);
AddAccount(row+1, new_account);
}
_accountsGrid->resizeColumnsToContents();
_wxUI->NeedReload();
_inModification = false;
}
void PreferencesPanel::OnAccountCellChanged(int row, int col, int previousRow, int previousCol)
{
User* user = _kiss->GetUser();
std::map::iterator it;
int i;
bool own;
QString owner;
QListWidgetItem* item;
if (!_sharedWith || _curAccountRow == row)
return ;
_curAccountRow = row ;
if (row >= (int) user->_accounts.size())
{
for(i=0; i<(int)_sharedWith->count(); i++)
_sharedWith->item(i)->setCheckState(Qt::Unchecked);
own = false;
}
else
{
_sharedOwners = _kiss->getSharedAccountOwners(user->_accounts[row].id);
owner = _kiss->getSharedAccountOwner(user->_accounts[row].id);
for(i=0; i<(int)_sharedWith->count(); i++)
{
item = _sharedWith->item(i);
item->setCheckState((_sharedOwners[item->text()].size() > 0 ||
item->text() == owner) ? Qt::Checked : Qt::Unchecked);
}
own = user->_accounts[row].is_owner;
}
_sharedWith->setEnabled(own);
}
void PreferencesPanel::OnSharedChange(QListWidgetItem *item)
{
User* user = _kiss->GetUser();
if (_inModification) return;
_inModification = true;
if (item->checkState() == Qt::Checked)
{
if (!user->_accounts[_curAccountRow].shared)
_accountsGrid->setItem(_curAccountRow, ACCOUNT_NUMBER,
new QTableWidgetItem(user->_accounts[_curAccountRow].number + "*"));
_kiss->AddSharedAccount(user->_accounts[_curAccountRow], item->text());
}
else
{
_kiss->RemoveSharedAccount(user->_accounts[_curAccountRow], _sharedOwners[item->text()].toInt());
if (!user->_accounts[_curAccountRow].shared)
_accountsGrid->setItem(_curAccountRow, ACCOUNT_NUMBER, new QTableWidgetItem(user->_accounts[_curAccountRow].number));
}
_inModification = false;
_wxUI->NeedReload();
}
void PreferencesPanel::OnCategoryModified(int row, int col)
{
int op_complete = 1;
QString value;
User* user = _kiss->GetUser();
static bool _inModification = false ;
Category new_cat, old_cat;
int new_id;
QTableWidgetItem* item = _categoriesGrid->item(row, col);
if (_inModification) return;
_inModification = true;
value = item->text();
if (value.size())
{
new_cat.name = value;
op_complete--;
}
new_cat.backcolor = item->background().color();
new_cat.forecolor = item->foreground().color();
new_cat.font = "";
new_cat.parent = 0;
// Categories modification
if (user->GetCategoriesNumber() && row < user->GetCategoriesNumber())
{
old_cat = user->_categories[row];
new_cat.id = user->_categories[row].id;
new_cat.fix_cost = user->_categories[row].fix_cost;
new_cat.backcolor = old_cat.backcolor;
new_cat.forecolor = old_cat.forecolor;
new_cat.font = old_cat.font;
new_cat.parent = old_cat.parent;
new_id = user->GetCategoryId(new_cat.name);
if (new_id != 0 && new_id != new_cat.id)
{
QMessageBox::critical(0, _("Error"), _("Category ")+new_cat.name+_(" already exists"));
_categoriesGrid->setItem(row, CATEGORY_NAME, new QTableWidgetItem(_(user->_categories[row].name.toStdString().c_str())));
_inModification = false;
return ;
}
_kiss->UpdateCategory(new_cat);
}
// New category
else
{
if (op_complete)
{
_inModification = false;
return ;
}
if (user->GetCategoryId(new_cat.name) != 0)
{
QMessageBox::critical(0, _("Error"), _("Category ")+new_cat.name+_(" already exists"));
_inModification = false;
return ;
}
new_cat.fix_cost = false;
_kiss->AddCategory(new_cat);
AddCategory(row, new_cat);
SET_READ_ONLY(row, CATEGORY_BACKGROUND_COLOR);
SET_READ_ONLY(row, CATEGORY_FOREGROUND_COLOR);
SET_READ_ONLY(row, CATEGORY_FONT);
SET_READ_ONLY(row, CATEGORY_DELETE);
new_cat.id = 0;
_categoriesGrid->setRowCount(row+2);
AddCategory(++row, new_cat);
}
_wxUI->NeedReload();
_inModification = false;
}
void PreferencesPanel::OnChangeName()
{
User* user = _kiss->GetUser();
QString name = _name->text();
if (name == user->_name)
return;
if (!name.size())
{
QMessageBox::critical(0, _("Error"), _("Invalid name !"));
return;
}
if (_kiss->UserExists(name))
{
QMessageBox::critical(0, _("Error"), _("User ") + name + _(" already exists"));
return;
}
_kiss->ChangeName(name);
QMessageBox::information(0, "KissCount", _("Name changed"));
_wxUI->NeedReload();
OnShow();
}
void PreferencesPanel::OnChangePassword()
{
PasswordDialog g(_kiss, _wxUI);
g.setModal(true);
g.exec();
}
void PreferencesPanel::OnOperationOrderChange(int index)
{
if (index == 0)
_kiss->SetOperationOrder("ASC");
else
_kiss->SetOperationOrder("DESC");
_wxUI->NeedReload();
}
void PreferencesPanel::OnLanguageChange()
{
SupportedLanguages::wxLanguage language = SupportedLanguages::languages[_language->currentRow()].language;
QString name_lang = SupportedLanguages::languages[_language->currentRow()].name;
if (_wxUI->SetLanguage(name_lang) || language == SupportedLanguages::wxLANGUAGE_ENGLISH)
{
_wxUI->NeedReload();
_kiss->SetLanguage(name_lang);
QMessageBox::information(0, "KissCount", _("Language successfully changed, please go to another panel"));
}
else
QMessageBox::critical(0, _("Error"), _("Language not changed"));
}
void PreferencesPanel::OnShow()
{
_wxUI->setWindowTitle(_kiss->GetUser()->_name + " - " + _("Preferences"));
}
void PreferencesPanel::OnKillMe()
{
User* user = _kiss->GetUser();
if (QMessageBox::question(0, "KissCount", _("Are you sure want to delete ")+user->_name+_(" profil ?"), QMessageBox::Yes|QMessageBox::No) == QMessageBox::No)
{
return;
}
_kiss->KillMe();
}