/*
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 .
*/
#include "SearchPanel.hpp"
SearchPanel::SearchPanel(KissCount* kiss, wxUI *parent, bool lowResolution) :
KissPanel(kiss, parent, lowResolution), _operations(0)
{
DEFAULT_FONT(font);
std::vector::iterator accountIt;
std::vector::iterator categoryIt;
QVBoxLayout *vbox = new QVBoxLayout;
QVBoxLayout *vbox2 = new QVBoxLayout;
QHBoxLayout *hbox = new QHBoxLayout;
_icons[KissPanel::LOW_RES_ICON] = SEARCH_LOW_ICON;
_icons[KissPanel::HIGH_RES_ICON] = SEARCH_ICON;
setLayout(vbox);
_searchButton = new QPushButton(_("Search"));
connect(_searchButton, SIGNAL(clicked()), this, SLOT(OnButtonSearch()));
_banner = new SearchBanner(kiss, this, this, OnEnter);
vbox->addWidget(_banner);
vbox->addWidget(_searchButton);
_grid = new GridAccount(_kiss, this, false, false, true);
hbox->addWidget(_grid);
_changeAccountButton = new QPushButton(_("Change account"));
_changeCategoryButton = new QPushButton(_("Change category"));
_renameButton = new QPushButton(_("Rename"));
connect(_changeAccountButton, SIGNAL(clicked()), this, SLOT(OnButtonChangeAccount()));
connect(_changeCategoryButton, SIGNAL(clicked()), this, SLOT(OnButtonChangeCategory()));
connect(_renameButton, SIGNAL(clicked()), this, SLOT(OnButtonRename()));
vbox2->addWidget(_changeAccountButton);
vbox2->addWidget(_changeCategoryButton);
vbox2->addWidget(_renameButton);
hbox->addLayout(vbox2);
vbox->addLayout(hbox, 2);
}
SearchPanel::~SearchPanel()
{
}
KissPanel* SearchPanel::CreatePanel()
{
return new SearchPanel(_kiss, _wxUI, _lowResolution);
}
QString SearchPanel::GetToolTip()
{
return _("Search");
}
void SearchPanel::OnEnter(void* caller)
{
SearchPanel* _this = (SearchPanel*) caller;
_this->OnButtonSearch();
}
void SearchPanel::OnButtonSearch()
{
_operations = _banner->Search();
if (!_operations) return;
if (_operations->size() > 1)
QMessageBox::information(0, _("KissCount"), QString::number(_operations->size()) + _(" entries found"));
else if (_operations->size() == 1)
QMessageBox::information(0, _("KissCount"), _("1 entry found"));
else
{
QMessageBox::information(0, _("KissCount"), _("No entry found"));
return;
}
_kiss->setOverrideCursor(QCursor(Qt::WaitCursor));
_wxUI->setEnabled(false);
_wxUI->repaint();
_grid->LoadOperations(_operations, 0, 0);
_wxUI->setEnabled(true);
_kiss->setOverrideCursor(QCursor(Qt::ArrowCursor));
_wxUI->layout();
}
static void ChangeAccount(Operation* op, void** params)
{
int* account = (int*) params[0];
op->account = *account;
}
void SearchPanel::OnButtonChangeAccount()
{
int i, a;
std::vector rows;
User* user = _kiss->GetUser();
QStringList accounts;
std::vector::iterator it;
int account;
void * params[] = {&account};
QString res;
if (!_operations) return;
_grid->GetSelectedOperations(&rows);
accounts << _("None");
for(i=0; i < user->GetAccountsNumber(); i++)
accounts << user->_accounts[i].name;
res = QInputDialog::getItem(0, "KissCount", _("Choose a new account"), accounts, 0, false);
if (!res.size()) return ;
a = (res.length()) ? accounts.indexOf(res) : 0;
account = (a) ? user->_accounts[a-1].id : 0;
_grid->MassUpdate(rows, true, ChangeAccount, params);
_wxUI->NeedReload();
}
static void ChangeCategory(Operation* op, void** params)
{
int* category = (int*) params[0];
bool* fix = (bool*) params[1];
op->category = *category;
op->fix_cost = * fix;
}
void SearchPanel::OnButtonChangeCategory()
{
int i, a;
std::vector rows;
User* user = _kiss->GetUser();
QStringList categories;
std::vector::iterator it;
QString res;
int category;
bool fix;
void * params[] = {&category, &fix};
if (!_operations) return;
_grid->GetSelectedOperations(&rows);
categories << _("None");
for(i=0; i < user->GetCategoriesNumber(); i++)
categories << _(user->_categories[i].name.toStdString().c_str());
res = QInputDialog::getItem(0, "KissCount", _("Choose a new category"), categories, 0, false);
if (res.length())
{
a = categories.indexOf(res);
category = user->_categories[a-1].id ;
fix = user->_categories[a-1].fix_cost;
}
else
{
category = 0;
fix = false;
}
_grid->MassUpdate(rows, true, ChangeCategory, params);
_wxUI->NeedReload();
}
static void ChangeName(Operation* op, void** params)
{
QString* description = (QString*) params[0];
op->description = *description;
}
void SearchPanel::OnButtonRename()
{
std::vector rows;
std::vector::iterator it;
QString description;
void * params[] = {&description};
if (!_operations) return;
_grid->GetSelectedOperations(&rows);
description = QInputDialog::getText(this, "", _("Enter a new description"));
if (!description.length()) return;
_grid->MassUpdate(rows, false, ChangeName, params);
_wxUI->NeedReload();
}
void SearchPanel::OnShow()
{
_wxUI->setWindowTitle(_kiss->GetUser()->_name + " - " + _("Search"));
}
void SearchPanel::OnOperationModified()
{
_wxUI->NeedReload();
}