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/>.
|
|
|
|
*/
|
|
|
|
|
2017-10-15 11:36:27 +02:00
|
|
|
#include <QMessageBox>
|
|
|
|
#include <QInputDialog>
|
|
|
|
|
2011-12-03 11:23:55 +01:00
|
|
|
#include "SearchPanel.hpp"
|
|
|
|
|
2012-06-29 20:33:33 +02:00
|
|
|
SearchPanel::SearchPanel(KissCount* kiss, wxUI *parent, bool lowResolution) :
|
|
|
|
KissPanel(kiss, parent, lowResolution), _operations(0)
|
2011-12-03 11:23:55 +01:00
|
|
|
{
|
|
|
|
DEFAULT_FONT(font);
|
|
|
|
std::vector<Account>::iterator accountIt;
|
|
|
|
std::vector<Category>::iterator categoryIt;
|
2013-02-26 16:04:27 +01:00
|
|
|
int i;
|
|
|
|
User* user = _kiss->GetUser();
|
2011-12-03 11:23:55 +01:00
|
|
|
|
|
|
|
QVBoxLayout *vbox = new QVBoxLayout;
|
|
|
|
QVBoxLayout *vbox2 = new QVBoxLayout;
|
|
|
|
QHBoxLayout *hbox = new QHBoxLayout;
|
|
|
|
|
2012-06-29 20:33:33 +02:00
|
|
|
_icons[KissPanel::LOW_RES_ICON] = SEARCH_LOW_ICON;
|
|
|
|
_icons[KissPanel::HIGH_RES_ICON] = SEARCH_ICON;
|
|
|
|
|
2011-12-03 11:23:55 +01:00
|
|
|
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);
|
2017-10-15 11:36:27 +02:00
|
|
|
_grid->setSizeAdjustPolicy(QAbstractScrollArea::AdjustToContents);
|
2011-12-03 11:23:55 +01:00
|
|
|
|
|
|
|
_changeAccountButton = new QPushButton(_("Change account"));
|
|
|
|
_changeCategoryButton = new QPushButton(_("Change category"));
|
2014-11-10 13:50:00 +01:00
|
|
|
_changeTagButton = new QPushButton(_("Change tag"));
|
2011-12-03 11:23:55 +01:00
|
|
|
_renameButton = new QPushButton(_("Rename"));
|
|
|
|
|
|
|
|
connect(_changeAccountButton, SIGNAL(clicked()), this, SLOT(OnButtonChangeAccount()));
|
|
|
|
connect(_changeCategoryButton, SIGNAL(clicked()), this, SLOT(OnButtonChangeCategory()));
|
2014-11-10 13:50:00 +01:00
|
|
|
connect(_changeTagButton, SIGNAL(clicked()), this, SLOT(OnButtonChangeTag()));
|
2011-12-03 11:23:55 +01:00
|
|
|
connect(_renameButton, SIGNAL(clicked()), this, SLOT(OnButtonRename()));
|
|
|
|
|
2013-02-26 16:04:27 +01:00
|
|
|
_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);
|
|
|
|
|
2011-12-03 11:23:55 +01:00
|
|
|
vbox2->addWidget(_changeAccountButton);
|
|
|
|
vbox2->addWidget(_changeCategoryButton);
|
2014-11-10 13:50:00 +01:00
|
|
|
vbox2->addWidget(_changeTagButton);
|
2011-12-03 11:23:55 +01:00
|
|
|
vbox2->addWidget(_renameButton);
|
|
|
|
|
2017-10-15 11:36:27 +02:00
|
|
|
hbox->addStretch();
|
2011-12-03 11:23:55 +01:00
|
|
|
hbox->addLayout(vbox2);
|
2017-10-15 11:36:27 +02:00
|
|
|
hbox->addStretch();
|
|
|
|
hbox->addWidget(_grid);
|
|
|
|
hbox->addStretch();
|
|
|
|
hbox->addWidget(_costRepartitionBanner, 2);
|
2013-02-26 16:04:27 +01:00
|
|
|
|
2012-05-12 11:02:37 +02:00
|
|
|
vbox->addLayout(hbox, 2);
|
2011-12-03 11:23:55 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
SearchPanel::~SearchPanel()
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
KissPanel* SearchPanel::CreatePanel()
|
|
|
|
{
|
2012-06-29 20:33:33 +02:00
|
|
|
return new SearchPanel(_kiss, _wxUI, _lowResolution);
|
2011-12-03 11:23:55 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
QString SearchPanel::GetToolTip()
|
|
|
|
{
|
|
|
|
return _("Search");
|
|
|
|
}
|
|
|
|
|
2013-02-26 16:04:27 +01:00
|
|
|
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);
|
|
|
|
}
|
|
|
|
|
2011-12-03 11:23:55 +01:00
|
|
|
void SearchPanel::OnEnter(void* caller)
|
|
|
|
{
|
|
|
|
SearchPanel* _this = (SearchPanel*) caller;
|
2012-05-12 18:06:15 +02:00
|
|
|
|
2011-12-03 11:23:55 +01:00
|
|
|
_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;
|
|
|
|
}
|
|
|
|
|
2012-05-12 18:06:15 +02:00
|
|
|
_kiss->setOverrideCursor(QCursor(Qt::WaitCursor));
|
|
|
|
_wxUI->setEnabled(false);
|
|
|
|
_wxUI->repaint();
|
|
|
|
|
2011-12-03 11:23:55 +01:00
|
|
|
_grid->LoadOperations(_operations, 0, 0);
|
|
|
|
|
2012-05-12 18:06:15 +02:00
|
|
|
_wxUI->setEnabled(true);
|
|
|
|
_kiss->setOverrideCursor(QCursor(Qt::ArrowCursor));
|
|
|
|
|
2013-02-26 16:04:27 +01:00
|
|
|
UpdateCostRepartition();
|
|
|
|
|
2011-12-03 11:23:55 +01:00
|
|
|
_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;
|
|
|
|
|
2012-04-30 21:22:52 +02:00
|
|
|
res = QInputDialog::getItem(0, "KissCount", _("Choose a new account"), accounts, 0, false);
|
2011-12-03 11:23:55 +01:00
|
|
|
|
2011-12-18 13:51:04 +01:00
|
|
|
if (!res.size()) return ;
|
2011-12-03 11:23:55 +01:00
|
|
|
a = (res.length()) ? accounts.indexOf(res) : 0;
|
|
|
|
account = (a) ? user->_accounts[a-1].id : 0;
|
|
|
|
|
|
|
|
_grid->MassUpdate(rows, true, ChangeAccount, params);
|
|
|
|
|
2013-02-26 16:04:27 +01:00
|
|
|
UpdateCostRepartition();
|
|
|
|
|
2017-10-29 18:21:54 +01:00
|
|
|
NeedReload();
|
2011-12-03 11:23:55 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
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<int> rows;
|
|
|
|
User* user = _kiss->GetUser();
|
|
|
|
QStringList categories;
|
|
|
|
std::vector<Operation>::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());
|
|
|
|
|
2012-04-30 21:22:52 +02:00
|
|
|
res = QInputDialog::getItem(0, "KissCount", _("Choose a new category"), categories, 0, false);
|
2011-12-03 11:23:55 +01:00
|
|
|
|
|
|
|
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);
|
|
|
|
|
2013-02-26 16:04:27 +01:00
|
|
|
UpdateCostRepartition();
|
|
|
|
|
2017-10-29 18:21:54 +01:00
|
|
|
NeedReload();
|
2011-12-03 11:23:55 +01:00
|
|
|
}
|
|
|
|
|
2014-11-10 13:50:00 +01:00
|
|
|
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();
|
|
|
|
|
2017-10-29 18:21:54 +01:00
|
|
|
NeedReload();
|
2014-11-10 13:50:00 +01:00
|
|
|
}
|
|
|
|
|
2011-12-03 11:23:55 +01:00
|
|
|
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);
|
|
|
|
|
2013-02-26 16:04:27 +01:00
|
|
|
UpdateCostRepartition();
|
|
|
|
|
2017-10-29 18:21:54 +01:00
|
|
|
NeedReload();
|
2011-12-03 11:23:55 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
void SearchPanel::OnShow()
|
|
|
|
{
|
|
|
|
_wxUI->setWindowTitle(_kiss->GetUser()->_name + " - " + _("Search"));
|
|
|
|
}
|
|
|
|
|
|
|
|
void SearchPanel::OnOperationModified()
|
|
|
|
{
|
2017-10-29 18:21:54 +01:00
|
|
|
NeedReload();
|
2011-12-03 11:23:55 +01:00
|
|
|
}
|