KissCount/src/view/GenerateDialog.cpp

235 lines
5.9 KiB
C++
Raw Normal View History

2010-07-10 16:34:30 +02:00
/*
Copyright 2010 Grégory Soutadé
2010-07-10 16:34:30 +02:00
This file is part of KissCount.
2010-07-10 16:34:30 +02:00
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.
2010-07-10 16:34:30 +02:00
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.
2010-07-10 16:34:30 +02:00
You should have received a copy of the GNU General Public License
along with KissCount. If not, see <http://www.gnu.org/licenses/>.
2010-07-10 16:34:30 +02:00
*/
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), wxID_ANY, _("Generate month")), _kiss(kiss), _wxUI(parent)
2010-06-23 14:25:30 +02:00
{
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;
2011-01-29 21:01:01 +01:00
wxBoxSizer *hbox = new wxBoxSizer(wxHORIZONTAL);
gridBagSizer = new wxGridBagSizer(4, 5);
2011-01-29 21:01:01 +01:00
curDate.SetToCurrent();
label = new wxStaticText(this, wxID_ANY, _("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, wxID_ANY, _("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(wxT(""));
_monthFrom->Append(wxT(""));
for(i=1, it = _ops.begin(); it != _ops.end(); it++, i++)
2010-06-23 14:25:30 +02:00
{
_yearFrom->Append(wxString::Format(wxT("%d"), it->first));
if (year == it->first)
toSelect = i;
2010-06-23 14:25:30 +02:00
}
if (toSelect != -1)
2010-06-23 14:25:30 +02:00
{
_yearFrom->Select(toSelect);
OnYearFromChange(event);
toSelect=0;
if (month != -1)
2010-06-23 14:25:30 +02:00
{
for(i=0; i<(int)_monthFrom->GetCount(); i++)
2010-06-23 14:25:30 +02:00
{
if (_monthFrom->GetString(i) == months[month])
2010-06-23 14:25:30 +02:00
{
toSelect = i;
break;
2010-06-23 14:25:30 +02:00
}
}
}
_monthFrom->Select(toSelect);
2010-06-23 14:25:30 +02:00
}
else
2010-06-23 20:30:42 +02:00
{
_yearFrom->Select(0);
OnYearFromChange(event);
2010-06-23 20:30:42 +02:00
}
2010-06-23 14:25:30 +02:00
for(i=2000; i<=2050; i++)
_yearTo->Append(wxString::Format(wxT("%d"), i));
2010-06-23 14:25:30 +02:00
if (year == -1)
2010-06-23 14:25:30 +02:00
{
_yearTo->Select(curDate.GetYear()-2000);
OnYearToChange(event);
_monthTo->Select(curDate.GetMonth()-1);
2010-06-23 14:25:30 +02:00
}
else
2010-06-23 14:25:30 +02:00
{
if (month == 11)
year++;
_yearTo->Select(year-2000);
OnYearToChange(event);
if (month == -1)
_monthTo->Select(0);
else
2010-06-23 14:25:30 +02:00
{
for(a=0, i=0, monthIt=_ops[year].begin(); monthIt!=_ops[year].end() && i<12; i++)
2010-06-23 14:25:30 +02:00
{
if (i != *monthIt)
2010-06-23 14:25:30 +02:00
{
a++;
continue;
2010-06-23 14:25:30 +02:00
}
if (*monthIt > month)
break;
monthIt++;
2010-06-23 14:25:30 +02:00
}
_monthTo->Select(a);
2010-06-23 14:25:30 +02:00
}
}
2011-01-29 21:01:01 +01:00
hbox->Add(gridBagSizer, 0, wxGROW|wxALL, 10);
SetSizer(hbox);
2010-06-23 14:25:30 +02:00
2011-01-29 21:01:01 +01:00
Fit();
Center();
2010-06-23 14:25:30 +02:00
}
void GenerateDialog::OnYearFromChange(wxCommandEvent& event)
{
wxString years = _yearFrom->GetString(_yearFrom->GetCurrentSelection());
int year;
std::vector<int>::iterator it;
2010-06-23 14:25:30 +02:00
_monthFrom->Clear();
2010-06-23 14:25:30 +02:00
if (!years.Length())
2010-06-23 14:25:30 +02:00
{
_monthFrom->Append(wxT(""));
return;
2010-06-23 14:25:30 +02:00
}
year = wxAtoi(years);
2010-06-23 14:25:30 +02:00
for(it=_ops[year].begin(); it!=_ops[year].end(); it++)
_monthFrom->Append(months[*it]);
_monthFrom->Select(0);
2010-06-23 14:25:30 +02:00
Layout();
2010-06-23 14:25:30 +02:00
}
void GenerateDialog::OnYearToChange(wxCommandEvent& event)
{
int year, i, ok;
std::vector<int>::iterator it;
2010-06-23 14:25:30 +02:00
_monthTo->Clear();
2010-06-23 14:25:30 +02:00
year = wxAtoi(_yearTo->GetString(_yearTo->GetCurrentSelection()));
2010-06-23 14:25:30 +02:00
for (i=0; i<12; i++)
2010-06-23 14:25:30 +02:00
{
ok = 1;
for(it=_ops[year].begin(); it!=_ops[year].end(); it++)
2010-06-23 14:25:30 +02:00
{
if (*it == i)
2010-06-23 14:25:30 +02:00
{
ok=0; break;
2010-06-23 14:25:30 +02:00
}
}
if (ok)
_monthTo->Append(months[i]);
2010-06-23 14:25:30 +02:00
}
_monthTo->Select(0);
2010-06-23 14:25:30 +02:00
Layout();
2010-06-23 14:25:30 +02:00
}
void GenerateDialog::OnOK(wxCommandEvent& event)
2010-06-23 19:32:42 +02:00
{
int monthFrom, yearFrom, monthTo, yearTo, i;
2010-06-23 19:32:42 +02:00
if (!_yearFrom->GetString(_yearFrom->GetCurrentSelection()).Length())
2010-06-23 19:32:42 +02:00
{
monthFrom = -1;
yearFrom = -1;
2010-06-23 19:32:42 +02:00
}
else
2010-06-23 19:32:42 +02:00
{
for (i=0; i<12; i++)
2010-06-23 19:32:42 +02:00
{
if (months[i] == _monthFrom->GetString(_monthFrom->GetCurrentSelection()))
2010-06-23 19:32:42 +02:00
{
monthFrom = i;
break;
2010-06-23 19:32:42 +02:00
}
}
yearFrom = wxAtoi(_yearFrom->GetString(_yearFrom->GetCurrentSelection()));
2010-06-23 19:32:42 +02:00
}
for (i=0; i<12; i++)
2010-06-23 19:32:42 +02:00
{
if (months[i] == _monthTo->GetString(_monthTo->GetCurrentSelection()))
2010-06-23 19:32:42 +02:00
{
monthTo = i;
break;
2010-06-23 19:32:42 +02:00
}
}
yearTo = wxAtoi(_yearTo->GetString(_yearTo->GetCurrentSelection()));
2010-06-23 19:32:42 +02:00
Close();
_kiss->GenerateMonth(monthFrom, yearFrom, monthTo, yearTo);
2010-06-23 14:25:30 +02:00
}
void GenerateDialog::OnCancel(wxCommandEvent& event)
{
Close();
2010-06-23 14:25:30 +02:00
}