3 Copyright 2008 - 2009 by jlh (jlh at gmx dot ch)
5 This program is free software; you can redistribute it and/or modify it
6 under the terms of the GNU General Public License as published by the
7 Free Software Foundation; either version 2 of the License, version 3 of
8 the License, or (at your option) any later version.
10 This program is distributed in the hope that it will be useful, but
11 WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 General Public License for more details.
15 You should have received a copy of the GNU General Public License along
16 with this program; if not, write to the Free Software Foundation, Inc.,
17 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
19 The GNU General Public License version 2 is included with the source of
20 this program under the file name COPYING. You can also get a copy on
24 #ifndef SMARTWIDGETS_H
25 #define SMARTWIDGETS_H
27 #include <QRadioButton>
34 #include "preferences.h"
36 // Smart widgets that automatically load preferences and set them
38 // Note: Smart widgets that need to be set up with addItem()/insertItem() can
39 // not automatically load the current preference. you need to call setupDone()
40 // on them after you set them up, so they load the current preference and make the
41 // widget reflect that.
43 class SmartRadioButton
: public QRadioButton
{
46 SmartRadioButton(const QString
&label
, Preference
&p
, const char *v
) : QRadioButton(label
), preference(p
), value(v
) {
47 setChecked(preference
.toString() == value
);
48 connect(this, SIGNAL(toggled(bool)), this, SLOT(set(bool)));
52 void set(bool active
) { if (active
) preference
.set(value
); }
55 Preference
&preference
;
56 const char * const value
;
61 class SmartCheckBox
: public QCheckBox
{
64 SmartCheckBox(const QString
&label
, Preference
&p
) : QCheckBox(label
), preference(p
) {
65 setChecked(preference
.toBool());
66 connect(this, SIGNAL(toggled(bool)), this, SLOT(set(bool)));
70 void set(bool active
) { preference
.set(active
); }
73 Preference
&preference
;
78 class SmartLineEdit
: public QLineEdit
{
81 SmartLineEdit(Preference
&p
) : preference(p
) {
82 setText(preference
.toString());
83 connect(this, SIGNAL(editingFinished()), this, SLOT(set()));
87 void setText(const QString
&text
) { QLineEdit::setText(text
); set(); }
90 void set() { preference
.set(text()); }
93 Preference
&preference
;
98 class SmartComboBox
: public QComboBox
{
101 SmartComboBox(Preference
&p
) : preference(p
) { }
103 int i
= findData(preference
.toString());
105 // TODO: what to do here?
108 connect(this, SIGNAL(currentIndexChanged(int)), this, SLOT(set(int)));
113 QVariant v
= itemData(i
);
114 if (v
.type() == QVariant::Int
)
115 preference
.set(v
.toInt());
117 preference
.set(v
.toString());
121 Preference
&preference
;
126 class SmartEditableComboBox
: public QComboBox
{
129 SmartEditableComboBox(Preference
&p
) : preference(p
) {
131 lineEdit()->setText(preference
.toString());
133 // qt 4.3.2 would overwrite any text set in the constructor, during the
134 // first call to addItem
136 lineEdit()->setText(preference
.toString());
137 connect(lineEdit(), SIGNAL(editingFinished()), this, SLOT(set()));
141 void set() { preference
.set(lineEdit()->text()); }
144 Preference
&preference
;
149 class SmartSlider
: public QSlider
{
152 SmartSlider(Preference
&p
) : preference(p
) {
155 setValue(preference
.toInt());
156 connect(this, SIGNAL(valueChanged(int)), this, SLOT(set(int)));
160 void set(int value
) { preference
.set(value
); }
163 Preference
&preference
;