Fix compiler warnings
[quassel.git] / src / uisupport / settingspage.cpp
1 /***************************************************************************
2  *   Copyright (C) 2005-09 by the Quassel Project                          *
3  *   devel@quassel-irc.org                                                 *
4  *                                                                         *
5  *   This program is free software; you can redistribute it and/or modify  *
6  *   it under the terms of the GNU General Public License as published by  *
7  *   the Free Software Foundation; either version 2 of the License, or     *
8  *   (at your option) any later version.                                   *
9  *                                                                         *
10  *   This program is distributed in the hope that it will be useful,       *
11  *   but WITHOUT ANY WARRANTY; without even the implied warranty of        *
12  *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the         *
13  *   GNU General Public License for more details.                          *
14  *                                                                         *
15  *   You should have received a copy of the GNU General Public License     *
16  *   along with this program; if not, write to the                         *
17  *   Free Software Foundation, Inc.,                                       *
18  *   59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.             *
19  ***************************************************************************/
20
21 #include "settingspage.h"
22
23 #include <QCheckBox>
24 #include <QComboBox>
25 #include <QSpinBox>
26 #include <QVariant>
27
28 #include "uisettings.h"
29
30 SettingsPage::SettingsPage(const QString &category, const QString &title, QWidget *parent)
31   : QWidget(parent),
32     _category(category),
33     _title(title),
34     _changed(false),
35     _autoWidgetsChanged(false)
36 {
37 }
38
39 void SettingsPage::setChangedState(bool hasChanged_) {
40   if(hasChanged_ != _changed) {
41     bool old = hasChanged();
42     _changed = hasChanged_;
43     if(hasChanged() != old)
44       emit changed(hasChanged());
45   }
46 }
47
48 void SettingsPage::load(QCheckBox *box, bool checked) {
49   box->setProperty("StoredValue", checked);
50   box->setChecked(checked);
51 }
52
53 bool SettingsPage::hasChanged(QCheckBox *box) {
54   return box->property("StoredValue").toBool() != box->isChecked();
55 }
56
57
58 void SettingsPage::load(QComboBox *box, int index) {
59   box->setProperty("StoredValue", index);
60   box->setCurrentIndex(index);
61 }
62
63 bool SettingsPage::hasChanged(QComboBox *box) {
64   return box->property("StoredValue").toInt() != box->currentIndex();
65 }
66
67 void SettingsPage::load(QSpinBox *box, int value) {
68   box->setProperty("StoredValue", value);
69   box->setValue(value);
70 }
71
72 bool SettingsPage::hasChanged(QSpinBox *box) {
73   return box->property("StoredValue").toInt() != box->value();
74 }
75
76 /*** Auto child widget handling ***/
77
78 void SettingsPage::initAutoWidgets() {
79   _autoWidgets.clear();
80
81   // find all descendants that should be considered auto widgets
82   // we need to climb the QObject tree recursively
83   findAutoWidgets(this, &_autoWidgets);
84
85   foreach(QObject *widget, _autoWidgets) {
86     if(widget->inherits("ColorButton"))
87       connect(widget, SIGNAL(colorChanged(QColor)), SLOT(autoWidgetHasChanged()));
88     else if(widget->inherits("QAbstractButton") || widget->inherits("QGroupBox"))
89       connect(widget, SIGNAL(toggled(bool)), SLOT(autoWidgetHasChanged()));
90     else if(widget->inherits("QLineEdit") || widget->inherits("QTextEdit"))
91       connect(widget, SIGNAL(textChanged(const QString &)), SLOT(autoWidgetHasChanged()));
92     else if(widget->inherits("QComboBox"))
93       connect(widget, SIGNAL(currentIndexChanged(int)), SLOT(autoWidgetHasChanged()));
94     else if(widget->inherits("QSpinBox"))
95       connect(widget, SIGNAL(valueChanged(int)), SLOT(autoWidgetHasChanged()));
96     else if(widget->inherits("FontSelector"))
97       connect(widget, SIGNAL(fontChanged(QFont)), SLOT(autoWidgetHasChanged()));
98     else
99       qWarning() << "SettingsPage::init(): Unknown autoWidget type" << widget->metaObject()->className();
100   }
101 }
102
103 void SettingsPage::findAutoWidgets(QObject *parent, QObjectList *autoList) const {
104   foreach(QObject *child, parent->children()) {
105     if(child->property("settingsKey").isValid())
106       autoList->append(child);
107     findAutoWidgets(child, autoList);
108   }
109 }
110
111 QByteArray SettingsPage::autoWidgetPropertyName(QObject *widget) const {
112   QByteArray prop;
113   if(widget->inherits("ColorButton"))
114     prop = "color";
115   else if(widget->inherits("QAbstractButton") || widget->inherits("QGroupBox"))
116     prop = "checked";
117   else if(widget->inherits("QLineEdit") || widget->inherits("QTextEdit"))
118     prop = "text";
119   else if(widget->inherits("QComboBox"))
120     prop = "currentIndex";
121   else if(widget->inherits("QSpinBox"))
122     prop = "value";
123   else if(widget->inherits("FontSelector"))
124     prop = "selectedFont";
125   else
126     qWarning() << "SettingsPage::autoWidgetPropertyName(): Unhandled widget type for" << widget;
127
128   return prop;
129 }
130
131 QString SettingsPage::autoWidgetSettingsKey(QObject *widget) const {
132   QString key = widget->property("settingsKey").toString();
133   if(key.isEmpty())
134     return QString("");
135   if(key.startsWith('/'))
136     key.remove(0, 1);
137   else
138     key.prepend(settingsKey() + '/');
139   return key;
140 }
141
142 void SettingsPage::autoWidgetHasChanged() {
143   bool changed_ = false;
144   foreach(QObject *widget, _autoWidgets) {
145     QVariant curValue = widget->property(autoWidgetPropertyName(widget));
146     if(!curValue.isValid())
147       qWarning() << "SettingsPage::autoWidgetHasChanged(): Unknown property";
148
149     if(curValue != widget->property("storedValue")) {
150       changed_ = true;
151       break;
152     }
153   }
154
155   if(changed_ != _autoWidgetsChanged) {
156     bool old = hasChanged();
157     _autoWidgetsChanged = changed_;
158     if(hasChanged() != old)
159       emit changed(hasChanged());
160   }
161 }
162
163 void SettingsPage::load() {
164   UiSettings s("");
165   foreach(QObject *widget, _autoWidgets) {
166     QString key = autoWidgetSettingsKey(widget);
167     QVariant val;
168     if(key.isEmpty())
169       val = loadAutoWidgetValue(widget->objectName());
170     else
171       val = s.value(key, QVariant());
172     if(!val.isValid())
173       val = widget->property("defaultValue");
174     widget->setProperty(autoWidgetPropertyName(widget), val);
175     widget->setProperty("storedValue", val);
176   }
177   bool old = hasChanged();
178   _autoWidgetsChanged = _changed = false;
179   if(hasChanged() != old)
180     emit changed(hasChanged());
181 }
182
183 void SettingsPage::save() {
184   UiSettings s("");
185   foreach(QObject *widget, _autoWidgets) {
186     QString key = autoWidgetSettingsKey(widget);
187     QVariant val = widget->property(autoWidgetPropertyName(widget));
188     widget->setProperty("storedValue", val);
189     if(key.isEmpty())
190       saveAutoWidgetValue(widget->objectName(), val);
191     else
192       s.setValue(key, val);
193   }
194   bool old = hasChanged();
195   _autoWidgetsChanged = _changed = false;
196   if(hasChanged() != old)
197     emit changed(hasChanged());
198 }
199
200 void SettingsPage::defaults() {
201   foreach(QObject *widget, _autoWidgets) {
202     QVariant val = widget->property("defaultValue");
203     widget->setProperty(autoWidgetPropertyName(widget), val);
204   }
205   autoWidgetHasChanged();
206 }
207
208 QVariant SettingsPage::loadAutoWidgetValue(const QString &widgetName) {
209   qWarning() << "Could not load value for SettingsPage widget" << widgetName;
210   return QVariant();
211 }
212
213 void SettingsPage::saveAutoWidgetValue(const QString &widgetName, const QVariant &) {
214   qWarning() << "Could not save value for SettingsPage widget" << widgetName;
215 }
216