Initialize _autoWidgetsChanged *cough*
[quassel.git] / src / uisupport / settingspage.cpp
1 /***************************************************************************
2  *   Copyright (C) 2005-08 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 <QDebug>
29
30 #include "uisettings.h"
31
32 SettingsPage::SettingsPage(const QString &category, const QString &title, QWidget *parent)
33   : QWidget(parent),
34     _category(category),
35     _title(title),
36     _changed(false),
37     _autoWidgetsChanged(false)
38 {
39 }
40
41 void SettingsPage::setChangedState(bool hasChanged_) {
42   if(hasChanged_ != _changed) {
43     bool old = hasChanged();
44     _changed = hasChanged_;
45     if(hasChanged() != old)
46       emit changed(hasChanged());
47   }
48 }
49
50 void SettingsPage::load(QCheckBox *box, bool checked) {
51   box->setProperty("StoredValue", checked);
52   box->setChecked(checked);
53 }
54
55 bool SettingsPage::hasChanged(QCheckBox *box) {
56   return box->property("StoredValue").toBool() == box->isChecked();
57 }
58
59
60 void SettingsPage::load(QComboBox *box, int index) {
61   box->setProperty("StoredValue", index);
62   box->setCurrentIndex(index);
63 }
64
65 bool SettingsPage::hasChanged(QComboBox *box) {
66   return box->property("StoredValue").toInt() == box->currentIndex();
67 }
68
69 void SettingsPage::load(QSpinBox *box, int value) {
70   box->setProperty("StoredValue", value);
71   box->setValue(value);
72 }
73
74 bool SettingsPage::hasChanged(QSpinBox *box) {
75   return box->property("StoredValue").toInt() == box->value();
76 }
77
78 /*** Auto child widget handling ***/
79
80 void SettingsPage::initAutoWidgets() {
81   _autoWidgets.clear();
82
83   if(settingsKey().isNull())
84     return;
85
86   // find all descendants that should be considered auto widgets
87   // we need to climb the QObject tree recursively
88   findAutoWidgets(this, &_autoWidgets);
89
90   foreach(QObject *widget, _autoWidgets) {
91     if(widget->inherits("QAbstractButton"))
92       connect(widget, SIGNAL(toggled(bool)), SLOT(autoWidgetHasChanged()));
93     else if(widget->inherits("QLineEdit") || widget->inherits("QTextEdit"))
94       connect(widget, SIGNAL(textChanged(const QString &)), SLOT(autoWidgetHasChanged()));
95     else if(widget->inherits("QComboBox"))
96       connect(widget, SIGNAL(currentIndexChanged(int)), SLOT(autoWidgetHasChanged()));
97     else if(widget->inherits("QSpinBox"))
98       connect(widget, SIGNAL(valueChanged(int)), SLOT(autoWidgetHasChanged()));
99     else
100       qWarning() << "SettingsPage::init(): Unknown autoWidget type" << widget->metaObject()->className();
101   }
102 }
103
104 void SettingsPage::findAutoWidgets(QObject *parent, QObjectList *autoList) const {
105   foreach(QObject *child, parent->children()) {
106     if(!child->property("settingsKey").toString().isEmpty())
107       autoList->append(child);
108     findAutoWidgets(child, autoList);
109   }
110 }
111
112 QByteArray SettingsPage::autoWidgetPropertyName(QObject *widget) const {
113   QByteArray prop;
114   if(widget->inherits("QAbstractButton"))
115     prop = "checked";
116   else if(widget->inherits("QLineEdit") || widget->inherits("QTextEdit"))
117     prop = "text";
118   else if(widget->inherits("QComboBox"))
119     prop = "currentIndex";
120   else if(widget->inherits("QSpinBox"))
121     prop = "value";
122   else
123     qWarning() << "SettingsPage::autoWidgetPropertyName(): Unhandled widget type for" << widget;
124
125   return prop;
126 }
127
128 QString SettingsPage::autoWidgetSettingsKey(QObject *widget) const {
129   QString key = widget->property("settingsKey").toString();
130   if(key.startsWith('/'))
131     key.remove(0, 1);
132   else
133     key.prepend(settingsKey() + '/');
134   return key;
135 }
136
137 void SettingsPage::autoWidgetHasChanged() {
138   bool changed_ = false;
139   foreach(QObject *widget, _autoWidgets) {
140     QVariant curValue = widget->property(autoWidgetPropertyName(widget));
141     if(!curValue.isValid())
142       qWarning() << "SettingsPage::autoWidgetHasChanged(): Unknown property";
143
144     if(curValue != widget->property("storedValue")) {
145       changed_ = true;
146       break;
147     }
148   }
149
150   if(changed_ != _autoWidgetsChanged) {
151     bool old = hasChanged();
152     _autoWidgetsChanged = changed_;
153     if(hasChanged() != old)
154       emit changed(hasChanged());
155   }
156 }
157
158 void SettingsPage::load() {
159   UiSettings s("");
160   foreach(QObject *widget, _autoWidgets) {
161     QVariant val = s.value(autoWidgetSettingsKey(widget), widget->property("defaultValue"));
162     widget->setProperty(autoWidgetPropertyName(widget), val);
163     widget->setProperty("storedValue", val);
164   }
165   bool old = hasChanged();
166   _autoWidgetsChanged = _changed = false;
167   if(hasChanged() != old)
168     emit changed(hasChanged());
169 }
170
171 void SettingsPage::save() {
172   UiSettings s("");
173   foreach(QObject *widget, _autoWidgets) {
174     QVariant val = widget->property(autoWidgetPropertyName(widget));
175     widget->setProperty("storedValue", val);
176     s.setValue(autoWidgetSettingsKey(widget), val);
177   }
178   bool old = hasChanged();
179   _autoWidgetsChanged = _changed = false;
180   if(hasChanged() != old)
181     emit changed(hasChanged());
182 }
183
184 void SettingsPage::defaults() {
185   foreach(QObject *widget, _autoWidgets) {
186     QVariant val = widget->property("defaultValue");
187     widget->setProperty(autoWidgetPropertyName(widget), val);
188   }
189   autoWidgetHasChanged();
190 }