KissCount/view/AccountPanel.cpp

307 lines
10 KiB
C++
Raw Normal View History

#include "AccountPanel.h"
2010-05-16 20:09:18 +02:00
static wxString colsName[] = {_("Description"), _("Date"), _("Debit"), _("Credit"), _("Category"), _("Account"), _("")};
2010-05-16 20:09:18 +02:00
AccountPanel::AccountPanel(KissCount* kiss, wxUI *parent) : wxPanel(&(*parent)), _kiss(kiss), _wxUI(parent), _tree(this, -1)
{
wxBoxSizer *hbox = new wxBoxSizer(wxHORIZONTAL);
wxChartPanel* chart ;
2010-05-27 21:09:02 +02:00
int i ;
User* user = _kiss->GetUser();
std::map<wxString, Account>::iterator accountIt;
std::map<wxString, wxString>::iterator it;
wxColour categoryColors[] = {wxColour(0x00, 0x45, 0x86),
wxColour(0xFF, 0x3E, 0x0E),
wxColour(0xFF, 0xD3, 0x20),
wxColour(0x58, 0x9D, 0x1B),
wxColour(0x7E, 0x00, 0x21),
wxColour(0x83, 0xCC, 0xFF),
wxColour(0x31, 0x40, 0x04),
wxColour(0xB0, 0xCF, 0x00),
wxColour(0x4B, 0x1F, 0x6F),
wxColour(0xFF, 0x93, 0x0E),
wxColour(0xC5, 0x00, 0x0D),
wxColour(0x00, 0x84, 0xD1)};
SetSizer(hbox);
2010-05-27 21:09:02 +02:00
ColorScheme* colorScheme = new ColorScheme(categoryColors, WXSIZEOF(categoryColors));
_pie = new PiePlot();
2010-05-27 21:09:02 +02:00
_accounts = new wxString[user->_accounts.size()+1];
_accounts[0] = _("Unknown");
for (i=1,
accountIt = user->_accounts.begin();
accountIt != user->_accounts.end();
accountIt++, i++)
_accounts[i] = user->_accounts[accountIt->first].name;
_categories = new wxString[user->GetCategoriesNumber()+1] ;
_categories[0] = _("Unknown");
_categoriesIndexes[_("Unknown")] = 0;
for(i=1, it = user->_preferences._categories.begin(); it != user->_preferences._categories.end(); it++, i++)
{
_categories[i] = it->second ;
_categoriesIndexes[it->second] = i;
}
_dataset = new CategorySimpleDataset(_categories, user->GetCategoriesNumber()+1);
_categoriesValues = new double[user->GetCategoriesNumber()+1];
for(i=0; i<user->GetCategoriesNumber()+1; i++)
_categoriesValues[i] = 1.0;
2010-05-24 20:14:15 +02:00
2010-05-27 21:09:02 +02:00
_dataset->AddSerie(_("Serie 1"), _categoriesValues, user->GetCategoriesNumber()+1);
_dataset->SetRenderer(new CategoryRenderer(*colorScheme));
_pie->SetDataset(_dataset);
_pie->SetColorScheme(colorScheme);
2010-05-24 20:14:15 +02:00
2010-05-27 21:09:02 +02:00
_pie->SetLegend(new Legend(wxBOTTOM, wxCENTER));
2010-05-24 20:14:15 +02:00
2010-05-16 20:09:18 +02:00
_grid = new GridAccount(this, -1);
_grid->CreateGrid(1, NUMBER_COLS);
_grid->SetColLabelSize(0);
_grid->SetRowLabelSize(0);
chart = new wxChartPanel(this);
chart->SetChart(new Chart(_pie, _("Cost repartition")));
2010-05-24 20:14:15 +02:00
chart->Fit();
chart->Layout();
chart->SetMinSize(// chart->GetSize()
2010-05-27 21:09:02 +02:00
wxSize(200,500));
hbox->Add(&_tree, 0);
2010-05-16 20:09:18 +02:00
hbox->Add(_grid, 0);
hbox->Add(chart, 0);
2010-05-16 10:35:34 +02:00
ChangeUser();
Fit();
SetMinSize(GetSize());
}
2010-05-16 10:35:34 +02:00
2010-05-27 21:09:02 +02:00
AccountPanel::~AccountPanel()
{
delete[] _categoriesValues;
delete[] _categories;
delete[] _accounts;
}
2010-05-16 10:35:34 +02:00
void AccountPanel::ChangeUser()
{
User* user = _kiss->GetUser();
2010-05-24 20:14:15 +02:00
int curYear = -1;
2010-05-16 10:35:34 +02:00
std::map<unsigned int, std::map<unsigned int, std::list<operation> >* >::iterator it;
wxDateTime curDate;
curDate.SetToCurrent();
for(it = user->_operations.begin(); it != user->_operations.end(); it++)
{
if ((int)it->first <= curDate.GetYear())
curYear = it->first;
_tree.AddRoot(wxString::Format(wxT("%d"), it->first));
}
2010-05-24 20:14:15 +02:00
if (curYear != -1)
LoadYear(curYear);
2010-05-16 10:35:34 +02:00
}
void AccountPanel::LoadYear(int year)
{
User* user = _kiss->GetUser();
int curMonth = -1;
std::map<unsigned int, std::list<operation> >::iterator it;
wxDateTime curDate;
wxTreeItemId parentNode, curMonthNode;
2010-05-27 21:09:02 +02:00
_curYear = year ;
if (user->_operations[year] != NULL)
2010-05-16 10:35:34 +02:00
{
ShowMonth(year, 0);
return;
}
_kiss->LoadYear(year);
curDate.SetToCurrent();
parentNode = _tree.GetSelection();
for (it = user->_operations[year]->begin(); it != user->_operations[year]->end(); it++)
{
if (year == curDate.GetYear() && (int)it->first <= curDate.GetMonth())
{
curMonth = it->first;
curMonthNode = _tree.AppendItem(parentNode, months[it->first]);
}
else if(curMonth == -1)
{
curMonthNode = _tree.AppendItem(parentNode, months[it->first]);
curMonth++;
}
else
_tree.AppendItem(parentNode, months[it->first]);
}
_tree.Expand(parentNode);
_tree.SelectItem(curMonthNode, true);
2010-05-27 21:09:02 +02:00
2010-05-16 10:35:34 +02:00
ShowMonth(year, curMonth);
}
2010-05-16 20:09:18 +02:00
#define SET_ROW_COLOR(row, color) for(int i=0; i<NUMBER_COLS; i++) \
{\
_grid->SetCellBackgroundColour(row, i, color);\
}
2010-05-16 10:35:34 +02:00
void AccountPanel::ShowMonth(int year, int month)
{
std::list<operation> operations;
2010-05-16 20:09:18 +02:00
std::list<operation>::iterator it;
int fixCosts = 0;
int curLine = 0;
2010-05-16 10:35:34 +02:00
User* user = _kiss->GetUser();
2010-05-16 20:09:18 +02:00
wxFont font(DEFAULT_FONT_SIZE, wxFONTFAMILY_DEFAULT, wxFONTSTYLE_NORMAL, wxFONTWEIGHT_NORMAL, false, DEFAULT_FONT);
2010-05-24 20:14:15 +02:00
std::map<wxString, wxString>::iterator categoryIt;
wxGridCellChoiceEditor* categoryEditor, *accountEditor;
2010-05-16 20:09:18 +02:00
int i;
2010-05-16 10:35:34 +02:00
2010-05-27 21:09:02 +02:00
_curMonth = month;
2010-05-16 10:35:34 +02:00
_wxUI->SetTitle(user->_name + _(" - ") + months[month]);
// Operations are ordered
2010-05-27 21:09:02 +02:00
_curOperations = &((*user->_operations[year])[month]);
2010-05-16 10:35:34 +02:00
2010-05-16 20:09:18 +02:00
_grid->CreateGrid(1, NUMBER_COLS);
2010-05-16 10:35:34 +02:00
// Creating headers
2010-05-16 20:09:18 +02:00
_grid->SetColSize (0, _grid->GetColSize(0)*3);
_grid->SetDefaultCellFont(font);
font.SetWeight(wxFONTWEIGHT_BOLD);
for(i=0; i<NUMBER_COLS; i++)
{
_grid->SetCellValue(0, i, colsName[i]);
_grid->SetCellBackgroundColour(0, i, OWN_CYAN);
_grid->SetCellFont(0, i, font);
_grid->SetReadOnly(0, i, true);
}
2010-05-16 10:35:34 +02:00
// SetCellBackgroundColour (int row, int col, const wxColour &colour);
// SetCellFont (int row, int col, const wxFont &font);
// SetCellValue (int row, int col, const wxString &s);
// GetColSize (int col) const ;
// SetColSize (int col, int width);
// AppendRows (int numRows=1, bool updateLabels=true);
// InsertRows (int pos=0, int numRows=1, bool updateLabels=true);
// SetReadOnly(row, col, bool)
2010-05-16 20:09:18 +02:00
2010-05-27 21:09:02 +02:00
it = _curOperations->begin();
2010-05-16 20:09:18 +02:00
/*
struct operation {
wxString id;
unsigned int day;
unsigned int month;
unsigned int year;
int amount;
wxString description;
wxString category;
bool fix_cost;
enum {DESCRIPTION, DATE, DEBIT, CREDIT, CATEGORY, ACCOUNT, VIEW, NUMBER_COLS};
} ;
*/
while (it->fix_cost)
{
_grid->AppendRows();
curLine++;
fixCosts++;
SET_ROW_COLOR(curLine, OWN_YELLOW);
_grid->SetCellValue(curLine, DESCRIPTION, it->description);
_grid->SetCellValue(curLine, DATE, wxString::Format(wxT("%02d/%02d/%d"), it->day+1, it->month+1, it->year));
if (it->amount < 0)
2010-05-27 21:09:02 +02:00
_grid->SetCellValue(curLine, DEBIT, wxString::Format(wxT("%.2lf"), -it->amount));
2010-05-16 20:09:18 +02:00
else
2010-05-27 21:09:02 +02:00
_grid->SetCellValue(curLine, CREDIT, wxString::Format(wxT("%.2lf"), it->amount));
_grid->SetCellEditor(curLine, DEBIT, new wxGridCellFloatEditor(-1, 2));
_grid->SetCellEditor(curLine, CREDIT, new wxGridCellFloatEditor(-1, 2));
categoryEditor = new wxGridCellChoiceEditor(user->GetCategoriesNumber()+1, _categories, false);
2010-05-16 20:09:18 +02:00
_grid->SetCellEditor(curLine, CATEGORY, categoryEditor);
2010-05-24 20:14:15 +02:00
_grid->SetCellValue(curLine, CATEGORY, user->GetCategoryName(it->category));
2010-05-27 21:09:02 +02:00
accountEditor = new wxGridCellChoiceEditor(user->_accounts.size()+1, _accounts, false);
2010-05-24 20:14:15 +02:00
_grid->SetCellEditor(curLine, ACCOUNT, accountEditor);
_grid->SetCellValue(curLine, ACCOUNT, user->GetAccountName(it->account));
2010-05-16 20:09:18 +02:00
it++;
}
_grid->AppendRows();
curLine++;
SET_ROW_COLOR(curLine, OWN_YELLOW);
2010-05-27 21:09:02 +02:00
categoryEditor = new wxGridCellChoiceEditor(user->_preferences._categories.size()+1, _categories, false);
2010-05-16 20:09:18 +02:00
_grid->SetCellEditor(curLine, CATEGORY, categoryEditor);
2010-05-27 21:09:02 +02:00
_grid->SetCellEditor(curLine, DEBIT, new wxGridCellFloatEditor(-1, 2));
_grid->SetCellEditor(curLine, CREDIT, new wxGridCellFloatEditor(-1, 2));
accountEditor = new wxGridCellChoiceEditor(user->_accounts.size()+1, _accounts, false);
2010-05-24 20:14:15 +02:00
_grid->SetCellEditor(curLine, ACCOUNT, accountEditor);
2010-05-16 20:09:18 +02:00
_grid->_fixCosts = ++fixCosts;
2010-05-27 21:09:02 +02:00
for (; it != _curOperations->begin(); it++)
2010-05-16 20:09:18 +02:00
{
_grid->AppendRows();
curLine++;
SET_ROW_COLOR(curLine, OWN_GREEN);
_grid->SetCellValue(curLine, DESCRIPTION, it->description);
_grid->SetCellValue(curLine, DATE, wxString::Format(wxT("%02d/%02d/%d"), it->day+1, it->month+1, it->year));
if (it->amount < 0)
2010-05-27 21:09:02 +02:00
_grid->SetCellValue(curLine, DEBIT, wxString::Format(wxT("%.2lf"), -it->amount));
2010-05-16 20:09:18 +02:00
else
2010-05-27 21:09:02 +02:00
_grid->SetCellValue(curLine, CREDIT, wxString::Format(wxT("%.2lf"), it->amount));
_grid->SetCellEditor(curLine, DEBIT, new wxGridCellFloatEditor(-1, 2));
_grid->SetCellEditor(curLine, CREDIT, new wxGridCellFloatEditor(-1, 2));
categoryEditor = new wxGridCellChoiceEditor(user->GetCategoriesNumber()+1, _categories, false);
2010-05-16 20:09:18 +02:00
_grid->SetCellEditor(curLine, CATEGORY, categoryEditor);
2010-05-24 20:14:15 +02:00
_grid->SetCellValue(curLine, CATEGORY, user->GetCategoryName(it->category));
2010-05-27 21:09:02 +02:00
accountEditor = new wxGridCellChoiceEditor(user->_accounts.size()+1, _accounts, false);
2010-05-24 20:14:15 +02:00
_grid->SetCellEditor(curLine, ACCOUNT, accountEditor);
_grid->SetCellValue(curLine, ACCOUNT, user->GetAccountName(it->account));
2010-05-16 20:09:18 +02:00
it++;
}
_grid->AppendRows();
curLine++;
SET_ROW_COLOR(curLine, OWN_GREEN);
2010-05-27 21:09:02 +02:00
categoryEditor = new wxGridCellChoiceEditor(user->_preferences._categories.size()+1, _categories, false);
2010-05-16 20:09:18 +02:00
_grid->SetCellEditor(curLine, CATEGORY, categoryEditor);
2010-05-27 21:09:02 +02:00
_grid->SetCellEditor(curLine, DEBIT, new wxGridCellFloatEditor(-1, 2));
_grid->SetCellEditor(curLine, CREDIT, new wxGridCellFloatEditor(-1, 2));
accountEditor = new wxGridCellChoiceEditor(user->_accounts.size()+1, _accounts, false);
2010-05-24 20:14:15 +02:00
_grid->SetCellEditor(curLine, ACCOUNT, accountEditor);
2010-05-16 20:09:18 +02:00
2010-05-24 20:14:15 +02:00
_grid->AutoSizeColumn(CATEGORY, false);
_grid->AutoSizeColumn(ACCOUNT, false);
2010-05-16 20:09:18 +02:00
2010-05-27 21:09:02 +02:00
UpdateChart();
2010-05-16 10:35:34 +02:00
Fit();
SetMinSize(GetSize());
}
2010-05-27 21:09:02 +02:00
void AccountPanel::UpdateChart()
{
int i;
User* user = _kiss->GetUser();
std::list<operation>::iterator it;
//_dataset->BeginUpdate();
for (i=0; i<user->GetCategoriesNumber(); i++)
_categoriesValues[i] = 0.0;
// _categoriesValues[0] += 1.0;
// _categoriesValues[1] += 2.0;
// _categoriesValues[2] += 3.0;
for (it=_curOperations->begin(); it!=_curOperations->end(); it++)
{
if (it->amount > 0)
_categoriesValues[_categoriesIndexes[user->GetCategoryName(it->category)]] += it->amount ;
//std::cout << _categoriesValues[_categoriesIndexes[it->category]] << "\n";
}
//_dataset->EndUpdate();
_pie->DatasetChanged(_dataset);
}