cmake: avoid de-duplication of user's CXXFLAGS
[quassel.git] / src / uisupport / settingspage.cpp
1 /***************************************************************************
2  *   Copyright (C) 2005-2022 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 <utility>
24
25 #include <QCheckBox>
26 #include <QComboBox>
27 #include <QSpinBox>
28 #include <QVariant>
29
30 #include "fontselector.h"
31 #include "uisettings.h"
32 #include "widgethelpers.h"
33
34 SettingsPage::SettingsPage(QString category, QString title, QWidget* parent)
35     : QWidget(parent)
36     , _category(std::move(category))
37     , _title(std::move(title))
38     , _changed(false)
39     , _autoWidgetsChanged(false)
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 void SettingsPage::load(QCheckBox* box, bool checked)
53 {
54     box->setProperty("storedValue", checked);
55     box->setChecked(checked);
56 }
57
58 bool SettingsPage::hasChanged(QCheckBox* box)
59 {
60     return box->property("storedValue").toBool() != box->isChecked();
61 }
62
63 void SettingsPage::load(QComboBox* box, int index)
64 {
65     box->setProperty("storedValue", index);
66     box->setCurrentIndex(index);
67 }
68
69 bool SettingsPage::hasChanged(QComboBox* box)
70 {
71     return box->property("storedValue").toInt() != box->currentIndex();
72 }
73
74 void SettingsPage::load(QSpinBox* box, int value)
75 {
76     box->setProperty("storedValue", value);
77     box->setValue(value);
78 }
79
80 bool SettingsPage::hasChanged(QSpinBox* box)
81 {
82     return box->property("storedValue").toInt() != box->value();
83 }
84
85 void SettingsPage::load(FontSelector* box, QFont value)
86 {
87     box->setProperty("storedValue", value);
88     box->setSelectedFont(value);
89 }
90
91 bool SettingsPage::hasChanged(FontSelector* box)
92 {
93     return box->property("storedValue").value<QFont>() != box->selectedFont();
94 }
95
96 /*** Auto child widget handling ***/
97
98 void SettingsPage::initAutoWidgets()
99 {
100     _autoWidgets.clear();
101
102     // find all descendants that should be considered auto widgets
103     // we need to climb the QObject tree recursively
104     findAutoWidgets(this, &_autoWidgets);
105
106     if (!connectToWidgetsChangedSignals(_autoWidgets, this, &SettingsPage::autoWidgetHasChanged)) {
107         qWarning() << "SettingsPage::initAutoWidgets(): Unsupported auto widget type(s)!";
108     }
109 }
110
111 void SettingsPage::findAutoWidgets(QObject* parent, QObjectList* autoList) const
112 {
113     foreach (QObject* child, parent->children()) {
114         if (child->property("settingsKey").isValid())
115             autoList->append(child);
116         findAutoWidgets(child, autoList);
117     }
118 }
119
120 QByteArray SettingsPage::autoWidgetPropertyName(QObject* widget) const
121 {
122     QByteArray prop;
123     if (widget->inherits("ColorButton"))
124         prop = "color";
125     else if (widget->inherits("QAbstractButton") || widget->inherits("QGroupBox"))
126         prop = "checked";
127     else if (widget->inherits("QLineEdit") || widget->inherits("QTextEdit"))
128         prop = "text";
129     else if (widget->inherits("QComboBox"))
130         prop = "currentIndex";
131     else if (widget->inherits("QSpinBox"))
132         prop = "value";
133     else if (widget->inherits("FontSelector"))
134         prop = "selectedFont";
135     else
136         qWarning() << "SettingsPage::autoWidgetPropertyName(): Unhandled widget type for" << widget;
137
138     return prop;
139 }
140
141 QString SettingsPage::autoWidgetSettingsKey(QObject* widget) const
142 {
143     QString key = widget->property("settingsKey").toString();
144     if (key.isEmpty())
145         return QString("");
146     if (key.startsWith('/'))
147         key.remove(0, 1);
148     else
149         key.prepend(settingsKey() + '/');
150     return key;
151 }
152
153 void SettingsPage::autoWidgetHasChanged()
154 {
155     bool changed_ = false;
156     foreach (QObject* widget, _autoWidgets) {
157         QVariant curValue = widget->property(autoWidgetPropertyName(widget));
158         if (!curValue.isValid())
159             qWarning() << "SettingsPage::autoWidgetHasChanged(): Unknown property";
160
161         if (curValue != widget->property("storedValue")) {
162             changed_ = true;
163             break;
164         }
165     }
166
167     if (changed_ != _autoWidgetsChanged) {
168         bool old = hasChanged();
169         _autoWidgetsChanged = changed_;
170         if (hasChanged() != old)
171             emit changed(hasChanged());
172     }
173 }
174
175 void SettingsPage::load()
176 {
177     UiSettings s("");
178     foreach (QObject* widget, _autoWidgets) {
179         QString key = autoWidgetSettingsKey(widget);
180         QVariant val;
181         if (key.isEmpty())
182             val = loadAutoWidgetValue(widget->objectName());
183         else
184             val = s.value(key, QVariant());
185         if (!val.isValid())
186             val = widget->property("defaultValue");
187         widget->setProperty(autoWidgetPropertyName(widget), val);
188         widget->setProperty("storedValue", val);
189     }
190     bool old = hasChanged();
191     _autoWidgetsChanged = _changed = false;
192     if (hasChanged() != old)
193         emit changed(hasChanged());
194 }
195
196 void SettingsPage::save()
197 {
198     UiSettings s("");
199     foreach (QObject* widget, _autoWidgets) {
200         QString key = autoWidgetSettingsKey(widget);
201         QVariant val = widget->property(autoWidgetPropertyName(widget));
202         widget->setProperty("storedValue", val);
203         if (key.isEmpty())
204             saveAutoWidgetValue(widget->objectName(), val);
205         else
206             s.setValue(key, val);
207     }
208     bool old = hasChanged();
209     _autoWidgetsChanged = _changed = false;
210     if (hasChanged() != old)
211         emit changed(hasChanged());
212 }
213
214 void SettingsPage::defaults()
215 {
216     foreach (QObject* widget, _autoWidgets) {
217         QVariant val = widget->property("defaultValue");
218         widget->setProperty(autoWidgetPropertyName(widget), val);
219     }
220     autoWidgetHasChanged();
221 }
222
223 QVariant SettingsPage::loadAutoWidgetValue(const QString& widgetName)
224 {
225     qWarning() << "Could not load value for SettingsPage widget" << widgetName;
226     return QVariant();
227 }
228
229 void SettingsPage::saveAutoWidgetValue(const QString& widgetName, const QVariant&)
230 {
231     qWarning() << "Could not save value for SettingsPage widget" << widgetName;
232 }