Replace strings representing preference names with constants
[skype-call-recorder.git] / smartwidgets.h
blob6451a75d77de5c45aba6f13052cff9319850076a
1 /*
2 Skype Call Recorder
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
21 http://www.fsf.org/
24 #ifndef SMARTWIDGETS_H
25 #define SMARTWIDGETS_H
27 #include <QRadioButton>
28 #include <QCheckBox>
29 #include <QLineEdit>
30 #include <QComboBox>
32 #include "common.h"
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 {
43 Q_OBJECT
44 public:
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)));
50 private slots:
51 void set(bool active) { if (active) preference.set(value); }
53 private:
54 Preference &preference;
55 const char * const value;
58 // --------
60 class SmartCheckBox : public QCheckBox {
61 Q_OBJECT
62 public:
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)));
68 private slots:
69 void set(bool active) { preference.set(active); }
71 private:
72 Preference &preference;
75 // --------
77 class SmartLineEdit : public QLineEdit {
78 Q_OBJECT
79 public:
80 SmartLineEdit(Preference &p) : preference(p) {
81 setText(preference.toString());
82 connect(this, SIGNAL(editingFinished()), this, SLOT(set()));
85 private slots:
86 void set() { preference.set(text()); }
88 private:
89 Preference &preference;
92 // --------
94 class SmartComboBox : public QComboBox {
95 Q_OBJECT
96 public:
97 SmartComboBox(Preference &p) : preference(p) { }
98 void setupDone() {
99 int i = findData(preference.toString());
100 if (i < 0)
101 // TODO: what to do here?
102 return;
103 setCurrentIndex(i);
104 connect(this, SIGNAL(currentIndexChanged(int)), this, SLOT(set(int)));
107 private slots:
108 void set(int i) {
109 QVariant v = itemData(i);
110 if (v.type() == QVariant::Int)
111 preference.set(v.toInt());
112 else
113 preference.set(v.toString());
116 private:
117 Preference &preference;
120 // --------
122 class SmartEditableComboBox : public QComboBox {
123 Q_OBJECT
124 public:
125 SmartEditableComboBox(Preference &p) : preference(p) {
126 setEditable(true);
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
131 void setupDone() {
132 lineEdit()->setText(preference.toString());
133 connect(lineEdit(), SIGNAL(editingFinished()), this, SLOT(set()));
136 private slots:
137 void set() { preference.set(lineEdit()->text()); }
139 private:
140 Preference &preference;
143 #endif