Category management in place

This commit is contained in:
Grégory Soutadé 2010-06-22 11:35:21 +02:00
parent c322a01adb
commit 6f1d7734bf
5 changed files with 195 additions and 3 deletions

View File

@ -116,3 +116,30 @@ void KissCount::DeleteAccount(struct Account ac)
if (it->id == ac.id) if (it->id == ac.id)
_user->_accounts.erase(_user->_accounts.begin()+i); _user->_accounts.erase(_user->_accounts.begin()+i);
} }
void KissCount::AddCategory(wxString name, wxColour colour)
{
wxString color;
color = _("#") ;
color += wxString::Format(_("%02X"), colour.Red());
color += wxString::Format(_("%02X"), colour.Green());
color += wxString::Format(_("%02X"), colour.Blue());
_db->AddCategory(_user, name, color);
}
void KissCount::UpdateCategory(wxString oldName, wxString name, wxColour colour)
{
wxString color;
color = _("#") ;
color += wxString::Format(_("%02X"), colour.Red());
color += wxString::Format(_("%02X"), colour.Green());
color += wxString::Format(_("%02X"), colour.Blue());
_db->UpdateCategory(_user, oldName, name, color);
}
void KissCount::DeleteCategory(wxString name)
{
_db->DeleteCategory(_user, name);
}

View File

@ -18,17 +18,24 @@ class KissCount
std::list<wxString> GetUsers(); std::list<wxString> GetUsers();
bool IsValidUser(wxString user, wxString password); bool IsValidUser(wxString user, wxString password);
void LoadUser(wxString user); void LoadUser(wxString user);
void LoadYear(int year, bool force=false);
User* GetUser(); User* GetUser();
double GetAccountAmount(wxString id, int month, int year);
void UpdateOperation(struct operation op); void LoadYear(int year, bool force=false);
wxString AddOperation(struct operation op); wxString AddOperation(struct operation op);
void UpdateOperation(struct operation op);
void DeleteOperation(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 value); void SetAccountAmount(int month, int year, wxString accountId, double value);
void AddAccount(struct Account ac); void AddAccount(struct Account ac);
void UpdateAccount(struct Account ac); void UpdateAccount(struct Account ac);
void DeleteAccount(struct Account ac); void DeleteAccount(struct Account ac);
void AddCategory(wxString name, wxColour colour);
void UpdateCategory(wxString oldName, wxString name, wxColour colour);
void DeleteCategory(wxString name);
private: private:
wxUI* _wxUI; wxUI* _wxUI;
Database* _db; Database* _db;

View File

@ -436,3 +436,80 @@ void Database::DeleteAccount(struct Account ac)
EXECUTE_SQL_UPDATE(req, ); EXECUTE_SQL_UPDATE(req, );
} }
void Database::AddCategory(User* user, wxString name, wxString color)
{
wxString req;
req = _("INSERT INTO preference ('user', 'type', 'name', 'value') VALUES ('") ;
req += user->_id + _("'");
req += _(", 'category'");
req += _(", 'name'");
req += _(", '") + name + _("'");
req += _(")");
EXECUTE_SQL_UPDATE(req, );
req = _("INSERT INTO preference ('user', 'type', 'name', 'value') VALUES ('") ;
req += user->_id + _("'");
req += _(", 'category_color'");
req += _(", '") + name + _("'");
req += _(", '") + color + _("'");
req += _(")");
EXECUTE_SQL_UPDATE(req, );
}
void Database::UpdateCategory(User* user, wxString oldName, wxString name, wxString color)
{
wxString req;
if (oldName != name)
{
req = _("UPDATE preference SET ") ;
req += _("value='") + name + _("'");
req += _(" WHERE user='") + user->_id + _("'");
req += _(" AND type='category'");
req += _(" AND name='name'");
req += _(" AND value='") + oldName + _("'");
EXECUTE_SQL_UPDATE(req, );
}
req = _("UPDATE preference SET ") ;
req += _("value='") + color + _("'");
req += _(" WHERE user='") + user->_id + _("'");
req += _(" AND type='category_color'");
req += _(" AND name='") + oldName + _("'");
EXECUTE_SQL_UPDATE(req, );
if (oldName != name)
{
req = _("UPDATE preference SET ") ;
req += _("name='") + name + _("'");
req += _(" WHERE user='") + user->_id + _("'");
req += _(" AND type='category_color'");
req += _(" AND name='") + oldName + _("'");
req += _(" AND value='") + color + _("'");
EXECUTE_SQL_UPDATE(req, );
}
}
void Database::DeleteCategory(User* user, wxString name)
{
wxString req;
req = _("DELETE FROM preference WHERE user='") + user->_id + _("'");
req += _(" AND type='category'");
req += _(" AND name='name'");
req += _(" AND value='") + name + _("'");
EXECUTE_SQL_UPDATE(req, );
req = _("DELETE FROM preference WHERE user='") + user->_id + _("'");
req += _(" AND type='category_color'");
req += _(" AND name='") + name + _("'");
EXECUTE_SQL_UPDATE(req, );
}

View File

@ -34,6 +34,10 @@ class Database
void UpdateAccount(struct Account ac); void UpdateAccount(struct Account ac);
void DeleteAccount(struct Account ac); void DeleteAccount(struct Account ac);
void AddCategory(User* user, wxString name, wxString color);
void UpdateCategory(User* user, wxString oldName, wxString name, wxString color);
void DeleteCategory(User* user, wxString name);
private: private:
wxSQLite3Database _db; wxSQLite3Database _db;

View File

@ -158,11 +158,17 @@ void PreferencesPanel::InitCategories(User* user)
_categoriesGrid->SetCellAlignment(curLine, CATEGORY_COLOR, wxALIGN_CENTRE, wxALIGN_CENTRE); _categoriesGrid->SetCellAlignment(curLine, CATEGORY_COLOR, wxALIGN_CENTRE, wxALIGN_CENTRE);
_categoriesGrid->SetCellAlignment(curLine, CATEGORY_FONT, wxALIGN_CENTRE, wxALIGN_CENTRE); _categoriesGrid->SetCellAlignment(curLine, CATEGORY_FONT, wxALIGN_CENTRE, wxALIGN_CENTRE);
_categoriesGrid->SetCellAlignment(curLine, CATEGORY_DELETE, wxALIGN_CENTRE, wxALIGN_CENTRE); _categoriesGrid->SetCellAlignment(curLine, CATEGORY_DELETE, wxALIGN_CENTRE, wxALIGN_CENTRE);
_categoriesIndexes[curLine] = it->second;
} }
_categoriesGrid->SetReadOnly(0, CATEGORY_DELETE, true); _categoriesGrid->SetReadOnly(0, CATEGORY_DELETE, true);
_categoriesGrid->AutoSizeColumns(true); _categoriesGrid->AutoSizeColumns(true);
_categoriesGrid->AppendRows(); _categoriesGrid->AppendRows();
_categoriesGrid->SetReadOnly(curLine, CATEGORY_COLOR, true);
_categoriesGrid->SetReadOnly(curLine, CATEGORY_FONT, true);
_categoriesGrid->SetReadOnly(curLine, CATEGORY_DELETE, true);
SET_ROW_COLOR(curLine, OWN_GREEN); SET_ROW_COLOR(curLine, OWN_GREEN);
} }
@ -316,4 +322,75 @@ void PreferencesPanel::OnAccountModified(wxGridEvent& event)
void PreferencesPanel::OnCategoryModified(wxGridEvent& event) void PreferencesPanel::OnCategoryModified(wxGridEvent& event)
{ {
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;
} }