KissCount/view/GenerateDialog.cpp

213 lines
4.7 KiB
C++
Raw Normal View History

2010-06-23 14:25:30 +02:00
#include "GenerateDialog.h"
enum {BUTTON_OK_ID=1, BUTTON_CANCEL_ID, YEAR_FROM_ID, MONTH_FROM_ID, YEAR_TO_ID, MONTH_TO_ID};
BEGIN_EVENT_TABLE(GenerateDialog, wxDialog)
EVT_BUTTON(BUTTON_OK_ID, GenerateDialog::OnOK)
EVT_BUTTON(BUTTON_CANCEL_ID, GenerateDialog::OnCancel)
EVT_CHOICE(YEAR_FROM_ID, GenerateDialog::OnYearFromChange)
EVT_CHOICE(YEAR_TO_ID, GenerateDialog::OnYearToChange)
END_EVENT_TABLE()
GenerateDialog::GenerateDialog(KissCount* kiss, wxUI *parent, int month, int year) : wxDialog(&(*parent), -1, _("Generate month")), _kiss(kiss), _wxUI(parent)
{
wxGridBagSizer *gridBagSizer;
wxStaticText* label;
std::map<int, std::vector<int> >::iterator it;
int i, a, toSelect=-1;
wxDateTime curDate;
wxCommandEvent event;
std::vector<int>::iterator monthIt;
curDate.SetToCurrent();
gridBagSizer = new wxGridBagSizer(4, 5);
label = new wxStaticText(this, -1, _("From "));
gridBagSizer->Add(label, wxGBPosition(0, 0));
_yearFrom = new wxChoice(this, YEAR_FROM_ID);
gridBagSizer->Add(_yearFrom, wxGBPosition(0, 1));
_monthFrom = new wxChoice(this, MONTH_FROM_ID);
gridBagSizer->Add(_monthFrom, wxGBPosition(0, 2));
label = new wxStaticText(this, -1, _("To "));
gridBagSizer->Add(label, wxGBPosition(1, 0));
_yearTo = new wxChoice(this, YEAR_TO_ID);
gridBagSizer->Add(_yearTo, wxGBPosition(1, 1));
_monthTo = new wxChoice(this, MONTH_TO_ID);
gridBagSizer->Add(_monthTo, wxGBPosition(1, 2));
wxButton* ok = new wxButton(this, BUTTON_OK_ID, _("OK"));
wxButton* cancel = new wxButton(this, BUTTON_CANCEL_ID, _("Cancel"));
gridBagSizer->Add(ok, wxGBPosition(3, 3));
gridBagSizer->Add(cancel, wxGBPosition(3, 4));
_ops = _kiss->GetAllOperations();
_yearFrom->Append(_(""));
_monthFrom->Append(_(""));
for(i=1, it = _ops.begin(); it != _ops.end(); it++, i++)
{
_yearFrom->Append(wxString::Format(_("%d"), it->first));
if (year == it->first)
toSelect = i;
}
if (toSelect != -1)
{
_yearFrom->Select(toSelect);
OnYearFromChange(event);
toSelect=0;
if (month != -1)
{
for(i=0; i<(int)_monthFrom->GetCount(); i++)
{
if (_monthFrom->GetString(i) == months[month])
{
2010-06-23 20:30:42 +02:00
toSelect = i;
2010-06-23 14:25:30 +02:00
break;
}
}
}
_monthFrom->Select(toSelect);
}
2010-06-23 20:30:42 +02:00
else
{
_yearFrom->Select(0);
OnYearFromChange(event);
}
2010-06-23 14:25:30 +02:00
for(i=2000; i<=2050; i++)
_yearTo->Append(wxString::Format(_("%d"), i));
if (year == -1)
{
_yearTo->Select(curDate.GetYear()-2000);
OnYearToChange(event);
_monthTo->Select(curDate.GetMonth()-1);
}
else
{
_yearTo->Select(year-2000);
OnYearToChange(event);
if (_ops[year].size())
month = _ops[year][0];
if (month == -1)
_monthTo->Select(0);
else
{
for(a=0, i=0, monthIt=_ops[year].begin(); monthIt!=_ops[year].end() && i<12; i++)
{
if (i != *monthIt)
{
a++;
continue;
}
if (*monthIt > month)
break;
monthIt++;
}
_monthTo->Select(a);
}
}
SetSizer(gridBagSizer);
Layout();
Center();
}
void GenerateDialog::OnYearFromChange(wxCommandEvent& event)
{
wxString years = _yearFrom->GetString(_yearFrom->GetCurrentSelection());
int year;
std::vector<int>::iterator it;
_monthFrom->Clear();
if (years == _(""))
{
_monthFrom->Append(_(""));
return;
}
year = wxAtoi(years);
for(it=_ops[year].begin(); it!=_ops[year].end(); it++)
_monthFrom->Append(months[*it]);
_monthFrom->Select(0);
Layout();
}
void GenerateDialog::OnYearToChange(wxCommandEvent& event)
{
int year, i, ok;
std::vector<int>::iterator it;
_monthTo->Clear();
year = wxAtoi(_yearTo->GetString(_yearTo->GetCurrentSelection()));
for (i=0; i<12; i++)
{
ok = 1;
for(it=_ops[year].begin(); it!=_ops[year].end(); it++)
{
if (*it == i)
{
ok=0; break;
}
}
if (ok)
_monthTo->Append(months[i]);
}
_monthTo->Select(0);
Layout();
}
void GenerateDialog::OnOK(wxCommandEvent& event)
2010-06-23 19:32:42 +02:00
{
int monthFrom, yearFrom, monthTo, yearTo, i;
2010-06-23 20:30:42 +02:00
if (_yearFrom->GetString(_yearFrom->GetCurrentSelection()) == _(""))
2010-06-23 19:32:42 +02:00
{
monthFrom = -1;
yearFrom = -1;
}
else
{
for (i=0; i<12; i++)
{
if (months[i] == _monthFrom->GetString(_monthFrom->GetCurrentSelection()))
{
monthFrom = i;
break;
}
}
yearFrom = wxAtoi(_yearFrom->GetString(_yearFrom->GetCurrentSelection()));
}
for (i=0; i<12; i++)
{
if (months[i] == _monthTo->GetString(_monthTo->GetCurrentSelection()))
{
monthTo = i;
break;
}
}
yearTo = wxAtoi(_yearTo->GetString(_yearTo->GetCurrentSelection()));
2010-06-23 14:25:30 +02:00
Close();
2010-06-23 19:32:42 +02:00
_kiss->GenerateMonth(monthFrom, yearFrom, monthTo, yearTo);
2010-06-23 14:25:30 +02:00
}
void GenerateDialog::OnCancel(wxCommandEvent& event)
{
Close();
}