KissCount/src/view/wxUI.cpp

385 lines
9.2 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 "AccountPanel.hpp"
#include "SearchPanel.hpp"
#include "PreferencesPanel.hpp"
#include "StatsPanel.hpp"
#include "ImportPanel.hpp"
#include "ExportPanel.hpp"
#include "wxUI.hpp"
#include "view.hpp"
#include "UsersDialog.hpp"
#include <QDesktopWidget>
QString wxUI::months[12] ;
QColor wxUI::categoryColors[MAX_CATEGORY] = {QColor(0x00, 0x45, 0x86),
QColor(0xFF, 0x3E, 0x0E),
QColor(0xFF, 0xD3, 0x20),
QColor(0x58, 0x9D, 0x1B),
QColor(0x7E, 0x00, 0x21),
QColor(0x83, 0xCC, 0xFF),
QColor(0x31, 0x40, 0x04),
QColor(0xB0, 0xCF, 0x00),
QColor(0x4B, 0x1F, 0x6F),
QColor(0xFF, 0x93, 0x0E),
QColor(0xC5, 0x00, 0x0D),
QColor(0x00, 0x84, 0xD1)};
wxUI::wxUI(KissCount* kiss, const QString& title)
: QFrame(0), _language(SupportedLanguages::languages[SupportedLanguages::English].name),
_kiss(kiss), _signalMapper(this), _curPanel(0), _locale(0),
_needReload(0)
{
QPushButton* button;
QDesktopWidget desk;
bool lowRes;
int w;
lowRes = (desk.availableGeometry().width() <= 1024);
SetLanguage("");
setWindowTitle(title);
connect(&_signalMapper, SIGNAL(mapped(int)), this, SLOT(OnButtonClicked(int)));
_vbox = new QVBoxLayout;
_buttonsBox = new QHBoxLayout;
w = (lowRes) ? 64 : 128;
button = new QPushButton(QIcon(lowRes ? CHANGE_USER_LOW_ICON : CHANGE_USER_ICON), "", this);
button->setFixedSize(w, w);
button->setIconSize(QSize(w, w));
connect(button, SIGNAL(clicked()), this, SLOT(OnButtonChangeUser()));
_buttonsBox->addWidget(button);
button = new QPushButton(QIcon(lowRes ? ABOUT_LOW_ICON : ABOUT_ICON), "", this);
button->setFixedSize(w, w);
button->setIconSize(QSize(w, w));
connect(button, SIGNAL(clicked()), this, SLOT(OnButtonAbout()));
_buttonsBox->addWidget(button);
button = new QPushButton(QIcon(lowRes ? QUIT_LOW_ICON : QUIT_ICON), "", this);
button->setFixedSize(w, w);
button->setIconSize(QSize(w, w));
connect(button, SIGNAL(clicked()), this, SLOT(OnButtonQuit()));
_buttonsBox->addWidget(button);
_vbox->addLayout(_buttonsBox);
setLayout(_vbox);
}
wxUI::~wxUI()
{
int i;
for (i=0; i<(int)_panels.size(); i++)
_buttons[i]->disconnect(&_signalMapper, SLOT(map()));
if (_locale) delete _locale;
}
bool wxUI::SetLanguage(QString language)
{
bool res = true;
int i;
if (_locale)
{
if (language == _language) return true;
}
if (language == "")
{
QLocale default_locale = QLocale::system();
for(i=0; i<SupportedLanguages::NB_SUPPORTED_LANGUAGES; i++)
{
if (default_locale.language() == SupportedLanguages::languages[i].language)
break;
}
if (i == SupportedLanguages::NB_SUPPORTED_LANGUAGES)
i = SupportedLanguages::English;
language = SupportedLanguages::languages[i].name;
}
else
{
for(i=0; i<SupportedLanguages::NB_SUPPORTED_LANGUAGES; i++)
{
if (language == SupportedLanguages::languages[i].name)
break;
}
if (i == SupportedLanguages::NB_SUPPORTED_LANGUAGES)
return false;
}
if (_locale)
{
delete _locale;
_kiss->removeTranslator(&_translator);
}
if (i != SupportedLanguages::English)
{
if (!_translator.load(SupportedLanguages::languages[i].filename, RESOURCES_ROOT "po"))
{
i = SupportedLanguages::English;
_translator.load(SupportedLanguages::languages[i].filename, RESOURCES_ROOT "po");
_language = SupportedLanguages::languages[i].name;
res = false;
}
_kiss->installTranslator(&_translator);
}
_language = SupportedLanguages::languages[i].language;
_curLanguage = SupportedLanguages::languages[i];
_locale = new QLocale(SupportedLanguages::languages[i].language);
QLocale::setDefault(*_locale);
months[0] = _("january");
months[1] = _("february");
months[2] = _("march");
months[3] = _("april");
months[4] = _("may");
months[5] = _("june");
months[6] = _("july");
months[7] = _("august");
months[8] = _("september");
months[9] = _("october");
months[10] = _("november");
months[11] = _("december") ;
return res;
}
#define ADD_PANEL(panelName) \
panel = new panelName(_kiss, this, lowRes); \
button = panel->GetButton(); \
button->setToolTip(panel->GetToolTip()); \
_buttonsBox->insertWidget(id, button); \
_buttons.insert(_buttons.begin()+id, button); \
_panels.push_back(panel); \
_signalMapper.setMapping(button, id); \
connect(button, SIGNAL(clicked()), &_signalMapper, SLOT(map())); \
id++;
void wxUI::InitPanels()
{
KissPanel* panel;
QPushButton* button;
_panels.clear();
int id=0;
QDesktopWidget desk;
bool lowRes;
lowRes = (desk.availableGeometry().width() <= 1024);
ADD_PANEL(AccountPanel);
ADD_PANEL(StatsPanel);
ADD_PANEL(SearchPanel);
ADD_PANEL(PreferencesPanel);
ADD_PANEL(ImportPanel);
ADD_PANEL(ExportPanel);
}
void wxUI::LoadPanels()
{
std::vector<KissPanel*>::iterator it;
KissPanel* temp;
int i;
if (_curPanel)
{
_vbox->removeWidget(_curPanel);
_curPanel = 0;
}
if (_panels.size())
{
for (i=0; i<(int)_panels.size(); i++)
{
if (i == _needReload) continue;
temp = _panels[i]->CreatePanel();
_buttons[i]->disconnect(&_signalMapper, SLOT(map()));
delete _panels[i];
_panels[i] = temp;
connect(_buttons[i], SIGNAL(clicked()), &_signalMapper, SLOT(map()));
_buttons[i]->setToolTip(temp->GetToolTip());
}
}
else
InitPanels();
}
void wxUI::LoadUser()
{
User* user = _kiss->GetUser();
int i;
if (user->GetLanguage().size())
SetLanguage(user->GetLanguage());
else
SetLanguage(SupportedLanguages::languages[0].name);
// Sort with translated names
std::sort(user->_categories.begin(), user->_categories.end(), Category());
for (i=0; i < (int)user->_categories.size(); i++)
{
user->_categoriesFonts[i] = KissCount::ExtractFont(user->_categories[i].font);
}
LoadPanels();
if (_panels.size())
ShowPanel(_panels[0]);
}
void wxUI::ShowPanel(KissPanel* panel)
{
int i;
User* user = _kiss->GetUser();
if (!panel) return;
if (_curPanel)
{
_vbox->removeWidget(_curPanel);
_curPanel->hide();
}
if (_needReload)
{
user->InvalidateOperations();
for(i=0; i<(int)_panels.size(); i++)
if (_panels[i] == panel) break;
LoadPanels();
_needReload = 0;
_curPanel = _panels[i];
}
else
_curPanel = panel;
_curPanel->OnShow();
_vbox->addWidget(_curPanel);
_curPanel->show();
layout();
}
void wxUI::OnButtonClicked(int id)
{
ShowPanel(_panels[id]);
}
void wxUI::OnButtonChangeUser()
{
ChangeUser();
}
void wxUI::ChangeUser()
{
try
{
UsersDialog u(_kiss, this);
u.exec();
}
catch (UsersDialog::ExceptionNewUser e)
{
}
}
void wxUI::OnButtonAbout()
{
QMessageBox::information(0, "KissCount " APP_VERSION, _("Personal accounting software") + "\n\nhttp://indefero.soutade.fr/p/kisscount/\n\n" + _("Licenced under GNU GPL v3") + "\n\nCopyright (C) 2010-2019 Grégory Soutadé");
}
void wxUI::OnButtonQuit()
{
if (QMessageBox::question(0, "KissCount", _("Quit KissCount ?"), QMessageBox::Yes|QMessageBox::No, QMessageBox::No) == QMessageBox::Yes)
close();
}
void wxUI::GenerateMonth(int month, int year)
{
(dynamic_cast<AccountPanel*>(_panels[0]))->GenerateMonth(month, year);
}
void wxUI::UpdateStats()
{
if (_curPanel == _panels[0])
(dynamic_cast<AccountPanel*>(_panels[0]))->UpdateStats();
}
void wxUI::KillMe()
{
std::vector<KissPanel*>::iterator it;
if (_curPanel)
{
_vbox->removeWidget(_curPanel);
_curPanel = 0;
}
for (it=_panels.begin(); it!= _panels.end(); it++)
{
if (*it)
{
_buttonsBox->removeWidget(*_buttons.begin());
delete *it;
delete *_buttons.begin();
}
_buttons.erase(_buttons.begin());
}
_panels.clear();
}
void wxUI::NeedReload(KissPanel* panel)
{
unsigned i;
// Reload all panels
_needReload = -1;
if (panel)
{
for(i=0; i<_panels.size(); i++)
{
if (panel == _panels[i])
{
_needReload = i;
break;
}
}
}
}
QString wxUI::GetDateLocalFormat()
{
return _curLanguage.dateFormat;
}