/*
Copyright 2010-2013 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 .
*/
#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::iterator accountIt;
std::vector::iterator categoryIt;
std::vector::iterator tagIt;
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);
_calendarFrom->setMaximumSize(_calendarFrom->sizeHint());
_calendarFrom->setSelectedDate(firstOfMonth);
connect(_calendarFrom, SIGNAL(currentPageChanged(int, int)), this, SLOT(OnCalendarFromPageChanged(int, int)));
_calendarTo = new QCalendarWidget(this);
_calendarTo->setGridVisible(false);
_calendarTo->setFirstDayOfWeek(Qt::Monday);
// _calendarTo->setNavigationBarVisible(false);
_calendarTo->setVerticalHeaderFormat(QCalendarWidget::NoVerticalHeader);
_calendarTo->setMaximumSize(_calendarTo->sizeHint());
_calendarTo->setSelectedDate(QDate::currentDate());
connect(_calendarTo, SIGNAL(currentPageChanged(int, int)), this, SLOT(OnCalendarToPageChanged(int, int)));
_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);
}
_category->setMaximumSize(QSize(_category->contentsRect().width()*1.5, _category->sizeHint().height()));
_tag = new QListWidget(this);
item = new QListWidgetItem(_("Unknown"), _tag);
item->setCheckState (Qt::Unchecked);
for(tagIt = user->_tags.begin(); tagIt != user->_tags.end(); tagIt++)
{
item = new QListWidgetItem(_(tagIt->name.toStdString().c_str()), _tag);
item->setCheckState (Qt::Unchecked);
}
_tag->setMaximumSize(QSize(_tag->contentsRect().width()*1.5, _tag->sizeHint().height()));
_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);
_optype->setMaximumSize(QSize(_optype->contentsRect().width()*1.5, _optype->sizeHint().height()));
_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);
}
_account->setMaximumSize(QSize(_account->contentsRect().width()*1.5, _account->sizeHint().height()));
QLabel* labelDescription = new QLabel(_("Description"));
QLabel* labelAmountFrom = new QLabel(_("Amount from"));
QLabel* labelAmountTo = new QLabel(_("Amount to"));
QLabel* labelCategory = new QLabel(_("Category"));
QLabel* labelTag = new QLabel(_("Tag"));
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(labelTag, 0, 6);
gridBagSizer->addWidget(_tag, 1, 6);
gridBagSizer->addWidget(labelOperations, 0, 7);
gridBagSizer->addWidget(_optype, 1, 7);
gridBagSizer->addWidget(labelAccount, 0, 8);
gridBagSizer->addWidget(_account, 1, 8);
setSizePolicy(QSizePolicy::Minimum, QSizePolicy::Minimum);
}
SearchBanner::~SearchBanner()
{
if (_operations) delete _operations;
}
void SearchBanner::OnCalendarFromPageChanged(int year, int month)
{
_calendarFrom->setSelectedDate(QDate(year, month, 1));
}
void SearchBanner::OnCalendarToPageChanged(int year, int month)
{
_calendarTo->setSelectedDate(QDate(year, month, 1));
}
std::vector * SearchBanner::Search()
{
QString *description=0;
int *amountFrom=0, *amountTo=0;
std::vector categories, tags, accounts;
QDate *dateFrom=0, *dateTo=0;
User* user= _kiss->GetUser();
int i, types=0;
std::vector::iterator it;
bool ok;
if (_operations)
{
delete _operations;
_operations = 0;
}
if (_checkDateFrom->checkState() == Qt::Checked)
{
dateFrom = new QDate();
*dateFrom = _calendarFrom->selectedDate();
}
if (_checkDateTo->checkState() == Qt::Checked)
{
dateTo = new QDate();
*dateTo = _calendarTo->selectedDate();
}
if (dateFrom && dateTo && *dateFrom > *dateTo)
{
QMessageBox::critical(0, _("Error"), _("Invalid date range"));
goto end;
}
if (_amountFrom->text().length())
{
amountFrom = new int;
*amountFrom = _amountFrom->text().toInt(&ok);
if (!ok)
{
QMessageBox::critical(0, _("Error"), _("Invalid amount from"));
goto end;
}
if (*amountFrom < 0) *amountFrom *= -1;
}
if (_amountTo->text().length())
{
amountTo = new int;
*amountTo = _amountTo->text().toInt(&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; iGetCategoriesNumber()+1; i++)
if (_category->item(i)->checkState() == Qt::Checked)
categories.push_back((i) ? user->_categories[i-1].id : 0);
for(i=0; iGetTagsNumber(); i++)
if (_tag->item(i+1)->checkState() == Qt::Checked)
tags.push_back(user->_tags[i].id);
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; iGetAccountsNumber()+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, tags);
end:
delete dateFrom;
delete dateTo;
delete amountFrom;
delete amountTo;
return _operations;
}
void SearchBanner::OnEnter()
{
if (_enterCallback)
_enterCallback(_caller);
}