Constify the variables in version.cpp
[skype-call-recorder.git] / smartwidgets.h
blobf600909881a82b3d494ab0e890aa9f1d2a984736
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 public slots:
86 void setText(const QString &text) { QLineEdit::setText(text); set(); }
88 private slots:
89 void set() { preference.set(text()); }
91 private:
92 Preference &preference;
95 // --------
97 class SmartComboBox : public QComboBox {
98 Q_OBJECT
99 public:
100 SmartComboBox(Preference &p) : preference(p) { }
101 void setupDone() {
102 int i = findData(preference.toString());
103 if (i < 0)
104 // TODO: what to do here?
105 return;
106 setCurrentIndex(i);
107 connect(this, SIGNAL(currentIndexChanged(int)), this, SLOT(set(int)));
110 private slots:
111 void set(int i) {
112 QVariant v = itemData(i);
113 if (v.type() == QVariant::Int)
114 preference.set(v.toInt());
115 else
116 preference.set(v.toString());
119 private:
120 Preference &preference;
123 // --------
125 class SmartEditableComboBox : public QComboBox {
126 Q_OBJECT
127 public:
128 SmartEditableComboBox(Preference &p) : preference(p) {
129 setEditable(true);
130 lineEdit()->setText(preference.toString());
132 // qt 4.3.2 would overwrite any text set in the constructor, during the
133 // first call to addItem
134 void setupDone() {
135 lineEdit()->setText(preference.toString());
136 connect(lineEdit(), SIGNAL(editingFinished()), this, SLOT(set()));
139 private slots:
140 void set() { preference.set(lineEdit()->text()); }
142 private:
143 Preference &preference;
146 #endif