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>
33 #include "preferences.h"
35 // Smart widgets that automatically load preferences and set them
37 // Note: Smart widgets that need to be set up with addItem()/insertItem() can
38 // not automatically load the current preference. you need to call setupDone()
39 // on them after you set them up, so they load the current preference and make the
40 // widget reflect that.
42 class SmartRadioButton
: public QRadioButton
{
45 SmartRadioButton(const QString
&label
, Preference
&p
, const char *v
) : QRadioButton(label
), preference(p
), value(v
) {
46 setChecked(preference
.toString() == value
);
47 connect(this, SIGNAL(toggled(bool)), this, SLOT(set(bool)));
51 void set(bool active
) { if (active
) preference
.set(value
); }
54 Preference
&preference
;
55 const char * const value
;
60 class SmartCheckBox
: public QCheckBox
{
63 SmartCheckBox(const QString
&label
, Preference
&p
) : QCheckBox(label
), preference(p
) {
64 setChecked(preference
.toBool());
65 connect(this, SIGNAL(toggled(bool)), this, SLOT(set(bool)));
69 void set(bool active
) { preference
.set(active
); }
72 Preference
&preference
;
77 class SmartLineEdit
: public QLineEdit
{
80 SmartLineEdit(Preference
&p
) : preference(p
) {
81 setText(preference
.toString());
82 connect(this, SIGNAL(editingFinished()), this, SLOT(set()));
86 void set() { preference
.set(text()); }
89 Preference
&preference
;
94 class SmartComboBox
: public QComboBox
{
97 SmartComboBox(Preference
&p
) : preference(p
) { }
99 int i
= findData(preference
.toString());
101 // TODO: what to do here?
104 connect(this, SIGNAL(currentIndexChanged(int)), this, SLOT(set(int)));
109 QVariant v
= itemData(i
);
110 if (v
.type() == QVariant::Int
)
111 preference
.set(v
.toInt());
113 preference
.set(v
.toString());
117 Preference
&preference
;
122 class SmartEditableComboBox
: public QComboBox
{
125 SmartEditableComboBox(Preference
&p
) : preference(p
) {
127 lineEdit()->setText(preference
.toString());
129 // qt 4.3.2 would overwrite any text set in the constructor, during the
130 // first call to addItem
132 lineEdit()->setText(preference
.toString());
133 connect(lineEdit(), SIGNAL(editingFinished()), this, SLOT(set()));
137 void set() { preference
.set(lineEdit()->text()); }
140 Preference
&preference
;