Revert "qtui: Don't create parented dialogs on the stack"
[quassel.git] / src / uisupport / settingspage.cpp
1 /***************************************************************************
2  *   Copyright (C) 2005-2018 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) version 3.                                           *
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  *   51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.         *
19  ***************************************************************************/
20
21 #include "settingspage.h"
22
23 #include <QCheckBox>
24 #include <QComboBox>
25 #include <QSpinBox>
26 #include <QVariant>
27
28 #include "fontselector.h"
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
42 void SettingsPage::setChangedState(bool hasChanged_)
43 {
44     if (hasChanged_ != _changed) {
45         bool old = hasChanged();
46         _changed = hasChanged_;
47         if (hasChanged() != old)
48             emit changed(hasChanged());
49     }
50 }
51
52
53 void SettingsPage::load(QCheckBox *box, bool checked)
54 {
55     box->setProperty("storedValue", checked);
56     box->setChecked(checked);
57 }
58
59
60 bool SettingsPage::hasChanged(QCheckBox *box)
61 {
62     return box->property("storedValue").toBool() != box->isChecked();
63 }
64
65
66 void SettingsPage::load(QComboBox *box, int index)
67 {
68     box->setProperty("storedValue", index);
69     box->setCurrentIndex(index);
70 }
71
72
73 bool SettingsPage::hasChanged(QComboBox *box)
74 {
75     return box->property("storedValue").toInt() != box->currentIndex();
76 }
77
78
79 void SettingsPage::load(QSpinBox *box, int value)
80 {
81     box->setProperty("storedValue", value);
82     box->setValue(value);
83 }
84
85
86 bool SettingsPage::hasChanged(QSpinBox *box)
87 {
88     return box->property("storedValue").toInt() != box->value();
89 }
90
91
92 void SettingsPage::load(FontSelector *box, QFont value)
93 {
94     box->setProperty("storedValue", value);
95     box->setSelectedFont(value);
96 }
97
98
99 bool SettingsPage::hasChanged(FontSelector *box)
100 {
101     return box->property("storedValue").value<QFont>() != box->selectedFont();
102 }
103
104
105 /*** Auto child widget handling ***/
106
107 void SettingsPage::initAutoWidgets()
108 {
109     _autoWidgets.clear();
110
111     // find all descendants that should be considered auto widgets
112     // we need to climb the QObject tree recursively
113     findAutoWidgets(this, &_autoWidgets);
114
115     foreach(QObject *widget, _autoWidgets) {
116         if (widget->inherits("ColorButton"))
117             connect(widget, SIGNAL(colorChanged(QColor)), SLOT(autoWidgetHasChanged()));
118         else if (widget->inherits("QAbstractButton") || widget->inherits("QGroupBox"))
119             connect(widget, SIGNAL(toggled(bool)), SLOT(autoWidgetHasChanged()));
120         else if (widget->inherits("QLineEdit") || widget->inherits("QTextEdit"))
121             connect(widget, SIGNAL(textChanged(const QString &)), SLOT(autoWidgetHasChanged()));
122         else if (widget->inherits("QComboBox"))
123             connect(widget, SIGNAL(currentIndexChanged(int)), SLOT(autoWidgetHasChanged()));
124         else if (widget->inherits("QSpinBox"))
125             connect(widget, SIGNAL(valueChanged(int)), SLOT(autoWidgetHasChanged()));
126         else if (widget->inherits("FontSelector"))
127             connect(widget, SIGNAL(fontChanged(QFont)), SLOT(autoWidgetHasChanged()));
128         else
129             qWarning() << "SettingsPage::init(): Unknown autoWidget type" << widget->metaObject()->className();
130     }
131 }
132
133
134 void SettingsPage::findAutoWidgets(QObject *parent, QObjectList *autoList) const
135 {
136     foreach(QObject *child, parent->children()) {
137         if (child->property("settingsKey").isValid())
138             autoList->append(child);
139         findAutoWidgets(child, autoList);
140     }
141 }
142
143
144 QByteArray SettingsPage::autoWidgetPropertyName(QObject *widget) const
145 {
146     QByteArray prop;
147     if (widget->inherits("ColorButton"))
148         prop = "color";
149     else if (widget->inherits("QAbstractButton") || widget->inherits("QGroupBox"))
150         prop = "checked";
151     else if (widget->inherits("QLineEdit") || widget->inherits("QTextEdit"))
152         prop = "text";
153     else if (widget->inherits("QComboBox"))
154         prop = "currentIndex";
155     else if (widget->inherits("QSpinBox"))
156         prop = "value";
157     else if (widget->inherits("FontSelector"))
158         prop = "selectedFont";
159     else
160         qWarning() << "SettingsPage::autoWidgetPropertyName(): Unhandled widget type for" << widget;
161
162     return prop;
163 }
164
165
166 QString SettingsPage::autoWidgetSettingsKey(QObject *widget) const
167 {
168     QString key = widget->property("settingsKey").toString();
169     if (key.isEmpty())
170         return QString("");
171     if (key.startsWith('/'))
172         key.remove(0, 1);
173     else
174         key.prepend(settingsKey() + '/');
175     return key;
176 }
177
178
179 void SettingsPage::autoWidgetHasChanged()
180 {
181     bool changed_ = false;
182     foreach(QObject *widget, _autoWidgets) {
183         QVariant curValue = widget->property(autoWidgetPropertyName(widget));
184         if (!curValue.isValid())
185             qWarning() << "SettingsPage::autoWidgetHasChanged(): Unknown property";
186
187         if (curValue != widget->property("storedValue")) {
188             changed_ = true;
189             break;
190         }
191     }
192
193     if (changed_ != _autoWidgetsChanged) {
194         bool old = hasChanged();
195         _autoWidgetsChanged = changed_;
196         if (hasChanged() != old)
197             emit changed(hasChanged());
198     }
199 }
200
201
202 void SettingsPage::load()
203 {
204     UiSettings s("");
205     foreach(QObject *widget, _autoWidgets) {
206         QString key = autoWidgetSettingsKey(widget);
207         QVariant val;
208         if (key.isEmpty())
209             val = loadAutoWidgetValue(widget->objectName());
210         else
211             val = s.value(key, QVariant());
212         if (!val.isValid())
213             val = widget->property("defaultValue");
214         widget->setProperty(autoWidgetPropertyName(widget), val);
215         widget->setProperty("storedValue", val);
216     }
217     bool old = hasChanged();
218     _autoWidgetsChanged = _changed = false;
219     if (hasChanged() != old)
220         emit changed(hasChanged());
221 }
222
223
224 void SettingsPage::save()
225 {
226     UiSettings s("");
227     foreach(QObject *widget, _autoWidgets) {
228         QString key = autoWidgetSettingsKey(widget);
229         QVariant val = widget->property(autoWidgetPropertyName(widget));
230         widget->setProperty("storedValue", val);
231         if (key.isEmpty())
232             saveAutoWidgetValue(widget->objectName(), val);
233         else
234             s.setValue(key, val);
235     }
236     bool old = hasChanged();
237     _autoWidgetsChanged = _changed = false;
238     if (hasChanged() != old)
239         emit changed(hasChanged());
240 }
241
242
243 void SettingsPage::defaults()
244 {
245     foreach(QObject *widget, _autoWidgets) {
246         QVariant val = widget->property("defaultValue");
247         widget->setProperty(autoWidgetPropertyName(widget), val);
248     }
249     autoWidgetHasChanged();
250 }
251
252
253 QVariant SettingsPage::loadAutoWidgetValue(const QString &widgetName)
254 {
255     qWarning() << "Could not load value for SettingsPage widget" << widgetName;
256     return QVariant();
257 }
258
259
260 void SettingsPage::saveAutoWidgetValue(const QString &widgetName, const QVariant &)
261 {
262     qWarning() << "Could not save value for SettingsPage widget" << widgetName;
263 }