2011-10-31 09:14:40 +01:00
|
|
|
/*
|
2012-02-01 11:02:54 +01:00
|
|
|
Copyright 2010-2012 Grégory Soutadé
|
2011-10-31 09:14:40 +01:00
|
|
|
|
|
|
|
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>
|
2014-02-08 15:14:04 +01:00
|
|
|
#include <QMessageBox>
|
2011-10-31 09:14:40 +01:00
|
|
|
|
|
|
|
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);
|
|
|
|
|
2018-01-06 19:43:35 +01:00
|
|
|
_ok = new QPushButton(_("OK"), this);
|
2011-10-31 09:14:40 +01:00
|
|
|
QPushButton* cancel = new QPushButton(_("Cancel"), this);
|
2018-01-06 19:43:35 +01:00
|
|
|
gridLayout->addWidget(_ok, 3, 1);
|
2011-10-31 09:14:40 +01:00
|
|
|
gridLayout->addWidget(cancel, 3, 2);
|
|
|
|
|
2018-01-06 19:43:35 +01:00
|
|
|
connect(_ok, SIGNAL(clicked()), this, SLOT(OnOK()));
|
2011-10-31 09:14:40 +01:00
|
|
|
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();
|
|
|
|
|
2018-01-06 19:43:35 +01:00
|
|
|
/* From */
|
|
|
|
for(i=0, it = _ops.begin(); it != _ops.end(); it++, i++)
|
2011-10-31 09:14:40 +01:00
|
|
|
{
|
2011-10-31 10:24:05 +01:00
|
|
|
_yearFrom->addItem(QString::number(it->first));
|
2011-10-31 09:14:40 +01:00
|
|
|
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);
|
|
|
|
}
|
|
|
|
|
2018-01-06 19:43:35 +01:00
|
|
|
/* To */
|
|
|
|
for(i=year-10; i<=year+10; i++)
|
2011-10-31 10:24:05 +01:00
|
|
|
_yearTo->addItem(QString::number(i));
|
2011-10-31 09:14:40 +01:00
|
|
|
|
|
|
|
if (year == -1)
|
|
|
|
{
|
2011-10-31 10:39:46 +01:00
|
|
|
_yearTo->setCurrentIndex(10);
|
2011-10-31 09:14:40 +01:00
|
|
|
_monthTo->setCurrentIndex(curDate.month());
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2018-01-06 19:43:35 +01:00
|
|
|
toSelect = 10;
|
2011-10-31 09:14:40 +01:00
|
|
|
if (month == 11)
|
2018-01-06 19:43:35 +01:00
|
|
|
{
|
2011-10-31 09:14:40 +01:00
|
|
|
year++;
|
2018-01-06 19:43:35 +01:00
|
|
|
toSelect++;
|
|
|
|
}
|
2011-10-31 09:14:40 +01:00
|
|
|
|
2018-01-06 19:43:35 +01:00
|
|
|
_yearTo->setCurrentIndex(toSelect);
|
2011-10-31 09:14:40 +01:00
|
|
|
|
|
|
|
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();
|
|
|
|
|
2018-01-06 19:43:35 +01:00
|
|
|
/* Avoid adding an existing month */
|
2011-10-31 09:14:40 +01:00
|
|
|
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);
|
|
|
|
|
2018-01-06 19:43:35 +01:00
|
|
|
if (_monthTo->count() == 0)
|
|
|
|
{
|
|
|
|
QMessageBox::critical(0, _("Error"), _("All months for this year has already been generated"));
|
|
|
|
_ok->setEnabled(false);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
_ok->setEnabled(true);
|
2011-10-31 09:14:40 +01:00
|
|
|
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();
|
|
|
|
|
2011-10-31 10:39:46 +01:00
|
|
|
QApplication::setOverrideCursor(QCursor(Qt::WaitCursor));
|
2011-10-31 09:14:40 +01:00
|
|
|
close();
|
2014-02-08 15:14:04 +01:00
|
|
|
if (_kiss->GenerateMonth(monthFrom, yearFrom, monthTo, yearTo))
|
|
|
|
QMessageBox::warning(0, _("Warning"), _("Some operations date has been updated"));
|
|
|
|
|
2011-10-31 10:39:46 +01:00
|
|
|
QApplication::restoreOverrideCursor();
|
2011-10-31 09:14:40 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
void GenerateDialog::OnCancel()
|
|
|
|
{
|
|
|
|
close();
|
|
|
|
}
|