Use a factory to create panels instead of hard coded panels

This commit is contained in:
Grégory Soutadé 2010-12-20 19:28:24 +01:00
parent 9101da3e2b
commit 7c85467a5a
4 changed files with 183 additions and 362 deletions

View File

@ -1,118 +0,0 @@
/*
Copyright 2010 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 "ButtonPanel.h"
enum {BUTTON_ACCOUNT_ID=1, BUTTON_STATS_ID, BUTTON_SEARCH_ID, BUTTON_PREFS_ID, BUTTON_CHANGE_USER_ID, BUTTON_ABOUT_ID, BUTTON_QUIT_ID};
BEGIN_EVENT_TABLE(ButtonPanel, wxPanel)
EVT_BUTTON(BUTTON_ACCOUNT_ID, ButtonPanel::OnButtonAccount)
EVT_BUTTON(BUTTON_STATS_ID, ButtonPanel::OnButtonStats)
EVT_BUTTON(BUTTON_SEARCH_ID, ButtonPanel::OnButtonSearch)
EVT_BUTTON(BUTTON_PREFS_ID, ButtonPanel::OnButtonPreferences)
EVT_BUTTON(BUTTON_CHANGE_USER_ID, ButtonPanel::OnButtonChangeUser)
EVT_BUTTON(BUTTON_ABOUT_ID, ButtonPanel::OnButtonAbout)
EVT_BUTTON(BUTTON_QUIT_ID, ButtonPanel::OnButtonQuit)
END_EVENT_TABLE()
ButtonPanel::ButtonPanel(KissCount* kiss, wxUI *parent) : wxPanel(&(*parent)), _kiss(kiss), _wxUI(parent)
{
wxBoxSizer *hbox = new wxBoxSizer(wxHORIZONTAL);
_account = new wxBitmapButton(this, BUTTON_ACCOUNT_ID, wxBitmap(wxT(ACCOUNT_ICON)), wxDefaultPosition, wxSize(128, 128));
_stats = new wxBitmapButton(this, BUTTON_STATS_ID, wxBitmap(wxT(STATS_ICON)), wxDefaultPosition, wxSize(128, 128));
_search = new wxBitmapButton(this, BUTTON_SEARCH_ID, wxBitmap(wxT(SEARCH_ICON)), wxDefaultPosition, wxSize(128, 128));
_prefs = new wxBitmapButton(this, BUTTON_PREFS_ID, wxBitmap(wxT(PREFS_ICON)), wxDefaultPosition, wxSize(128, 128));
_changeUser = new wxBitmapButton(this, BUTTON_CHANGE_USER_ID, wxBitmap(wxT(CHANGE_USER_ICON)), wxDefaultPosition, wxSize(128, 128));
_about = new wxBitmapButton(this, BUTTON_ABOUT_ID, wxBitmap(wxT(ABOUT_ICON)), wxDefaultPosition, wxSize(128, 128));
_quit = new wxBitmapButton(this, BUTTON_QUIT_ID, wxBitmap(wxT(QUIT_ICON)), wxDefaultPosition, wxSize(128, 128));
SetSizer(hbox);
hbox->Add(_account);
hbox->Add(_stats);
hbox->Add(_search);
hbox->Add(_prefs);
hbox->Add(_changeUser);
hbox->Add(_about);
hbox->Add(_quit);
Fit();
SetMinSize(GetSize());
}
ButtonPanel::~ButtonPanel()
{
delete _account;
delete _stats;
delete _prefs;
delete _changeUser;
}
void ButtonPanel::SetToolTips()
{
_account->SetToolTip(_("Operations"));
_stats->SetToolTip(_("Statistics"));
_search->SetToolTip(_("Search"));
_prefs->SetToolTip(_("Preferences"));
_changeUser->SetToolTip(_("Change user"));
_about->SetToolTip(_("About"));
_quit->SetToolTip(_("Quit"));
}
void ButtonPanel::OnButtonAccount(wxCommandEvent& event)
{
_wxUI->ShowAccount();
}
void ButtonPanel::OnButtonStats(wxCommandEvent& event)
{
_wxUI->ShowStats();
}
void ButtonPanel::OnButtonSearch(wxCommandEvent& event)
{
_wxUI->ShowSearch();
}
void ButtonPanel::OnButtonPreferences(wxCommandEvent& event)
{
_wxUI->ShowPreferences();
}
void ButtonPanel::OnButtonChangeUser(wxCommandEvent& event)
{
_wxUI->ChangeUser();
}
void ButtonPanel::OnButtonAbout(wxCommandEvent& event)
{
wxMessageBox( _("Personal accounting software\n\nhttp://indefero.soutade.fr/p/kisscount/\n\nLicenced under GNU GPL v3\n\nCopyright (C) 2010 Grégory Soutadé"),
wxT("KissCount " APP_VERSION "\n\n"),
wxOK | wxICON_INFORMATION, _wxUI );
}
void ButtonPanel::OnButtonQuit(wxCommandEvent& event)
{
wxMessageDialog dialog(_wxUI, _("Quit KissCount ?"), wxT("KissCount"), wxYES_NO);
if (dialog.ShowModal() == wxID_NO)
{
return;
}
_wxUI->Close(true);
}

View File

@ -1,64 +0,0 @@
/*
Copyright 2010 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/>.
*/
#ifndef BUTTONPANEL_H
#define BUTTONPANEL_H
#include <wx/wx.h>
#include <controller/KissCount.h>
#include "wxUI.h"
#include "view.h"
class KissCount;
class wxUI;
class ButtonPanel: public wxPanel
{
public:
ButtonPanel(KissCount* kiss, wxUI *parent);
~ButtonPanel();
void SetToolTips();
void OnButtonAccount(wxCommandEvent& event);
void OnButtonStats(wxCommandEvent& event);
void OnButtonSearch(wxCommandEvent& event);
void OnButtonPreferences(wxCommandEvent& event);
void OnButtonChangeUser(wxCommandEvent& event);
void OnButtonAbout(wxCommandEvent& event);
void OnButtonQuit(wxCommandEvent& event);
private:
KissCount* _kiss;
wxUI* _wxUI;
wxBitmapButton* _account;
wxBitmapButton* _stats;
wxBitmapButton* _search;
wxBitmapButton* _prefs;
wxBitmapButton* _changeUser;
wxBitmapButton* _about;
wxBitmapButton* _quit;
DECLARE_EVENT_TABLE();
};
#endif

View File

@ -19,38 +19,64 @@
#include "wxUI.h"
enum {BUTTON_CHANGE_USER_ID=-4, BUTTON_ABOUT_ID=-5, BUTTON_QUIT_ID=-6};
BEGIN_EVENT_TABLE(wxUI, wxFrame)
EVT_BUTTON(BUTTON_CHANGE_USER_ID, wxUI::OnButtonChangeUser)
EVT_BUTTON(BUTTON_ABOUT_ID, wxUI::OnButtonAbout)
EVT_BUTTON(BUTTON_QUIT_ID, wxUI::OnButtonQuit)
END_EVENT_TABLE()
wxString months[12] ;
wxColour categoryColors[12] = {wxColour(0x00, 0x45, 0x86),
wxColour(0xFF, 0x3E, 0x0E),
wxColour(0xFF, 0xD3, 0x20),
wxColour(0x58, 0x9D, 0x1B),
wxColour(0x7E, 0x00, 0x21),
wxColour(0x83, 0xCC, 0xFF),
wxColour(0x31, 0x40, 0x04),
wxColour(0xB0, 0xCF, 0x00),
wxColour(0x4B, 0x1F, 0x6F),
wxColour(0xFF, 0x93, 0x0E),
wxColour(0xC5, 0x00, 0x0D),
wxColour(0x00, 0x84, 0xD1)};
wxColour(0xFF, 0x3E, 0x0E),
wxColour(0xFF, 0xD3, 0x20),
wxColour(0x58, 0x9D, 0x1B),
wxColour(0x7E, 0x00, 0x21),
wxColour(0x83, 0xCC, 0xFF),
wxColour(0x31, 0x40, 0x04),
wxColour(0xB0, 0xCF, 0x00),
wxColour(0x4B, 0x1F, 0x6F),
wxColour(0xFF, 0x93, 0x0E),
wxColour(0xC5, 0x00, 0x0D),
wxColour(0x00, 0x84, 0xD1)};
wxUI::wxUI(KissCount* kiss, const wxString& title, const wxPoint& pos, const wxSize& size)
: wxFrame(NULL, wxID_ANY, title, pos, size), _kiss(kiss), _buttonPanel(NULL), _accountPanel(NULL), _statsPanel(NULL),
_searchPanel(NULL), _preferencesPanel(NULL), _curPanel(NULL), _locale(NULL), _needReload(false)
: wxFrame(NULL, -1, title, pos, size), _kiss(kiss),
_curPanel(NULL), _locale(NULL), _needReload(false)
{
_hbox = new wxBoxSizer(wxVERTICAL);
_buttonPanel = new ButtonPanel(_kiss, this);
_vbox = new wxBoxSizer(wxVERTICAL);
_buttonsBox = new wxBoxSizer(wxHORIZONTAL);
// ButtonPanel* buttons = new ButtonPanel(_kiss, this);
// wxMenu *menuFile = new wxMenu;
SetSizer(_hbox);
// menuFile->Append( ID_About, wxT("&About...") );
// menuFile->AppendSeparator();
// menuFile->Append( ID_Quit, wxT("E&xit") );
_hbox->Add(_buttonPanel);
// wxMenuBar *menuBar = new wxMenuBar;
// menuBar->Append( menuFile, wxT("&File") );
// SetMenuBar( menuBar );
// CreateStatusBar();
// SetStatusText( wxT("Welcome to wxWidgets!") );
_buttonsBox->Add(new wxBitmapButton(this, BUTTON_CHANGE_USER_ID, wxBitmap(wxT(CHANGE_USER_ICON)), wxDefaultPosition, wxSize(128, 128)));
_buttonsBox->Add(new wxBitmapButton(this, BUTTON_ABOUT_ID, wxBitmap(wxT(ABOUT_ICON)), wxDefaultPosition, wxSize(128, 128)));
_buttonsBox->Add(new wxBitmapButton(this, BUTTON_QUIT_ID, wxBitmap(wxT(QUIT_ICON)), wxDefaultPosition, wxSize(128, 128)));
SetSizer(_vbox);
_vbox->Add(_buttonsBox, 0, wxGROW);
}
wxUI::~wxUI()
{
if (_accountPanel) delete _accountPanel;
if (_preferencesPanel) delete _preferencesPanel;
if (_searchPanel) delete _searchPanel;
if (_statsPanel) delete _statsPanel;
int i;
for (i=0; i<(int)_panels.size(); i++)
Disconnect(i, wxEVT_COMMAND_BUTTON_CLICKED, wxCommandEventHandler(wxUI::OnButtonClicked), _panels[i], this);
if (_locale) delete _locale;
}
@ -64,31 +90,31 @@ bool wxUI::SetLanguage(long language)
// load language if possible, fall back to english otherwise
if(wxLocale::IsAvailable(language))
{
_locale = new wxLocale( language, wxLOCALE_CONV_ENCODING );
_locale = new wxLocale( language, wxLOCALE_CONV_ENCODING );
#ifdef __WXGTK__
_locale->AddCatalogLookupPathPrefix(wxT("./ressources/po"));
_locale->AddCatalogLookupPathPrefix(wxT("./ressources/po"));
#endif
_locale->AddCatalog(wxT("french"));
_locale->AddCatalog(wxT("kisscount"));
_locale->AddCatalog(wxT("french"));
_locale->AddCatalog(wxT("kisscount"));
_language = (wxLanguage) language;
_language = (wxLanguage) language;
}
if (_locale == NULL || !_locale->IsOk())
{
if (_locale) delete _locale;
_locale = new wxLocale();
if (_locale) delete _locale;
_locale = new wxLocale();
#ifdef __WXGTK__
_locale->AddCatalogLookupPathPrefix(wxT("./ressources/po"));
_locale->AddCatalogLookupPathPrefix(wxT("./ressources/po"));
#endif
_locale->AddCatalog(wxT("kisscount"));
_locale->AddCatalog(wxT("kisscount"));
_language = wxLANGUAGE_ENGLISH;
res = false;
_language = wxLANGUAGE_ENGLISH;
res = false;
}
months[0] = _("january");
@ -104,30 +130,108 @@ bool wxUI::SetLanguage(long language)
months[10] = _("november");
months[11] = _("december") ;
if (_buttonPanel)
_buttonPanel->SetToolTips();
return res;
}
void wxUI::ShowAccount()
#define ADD_PANEL(panelName, id) \
panel = new panelName(_kiss, this); \
button = panel->GetButton(id); \
button->SetToolTip(panel->GetToolTip()); \
_buttonsBox->Insert(id, button); \
_buttons.insert(_buttons.begin()+id, button); \
_panels.push_back(panel); \
Connect(id, wxEVT_COMMAND_BUTTON_CLICKED, wxCommandEventHandler(wxUI::OnButtonClicked), panel, this);
void wxUI::InitPanels()
{
ShowPanel(_accountPanel);
KissPanel* panel;
wxBitmapButton* button;
_panels.clear();
ADD_PANEL(AccountPanel, 0);
ADD_PANEL(StatsPanel, 1);
ADD_PANEL(SearchPanel, 2);
ADD_PANEL(PreferencesPanel, 3);
}
void wxUI::ShowSearch()
void wxUI::LoadPanels()
{
ShowPanel(_searchPanel);
std::vector<KissPanel*>::iterator it;
KissPanel* temp;
int i;
if (_curPanel)
{
_vbox->Detach(_curPanel);
_curPanel = NULL;
}
if (_panels.size())
{
for (i=0; i<(int)_panels.size(); i++)
{
temp = _panels[i]->CreatePanel();
Disconnect(i, wxEVT_COMMAND_BUTTON_CLICKED, wxCommandEventHandler(wxUI::OnButtonClicked), _panels[i], this);
_panels[i] = temp;
Connect(i, wxEVT_COMMAND_BUTTON_CLICKED, wxCommandEventHandler(wxUI::OnButtonClicked), temp, this);
std::cout << temp->GetToolTip().mb_str() << "\n";
_buttons[i]->SetToolTip(temp->GetToolTip());
}
}
else
InitPanels();
}
void wxUI::ShowStats()
void wxUI::LoadUser()
{
ShowPanel(_statsPanel);
User* user = _kiss->GetUser();
LoadPanels();
if (user->_preferences[wxT("language")] != wxT(""))
SetLanguage(user->GetLanguage());
if (_panels.size())
ShowPanel(_panels[0]);
}
void wxUI::ShowPreferences()
void wxUI::ShowPanel(KissPanel* panel)
{
ShowPanel(_preferencesPanel);
wxShowEvent event;
int i;
if (!panel) return;
if (_curPanel)
{
_vbox->Detach(_curPanel);
_curPanel->Hide();
}
if (_needReload)
{
for(i=0; i<(int)_panels.size(); i++)
if (_panels[i] == panel) break;
LoadPanels();
_needReload = false;
_curPanel = _panels[i];
}
else
_curPanel = panel;
_curPanel->OnShow(event);
_vbox->Add(_curPanel);
_curPanel->Show();
Layout();
}
void wxUI::OnButtonClicked(wxCommandEvent& event)
{
ShowPanel(_panels[event.GetId()]);
}
void wxUI::OnButtonChangeUser(wxCommandEvent& event)
{
ChangeUser();
}
void wxUI::ChangeUser()
@ -135,151 +239,47 @@ void wxUI::ChangeUser()
UsersDialog u(_kiss, this);
}
void wxUI::LoadUser()
void wxUI::OnButtonAbout(wxCommandEvent& event)
{
User* user = _kiss->GetUser();
wxShowEvent event;
if (_curPanel)
{
_hbox->Detach(_curPanel);
_curPanel = NULL;
}
if (_accountPanel)
delete _accountPanel;
if (_preferencesPanel)
delete _preferencesPanel;
if (_searchPanel)
delete _searchPanel;
if (_statsPanel)
delete _statsPanel;
if (user->_preferences[wxT("language")].Length())
SetLanguage(user->GetLanguage());
_accountPanel = new AccountPanel(_kiss, this);
_statsPanel = new StatsPanel(_kiss, this);
_searchPanel = new SearchPanel(_kiss, this);
_preferencesPanel = new PreferencesPanel(_kiss, this);
ShowPanel(_accountPanel);
_accountPanel->OnShow(event);
wxMessageBox( _("Personal accounting software\n\nhttp://indefero.soutade.fr/p/kisscount/\n\nLicenced under GNU GPL v3\n\nCopyright (C) 2010 Grégory Soutadé"),
wxT("KissCount " APP_VERSION "\n\n"),
wxOK | wxICON_INFORMATION, this );
}
void wxUI::ShowPanel(wxPanel* panel)
void wxUI::OnButtonQuit(wxCommandEvent& event)
{
int month, year=-1, account=0, preferences=0, search=0, stats=0;
wxShowEvent event;
if (!panel || (panel == _curPanel && !_needReload)) return;
if (_curPanel)
wxMessageDialog dialog(this, _("Quit KissCount ?"), wxT("KissCount"), wxYES_NO);
if (dialog.ShowModal() == wxID_NO)
{
_hbox->Detach(_curPanel);
_curPanel->Hide();
return;
}
if (_needReload)
{
if (panel == _accountPanel)
{
account = 1;
month = _accountPanel->_curMonth;
year = _accountPanel->_curYear;
}
if (panel == _preferencesPanel)
{
preferences = 1;
}
if (panel == _searchPanel)
{
search = 1;
}
if (panel == _statsPanel)
{
stats = 1;
}
delete _accountPanel;
delete _preferencesPanel;
delete _searchPanel;
delete _statsPanel;
_accountPanel = new AccountPanel(_kiss, this);
if (year != -1)
{
_kiss->LoadYear(year, true);
_accountPanel->ShowMonth(month, year);
}
_preferencesPanel = new PreferencesPanel(_kiss, this);
_searchPanel = new SearchPanel(_kiss, this);
_statsPanel = new StatsPanel(_kiss, this);
if (account)
{
_accountPanel->OnShow(event);
panel = _accountPanel;
}
else if (preferences)
{
_preferencesPanel->OnShow(event);
panel = _preferencesPanel;
}
else if (search)
{
_searchPanel->OnShow(event);
panel = _searchPanel;
}
else if (stats)
{
_statsPanel->OnShow(event);
panel = _statsPanel;
}
_hbox->Detach(_accountPanel);
_accountPanel->Hide();
_needReload = false;
}
_curPanel = panel;
_hbox->Add(panel, 1, wxGROW|wxALL, 0);
_curPanel->Show();
Layout();
Close(true);
}
void wxUI::GenerateMonth(int month, int year)
{
_accountPanel->GenerateMonth(month, year);
((AccountPanel*)_panels[0])->GenerateMonth(month, year);
}
void wxUI::KillMe()
{
std::vector<KissPanel*>::iterator it;
if (_curPanel)
{
_hbox->Detach(_curPanel);
_curPanel = NULL;
_vbox->Detach(_curPanel);
_curPanel = NULL;
}
if (_accountPanel)
delete _accountPanel;
for (it=_panels.begin(); it!= _panels.end(); it++)
{
if (*it) delete *it;
_buttonsBox->Remove(0);
_buttons.erase(_buttons.begin());
}
if (_preferencesPanel)
delete _preferencesPanel;
if (_searchPanel)
delete _searchPanel;
_accountPanel = NULL;
_preferencesPanel = NULL;
_searchPanel = NULL;
_panels.clear();
}
void wxUI::NeedReload()

View File

@ -22,7 +22,6 @@
#include <wx/wx.h>
#include "AccountPanel.h"
#include "ButtonPanel.h"
#include "PreferencesPanel.h"
#include "UsersDialog.h"
#include "GenerateDialog.h"
@ -35,10 +34,8 @@
#include "grid/wxGridCellButtonEditor.h"
class KissCount;
class ButtonPanel;
class AccountPanel;
class PreferencesPanel;
class StatsPanel;
extern wxString months[12];
extern wxColour categoryColors[12];
@ -56,29 +53,35 @@ public:
void LoadUser();
void ShowAccount();
void ShowStats();
void ShowSearch();
void ShowPreferences();
void GenerateMonth(int month, int year);
void KillMe();
void ShowPanel(wxPanel* panel);
void ShowPanel(KissPanel* panel);
void NeedReload();
wxLanguage _language;
void OnButtonClicked(wxCommandEvent& event);
void OnButtonChangeUser(wxCommandEvent& event);
void OnButtonAbout(wxCommandEvent& event);
void OnButtonQuit(wxCommandEvent& event);
private:
KissCount *_kiss;
wxBoxSizer *_hbox;
ButtonPanel *_buttonPanel;
AccountPanel *_accountPanel;
StatsPanel *_statsPanel;
SearchPanel *_searchPanel;
PreferencesPanel *_preferencesPanel;
wxPanel *_curPanel;
wxBoxSizer *_vbox, *_buttonsBox;
KissPanel *_curPanel;
std::vector<KissPanel*> _panels;
std::vector<wxBitmapButton*> _buttons;
wxLocale *_locale;
bool _needReload;
void InitPanels();
void LoadPanels();
DECLARE_EVENT_TABLE();
};
#endif