KissCount/src/view/SearchPanel.cpp

343 lines
8.3 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 <QMessageBox>
#include <QInputDialog>
#include "SearchPanel.hpp"
SearchPanel::SearchPanel(KissCount* kiss, wxUI *parent, bool lowResolution) :
KissPanel(kiss, parent, lowResolution), _operations(0)
{
DEFAULT_FONT(font);
std::vector<Account>::iterator accountIt;
std::vector<Category>::iterator categoryIt;
int i;
User* user = _kiss->GetUser();
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);
_grid->setSizeAdjustPolicy(QAbstractScrollArea::AdjustToContents);
_changeAccountButton = new QPushButton(_("Change account"));
_changeCategoryButton = new QPushButton(_("Change category"));
_changeTagButton = new QPushButton(_("Change tag"));
_renameButton = new QPushButton(_("Rename"));
connect(_changeAccountButton, SIGNAL(clicked()), this, SLOT(OnButtonChangeAccount()));
connect(_changeCategoryButton, SIGNAL(clicked()), this, SLOT(OnButtonChangeCategory()));
connect(_changeTagButton, SIGNAL(clicked()), this, SLOT(OnButtonChangeTag()));
connect(_renameButton, SIGNAL(clicked()), this, SLOT(OnButtonRename()));
_categories = new QString[user->GetCategoriesNumber()] ;
_categoriesValues = new int[user->GetCategoriesNumber()] ;
for(i=0, categoryIt = user->_categories.begin();
categoryIt != user->_categories.end();
categoryIt++, i++)
{
_categoriesIndexes[categoryIt->id] = i;
_categories[i] = _(categoryIt->name.toStdString().c_str()) ;
}
_costRepartitionBanner = new CostRepartitionBanner(_kiss, this, _categories);
vbox2->addWidget(_changeAccountButton);
vbox2->addWidget(_changeCategoryButton);
vbox2->addWidget(_changeTagButton);
vbox2->addWidget(_renameButton);
hbox->addStretch();
hbox->addLayout(vbox2);
hbox->addStretch();
hbox->addWidget(_grid);
hbox->addStretch();
hbox->addWidget(_costRepartitionBanner, 2);
vbox->addLayout(hbox, 2);
}
SearchPanel::~SearchPanel()
{
}
KissPanel* SearchPanel::CreatePanel()
{
return new SearchPanel(_kiss, _wxUI, _lowResolution);
}
QString SearchPanel::GetToolTip()
{
return _("Search");
}
void SearchPanel::UpdateCostRepartition()
{
int i;
User* user = _kiss->GetUser();
double total=0.0;
std::vector<Operation>::iterator it;
Account account;
Operation op;
_costRepartitionBanner->Reset();
for (i=0; i<user->GetCategoriesNumber(); i++)
_categoriesValues[i] = 0.0;
for(it=_operations->begin(); it!=_operations->end(); it++)
{
if (it->meta) continue;
op = *it;
account = user->GetAccount(op.account);
if (account.blocked && op.transfert && op.amount > 0)
op.amount = -op.amount;
if (op.amount >= 0) continue;
_categoriesValues[_categoriesIndexes[op.category]] -= op.amount;
total -= op.amount;
}
_costRepartitionBanner->UpdateCosts(_categoriesValues, total);
}
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));
UpdateCostRepartition();
_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<int> rows;
User* user = _kiss->GetUser();
QStringList accounts;
std::vector<Operation>::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);
UpdateCostRepartition();
NeedReload();
}
static void ChangeCategory(Operation* op, void** params)
{
int* category = (int*) params[0];
op->category = *category;
}
void SearchPanel::OnButtonChangeCategory()
{
int i, a;
std::vector<int> rows;
User* user = _kiss->GetUser();
QStringList categories;
std::vector<Operation>::iterator it;
QString res;
int category;
void * params[] = {&category};
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 ;
}
else
{
category = 0;
}
_grid->MassUpdate(rows, true, ChangeCategory, params);
UpdateCostRepartition();
NeedReload();
}
static void ChangeTag(Operation* op, void** params)
{
int* tag = (int*) params[0];
op->tag = *tag;
}
void SearchPanel::OnButtonChangeTag()
{
int i, a;
std::vector<int> rows;
User* user = _kiss->GetUser();
QStringList tags;
std::vector<Operation>::iterator it;
QString res;
int tag;
void * params[] = {&tag};
if (!_operations) return;
_grid->GetSelectedOperations(&rows);
tags << _("None");
for(i=0; i < user->GetTagsNumber(); i++)
tags << _(user->_tags[i].name.toStdString().c_str());
res = QInputDialog::getItem(0, "KissCount", _("Choose a new tag"), tags, 0, false);
if (res.length())
{
a = tags.indexOf(res);
tag = user->_tags[a].id ;
}
else
{
tag = 0;
}
_grid->MassUpdate(rows, true, ChangeTag, params);
UpdateCostRepartition();
NeedReload();
}
static void ChangeName(Operation* op, void** params)
{
QString* description = (QString*) params[0];
op->description = *description;
}
void SearchPanel::OnButtonRename()
{
std::vector<int> rows;
std::vector<Operation>::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);
UpdateCostRepartition();
NeedReload();
}
void SearchPanel::OnShow()
{
_wxUI->setWindowTitle(_kiss->GetUser()->_name + " - " + _("Search"));
}
void SearchPanel::OnOperationModified()
{
NeedReload();
}