diff --git a/src/view/AccountPanel.cpp b/src/view/AccountPanel.cpp new file mode 100644 index 0000000..0235f35 --- /dev/null +++ b/src/view/AccountPanel.cpp @@ -0,0 +1,1112 @@ +/* + Copyright 2010-2011 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 "grid/wxGridCellBitmapRenderer.hpp" +// #include "GenerateDialog.hpp" + +#include "AccountPanel.hpp" + +enum {ACCOUNT_NUMBER, ACCOUNT_NAME, ACCOUNT_INIT, ACCOUNT_CUR, ACCOUNT_FINAL, NUMBER_COLS_ACCOUNTS}; +enum {CUR_CREDIT, CUR_DEBIT, TOTAL_CREDIT, TOTAL_DEBIT, BALANCE, STATS_ROW, CATS_STATS, NON_FIX}; + +enum {VIRTUAL_MODE=0, REAL_MODE, CHECK_MODE}; + +AccountPanel::AccountPanel(KissCount* kiss, wxUI *parent) : KissPanel(kiss, parent), _curMonth(-1), _curYear(-1) //, _tree(this, CALENDAR_TREE_ID, wxDefaultPosition, wxDefaultSize, wxTR_HIDE_ROOT) +{ + // wxBoxSizer *hbox = new wxBoxSizer(wxHORIZONTAL); + // wxBoxSizer *hbox2 = new wxBoxSizer(wxHORIZONTAL); + // wxBoxSizer *vbox = new wxBoxSizer(wxVERTICAL); + // wxBoxSizer *vbox2 = new wxBoxSizer(wxVERTICAL); + // wxBoxSizer *vbox3 = new wxBoxSizer(wxVERTICAL); + // wxChartPanel* chart ; + // int i ; + // User* user = _kiss->GetUser(); + // std::vector::iterator accountIt; + // std::vector::iterator categoryIt; + // DEFAULT_FONT(font); + // wxRect rect = wxDisplay().GetGeometry(); + // int nbCategories; + + // SetSizer(hbox); + + _categoriesValues=0; + _categories=0; + _accounts=0; + + // ColorScheme* colorScheme = new ColorScheme(wxUI::categoryColors, WXSIZEOF(wxUI::categoryColors)); + + // _pie = new PiePlot(); + // _calendar = new wxCalendarCtrl(this, CALENDAR_ID, wxDefaultDateTime, wxDefaultPosition, wxDefaultSize, + // wxCAL_MONDAY_FIRST | wxCAL_NO_MONTH_CHANGE | wxCAL_SEQUENTIAL_MONTH_SELECTION); + // _calendar->EnableMonthChange(false); + // _calendar->EnableYearChange(false); + // _calendar->EnableHolidayDisplay(false); + // _calendar->Enable(false); + + // _accounts = new wxString[user->GetAccountsNumber()]; + // for (i=0, + // accountIt = user->_accounts.begin(); + // accountIt != user->_accounts.end(); + // accountIt++, i++) + // _accounts[i] = accountIt->name; + + // _categories = new wxString[user->GetCategoriesNumber()] ; + // for(i=0, categoryIt = user->_categories.begin(); + // categoryIt != user->_categories.end(); + // categoryIt++, i++) + // { + // _categories[i] = wxGetTranslation(categoryIt->name) ; + // _categoriesIndexes[categoryIt->name] = i; + // } + + // nbCategories = (user->GetCategoriesNumber() <= wxUI::MAX_CATEGORY) ? user->GetCategoriesNumber() : wxUI::MAX_CATEGORY; + + // _categoriesValues = new double[user->GetCategoriesNumber()]; + // for(i=0; iGetCategoriesNumber(); i++) + // _categoriesValues[i] = 0.0; + + // _dataset = new CategorySimpleDataset(_categories, nbCategories); + // _dataset->AddSerie(_("Serie 1"), _categoriesValues, nbCategories); + + // _dataset->SetRenderer(new CategoryRenderer(*colorScheme)); + // _pie->SetDataset(_dataset); + // _pie->SetColorScheme(colorScheme); + + // _pie->SetLegend(new Legend(wxBOTTOM, wxCENTER)); + + // _grid = new GridAccount(_kiss, this, OPS_GRID_ID, true, true, true); + + // _accountsGrid = new wxGrid(this, ACCOUNTS_GRID_ID); + // _accountsGrid->CreateGrid(0, NUMBER_COLS_ACCOUNTS); + // _accountsGrid->SetRowLabelSize(0); + + // _accountsGrid->SetDefaultCellFont(font); + + // _accountsGrid->SetColLabelValue(ACCOUNT_NUMBER, _("Account number")); + // _accountsGrid->SetColLabelValue(ACCOUNT_NAME, _("Account name")); + // _accountsGrid->SetColLabelValue(ACCOUNT_INIT, _("Initial value")); + // _accountsGrid->SetColLabelValue(ACCOUNT_CUR, _("Current value")); + // _accountsGrid->SetColLabelValue(ACCOUNT_FINAL, _("Final value")); + + // _accountsGrid->AutoSizeColumns(true); + + // _statsGrid = new wxGrid(this, wxID_ANY); + + // chart = new wxChartPanel(this); + // chart->SetChart(new Chart(_pie, _("Cost repartition"))); + // chart->Fit(); + // chart->Layout(); + // chart->SetMinSize(// chart->GetSize() + // wxSize(200,300)); + + // wxString modes[3] = {_("Virtual"), _("Real"), _("Check")}; + // _radioMode = new wxRadioBox(this, DISPLAY_MODE_ID, _("Mode"), wxDefaultPosition, wxDefaultSize, + // 3, modes); + + // _tree.SetIndent(5); + + // wxButton* buttonGroup = new wxButton(this, GROUP_ID, _("Group")); + // wxButton* buttonUnGroup = new wxButton(this, UNGROUP_ID, _("UnGroup")); + // wxButton* buttonUpdateNextMonths = new wxButton(this, UPDATE_NEXT_MONTHS_ID, _("Update next months")); + + // vbox3->Add(&_tree, 0, wxGROW|wxALL, 2); + // vbox3->Add(buttonUpdateNextMonths, 0, wxALIGN_CENTER_HORIZONTAL|wxALL, 10); + // vbox3->Add(buttonGroup, 0, wxALIGN_CENTER_HORIZONTAL|wxALL, 10); + // vbox3->Add(buttonUnGroup, 0, wxALIGN_CENTER_HORIZONTAL|wxALL, 10); + // vbox3->Add(_radioMode, 0, wxALIGN_CENTER_HORIZONTAL|wxUP, 50); + + // hbox->Add(vbox3, 0, wxALL, 2); + // // hbox->Add(vbox3, 0, wxGROW|wxALL, 2); + // hbox2->Add(_accountsGrid, 0, wxGROW|wxALL, 2); + // hbox2->Add(_calendar, 0, wxALL, 2); + // vbox2->Add(hbox2, 0); + // vbox2->Add(_grid, 0, wxGROW|wxALL, 2); + // hbox->Add(vbox2, 0, wxGROW|wxALL, 2); + // vbox->Add(_statsGrid, 0, wxGROW); + // vbox->Add(chart, 0, wxALIGN_CENTER_HORIZONTAL|wxUP, 10); + // hbox->Add(vbox, 0, wxGROW|wxALL, 2); + + // ChangeUser(); + + // Fit(); + + // SetMinSize(wxSize(rect.width-rect.x-15, rect.height-rect.y-128-25)); + // SetMaxSize(wxSize(rect.width-rect.x-15, rect.height-rect.y-128-25)); + // SetScrollbars(10, 10, 100/10, 100/10); +} + +AccountPanel::~AccountPanel() +{ + delete[] _categoriesValues; + delete[] _categories; + delete[] _accounts; +} + +KissPanel* AccountPanel::CreatePanel() +{ + return new AccountPanel(_kiss, _wxUI); +} + +QPushButton* AccountPanel::GetButton() +{ + if (!_KissButton) + { + _KissButton = new QPushButton(QIcon(ACCOUNT_ICON), "", this); + _KissButton->setFixedSize(128, 128); + _KissButton->setIconSize(QSize(128, 128)); + } + + return _KissButton; +} + +QString AccountPanel::GetToolTip() +{ + return _("Operations"); +} + +void AccountPanel::InitStatsGrid(User* user) +{ + // int i; + // int nb_categories = user->GetCategoriesNumber(); + // DEFAULT_FONT(font); + + // if (!_statsGrid->GetNumberRows()) + // { + // _statsGrid->CreateGrid(nb_categories+CATS_STATS+1, 2); // Headers + blank + categories + non fix + // _statsGrid->SetColLabelSize(0); + // _statsGrid->SetRowLabelSize(0); + // _statsGrid->EnableEditing(false); + // } + // else + // { + // _statsGrid->DeleteRows(0, _statsGrid->GetNumberRows()); + // _statsGrid->InsertRows(0, nb_categories+CATS_STATS); + // } + + // _statsGrid->SetDefaultCellFont(font); + + // _statsGrid->SetCellValue(TOTAL_CREDIT, 0, _("Total Credit")); + // _statsGrid->SetCellValue(TOTAL_DEBIT, 0, _("Total Debit")); + + // for(i=0; iSetCellValue(CATS_STATS+i+1, 0, _categories[i]); + // _statsGrid->SetCellAlignment(CATS_STATS+i+1, 1, wxALIGN_RIGHT, wxALIGN_CENTRE); + // } + // else + // { + // _statsGrid->SetCellValue(CATS_STATS+i, 0, _categories[i]); + // _statsGrid->SetCellAlignment(CATS_STATS+i, 1, wxALIGN_RIGHT, wxALIGN_CENTRE); + // } + // } + + // _statsGrid->AutoSizeColumn(0, false); + // font.SetWeight(wxFONTWEIGHT_BOLD); + // _statsGrid->SetCellFont(CUR_CREDIT, 0, font); + // _statsGrid->SetCellFont(CUR_DEBIT, 0, font); + // _statsGrid->SetCellFont(BALANCE, 0, font); + // _statsGrid->SetCellFont(BALANCE, 1, font); + + // _statsGrid->SetCellValue(CUR_CREDIT, 0, _("Cur Credit")); + // _statsGrid->SetCellValue(CUR_DEBIT, 0, _("Cur Debit")); + // _statsGrid->SetCellValue(BALANCE, 0, _("Balance")); + // _statsGrid->SetCellValue(NON_FIX, 0, _("Non fix")); + + // _statsGrid->SetCellAlignment(CUR_DEBIT, 1, wxALIGN_RIGHT, wxALIGN_CENTRE); + // _statsGrid->SetCellAlignment(CUR_CREDIT, 1, wxALIGN_RIGHT, wxALIGN_CENTRE); + // _statsGrid->SetCellAlignment(TOTAL_DEBIT, 1, wxALIGN_RIGHT, wxALIGN_CENTRE); + // _statsGrid->SetCellAlignment(TOTAL_CREDIT, 1, wxALIGN_RIGHT, wxALIGN_CENTRE); + // _statsGrid->SetCellAlignment(BALANCE, 1, wxALIGN_RIGHT, wxALIGN_CENTRE); + // _statsGrid->SetCellAlignment(NON_FIX, 1, wxALIGN_RIGHT, wxALIGN_CENTRE); +} + +void AccountPanel::ChangeUser() +{ + // User* user = _kiss->GetUser(); + // int curYear = -1; + // wxDateTime curDate; + // wxTreeItemId rootNode, curNode; + // std::map > ops; + // std::map >::iterator it; + + // ops = _kiss->GetAllOperations(); + + // InitStatsGrid(user); + + // _tree.DeleteAllItems(); + // rootNode = _tree.AddRoot(wxT("")); + + // curDate.SetToCurrent(); + // if (ops.size()) + // { + // for(it = ops.begin(); it != ops.end(); it++) + // { + // if ((int)it->first <= curDate.GetYear()) + // { + // curYear = it->first; + // curNode = _tree.AppendItem(rootNode, wxString::Format(wxT("%d"), it->first)); + // } + // else + // _tree.AppendItem(rootNode, wxString::Format(wxT("%d"), it->first)); + // } + // Fit(); + // if (curYear != -1) + // { + // _tree.SelectItem(curNode, true); + // LoadYear(curYear); + // } + // } + // else + // { + // curNode = _tree.AppendItem(rootNode, wxString::Format(wxT("%d"), curDate.GetYear())); + // _tree.AppendItem(curNode, wxUI::months[(int)curDate.GetYear()]); + // Fit(); + // } +} + +void AccountPanel::LoadYear(int year, bool showMonth) +{ + // User* user = _kiss->GetUser(); + // int curMonth = -1; + // wxDateTime curDate; + // wxTreeItemId parentNode, curMonthNode; + // std::map > ops ; + // std::vector::iterator it; + + // if (user->_operations[year] && _tree.GetChildrenCount(_tree.GetSelection(), true)) + // { + // if (showMonth) + // ShowMonth(-1, year); + // return; + // } + + // _curYear = year ; + // _kiss->LoadYear(year); + // ops = _kiss->GetAllOperations(); + + // curDate.SetToCurrent(); + // parentNode = _tree.GetSelection(); + + // for (it = ops[year].begin(); it != ops[year].end(); it++) + // { + // if (curMonth == -1 || (year == curDate.GetYear() && *it <= curDate.GetMonth())) + // { + // curMonth = *it; + // curMonthNode = _tree.AppendItem(parentNode, wxUI::months[*it]); + // } + // else + // _tree.AppendItem(parentNode, wxUI::months[*it]); + // } + + // _tree.Expand(parentNode); + // Fit(); + // if (showMonth) + // { + // _tree.SelectItem(curMonthNode, true); + // ShowMonth(curMonth, year); + // } + + // _wxUI->Layout(); +} + +void AccountPanel::ShowMonth(int month, int year) +{ + // std::vector operations; + // _fixCosts = 0; + // User* user = _kiss->GetUser(); + // DEFAULT_FONT(font); + // std::vector::iterator categoryIt; + // std::map >::iterator monthIt; + // wxDateTime curDate; + // curDate.SetToCurrent(); + + // if (month == -1) + // { + // // Near month + // if (year == curDate.GetYear()) + // { + // for (monthIt = user->_operations[year]->begin(); monthIt != user->_operations[year]->end(); monthIt++) + // { + // if ((int)monthIt->first <= curDate.GetMonth()) + // { + // month = monthIt->first; + // } + // } + // } + + // // First month + // if (month == -1) + // { + // monthIt = user->_operations[year]->begin(); + // if (user->_operations[year]->size() == 0 && year == curDate.GetYear()) + // month = curDate.GetMonth(); + // else + // month = monthIt->first; + // } + // } + + // _curYear = year; + // _curMonth = month; + // _wxUI->SetTitle(user->_name + wxT(" - ") + wxUI::months[month] + wxT(" ") + wxString::Format(wxT("%d"), year)); + // _calendar->Enable(true); + + // if (_grid->GetNumberRows() > 1) + // _grid->DeleteRows(1, _grid->GetNumberRows()-1); + + // // Operations are ordered + // _curOperations = &((*user->_operations[year])[month]); + + // _grid->LoadOperations(_curOperations, _curMonth, _curYear); + + // InitAccountsGrid(user, month, year); + + // _calendar->EnableMonthChange(true); + // _calendar->EnableYearChange(true); + // if (curDate.GetMonth() == month && curDate.GetYear() == year) + // _calendar->SetDate(curDate) ; + // else if (curDate.GetMonth() > month || curDate.GetYear() > year) + // _calendar->SetDate(curDate.GetLastMonthDay((wxDateTime::Month)month, year)); + // else if (curDate.GetMonth() < month || curDate.GetYear() < year) + // _calendar->SetDate(wxDateTime(1, (wxDateTime::Month)month, year)); + + // _calendar->EnableMonthChange(false); + // _calendar->EnableYearChange(false); + // _calendar->SetSize(_calendar->GetMinSize()); + + // UpdateStats(); + + // Fit(); + // // SetMinSize(GetSize()); +} + +void AccountPanel::InitAccountsGrid(User* user, int month, int year) +{ + // std::vector::iterator it; + // int curLine = 0; + // double value; + // int i, a; + // DEFAULT_FONT(font); + + // if (_accountsGrid->GetNumberRows()) + // _accountsGrid->DeleteRows(0, _accountsGrid->GetNumberRows()); + + // font.SetWeight(wxFONTWEIGHT_BOLD); + + // for (i=0, it = user->_accounts.begin(); it != user->_accounts.end(); i++, it++, curLine++) + // { + // _accountsGrid->AppendRows(); + + // if (it->shared) + // _accountsGrid->SetCellValue(curLine, ACCOUNT_NUMBER, it->number + wxT("*")); + // else + // _accountsGrid->SetCellValue(curLine, ACCOUNT_NUMBER, it->number); + + // _accountsGrid->SetCellValue(curLine, ACCOUNT_NAME, it->name); + // value = _kiss->GetAccountAmount(it->id, month, year); + // _accountsGrid->SetCellEditor(curLine, ACCOUNT_INIT, new wxGridCellFloatEditor(-1, 2)); + // _accountsGrid->SetCellValue(curLine, ACCOUNT_INIT, wxString::Format(wxT("%.2lf"), value)); + // for (a=0; aSetReadOnly(curLine, a, a != ACCOUNT_INIT); + // _accountsGrid->SetCellFont(curLine, ACCOUNT_CUR, font); + + // _accountsInitValues[it->id] = value; + // _accountsGrid->SetCellAlignment(curLine, ACCOUNT_INIT, wxALIGN_RIGHT, wxALIGN_CENTRE); + // _accountsGrid->SetCellAlignment(curLine, ACCOUNT_CUR, wxALIGN_RIGHT, wxALIGN_CENTRE); + // _accountsGrid->SetCellAlignment(curLine, ACCOUNT_FINAL, wxALIGN_RIGHT, wxALIGN_CENTRE); + // } + + // _accountsGrid->AutoSizeColumns(true); +} + +void AccountPanel::UpdateStats() +{ + // int i; + // User* user = _kiss->GetUser(); + // std::vector::iterator it; + // double curCredit, curDebit, totalCredit, totalDebit, balance, value, value2, percents; + // std::map curAccountAmount, finalAccountAmount; + // std::map::iterator doubleIt; + // std::map::iterator intIt; + // std::vector::iterator accountIt; + // unsigned int day; + // int mode; + // std::map* notChecked = 0; + // std::map* virtuals = 0; + // Account account; + // Operation op; + // bool blocked_account ; + + // mode = _radioMode->GetSelection(); + + // curCredit = curDebit = totalCredit = totalDebit = percents = 0.0; + + // if (mode == CHECK_MODE) + // notChecked = _kiss->GetNotChecked(_curMonth, _curYear); + + // if (mode == REAL_MODE || mode == CHECK_MODE) + // virtuals = _kiss->GetVirtualAmount(_curMonth, _curYear); + + // day = _calendar->GetDate().GetDay()-1; + + // for (i=0; iGetCategoriesNumber(); i++) + // _categoriesValues[i] = 0.0; + + // for (doubleIt=_accountsInitValues.begin(); doubleIt!=_accountsInitValues.end(); doubleIt++) + // { + // curAccountAmount[doubleIt->first] = _accountsInitValues[doubleIt->first]; + // finalAccountAmount[doubleIt->first] = _accountsInitValues[doubleIt->first]; + + // if (mode == REAL_MODE || mode == CHECK_MODE) + // { + // curAccountAmount[doubleIt->first] += -(*virtuals)[doubleIt->first]; + // finalAccountAmount[doubleIt->first] += -(*virtuals)[doubleIt->first]; + // } + // } + + // for (it=_curOperations->begin(); it!=_curOperations->end(); it++) + // { + // if (it->meta) continue; + + // op = *it; + // blocked_account = false; + + // // A credit on a blocked account must be considered as a debit + // if (op.transfert.Length() && op.amount > 0) + // { + // account = user->GetAccount(op.account); + + // if (account.blocked) + // { + // op.amount = -op.amount; + // op.transfert = wxT(""); + // blocked_account = true; + // } + // } + + + // switch(mode) + // { + // case VIRTUAL_MODE: + // if (op.amount >= 0) + // { + // if (!op.transfert.Length()) + // totalCredit += op.amount; + + // if (day >= op.day) + // { + // if (!op.transfert.Length()) + // curCredit += op.amount; + // curAccountAmount[op.account] += op.amount; + // } + // finalAccountAmount[op.account] += op.amount; + // } + // else + // { + // if (!op.transfert.Length() && user->GetCategoryName(op.category) != _("Unknown")) + // _categoriesValues[_categoriesIndexes[user->GetCategoryName(op.category)]] += -op.amount ; + + // if (!op.transfert.Length()) + // totalDebit += -op.amount; + + // if (blocked_account) + // op.amount = -op.amount; + + // if (day >= op.day) + // { + // if (!op.transfert.Length()) + // curDebit += -op.amount; + // curAccountAmount[op.account] += op.amount; + // } + // finalAccountAmount[op.account] += op.amount; + // } + // break; + // case REAL_MODE: + // if (op.amount >= 0) + // { + // if (!op.transfert.Length()) + // totalCredit += op.amount; + + // if (day >= op.day) + // { + // if (!op.transfert.Length()) + // curCredit += op.amount; + // if (!op._virtual) + // curAccountAmount[op.account] += op.amount; + // } + // if (!op._virtual) + // finalAccountAmount[op.account] += op.amount; + // } + // else + // { + // if (!op.transfert.Length() && user->GetCategoryName(op.category) != _("Unknown")) + // _categoriesValues[_categoriesIndexes[user->GetCategoryName(op.category)]] += -op.amount ; + + // if (!op.transfert.Length() && !op._virtual) + // totalDebit += -op.amount; + + // if (blocked_account) + // op.amount = -op.amount; + + // if (day >= op.day) + // { + // if (!op.transfert.Length() && !op._virtual) + // curDebit += -op.amount; + // if (!op._virtual) + // curAccountAmount[op.account] += op.amount; + // } + // if (!op._virtual) + // finalAccountAmount[op.account] += op.amount; + // } + // break; + // case CHECK_MODE: + // if (op.amount >= 0) + // { + // if (!op.transfert.Length() && !op._virtual) + // totalCredit += op.amount; + + // if (day >= op.day) + // { + // if (!op.transfert.Length() && !op._virtual) + // curCredit += op.amount; + // if (op.checked && !op._virtual) + // curAccountAmount[op.account] += op.amount; + // } + // if (op.checked && !op._virtual) + // finalAccountAmount[op.account] += op.amount; + // } + // else + // { + // if (!op.transfert.Length() && user->GetCategoryName(op.category) != _("Unknown")) + // _categoriesValues[_categoriesIndexes[user->GetCategoryName(op.category)]] += -op.amount ; + + // if (!op.transfert.Length() && !op._virtual) + // totalDebit += -op.amount; + + // if (blocked_account) + // op.amount = -op.amount; + + // if (day >= op.day) + // { + // if (!op.transfert.Length() && !op._virtual) + // curDebit += -op.amount; + // if (op.checked && !op._virtual) + // curAccountAmount[op.account] += op.amount; + // } + // if (op.checked && !op._virtual) + // finalAccountAmount[op.account] += op.amount; + // } + // break; + // } + // } + + // balance = totalCredit - totalDebit; + // _statsGrid->SetCellValue(CUR_CREDIT, 1, wxString::Format(wxT("%.2lf"), curCredit)); + // _statsGrid->SetCellValue(CUR_DEBIT, 1, wxString::Format(wxT("%.2lf"), curDebit)); + // _statsGrid->SetCellValue(TOTAL_CREDIT, 1, wxString::Format(wxT("%.2lf"), totalCredit)); + // _statsGrid->SetCellValue(TOTAL_DEBIT, 1, wxString::Format(wxT("%.2lf"), totalDebit)); + + // _statsGrid->SetCellTextColour(BALANCE, 1, (balance >= 0) ? wxColor(0x00, 0xFF, 0x00) : wxColor(0xFF, 0x00, 0x00)); + // _statsGrid->SetCellValue(BALANCE, 1, wxString::Format(wxT("%.2lf"), balance)); + + // for(i=0; iGetCategoriesNumber()+1; i++) + // { + // if (totalDebit != 0) + // percents = ((double) (_categoriesValues[i]*100))/totalDebit; + // else + // percents = 0.0; + // if (i) + // _statsGrid->SetCellValue(CATS_STATS+i+1, 1, wxString::Format(wxT("%.2lf (%02d %%)"), _categoriesValues[i], (int)percents)); + // else + // _statsGrid->SetCellValue(CATS_STATS+i, 1, wxString::Format(wxT("%.2lf (%02d %%)"), _categoriesValues[i], (int)percents)); + // } + + // value = totalDebit - _categoriesValues[0]; + // if (totalDebit != 0) + // percents = ((double) (value*100))/totalDebit; + // else + // percents = 0.0; + // _statsGrid->SetCellValue(NON_FIX, 1, wxString::Format(wxT("%.2lf (%02d %%)"), value, (int)percents)); + + // for (i=0, accountIt=user->_accounts.begin(); accountIt!=user->_accounts.end(); accountIt++, i++) + // { + // if (mode != CHECK_MODE) + // { + // value = _accountsInitValues[accountIt->id]; + // if (mode == REAL_MODE) + // value -= (*virtuals)[accountIt->id]; + // _accountsGrid->SetCellValue(i, ACCOUNT_INIT, wxString::Format(wxT("%.2lf"), value)); + // value = curAccountAmount[accountIt->id]; + // _accountsGrid->SetCellValue(i, ACCOUNT_CUR, wxString::Format(wxT("%.2lf"), value)); + // _accountsGrid->SetCellTextColour(i, ACCOUNT_CUR, (value >= 0.0) ? wxColor(0x00, 0x00, 0x00) : wxColor(0xFF, 0x00, 0x00)); + // value = finalAccountAmount[accountIt->id]; + // _accountsGrid->SetCellValue(i, ACCOUNT_FINAL, wxString::Format(wxT("%.2lf"), value)); + // } + // else + // { + // value = _accountsInitValues[accountIt->id] - (*virtuals)[accountIt->id]; + // value2 = (*notChecked)[accountIt->id]; + + // _accountsGrid->SetCellValue(i, ACCOUNT_INIT, wxString::Format(wxT("%.2lf (%.2lf)"), value, value-value2)); + // value = curAccountAmount[accountIt->id]; + // _accountsGrid->SetCellValue(i, ACCOUNT_CUR, wxString::Format(wxT("%.2lf (%.2lf)"), value, value-value2)); + // _accountsGrid->SetCellTextColour(i, ACCOUNT_CUR, (value >= 0.0) ? wxColor(0x00, 0x00, 0x00) : wxColor(0xFF, 0x00, 0x00)); + // value = finalAccountAmount[accountIt->id]; + // _accountsGrid->SetCellValue(i, ACCOUNT_FINAL, wxString::Format(wxT("%.2lf (%.2lf)"), value, value-value2)); + // } + // } + + // _accountsGrid->AutoSizeColumn(ACCOUNT_INIT, true); + // _accountsGrid->AutoSizeColumn(ACCOUNT_CUR, true); + // _accountsGrid->AutoSizeColumn(ACCOUNT_FINAL, true); + + // if (notChecked) delete notChecked; + // if (virtuals) delete virtuals; + + // _statsGrid->AutoSizeColumn(1, true); + + // _pie->DatasetChanged(_dataset); + + // Layout(); +} + +void AccountPanel::OnOperationModified() +{ + // UpdateStats(); + + // Fit(); +} + +void AccountPanel::OnAccountModified() +{ + // User* user = _kiss->GetUser(); + // int row = event.GetRow(); + // double amount; + // wxString id = user->GetAccountId(_accounts[row]); + + // static bool inModification = false; + + // if (inModification) return ; + + // inModification = true; + + // _accountsGrid->GetCellValue(row, event.GetCol()).ToDouble(&amount); + + // _kiss->SetAccountAmount(id, _curMonth, _curYear, amount); + // _accountsInitValues[id] = amount; + + // UpdateStats(); + + // inModification = false; +} + +void AccountPanel::OnTreeRightClick() +{ + // wxMenu menu(0); + + // menu.Append(MENU_GENERATE_ID, _("Generate month")); + // menu.AppendSeparator(); + // if (_tree.GetCount() > 1) + // menu.Append(MENU_DELETE_ID, _("Delete")); + + // PopupMenu(&menu, event.GetPoint()); +} + +void AccountPanel::OnTreeChange() +{ + // int month=-1, year; + // int i; + // wxString monthString; + // static bool inModification = false ; + + // if (inModification) return; + + // inModification = true; + + // monthString = _tree.GetItemText(event.GetItem()); + // for (i=0; i<12; i++) + // if (monthString == wxUI::months[i]) + // { + // month = i; + // break; + // } + + // if (month == -1) + // { + // year = wxAtoi(monthString); + + // // Error + // if (year == 0) + // { + // inModification = false; + // return; + // } + + // if (year == _curYear) + // { + // inModification = false; + // return; + // } + + // // _tree.CollapseAll(); + // // _tree.Expand(event.GetItem()); + // LoadYear(year, false); + // } + // else + // { + // year = wxAtoi(_tree.GetItemText(_tree.GetItemParent(event.GetItem()))); + + // // Error + // if (year == 0) + // { + // inModification = false; + // return; + // } + + // if (year != _curYear || month != _curMonth) + // { + // ShowMonth(month, year); + // } + // } + + // inModification = false; +} + +void AccountPanel::GetTreeSelection(int* month, int* year) +{ + // wxString monthString; + // int i; + + // *month = -1; *year = -1; + + // monthString = _tree.GetItemText(_tree.GetSelection()); + // for (i=0; i<12; i++) + // if (monthString == wxUI::months[i]) + // { + // *month = i; + // break; + // } + + // if (*month == -1) + // { + // *year = wxAtoi(monthString); + + // // Error + // if (year == 0) + // { + // *month = -1; + // *year = -1; + // return; + // } + // } + // else + // { + // *year = wxAtoi(_tree.GetItemText(_tree.GetItemParent(_tree.GetSelection()))); + + // // Error + // if (year == 0) + // { + // *month = -1; + // *year = -1; + // return; + // } + // } +} + +void AccountPanel::OnMenuGenerate() +{ + // int month, year; + // wxDateTime curDate; + + // curDate.SetToCurrent(); + + // GetTreeSelection(&month, &year); + + // if (month == -1 && year == curDate.GetYear()) + // { + // month = _curMonth; + // } + + // GenerateDialog g(_kiss, _wxUI, month, year); + // g.ShowModal(); +} + +void AccountPanel::OnMenuDelete() +{ + // int month, year; + // wxString message; + // wxTreeItemId curNode, node ; + // std::map > ops ; + + // GetTreeSelection(&month, &year); + + // ops = _kiss->GetAllOperations(); + + // if (ops.size() == 1 && (ops[year].size() == 1 || month == -1)) + // { + // wxMessageBox(_("It must be at least one month !"), _("Error"), wxICON_ERROR | wxOK); + // return; + // } + + // message = _("Are you sure want to delete "); + // if (month != -1) + // message += wxUI::months[month] + wxT(" "); + // message += wxString::Format(wxT("%d"), year); + + // message += _(" operations ?"); + + // wxMessageDialog dialog(_wxUI, message, wxT("KissCount"), wxYES_NO); + // if (dialog.ShowModal() == wxID_NO) + // return; + + // curNode = _tree.GetSelection(); + + // if (ops[year].size() == 1 && month != -1) + // curNode = _tree.GetItemParent(curNode); + + // _kiss->DeleteOperations(month, year); + + // node = _tree.GetNextSibling(curNode); + + // if (!node.IsOk()) + // node = _tree.GetPrevSibling(curNode); + + // _tree.Delete(curNode); + + // if (!node.IsOk()) + // ChangeUser(); + // else + // { + // _tree.SelectItem(node); + // GetTreeSelection(&month, &year); + // if (month == -1) + // month = ops[year][0]; + // ShowMonth(month, year); + // } + // _wxUI->NeedReload(); +} + +void AccountPanel::GenerateMonth(int month, int year) +{ + // wxTreeItemId root, years, node ; + // wxTreeItemIdValue cookie; + // wxString monthString, yearString; + // std::map > ops ; + // std::vector::iterator it ; + // int i; + + // root = _tree.GetRootItem(); + // yearString = wxString::Format(wxT("%d"), year); + // monthString = wxUI::months[month]; + + // ops = _kiss->GetAllOperations(); + + // if (_tree.GetChildrenCount(root, true) < 1) + // { + // node = _tree.AppendItem(root, yearString); + // node = _tree.AppendItem(node, monthString); + + // _tree.SelectItem(node, true); + // ShowMonth(month, year); + // return ; + // } + + // years = _tree.GetFirstChild(root, cookie); + // while (years.IsOk()) + // { + // if (_tree.GetItemText(years) == yearString) + // break; + // if (wxAtoi(_tree.GetItemText(years)) > year) + // { + // years = _tree.GetPrevSibling(years); + // if (!years.IsOk()) + // years = _tree.PrependItem(root, yearString); + // else + // years = _tree.InsertItem(root, years, yearString); + // break; + // } + // years = _tree.GetNextSibling(years); + // } + + // if (!years.IsOk()) + // { + // years = _tree.GetFirstChild(root, cookie); + // if (wxAtoi(_tree.GetItemText(years)) > year) + // years = _tree.PrependItem(root, yearString); + // else + // years = _tree.AppendItem(root, yearString); + // } + + // if (!_tree.GetChildrenCount(years, true)) + // node = _tree.AppendItem(years, monthString); + // else + // { + // for(i=0, it = ops[year].begin(); + // it != ops[year].end(); + // it++, i++) + // { + // if (*it > month) + // break; + // } + // if (it == ops[year].end()) + // years = _tree.AppendItem(years, monthString); + // else + // years = _tree.InsertItem(years, i-1, monthString); + // } + + // _tree.SelectItem(node, true); + // ShowMonth(month, year); + // _wxUI->NeedReload(); +} + +void AccountPanel::OnShow() +{ + // if (_curMonth != -1) + // _wxUI->SetTitle(_kiss->GetUser()->_name + wxT(" - ") + wxUI::months[_curMonth] + wxT(" ") + wxString::Format(wxT("%d"), _curYear)); + // else + // _wxUI->SetTitle(_kiss->GetUser()->_name); +} + +void AccountPanel::OnCalendarChange() +{ + UpdateStats(); +} + +void AccountPanel::OnModeChange() +{ + UpdateStats(); +} + +void AccountPanel::OnGroup() +{ + // _grid->Group(); +} + +void AccountPanel::OnUnGroup() +{ + // _grid->UnGroup(); +} + +void AccountPanel::OnUpdateNextMonths() +{ +// double* deltas, *cur_amounts, amount; +// int i, a; +// User* user = _kiss->GetUser(); +// bool had_values, accounts_updated = false; +// int last_month = 0, last_year = 0, account_updated = 0; +// std::map > operations; + +// deltas = new double[user->_accounts.size()] ; +// cur_amounts = new double[user->_accounts.size()] ; + +// operations = _kiss->GetAllOperations(); + +// if (_curMonth == 11) +// { +// last_month = 0; +// last_year = _curYear+1; +// } +// else +// { +// last_month = _curMonth+1; +// last_year = _curYear; +// } + +// for (i=0; i<(int)user->_accounts.size(); i++) +// { +// deltas[i] = _kiss->GetAccountAmount(user->_accounts[i].id, _curMonth, _curYear); +// cur_amounts[i] = deltas[i] += _kiss->CalcAccountAmount(user->_accounts[i].id, _curMonth, _curYear, &had_values); + +// for (a=0; a<(int)operations[last_year].size(); a++) +// if (operations[last_year][a] == last_month) break; + +// if (a == (int)operations[last_year].size()) +// { +// deltas[i] = 0; +// continue; +// } + +// amount = _kiss->GetAccountAmount(user->_accounts[i].id, last_month, last_year); + +// deltas[i] -= amount; + +// account_updated++; +// } + +// if (!account_updated) +// goto end; + +// last_month = _curMonth; +// last_year = _curYear; + +// while (1) +// { +// account_updated = 0; + +// if (last_month == 11) +// { +// last_month = 0; +// last_year = last_year+1; +// } +// else +// last_month++; + +// for (i=0; i<(int)user->_accounts.size(); i++) +// { +// if (deltas[i] == 0.0) continue; + +// amount = _kiss->GetAccountAmount(user->_accounts[i].id, last_month, last_year); +// if ((cur_amounts[i] - amount) != deltas[i]) continue; + +// cur_amounts[i] = amount + deltas[i]; +// _kiss->SetAccountAmount(user->_accounts[i].id, last_month, last_year, cur_amounts[i]); +// cur_amounts[i] += _kiss->CalcAccountAmount(user->_accounts[i].id, last_month, last_year, &had_values); + +// account_updated++; +// } + +// if (!account_updated) break; + +// accounts_updated = true; +// } + +// if (last_month == 0) +// { +// last_month = 11; +// last_year--; +// } +// else +// last_month--; + +// end: +// if (accounts_updated) +// { +// wxString message = _("Accounts updated until ") + wxUI::months[last_month]; + +// message += wxT(" ") + wxString::Format(wxT("%d"), last_year); + +// wxMessageBox(message, wxT("KissCount"), wxICON_INFORMATION | wxOK); +// } +// else +// wxMessageBox(_("Any account updated !"), wxT("KissCount"), wxICON_INFORMATION | wxOK); + +// delete[] deltas; +// delete[] cur_amounts; +} diff --git a/src/view/AccountPanel.hpp b/src/view/AccountPanel.hpp new file mode 100644 index 0000000..0c91fed --- /dev/null +++ b/src/view/AccountPanel.hpp @@ -0,0 +1,93 @@ +/* + Copyright 2010-2011 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 . +*/ + +#ifndef ACCOUNTPANEL_H +#define ACCOUNTPANEL_H + +// #include +// #include +// #include +// #include +// #include +// #include + +#include "view.hpp" + +#include +// #include "grid/GridAccount.hpp" +// #include "grid/CalendarEditor.hpp" +// #include + +class GridAccount; + +class AccountPanel: public KissPanel +{ + Q_OBJECT; + +public: + AccountPanel(KissCount* kiss, wxUI *parent); + ~AccountPanel(); + + KissPanel* CreatePanel(); + QPushButton* GetButton(); + QString GetToolTip(); + void OnShow(); + + void ChangeUser(); + void LoadYear(int year, bool showMonth=true); + void ShowMonth(int month, int year); + void GenerateMonth(int month, int year); + + void OnOperationModified(); + void OnAccountModified(); + void OnTreeRightClick(); + void OnTreeChange(); + void OnMenuGenerate(); + void OnMenuDelete(); + void OnCalendarChange(); + void OnModeChange(); + void OnGroup(); + void OnUnGroup(); + void OnUpdateNextMonths(); + + int _curMonth, _curYear; + +private: + // wxTreeCtrl _tree; + // wxCalendarCtrl* _calendar; + // GridAccount* _grid; + // wxGrid *_statsGrid, *_accountsGrid; + // PiePlot* _pie; + double *_categoriesValues; + // wxRadioBox *_radioMode; + std::map _categoriesIndexes; + std::vector* _curOperations; + QString* _categories, *_accounts; + std::map _accountsInitValues; + // CategorySimpleDataset* _dataset; + int _fixCosts; + + void InitStatsGrid(User* user); + void InitAccountsGrid(User* user, int month, int year); + void UpdateStats(); + void InsertOperation(User* user, Operation* op, int line, bool fix); + void GetTreeSelection(int* month, int* year); +}; + +#endif diff --git a/src/view/wxUI.cpp b/src/view/wxUI.cpp index c6f7409..d6db41f 100644 --- a/src/view/wxUI.cpp +++ b/src/view/wxUI.cpp @@ -19,8 +19,8 @@ #include -/*#include "AccountPanel.hpp" -#include "PreferencesPanel.hpp" +#include "AccountPanel.hpp" +/*#include "PreferencesPanel.hpp" #include "UsersDialog.hpp" #include "GenerateDialog.hpp" #include "SearchPanel.hpp" @@ -53,12 +53,14 @@ QColor wxUI::categoryColors[MAX_CATEGORY] = {QColor(0x00, 0x45, 0x86), QColor(0x00, 0x84, 0xD1)}; wxUI::wxUI(KissCount* kiss, const QString& title) - : QFrame(0), _kiss(kiss), + : QFrame(0), _kiss(kiss), _signalMapper(this), _curPanel(0), _locale(0), _needReload(false) { QPushButton* button; setWindowTitle(title); + + connect(&_signalMapper, SIGNAL(mapped(int)), this, SLOT(OnButtonClicked(int))); _vbox = new QVBoxLayout; _buttonsBox = new QHBoxLayout; @@ -87,10 +89,10 @@ wxUI::wxUI(KissCount* kiss, const QString& title) wxUI::~wxUI() { - //int i; + int i; - // for (i=0; i<(int)_panels.size(); i++) - // Disconnect(i, wxEVT_COMMAND_BUTTON_CLICKED, wxCommandEventHandler(wxUI::OnButtonClicked), _panels[i], this); + for (i=0; i<(int)_panels.size(); i++) + _buttons[i]->disconnect(&_signalMapper, SLOT(map())); if (_locale) delete _locale; } @@ -148,22 +150,25 @@ bool wxUI::SetLanguage(QString language) return res; } -#define ADD_PANEL(panelName, id) \ - panel = new panelName(_kiss, this); \ - button = panel->GetButton(id); \ - button->SetToolTip(panel->GetToolTip()); \ - _buttonsBox->Insert(id, button); \ - _buttons.insert(_buttons.begin()+id, button); \ - _panels.push_back(panel); \ - Connect(id, wxEVT_COMMAND_BUTTON_CLICKED, wxCommandEventHandler(wxUI::OnButtonClicked), panel, this); +#define ADD_PANEL(panelName) \ + panel = new panelName(_kiss, this); \ + button = panel->GetButton(); \ + button->setToolTip(panel->GetToolTip()); \ + _buttonsBox->insertWidget(id, button); \ + _buttons.insert(_buttons.begin()+id, button); \ + _panels.push_back(panel); \ + _signalMapper.setMapping(button, id); \ + connect(button, SIGNAL(clicked()), &_signalMapper, SLOT(map())); \ + id++; void wxUI::InitPanels() { - // KissPanel* panel; - // wxBitmapButton* button; - // _panels.clear(); + KissPanel* panel; + QPushButton* button; + _panels.clear(); + int id=0; - // ADD_PANEL(AccountPanel, 0); + ADD_PANEL(AccountPanel); // ADD_PANEL(StatsPanel, 1); // ADD_PANEL(SearchPanel, 2); // ADD_PANEL(PreferencesPanel, 3); @@ -173,29 +178,29 @@ void wxUI::InitPanels() void wxUI::LoadPanels() { - // std::vector::iterator it; - // KissPanel* temp; - // int i; + std::vector::iterator it; + KissPanel* temp; + int i; - // if (_curPanel) - // { - // _vbox->Detach(_curPanel); - // _curPanel = 0; - // } + if (_curPanel) + { + _vbox->removeWidget(_curPanel); + _curPanel = 0; + } - // if (_panels.size()) - // { - // for (i=0; i<(int)_panels.size(); i++) - // { - // temp = _panels[i]->CreatePanel(); - // Disconnect(i, wxEVT_COMMAND_BUTTON_CLICKED, wxCommandEventHandler(wxUI::OnButtonClicked), _panels[i], this); - // _panels[i] = temp; - // Connect(i, wxEVT_COMMAND_BUTTON_CLICKED, wxCommandEventHandler(wxUI::OnButtonClicked), temp, this); - // _buttons[i]->SetToolTip(temp->GetToolTip()); - // } - // } - // else - // InitPanels(); + if (_panels.size()) + { + for (i=0; i<(int)_panels.size(); i++) + { + temp = _panels[i]->CreatePanel(); + _buttons[i]->disconnect(&_signalMapper, SLOT(map())); + _panels[i] = temp; + connect(_buttons[i], SIGNAL(clicked()), &_signalMapper, SLOT(map())); + _buttons[i]->setToolTip(temp->GetToolTip()); + } + } + else + InitPanels(); } void wxUI::LoadUser() @@ -213,39 +218,38 @@ void wxUI::LoadUser() void wxUI::ShowPanel(KissPanel* panel) { - // wxShowEvent event; - // int i; - // User* user = _kiss->GetUser(); + int i; + User* user = _kiss->GetUser(); - // if (!panel) return; + if (!panel) return; - // if (_curPanel) - // { - // _vbox->Detach(_curPanel); - // _curPanel->Hide(); - // } + if (_curPanel) + { + _vbox->removeWidget(_curPanel); + _curPanel->hide(); + } - // if (_needReload) - // { - // user->InvalidateOperations(); - // for(i=0; i<(int)_panels.size(); i++) - // if (_panels[i] == panel) break; - // LoadPanels(); - // _needReload = false; - // _curPanel = _panels[i]; - // } - // else - // _curPanel = panel; + if (_needReload) + { + user->InvalidateOperations(); + for(i=0; i<(int)_panels.size(); i++) + if (_panels[i] == panel) break; + LoadPanels(); + _needReload = false; + _curPanel = _panels[i]; + } + else + _curPanel = panel; - // _curPanel->OnShow(event); - // _vbox->Add(_curPanel); - // _curPanel->Show(); - // Layout(); + _curPanel->OnShow(); + _vbox->addWidget(_curPanel); + _curPanel->show(); + layout(); } -void wxUI::OnButtonClicked() +void wxUI::OnButtonClicked(int id) { - // ShowPanel(_panels[event.GetId()]); + ShowPanel(_panels[id]); } void wxUI::OnButtonChangeUser() @@ -272,27 +276,30 @@ void wxUI::OnButtonQuit() void wxUI::GenerateMonth(int month, int year) { - // ((AccountPanel*)_panels[0])->GenerateMonth(month, year); + (dynamic_cast(_panels[0]))->GenerateMonth(month, year); } void wxUI::KillMe() { - // std::vector::iterator it; + std::vector::iterator it; - // if (_curPanel) - // { - // _vbox->Detach(_curPanel); - // _curPanel = 0; - // } + if (_curPanel) + { + _vbox->removeWidget(_curPanel); + _curPanel = 0; + } - // for (it=_panels.begin(); it!= _panels.end(); it++) - // { - // if (*it) delete *it; - // _buttonsBox->Remove(0); - // _buttons.erase(_buttons.begin()); - // } + for (it=_panels.begin(); it!= _panels.end(); it++) + { + if (*it) + { + _buttonsBox->removeWidget(*it); + delete *it; + } + _buttons.erase(_buttons.begin()); + } - // _panels.clear(); + _panels.clear(); } void wxUI::NeedReload() diff --git a/src/view/wxUI.hpp b/src/view/wxUI.hpp index 318b5a0..a4432d7 100644 --- a/src/view/wxUI.hpp +++ b/src/view/wxUI.hpp @@ -28,6 +28,7 @@ class ImportEngine; #include #include #include +#include #include @@ -71,13 +72,14 @@ public: int _language; private slots: - void OnButtonClicked(); + void OnButtonClicked(int id); void OnButtonChangeUser(); void OnButtonAbout(); void OnButtonQuit(); private: KissCount *_kiss; + QSignalMapper _signalMapper; QVBoxLayout *_vbox; QHBoxLayout *_buttonsBox; KissPanel *_curPanel; @@ -100,9 +102,9 @@ public: _KissButton(0) {hide();} - virtual void OnShow(QShowEvent * event)=0; + virtual void OnShow()=0; virtual KissPanel* CreatePanel()=0; - virtual QPushButton* GetButton(int id) {return 0;} + virtual QPushButton* GetButton() {return 0;} virtual QString GetToolTip() {return "";} protected: