Date:2010-07-28 01:46:32 (13 years 4 months ago)
Author:Maarten ter Huurne
Commit:d1d55a766642bb09b8217e9f530b2e353d7a7e4b
Message:Added MenuSettingStringBase, an abstract base class for MenuSettingString, MenuSettingFile and MenuSettingDir.

This removes a lot of duplicate source code and also decreases the binary size slightly.
Files: src/Makefile.am (2 diffs)
src/menusettingdir.cpp (1 diff)
src/menusettingdir.h (1 diff)
src/menusettingfile.cpp (2 diffs)
src/menusettingfile.h (1 diff)
src/menusettingimage.cpp (1 diff)
src/menusettingimage.h (1 diff)
src/menusettingstring.cpp (2 diffs)
src/menusettingstring.h (1 diff)
src/menusettingstringbase.cpp (1 diff)
src/menusettingstringbase.h (1 diff)

Change Details

src/Makefile.am
66    menu.cpp menusettingbool.cpp menusetting.cpp menusettingdir.cpp \
77    menusettingfile.cpp menusettingimage.cpp menusettingint.cpp \
88    menusettingmultistring.cpp menusettingrgba.cpp menusettingstring.cpp \
9    menusettingstringbase.cpp \
910    messagebox.cpp selector.cpp \
1011    settingsdialog.cpp sfontplus.cpp surfacecollection.cpp surface.cpp \
1112    textdialog.cpp textmanualdialog.cpp touchscreen.cpp translator.cpp \
...... 
1819    menu.h menusettingbool.h menusettingdir.h \
1920    menusettingfile.h menusetting.h menusettingimage.h menusettingint.h \
2021    menusettingmultistring.h menusettingrgba.h menusettingstring.h \
22    menusettingstringbase.h \
2123    messagebox.h selector.h settingsdialog.h \
2224    sfontplus.h surfacecollection.h surface.h textdialog.h textmanualdialog.h \
2325    touchscreen.h translator.h utilities.h wallpaperdialog.h \
src/menusettingdir.cpp
1919 ***************************************************************************/
2020#include "menusettingdir.h"
2121#include "dirdialog.h"
22#include "utilities.h"
2322
2423using namespace std;
2524using namespace fastdelegate;
2625
27MenuSettingDir::MenuSettingDir(GMenu2X *gmenu2x, const string &name, const string &description, string *value)
28    : MenuSetting(gmenu2x,name,description) {
26MenuSettingDir::MenuSettingDir(
27        GMenu2X *gmenu2x, const string &name,
28        const string &description, string *value)
29    : MenuSettingStringBase(gmenu2x, name, description, value)
30{
2931    IconButton *btn;
3032
31    _value = value;
32    originalValue = *value;
33
3433    btn = new IconButton(gmenu2x, "skin:imgs/buttons/x.png", gmenu2x->tr["Clear"]);
3534    btn->setAction(MakeDelegate(this, &MenuSettingDir::clear));
3635    buttonBox.add(btn);
3736
3837    btn = new IconButton(gmenu2x, "skin:imgs/buttons/b.png", gmenu2x->tr["Select a directory"]);
39    btn->setAction(MakeDelegate(this, &MenuSettingDir::select));
38    btn->setAction(MakeDelegate(this, &MenuSettingDir::edit));
4039    buttonBox.add(btn);
4140}
4241
43void MenuSettingDir::draw(int y)
44{
45    MenuSetting::draw(y);
46    gmenu2x->s->write( gmenu2x->font, value(), 155, y+gmenu2x->font->getHalfHeight(), SFontHAlignLeft, SFontVAlignMiddle );
47}
48
49void MenuSettingDir::manageInput()
50{
51    if (gmenu2x->input[ACTION_X]) setValue("");
52    if (gmenu2x->input[ACTION_B]) select();
53}
54
55void MenuSettingDir::clear()
56{
57    setValue("");
58}
59
60void MenuSettingDir::select()
42void MenuSettingDir::edit()
6143{
6244    DirDialog dd(gmenu2x, description, value());
6345    if (dd.exec()) setValue( dd.getPath() );
6446}
65
66void MenuSettingDir::setValue(const string &value)
67{
68    *_value = value;
69}
70
71const string &MenuSettingDir::value()
72{
73    return *_value;
74}
75
76void MenuSettingDir::adjustInput() {}
77
78bool MenuSettingDir::edited() {
79    return originalValue != value();
80}
src/menusettingdir.h
2020#ifndef MENUSETTINGDIR_H
2121#define MENUSETTINGDIR_H
2222
23#include "gmenu2x.h"
24#include "menusetting.h"
23#include "menusettingstringbase.h"
2524
2625using std::string;
2726
28class MenuSettingDir : public MenuSetting {
29private:
30    string originalValue;
31    string *_value;
27class MenuSettingDir : public MenuSettingStringBase {
28protected:
29    virtual void edit();
3230
33    void select();
34    void clear();
3531public:
36    MenuSettingDir(GMenu2X *gmenu2x, const string &name, const string &description, string *value);
37    virtual ~MenuSettingDir() {};
38
39    virtual void draw(int y);
40    virtual void manageInput();
41    virtual void adjustInput();
42    virtual bool edited();
43
44    void setValue(const string &value);
45    const string &value();
32    MenuSettingDir(
33            GMenu2X *gmenu2x, const string &name,
34            const string &description, string *value);
35    virtual ~MenuSettingDir() {}
4636};
4737
4838#endif
src/menusettingfile.cpp
1919 ***************************************************************************/
2020#include "menusettingfile.h"
2121#include "filedialog.h"
22#include "utilities.h"
2322
2423using namespace std;
2524using namespace fastdelegate;
2625
27MenuSettingFile::MenuSettingFile(GMenu2X *gmenu2x, const string &name, const string &description, string *value, const string &filter_)
28    : MenuSetting(gmenu2x, name, description)
29    , originalValue(*value)
30    , _value(value)
26MenuSettingFile::MenuSettingFile(
27        GMenu2X *gmenu2x, const string &name,
28        const string &description, string *value, const string &filter_)
29    : MenuSettingStringBase(gmenu2x, name, description, value)
3130    , filter(filter_)
3231{
3332    IconButton *btn;
...... 
3736    buttonBox.add(btn);
3837
3938    btn = new IconButton(gmenu2x, "skin:imgs/buttons/b.png", gmenu2x->tr["Select a file"]);
40    btn->setAction(MakeDelegate(this, &MenuSettingFile::select));
39    btn->setAction(MakeDelegate(this, &MenuSettingFile::edit));
4140    buttonBox.add(btn);
4241}
4342
44void MenuSettingFile::draw(int y)
45{
46    MenuSetting::draw(y);
47    gmenu2x->s->write( gmenu2x->font, value(), 155, y+gmenu2x->font->getHalfHeight(), SFontHAlignLeft, SFontVAlignMiddle );
48}
49
50void MenuSettingFile::manageInput()
51{
52    if (gmenu2x->input[ACTION_X]) clear();
53    if (gmenu2x->input[ACTION_B]) select();
54}
55
56void MenuSettingFile::clear()
57{
58    setValue("");
59}
60
61void MenuSettingFile::select()
43void MenuSettingFile::edit()
6244{
6345    FileDialog fd(gmenu2x, description, filter, value());
6446    if (fd.exec()) {
6547        setValue(fd.getPath() + "/" + fd.getFile());
6648    }
6749}
68
69void MenuSettingFile::setValue(const string &value)
70{
71    *_value = value;
72}
73
74const string &MenuSettingFile::value()
75{
76    return *_value;
77}
78
79void MenuSettingFile::adjustInput() {}
80
81bool MenuSettingFile::edited() {
82    return originalValue != value();
83}
src/menusettingfile.h
2020#ifndef MENUSETTINGFILE_H
2121#define MENUSETTINGFILE_H
2222
23#include "gmenu2x.h"
24#include "menusetting.h"
23#include "menusettingstringbase.h"
2524
2625using std::string;
2726
28class MenuSettingFile : public MenuSetting {
27class MenuSettingFile : public MenuSettingStringBase {
2928protected:
30    string originalValue;
31    string *_value;
32    string filter;
29    virtual void edit();
3330
34    virtual void select();
35    void clear();
31    string filter;
3632
3733public:
38    MenuSettingFile(GMenu2X *gmenu2x, const string &name,
39                    const string &description, string *value,
40                    const string &filter = "");
34    MenuSettingFile(
35            GMenu2X *gmenu2x, const string &name,
36            const string &description, string *value,
37            const string &filter = "");
4138    virtual ~MenuSettingFile() {}
42
43    virtual void draw(int y);
44    virtual void manageInput();
45    virtual void adjustInput();
46    virtual bool edited();
47
48    virtual void setValue(const string &value);
49    const string &value();
5039};
5140
5241#endif
src/menusettingimage.cpp
2828{
2929}
3030
31void MenuSettingImage::select() {
31void MenuSettingImage::edit() {
3232    ImageDialog id(gmenu2x, description, filter, value());
3333    if (id.exec()) setValue(id.getPath() + "/" + id.getFile());
3434}
src/menusettingimage.h
2626
2727class MenuSettingImage : public MenuSettingFile {
2828protected:
29    virtual void select();
29    virtual void edit();
3030
3131public:
3232    MenuSettingImage(GMenu2X *gmenu2x, const string &name,
src/menusettingstring.cpp
1919 ***************************************************************************/
2020#include "menusettingstring.h"
2121#include "inputdialog.h"
22#include "utilities.h"
2322
2423using namespace std;
2524using namespace fastdelegate;
2625
27MenuSettingString::MenuSettingString(GMenu2X *gmenu2x, const string &name, const string &description, string *value, const string &diagTitle, const string &diagIcon)
28    : MenuSetting(gmenu2x, name, description)
26MenuSettingString::MenuSettingString(
27        GMenu2X *gmenu2x, const string &name,
28        const string &description, string *value,
29        const string &diagTitle_, const string &diagIcon_)
30    : MenuSettingStringBase(gmenu2x, name, description, value)
31    , diagTitle(diagTitle_)
32    , diagIcon(diagIcon_)
2933{
3034    IconButton *btn;
3135
32    _value = value;
33    originalValue = *value;
34    this->diagTitle = diagTitle;
35    this->diagIcon = diagIcon;
36
3736    btn = new IconButton(gmenu2x, "skin:imgs/buttons/x.png", gmenu2x->tr["Clear"]);
3837    btn->setAction(MakeDelegate(this, &MenuSettingString::clear));
3938    buttonBox.add(btn);
...... 
4342    buttonBox.add(btn);
4443}
4544
46void MenuSettingString::draw(int y)
47{
48    MenuSetting::draw(y);
49    gmenu2x->s->write(gmenu2x->font, value(), 155, y+gmenu2x->font->getHalfHeight(), SFontHAlignLeft, SFontVAlignMiddle);
50}
51
52void MenuSettingString::manageInput()
53{
54    if (gmenu2x->input[ACTION_X])
55        clear();
56    if (gmenu2x->input[ACTION_B])
57        edit();
58}
59
60void MenuSettingString::setValue(const string &value)
61{
62    *_value = value;
63}
64
65const string &MenuSettingString::value()
66{
67    return *_value;
68}
69
70void MenuSettingString::adjustInput() {}
71
72void MenuSettingString::clear()
73{
74    setValue("");
75}
76
7745void MenuSettingString::edit()
7846{
79    InputDialog id(gmenu2x, gmenu2x->input, gmenu2x->ts,
80                   description, value(), diagTitle, diagIcon);
47    InputDialog id(
48            gmenu2x, gmenu2x->input, gmenu2x->ts,
49            description, value(), diagTitle, diagIcon);
8150    if (id.exec()) setValue(id.getInput());
8251}
83
84bool MenuSettingString::edited() {
85    return originalValue != value();
86}
src/menusettingstring.h
2020#ifndef MENUSETTINGSTRING_H
2121#define MENUSETTINGSTRING_H
2222
23#include "gmenu2x.h"
24#include "menusetting.h"
23#include "menusettingstringbase.h"
2524
2625using std::string;
2726
28class MenuSettingString : public MenuSetting {
29private:
30    string originalValue, diagTitle, diagIcon;
31    string *_value;
27class MenuSettingString : public MenuSettingStringBase {
28protected:
29    virtual void edit();
3230
33    void edit();
34    void clear();
31    string diagTitle, diagIcon;
3532
3633public:
37    MenuSettingString(GMenu2X *gmenu2x, const string &name, const string &description, string *value, const string &diagTitle="", const string &diagIcon="");
38    virtual ~MenuSettingString() {};
39
40    virtual void draw(int y);
41    virtual void manageInput();
42    virtual void adjustInput();
43    virtual bool edited();
44
45    void setValue(const string &value);
46    const string &value();
34    MenuSettingString(GMenu2X *gmenu2x, const string &name,
35                      const string &description, string *value,
36                      const string &diagTitle = "",
37                      const string &diagIcon = "");
38    virtual ~MenuSettingString() {}
4739};
4840
4941#endif
src/menusettingstringbase.cpp
1/***************************************************************************
2 * Copyright (C) 2006 by Massimiliano Torromeo *
3 * massimiliano.torromeo@gmail.com *
4 * *
5 * This program is free software; you can redistribute it and/or modify *
6 * it under the terms of the GNU General Public License as published by *
7 * the Free Software Foundation; either version 2 of the License, or *
8 * (at your option) any later version. *
9 * *
10 * This program is distributed in the hope that it will be useful, *
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of *
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
13 * GNU General Public License for more details. *
14 * *
15 * You should have received a copy of the GNU General Public License *
16 * along with this program; if not, write to the *
17 * Free Software Foundation, Inc., *
18 * 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. *
19 ***************************************************************************/
20#include "menusettingstringbase.h"
21
22using namespace std;
23using namespace fastdelegate;
24
25MenuSettingStringBase::MenuSettingStringBase(
26        GMenu2X *gmenu2x, const string &name,
27        const string &description, string *value)
28    : MenuSetting(gmenu2x, name, description)
29    , originalValue(*value)
30    , _value(value)
31{
32}
33
34MenuSettingStringBase::~MenuSettingStringBase()
35{
36}
37
38void MenuSettingStringBase::draw(int y)
39{
40    MenuSetting::draw(y);
41    gmenu2x->s->write(
42            gmenu2x->font, value(),
43            155, y + gmenu2x->font->getHalfHeight(),
44            SFontHAlignLeft, SFontVAlignMiddle);
45}
46
47void MenuSettingStringBase::manageInput()
48{
49    if (gmenu2x->input[ACTION_X]) clear();
50    if (gmenu2x->input[ACTION_B]) edit();
51}
52
53void MenuSettingStringBase::adjustInput()
54{
55}
56
57void MenuSettingStringBase::clear()
58{
59    setValue("");
60}
61
62bool MenuSettingStringBase::edited()
63{
64    return originalValue != value();
65}
src/menusettingstringbase.h
1/***************************************************************************
2 * Copyright (C) 2006 by Massimiliano Torromeo *
3 * massimiliano.torromeo@gmail.com *
4 * *
5 * This program is free software; you can redistribute it and/or modify *
6 * it under the terms of the GNU General Public License as published by *
7 * the Free Software Foundation; either version 2 of the License, or *
8 * (at your option) any later version. *
9 * *
10 * This program is distributed in the hope that it will be useful, *
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of *
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
13 * GNU General Public License for more details. *
14 * *
15 * You should have received a copy of the GNU General Public License *
16 * along with this program; if not, write to the *
17 * Free Software Foundation, Inc., *
18 * 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. *
19 ***************************************************************************/
20#ifndef MENUSETTINGSTRINGBASE_H
21#define MENUSETTINGSTRINGBASE_H
22
23#include "menusetting.h"
24
25using std::string;
26
27class MenuSettingStringBase : public MenuSetting {
28protected:
29    string originalValue;
30    string *_value;
31
32    virtual void edit() = 0;
33    void clear();
34
35public:
36    MenuSettingStringBase(
37            GMenu2X *gmenu2x, const string &name,
38            const string &description, string *value);
39    virtual ~MenuSettingStringBase();
40
41    virtual void draw(int y);
42    virtual void manageInput();
43    virtual void adjustInput();
44    virtual bool edited();
45
46    void setValue(const string &value) { *_value = value; }
47    const string &value() { return *_value; }
48};
49
50#endif

Archive Download the corresponding diff file



interactive