3 Copyright (C) 2008 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>
31 #include "preferences.h"
33 // Smart widgets that automatically load preferences and set them
35 // Note: Smart widgets that need to be set up with addItem()/insertItem() can
36 // not automatically load the current preference. you need to call setupDone()
37 // on them after you set them up, so they load the current preference and make the
38 // widget reflect that.
40 class SmartRadioButton
: public QRadioButton
{
43 SmartRadioButton(const QString
&label
, Preference
&p
, const char *v
) : QRadioButton(label
), preference(p
), value(v
) {
44 setChecked(preference
.toString() == value
);
45 connect(this, SIGNAL(toggled(bool)), this, SLOT(set(bool)));
49 void set(bool active
) { if (active
) preference
.set(value
); }
52 Preference
&preference
;
53 const char * const value
;
58 class SmartCheckBox
: public QCheckBox
{
61 SmartCheckBox(const QString
&label
, Preference
&p
) : QCheckBox(label
), preference(p
) {
62 setChecked(preference
.toBool());
63 connect(this, SIGNAL(toggled(bool)), this, SLOT(set(bool)));
67 void set(bool active
) { preference
.set(active
); }
70 Preference
&preference
;
75 class SmartLineEdit
: public QLineEdit
{
78 SmartLineEdit(Preference
&p
) : preference(p
) {
79 setText(preference
.toString());
80 connect(this, SIGNAL(editingFinished()), this, SLOT(set()));
84 void set() { preference
.set(text()); }
87 Preference
&preference
;
92 class SmartComboBox
: public QComboBox
{
95 SmartComboBox(Preference
&p
) : preference(p
) { }
97 int i
= findData(preference
.toString());
99 // TODO: what to do here?
102 connect(this, SIGNAL(currentIndexChanged(int)), this, SLOT(set(int)));
107 QVariant v
= itemData(i
);
108 if (v
.type() == QVariant::Int
)
109 preference
.set(v
.toInt());
111 preference
.set(v
.toString());
115 Preference
&preference
;
120 class SmartEditableComboBox
: public QComboBox
{
123 SmartEditableComboBox(Preference
&p
) : preference(p
) {
125 lineEdit()->setText(preference
.toString());
127 // qt 4.3.2 would overwrite any text set in the constructor, during the
128 // first call to addItem
130 lineEdit()->setText(preference
.toString());
131 connect(lineEdit(), SIGNAL(editingFinished()), this, SLOT(set()));
135 void set() { preference
.set(lineEdit()->text()); }
138 Preference
&preference
;