2011-12-03 11:23:55 +01:00
|
|
|
/*
|
2012-02-01 11:02:54 +01:00
|
|
|
Copyright 2010-2012 Grégory Soutadé
|
2011-12-03 11:23:55 +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 "SearchBanner.hpp"
|
|
|
|
|
|
|
|
SearchBanner::SearchBanner(KissCount* kiss, QFrame *parent, void* caller, OnButtonEnter enterCallback) : QFrame(parent), _kiss(kiss), _caller(caller), _enterCallback(enterCallback), _operations(0)
|
|
|
|
{
|
|
|
|
DEFAULT_FONT(font);
|
|
|
|
User* user = _kiss->GetUser();
|
|
|
|
std::vector<Account>::iterator accountIt;
|
|
|
|
std::vector<Category>::iterator categoryIt;
|
|
|
|
QDate firstOfMonth = QDate::currentDate();
|
|
|
|
|
|
|
|
_checkDateFrom = new QCheckBox(_("Date from"));
|
|
|
|
_checkDateTo = new QCheckBox(_("Date to"));
|
|
|
|
|
|
|
|
_checkDateFrom->setCheckState(Qt::Checked);
|
|
|
|
_checkDateTo->setCheckState(Qt::Checked);
|
|
|
|
|
|
|
|
QGridLayout *gridBagSizer = new QGridLayout(this);
|
|
|
|
|
|
|
|
firstOfMonth.setDate(firstOfMonth.year(), firstOfMonth.month(), 1);
|
|
|
|
_calendarFrom = new QCalendarWidget(this);
|
|
|
|
_calendarFrom->setGridVisible(false);
|
|
|
|
_calendarFrom->setFirstDayOfWeek(Qt::Monday);
|
|
|
|
// _calendarFrom->setNavigationBarVisible(false);
|
|
|
|
_calendarFrom->setVerticalHeaderFormat(QCalendarWidget::NoVerticalHeader);
|
|
|
|
|
|
|
|
_calendarTo = new QCalendarWidget(this);
|
|
|
|
_calendarTo->setGridVisible(false);
|
|
|
|
_calendarTo->setFirstDayOfWeek(Qt::Monday);
|
|
|
|
// _calendarTo->setNavigationBarVisible(false);
|
|
|
|
_calendarTo->setVerticalHeaderFormat(QCalendarWidget::NoVerticalHeader);
|
|
|
|
|
|
|
|
|
|
|
|
_description = new QLineEdit(this);
|
|
|
|
_description->setMinimumWidth(_description->width()*2);
|
|
|
|
|
|
|
|
connect(_description, SIGNAL(returnPressed ()), this, SLOT(OnEnter()));
|
|
|
|
|
|
|
|
_amountFrom = new QLineEdit(this);
|
|
|
|
_amountTo = new QLineEdit(this);
|
|
|
|
|
|
|
|
_category = new QListWidget(this);
|
|
|
|
QListWidgetItem * item = new QListWidgetItem(_("Unknown"), _category);
|
|
|
|
item->setCheckState (Qt::Unchecked);
|
|
|
|
for(categoryIt = user->_categories.begin(); categoryIt != user->_categories.end(); categoryIt++)
|
|
|
|
{
|
|
|
|
item = new QListWidgetItem(_(categoryIt->name.toStdString().c_str()), _category);
|
|
|
|
item->setCheckState (Qt::Unchecked);
|
|
|
|
}
|
|
|
|
|
|
|
|
_optype = new QListWidget(this);
|
|
|
|
item = new QListWidgetItem(_("Fix"), _optype);
|
|
|
|
item->setCheckState (Qt::Unchecked);
|
|
|
|
item = new QListWidgetItem(_("Non fix"), _optype);
|
|
|
|
item->setCheckState (Qt::Unchecked);
|
|
|
|
item = new QListWidgetItem(_("Checked"), _optype);
|
|
|
|
item->setCheckState (Qt::Unchecked);
|
|
|
|
item = new QListWidgetItem(_("Not checked"), _optype);
|
|
|
|
item->setCheckState (Qt::Unchecked);
|
|
|
|
|
|
|
|
_account = new QListWidget(this);
|
|
|
|
item = new QListWidgetItem(_("Unknown"), _account);
|
|
|
|
item->setCheckState (Qt::Unchecked);
|
|
|
|
for(accountIt = user->_accounts.begin(); accountIt != user->_accounts.end(); accountIt++)
|
|
|
|
{
|
|
|
|
item = new QListWidgetItem(accountIt->name, _account);
|
|
|
|
item->setCheckState (Qt::Unchecked);
|
|
|
|
}
|
|
|
|
|
|
|
|
QLabel* labelDescription = new QLabel(_("Description"));
|
|
|
|
QLabel* labelAmountFrom = new QLabel(_("Amount from"));
|
|
|
|
QLabel* labelAmountTo = new QLabel(_("Amount to"));
|
|
|
|
QLabel* labelCategory = new QLabel(_("Category"));
|
|
|
|
QLabel* labelOperations = new QLabel(_("Operations"));
|
|
|
|
QLabel* labelAccount = new QLabel(_("Account"));
|
|
|
|
|
|
|
|
gridBagSizer->addWidget(labelDescription, 0, 0);
|
|
|
|
gridBagSizer->addWidget(_description, 1, 0);
|
|
|
|
gridBagSizer->addWidget(_checkDateFrom, 0, 1);
|
|
|
|
gridBagSizer->addWidget(_calendarFrom, 1, 1);
|
|
|
|
gridBagSizer->addWidget(_checkDateTo, 0, 2);
|
|
|
|
gridBagSizer->addWidget(_calendarTo, 1, 2);
|
|
|
|
gridBagSizer->addWidget(labelAmountFrom, 0, 3);
|
|
|
|
gridBagSizer->addWidget(_amountFrom, 1, 3);
|
|
|
|
gridBagSizer->addWidget(labelAmountTo, 0, 4);
|
|
|
|
gridBagSizer->addWidget(_amountTo, 1, 4);
|
|
|
|
gridBagSizer->addWidget(labelCategory, 0, 5);
|
|
|
|
gridBagSizer->addWidget(_category, 1, 5);
|
|
|
|
gridBagSizer->addWidget(labelOperations, 0, 6);
|
|
|
|
gridBagSizer->addWidget(_optype, 1, 6);
|
|
|
|
gridBagSizer->addWidget(labelAccount, 0, 7);
|
|
|
|
gridBagSizer->addWidget(_account, 1, 7);
|
|
|
|
}
|
|
|
|
|
|
|
|
SearchBanner::~SearchBanner()
|
|
|
|
{
|
|
|
|
if (_operations) delete _operations;
|
|
|
|
}
|
|
|
|
|
|
|
|
std::vector<Operation> * SearchBanner::Search()
|
|
|
|
{
|
|
|
|
QString *description=0;
|
|
|
|
double *amountFrom=0, *amountTo=0;
|
|
|
|
std::vector<int> categories, accounts;
|
|
|
|
QDate *dateFrom=0, *dateTo=0;
|
|
|
|
User* user= _kiss->GetUser();
|
|
|
|
int i, types=0;
|
|
|
|
std::vector<Operation>::iterator it;
|
|
|
|
bool ok;
|
|
|
|
|
|
|
|
if (_operations)
|
|
|
|
{
|
|
|
|
delete _operations;
|
|
|
|
_operations = 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (_checkDateFrom->checkState() == Qt::Checked)
|
|
|
|
{
|
|
|
|
dateFrom = new QDate();
|
|
|
|
*dateFrom = _calendarFrom->selectedDate().addMonths(-1);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (_checkDateTo->checkState() == Qt::Checked)
|
|
|
|
{
|
|
|
|
dateTo = new QDate();
|
|
|
|
*dateTo = _calendarTo->selectedDate().addMonths(-1);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (dateFrom && dateTo && *dateFrom > *dateTo)
|
|
|
|
{
|
|
|
|
QMessageBox::critical(0, _("Error"), _("Invalid date range"));
|
|
|
|
goto end;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (_amountFrom->text().length())
|
|
|
|
{
|
|
|
|
amountFrom = new double;
|
|
|
|
*amountFrom = _amountFrom->text().toDouble(&ok);
|
|
|
|
if (!ok)
|
|
|
|
{
|
|
|
|
QMessageBox::critical(0, _("Error"), _("Invalid amount from"));
|
|
|
|
goto end;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (*amountFrom < 0) *amountFrom *= -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (_amountTo->text().length())
|
|
|
|
{
|
|
|
|
amountTo = new double;
|
|
|
|
*amountTo = _amountTo->text().toDouble(&ok);
|
|
|
|
if (!ok)
|
|
|
|
{
|
|
|
|
QMessageBox::critical(0, _("Error"), _("Invalid amount to"));
|
|
|
|
goto end;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (*amountTo < 0) *amountTo *= -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (amountFrom && amountTo && *amountFrom > *amountTo)
|
|
|
|
{
|
|
|
|
QMessageBox::critical(0, _("Error"), _("Invalid amount range"));
|
|
|
|
goto end;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (_description->text().length())
|
|
|
|
{
|
|
|
|
description = new QString;
|
|
|
|
*description = _description->text();
|
|
|
|
}
|
|
|
|
|
|
|
|
for(i=0; i<user->GetCategoriesNumber()+1; i++)
|
|
|
|
if (_category->item(i)->checkState() == Qt::Checked)
|
|
|
|
categories.push_back((i) ? user->_categories[i-1].id : 0);
|
|
|
|
|
|
|
|
types |= (_optype->item(0)->checkState() == Qt::Checked) ? Database::FIX_OP : 0;
|
|
|
|
types |= (_optype->item(1)->checkState() == Qt::Checked) ? Database::NON_FIX_OP : 0;
|
|
|
|
types |= (_optype->item(2)->checkState() == Qt::Checked) ? Database::CHECKED_OP : 0;
|
|
|
|
types |= (_optype->item(3)->checkState() == Qt::Checked) ? Database::NOT_CHECKED_OP : 0;
|
|
|
|
|
|
|
|
for(i=0; i<user->GetAccountsNumber()+1; i++)
|
|
|
|
if (_account->item(i)->checkState() == Qt::Checked)
|
|
|
|
accounts.push_back((i) ? user->_accounts[i-1].id : 0);
|
|
|
|
|
|
|
|
_operations = _kiss->Search(description, dateFrom, dateTo, amountFrom, amountTo, categories, types, accounts);
|
|
|
|
|
|
|
|
end:
|
|
|
|
delete dateFrom;
|
|
|
|
delete dateTo;
|
|
|
|
delete amountFrom;
|
|
|
|
delete amountTo;
|
|
|
|
|
|
|
|
return _operations;
|
|
|
|
}
|
|
|
|
|
|
|
|
void SearchBanner::OnEnter()
|
|
|
|
{
|
|
|
|
if (_enterCallback)
|
|
|
|
_enterCallback(_caller);
|
|
|
|
}
|