Add 2009 to copyright and fix its format
[skype-call-recorder.git] / smartwidgets.h
blob7481da0caeb435b2afb909c2cdffb7ec01250e76
1 /*
2 Skype Call Recorder
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
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>
31 #include <QSlider>
33 #include "common.h"
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 {
44 Q_OBJECT
45 public:
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)));
51 private slots:
52 void set(bool active) { if (active) preference.set(value); }
54 private:
55 Preference &preference;
56 const char * const value;
59 // --------
61 class SmartCheckBox : public QCheckBox {
62 Q_OBJECT
63 public:
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)));
69 private slots:
70 void set(bool active) { preference.set(active); }
72 private:
73 Preference &preference;
76 // --------
78 class SmartLineEdit : public QLineEdit {
79 Q_OBJECT
80 public:
81 SmartLineEdit(Preference &p) : preference(p) {
82 setText(preference.toString());
83 connect(this, SIGNAL(editingFinished()), this, SLOT(set()));
86 public slots:
87 void setText(const QString &text) { QLineEdit::setText(text); set(); }
89 private slots:
90 void set() { preference.set(text()); }
92 private:
93 Preference &preference;
96 // --------
98 class SmartComboBox : public QComboBox {
99 Q_OBJECT
100 public:
101 SmartComboBox(Preference &p) : preference(p) { }
102 void setupDone() {
103 int i = findData(preference.toString());
104 if (i < 0)
105 // TODO: what to do here?
106 return;
107 setCurrentIndex(i);
108 connect(this, SIGNAL(currentIndexChanged(int)), this, SLOT(set(int)));
111 private slots:
112 void set(int i) {
113 QVariant v = itemData(i);
114 if (v.type() == QVariant::Int)
115 preference.set(v.toInt());
116 else
117 preference.set(v.toString());
120 private:
121 Preference &preference;
124 // --------
126 class SmartEditableComboBox : public QComboBox {
127 Q_OBJECT
128 public:
129 SmartEditableComboBox(Preference &p) : preference(p) {
130 setEditable(true);
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
135 void setupDone() {
136 lineEdit()->setText(preference.toString());
137 connect(lineEdit(), SIGNAL(editingFinished()), this, SLOT(set()));
140 private slots:
141 void set() { preference.set(lineEdit()->text()); }
143 private:
144 Preference &preference;
147 // --------
149 class SmartSlider : public QSlider {
150 Q_OBJECT
151 public:
152 SmartSlider(Preference &p) : preference(p) {
154 void setupDone() {
155 setValue(preference.toInt());
156 connect(this, SIGNAL(valueChanged(int)), this, SLOT(set(int)));
159 private slots:
160 void set(int value) { preference.set(value); }
162 private:
163 Preference &preference;
166 #endif