From a71638ace6fb40e1b174458a49d1e3867e7f7a5f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Gr=C3=A9gory=20Soutad=C3=A9?= Date: Sat, 7 Jan 2012 09:21:22 +0100 Subject: [PATCH] Work on PreferencesPanel and fix a bug in Database (bad request for AddOperation) --- src/model/Database.cpp | 6 +- src/view/PreferencesPanel.cpp | 366 ++++++++++++++++++-------------- src/view/PreferencesPanel.hpp | 2 +- src/view/SupportedLanguages.hpp | 4 +- 4 files changed, 212 insertions(+), 166 deletions(-) diff --git a/src/model/Database.cpp b/src/model/Database.cpp index b3bd7b6..ef72758 100644 --- a/src/model/Database.cpp +++ b/src/model/Database.cpp @@ -591,12 +591,12 @@ int Database::AddOperation(User* user, Operation& op, bool checkTransfert) ESCAPE_CHARS(op.description); - req = "INSERT INTO operation ('user', 'parent', 'account', 'year', 'month', 'day', 'amount', 'description', 'category', 'fix_cost', 'formula', 'transfert', 'meta', 'virtual') VALUES ('%1', '%2', '%3', '%4', '%5', '%6', '%7', '%8'" ; + req = "INSERT INTO operation ('user', 'parent', 'account', 'year', 'month', 'day', 'amount', 'description', 'category', 'fix_cost', 'formula', 'transfert', 'meta', 'virtual', 'checked') VALUES ('%1', '%2', '%3', '%4', '%5', '%6', '%7', '%8'" ; req = req.arg(QString::number(user->_id), (op.parent) ? QString::number(op.parent): "", QString::number(op.account), QString::number(op.year), QString::number(op.month), QString::number(op.day), DoubleToString(op.amount), op.description); - req += ", '%1', '%2', '%3', '%4', '%5', '%6')"; + req += ", '%1', '%2', '%3', '%4', '%5', '%6', '%7')"; req = req.arg(QString::number(op.category), QString::number(op.fix_cost), op.formula, (op.transfert) ? QString::number(op.transfert): "", - QString::number(op.meta), QString::number(op._virtual)); + QString::number(op.meta), QString::number(op._virtual), QString::number(op.checked)); if (!query.exec(req)) { diff --git a/src/view/PreferencesPanel.cpp b/src/view/PreferencesPanel.cpp index 7a1466a..1251fb5 100644 --- a/src/view/PreferencesPanel.cpp +++ b/src/view/PreferencesPanel.cpp @@ -18,7 +18,7 @@ */ -// #include "PasswordDialog.hpp" +#include "PasswordDialog.hpp" #include "PreferencesPanel.hpp" #include "grid/StarDelegate.hpp" @@ -133,6 +133,8 @@ PreferencesPanel::PreferencesPanel(KissCount* kiss, wxUI *parent) : KissPanel(ki InitCategories(user); + connect(_categoriesGrid, SIGNAL(cellChanged(int, int)), this, SLOT(OnCategoryModified(int, int))); + vbox->addWidget(staticCategories); // Operation Order @@ -361,7 +363,9 @@ void PreferencesPanel::InitCategories(User* user) if (!_categoriesGrid->item(curLine, i)) _categoriesGrid->setItem(curLine, i, new QTableWidgetItem("")); - SET_READ_ONLY(curLine, CATEGORY_NAME); + 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; @@ -434,11 +438,13 @@ void PreferencesPanel::AddCategory(int line, Category cat) void PreferencesPanel::InitLanguage(User* user) { int i, select=0; + QListWidgetItem* item; for (i=0; iaddItem(SupportedLanguages::languages[i].name); - //_language->setItemWidget(new QListWidgetItem(SupportedLanguages::languages[i].name), QIcon(SupportedLanguages::languages[i].icon)); + item = new QListWidgetItem(SupportedLanguages::languages[i].name); + item->setIcon(QIcon(SupportedLanguages::languages[i].icon)), + _language->addItem(item); if (SupportedLanguages::languages[i].language == _wxUI->_language) select = i; @@ -607,16 +613,139 @@ void PreferencesPanel::OnAccountDeleteClicked(int id) } 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) { @@ -766,170 +895,88 @@ void PreferencesPanel::OnSharedChange(QListWidgetItem *item) _wxUI->NeedReload(); } -void PreferencesPanel::OnCategoryModified() +void PreferencesPanel::OnCategoryModified(int row, int col) { - // int op_complete = 1; - // wxString value; - // User* user = _kiss->GetUser(); - // int row = event.GetRow(); - // int col = event.GetCol(); - // static bool _inModification = false ; - // Category new_cat, cat_tmp; - // int i, a; + 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; + if (_inModification) return; - // _inModification = true; + _inModification = true; - // if (event.GetCol() == CATEGORY_BACKGROUND_COLOR) - // { - // wxColourData color; - // color.SetColour(wxColor(user->_categories[row].backcolor)); - // wxColourDialog dial(this, &color); + value = item->text(); + if (value.size()) + { + new_cat.name = value; + op_complete--; + } - // if (dial.ShowModal() == wxID_OK) - // { - // user->_categories[row].backcolor = dial.GetColourData().GetColour(); - // _kiss->UpdateCategory(user->_categories[row]); - - // SET_ROW_COLOR(row, user->_categories[row].backcolor, user->_categories[row].forecolor); - // _wxUI->NeedReload(); - // } + new_cat.backcolor = item->background().color(); + new_cat.forecolor = item->foreground().color(); + new_cat.font = ""; + new_cat.parent = 0; - // _inModification = false ; - // return ; - // } + // 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; - // if (event.GetCol() == CATEGORY_FOREGROUND_COLOR) - // { - // wxColourData color; - // color.SetColour(wxColor(user->_categories[row].forecolor)); - // wxColourDialog dial(this, &color); + 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 ; + } - // if (dial.ShowModal() == wxID_OK) - // { - // user->_categories[row].forecolor = dial.GetColourData().GetColour(); - // _kiss->UpdateCategory(user->_categories[row]); - - // SET_ROW_COLOR(row, user->_categories[row].backcolor, user->_categories[row].forecolor); - // _wxUI->NeedReload(); - // } - - // _inModification = false ; - // return ; - // } - - // if (event.GetCol() == CATEGORY_FONT) - // { - // wxFontData font; - // font.SetInitialFont(_kiss->ExtractFont(user->_categories[row].font)); - // wxFontDialog dial(this, font); - - // if (dial.ShowModal() == wxID_OK) - // { - // font = dial.GetFontData(); - - // user->_categories[row].font = _kiss->CompactFont(font.GetChosenFont()); - // _kiss->UpdateCategory(user->_categories[row]); - - // SET_ROW_FONT(row, font.GetChosenFont()); - // _wxUI->NeedReload(); - // } - - // _inModification = false ; - // return ; - // } - - // value = _categoriesGrid->GetCellValue(row, CATEGORY_NAME); - // if (value.Length()) - // { - // new_cat.name = value; - // op_complete--; - // } - - // new_cat.backcolor = _categoriesGrid->GetCellBackgroundColour(row, col); - // new_cat.forecolor = _categoriesGrid->GetCellTextColour(row, col); - // new_cat.font = wxT(""); - // new_cat.parent = wxT("0"); - - // // Categories modification - // if (user->GetCategoriesNumber() && row < user->GetCategoriesNumber()) - // { - // new_cat.id = user->_categories[row].id; - // new_cat.fix_cost = user->_categories[row].fix_cost; - // if (col == CATEGORY_DELETE) - // { - // wxString *categories = new wxString[user->GetCategoriesNumber()]; - // categories[0] = _("None"); - // a = 0; - // for(i=0; i < user->GetCategoriesNumber(); i++) - // if (user->_categories[i].id != new_cat.id) - // categories[++a] = wxGetTranslation(user->_categories[i].name); - // wxSingleChoiceDialog dialog(_wxUI, _("Wich category will replace this one ?"), wxT("KissCount"), user->GetCategoriesNumber(), categories); - // if (dialog.ShowModal() == wxID_CANCEL) - // { - // _categoriesGrid->setItem(row, col, wxT("0")); - // } - // else - // { - // _categoriesGrid->DeleteRows(row, 1); - // i = dialog.GetSelection(); - // _kiss->DeleteCategory(user->_categories[row], (!i) ? wxT("0") : user->GetCategoryId(categories[i])); - // Fit(); - // _wxUI->NeedReload(); - // } - // _inModification = false; - // return; - // } - - // value = user->GetCategoryId(new_cat.name); - // if (value != wxT("0") && value != new_cat.id) - // { - // wxMessageBox(_("Category ")+new_cat.name+_(" already exists"), _("Error"), wxICON_ERROR | wxOK ); - // _categoriesGrid->setItem(row, CATEGORY_NAME, user->_categories[row].name); - // _inModification = false; - // return ; - // } - - // _kiss->UpdateCategory(new_cat); - // } - // // New category - // else - // { - // if (op_complete) - // { - // _inModification = false; - // return ; - // } + _kiss->UpdateCategory(new_cat); + } + // New category + else + { + if (op_complete) + { + _inModification = false; + return ; + } - // if (user->GetCategoryId(new_cat.name) != wxT("0")) - // { - // wxMessageBox(_("Category ")+new_cat.name+_(" already exists"), _("Error"), wxICON_ERROR | wxOK ); - // _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; + new_cat.fix_cost = false; - // _kiss->AddCategory(new_cat); - // AddCategory(row, new_cat); + _kiss->AddCategory(new_cat); + AddCategory(row, new_cat); - // _categoriesGrid->SetReadOnly(row, CATEGORY_BACKGROUND_COLOR, false); - // _categoriesGrid->SetReadOnly(row, CATEGORY_FOREGROUND_COLOR, false); - // _categoriesGrid->SetReadOnly(row, CATEGORY_FONT, false); - // _categoriesGrid->SetReadOnly(row, CATEGORY_DELETE, false); + 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 = wxT("0"); - // _categoriesGrid->AppendRows(); - // AddCategory(++row, new_cat); - // } + new_cat.id = 0; + _categoriesGrid->setRowCount(row+2); + AddCategory(++row, new_cat); + } - // Fit(); - // _wxUI->NeedReload(); + _wxUI->NeedReload(); - // _inModification = false; - - // return; + _inModification = false; } void PreferencesPanel::OnChangeName() @@ -961,8 +1008,9 @@ void PreferencesPanel::OnChangeName() void PreferencesPanel::OnChangePassword() { - // PasswordDialog p(_kiss, _wxUI); - // p.ShowModal(); + PasswordDialog g(_kiss, _wxUI); + g.setModal(true); + g.exec(); } void PreferencesPanel::OnOperationOrderChange(int index) diff --git a/src/view/PreferencesPanel.hpp b/src/view/PreferencesPanel.hpp index 31b6ce6..79ec592 100644 --- a/src/view/PreferencesPanel.hpp +++ b/src/view/PreferencesPanel.hpp @@ -52,7 +52,7 @@ private slots: void OnAccountModified(int row, int col); void OnAccountCellChanged(int row, int col, int, int); void OnSharedChange(QListWidgetItem *item); - void OnCategoryModified(); + void OnCategoryModified(int row, int col); void OnChangeName(); void OnChangePassword(); void OnOperationOrderChange(int index); diff --git a/src/view/SupportedLanguages.hpp b/src/view/SupportedLanguages.hpp index 9ab3fe5..3102a9d 100644 --- a/src/view/SupportedLanguages.hpp +++ b/src/view/SupportedLanguages.hpp @@ -23,7 +23,7 @@ namespace SupportedLanguages { #define ICONS_PATH RESSOURCES_ROOT "icons/" - enum wxLanguage {wxLANGUAGE_ENGLISH, wxLANGUAGE_FRENCH} ; + enum wxLanguage {wxLANGUAGE_ENGLISH, wxLANGUAGE_FRENCH, NB_SUPPORTED_LANGUAGES} ; typedef struct { QString name; @@ -31,8 +31,6 @@ namespace SupportedLanguages { wxLanguage language; } language ; - const int NB_SUPPORTED_LANGUAGES = 2; - static const language languages[NB_SUPPORTED_LANGUAGES] = { { "English", ICONS_PATH "/United Kingdom.png", wxLANGUAGE_ENGLISH}, { "Français",ICONS_PATH "/France.png", wxLANGUAGE_FRENCH}