KissCount/src/view/GenerateDialog.cpp

221 lines
5.4 KiB
C++

/*
Copyright 2010-2012 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 <http://www.gnu.org/licenses/>.
*/
#include "GenerateDialog.hpp"
#include <QLabel>
GenerateDialog::GenerateDialog(KissCount* kiss, wxUI *parent, int month, int year) : QDialog(0, Qt::Dialog), _kiss(kiss), _wxUI(parent)
{
QGridLayout* gridLayout;
std::map<int, std::vector<int> >::iterator it;
int i, a, toSelect=-1;
QDate curDate = QDate::currentDate();
std::vector<int>::iterator monthIt;
// wxBoxSizer *hbox = new wxBoxSizer(wxHORIZONTAL);
gridLayout = new QGridLayout(this);
setWindowTitle(_("Generate month"));
setModal(true);
gridLayout->addWidget(new QLabel(_("From "), this), 0, 0);
_yearFrom = new QComboBox(this);
gridLayout->addWidget(_yearFrom, 0, 1);
_monthFrom = new QComboBox(this);
gridLayout->addWidget(_monthFrom, 0, 2);
_yearTo = new QComboBox(this);
gridLayout->addWidget(_yearTo, 1, 1);
_monthTo = new QComboBox(this);
gridLayout->addWidget(_monthTo, 1, 2);
QPushButton* ok = new QPushButton(_("OK"), this);
QPushButton* cancel = new QPushButton(_("Cancel"), this);
gridLayout->addWidget(ok, 3, 1);
gridLayout->addWidget(cancel, 3, 2);
connect(ok, SIGNAL(clicked()), this, SLOT(OnOK()));
connect(cancel, SIGNAL(clicked()), this, SLOT(OnCancel()));
connect(_yearTo, SIGNAL(currentIndexChanged(int)), this, SLOT(OnYearToChange(int)));
connect(_yearFrom, SIGNAL(currentIndexChanged(int)), this, SLOT(OnYearFromChange(int)));
_ops = _kiss->GetAllOperations();
_yearFrom->addItem("");
_monthFrom->addItem("");
for(i=1, it = _ops.begin(); it != _ops.end(); it++, i++)
{
_yearFrom->addItem(QString::number(it->first));
if (year == it->first)
toSelect = i;
}
if (toSelect != -1)
{
_yearFrom->setCurrentIndex(toSelect);
toSelect=0;
if (month != -1)
{
for(i=0; i<(int)_monthFrom->count(); i++)
{
if (_monthFrom->itemText(i) == wxUI::months[month])
{
toSelect = i;
break;
}
}
}
_monthFrom->setCurrentIndex(toSelect);
}
else
{
_yearFrom->setCurrentIndex(0);
}
for(i=curDate.year()-10; i<=curDate.year()+10; i++)
_yearTo->addItem(QString::number(i));
if (year == -1)
{
_yearTo->setCurrentIndex(10);
_monthTo->setCurrentIndex(curDate.month());
}
else
{
if (month == 11)
year++;
_yearTo->setCurrentIndex(year-(curDate.year()-10));
if (month == -1)
_monthTo->setCurrentIndex(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->setCurrentIndex(a);
}
}
}
void GenerateDialog::OnYearFromChange(int index)
{
QString years = _yearFrom->itemText(index);
int year;
std::vector<int>::iterator it;
_monthFrom->clear();
if (!years.length())
{
_monthFrom->addItem("");
return;
}
year = years.toInt();
for(it=_ops[year].begin(); it!=_ops[year].end(); it++)
_monthFrom->addItem(wxUI::months[*it]);
_monthFrom->setCurrentIndex(0);
layout();
}
void GenerateDialog::OnYearToChange(int index)
{
int year, i, ok;
std::vector<int>::iterator it;
_monthTo->clear();
year = _yearTo->currentText().toInt();
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->addItem(wxUI::months[i]);
}
_monthTo->setCurrentIndex(0);
layout();
}
void GenerateDialog::OnOK()
{
int monthFrom, yearFrom, monthTo, yearTo, i;
if (!_yearFrom->itemText(_yearFrom->currentIndex()).length())
{
monthFrom = -1;
yearFrom = -1;
}
else
{
for (i=0; i<12; i++)
{
if (wxUI::months[i] == _monthFrom->itemText(_monthFrom->currentIndex()))
{
monthFrom = i;
break;
}
}
yearFrom = _yearFrom->itemText(_yearFrom->currentIndex()).toInt();
}
for (i=0; i<12; i++)
{
if (wxUI::months[i] == _monthTo->itemText(_monthTo->currentIndex()))
{
monthTo = i;
break;
}
}
yearTo = _yearTo->itemText(_yearTo->currentIndex()).toInt();
QApplication::setOverrideCursor(QCursor(Qt::WaitCursor));
close();
_kiss->GenerateMonth(monthFrom, yearFrom, monthTo, yearTo);
QApplication::restoreOverrideCursor();
}
void GenerateDialog::OnCancel()
{
close();
}