Fix deprecation warning, to fix Werror build
[quassel.git] / src / qtui / settingspages / appearancesettingspage.cpp
1 /***************************************************************************
2  *   Copyright (C) 2005-2019 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 "appearancesettingspage.h"
22
23 #include <QCheckBox>
24 #include <QDir>
25 #include <QFile>
26 #include <QFileDialog>
27 #include <QStyleFactory>
28
29 #include "buffersettings.h"
30 #include "qtui.h"
31 #include "qtuisettings.h"
32 #include "qtuistyle.h"
33
34 AppearanceSettingsPage::AppearanceSettingsPage(QWidget* parent)
35     : SettingsPage(tr("Interface"), QString(), parent)
36 {
37     ui.setupUi(this);
38
39 #ifdef QT_NO_SYSTEMTRAYICON
40     ui.useSystemTrayIcon->hide();
41 #endif
42
43     // If no system icon theme is given, showing the override option makes no sense.
44     // Also don't mention a "fallback".
45     if (QtUi::instance()->systemIconTheme().isEmpty()) {
46         ui.iconThemeLabel->setText(tr("Icon theme:"));
47         ui.overrideSystemIconTheme->hide();
48     }
49
50     initAutoWidgets();
51     initStyleComboBox();
52     initLanguageComboBox();
53     initIconThemeComboBox();
54
55     foreach (QComboBox* comboBox, findChildren<QComboBox*>()) {
56         connect(comboBox, &QComboBox::currentTextChanged, this, &AppearanceSettingsPage::widgetHasChanged);
57     }
58     foreach (QCheckBox* checkBox, findChildren<QCheckBox*>()) {
59         connect(checkBox, &QAbstractButton::clicked, this, &AppearanceSettingsPage::widgetHasChanged);
60     }
61
62     connect(ui.chooseStyleSheet, &QAbstractButton::clicked, this, &AppearanceSettingsPage::chooseStyleSheet);
63
64     connect(ui.userNoticesInDefaultBuffer, &QAbstractButton::clicked, this, &AppearanceSettingsPage::widgetHasChanged);
65     connect(ui.userNoticesInStatusBuffer, &QAbstractButton::clicked, this, &AppearanceSettingsPage::widgetHasChanged);
66     connect(ui.userNoticesInCurrentBuffer, &QAbstractButton::clicked, this, &AppearanceSettingsPage::widgetHasChanged);
67
68     connect(ui.serverNoticesInDefaultBuffer, &QAbstractButton::clicked, this, &AppearanceSettingsPage::widgetHasChanged);
69     connect(ui.serverNoticesInStatusBuffer, &QAbstractButton::clicked, this, &AppearanceSettingsPage::widgetHasChanged);
70     connect(ui.serverNoticesInCurrentBuffer, &QAbstractButton::clicked, this, &AppearanceSettingsPage::widgetHasChanged);
71
72     connect(ui.errorMsgsInDefaultBuffer, &QAbstractButton::clicked, this, &AppearanceSettingsPage::widgetHasChanged);
73     connect(ui.errorMsgsInStatusBuffer, &QAbstractButton::clicked, this, &AppearanceSettingsPage::widgetHasChanged);
74     connect(ui.errorMsgsInCurrentBuffer, &QAbstractButton::clicked, this, &AppearanceSettingsPage::widgetHasChanged);
75 }
76
77 void AppearanceSettingsPage::initStyleComboBox()
78 {
79     QStringList styleList = QStyleFactory::keys();
80     ui.styleComboBox->addItem(tr("<System Default>"));
81     foreach (QString style, styleList) {
82         ui.styleComboBox->addItem(style);
83     }
84 }
85
86 void AppearanceSettingsPage::initLanguageComboBox()
87 {
88     QDir i18nDir(Quassel::translationDirPath(), "*.qm");
89
90     QRegExp rx("(qt_)?([a-zA-Z_]+)\\.qm");
91     foreach (QString translationFile, i18nDir.entryList()) {
92         if (!rx.exactMatch(translationFile))
93             continue;
94         if (!rx.cap(1).isEmpty())
95             continue;
96         QLocale locale(rx.cap(2));
97         _locales[QLocale::languageToString(locale.language())] = locale;
98     }
99     foreach (QString language, _locales.keys()) {
100         ui.languageComboBox->addItem(language);
101     }
102 }
103
104 void AppearanceSettingsPage::initIconThemeComboBox()
105 {
106     auto availableThemes = QtUi::instance()->availableIconThemes();
107
108     ui.iconThemeComboBox->addItem(tr("Automatic"), QString{});
109     for (auto&& p : QtUi::instance()->availableIconThemes()) {
110         ui.iconThemeComboBox->addItem(p.second, p.first);
111     }
112 }
113
114 void AppearanceSettingsPage::defaults()
115 {
116     ui.styleComboBox->setCurrentIndex(0);
117     ui.languageComboBox->setCurrentIndex(1);
118
119     SettingsPage::defaults();
120     widgetHasChanged();
121 }
122
123 void AppearanceSettingsPage::load()
124 {
125     QtUiSettings uiSettings;
126
127     // Gui Style
128     QString style = uiSettings.value("Style", QString("")).toString();
129     if (style.isEmpty()) {
130         ui.styleComboBox->setCurrentIndex(0);
131     }
132     else {
133         ui.styleComboBox->setCurrentIndex(ui.styleComboBox->findText(style, Qt::MatchExactly));
134     }
135     ui.styleComboBox->setProperty("storedValue", ui.styleComboBox->currentIndex());
136
137     // Language
138     QLocale locale = uiSettings.value("Locale", QLocale::system()).value<QLocale>();
139     if (locale == QLocale::system())
140         ui.languageComboBox->setCurrentIndex(1);
141     else if (locale.language() == QLocale::C)  // we use C for "untranslated"
142         ui.languageComboBox->setCurrentIndex(0);
143     else
144         ui.languageComboBox->setCurrentIndex(ui.languageComboBox->findText(QLocale::languageToString(locale.language()), Qt::MatchExactly));
145     ui.languageComboBox->setProperty("storedValue", ui.languageComboBox->currentIndex());
146     Quassel::loadTranslation(selectedLocale());
147
148     // IconTheme
149     QString icontheme = UiStyleSettings{}.value("Icons/FallbackTheme", QString{}).toString();
150     if (icontheme.isEmpty()) {
151         ui.iconThemeComboBox->setCurrentIndex(0);
152     }
153     else {
154         auto idx = ui.iconThemeComboBox->findData(icontheme);
155         ui.iconThemeComboBox->setCurrentIndex(idx > 0 ? idx : 0);
156     }
157     ui.iconThemeComboBox->setProperty("storedValue", ui.iconThemeComboBox->currentIndex());
158
159     // bufferSettings:
160     BufferSettings bufferSettings;
161     int redirectTarget = bufferSettings.userNoticesTarget();
162     SettingsPage::load(ui.userNoticesInDefaultBuffer, redirectTarget & BufferSettings::DefaultBuffer);
163     SettingsPage::load(ui.userNoticesInStatusBuffer, redirectTarget & BufferSettings::StatusBuffer);
164     SettingsPage::load(ui.userNoticesInCurrentBuffer, redirectTarget & BufferSettings::CurrentBuffer);
165
166     redirectTarget = bufferSettings.serverNoticesTarget();
167     SettingsPage::load(ui.serverNoticesInDefaultBuffer, redirectTarget & BufferSettings::DefaultBuffer);
168     SettingsPage::load(ui.serverNoticesInStatusBuffer, redirectTarget & BufferSettings::StatusBuffer);
169     SettingsPage::load(ui.serverNoticesInCurrentBuffer, redirectTarget & BufferSettings::CurrentBuffer);
170
171     redirectTarget = bufferSettings.errorMsgsTarget();
172     SettingsPage::load(ui.errorMsgsInDefaultBuffer, redirectTarget & BufferSettings::DefaultBuffer);
173     SettingsPage::load(ui.errorMsgsInStatusBuffer, redirectTarget & BufferSettings::StatusBuffer);
174     SettingsPage::load(ui.errorMsgsInCurrentBuffer, redirectTarget & BufferSettings::CurrentBuffer);
175
176     SettingsPage::load();
177     setChangedState(false);
178 }
179
180 void AppearanceSettingsPage::save()
181 {
182     QtUiSettings uiSettings;
183     UiStyleSettings styleSettings;
184
185     if (ui.styleComboBox->currentIndex() < 1) {
186         uiSettings.setValue("Style", QString(""));
187     }
188     else {
189         uiSettings.setValue("Style", ui.styleComboBox->currentText());
190         QApplication::setStyle(ui.styleComboBox->currentText());
191     }
192     ui.styleComboBox->setProperty("storedValue", ui.styleComboBox->currentIndex());
193
194     if (ui.languageComboBox->currentIndex() == 1) {
195         uiSettings.remove("Locale");  // force the default (QLocale::system())
196     }
197     else {
198         uiSettings.setValue("Locale", selectedLocale());
199     }
200     ui.languageComboBox->setProperty("storedValue", ui.languageComboBox->currentIndex());
201
202     bool needsIconThemeRefresh = ui.iconThemeComboBox->currentIndex() != ui.iconThemeComboBox->property("storedValue").toInt()
203                                  || ui.overrideSystemIconTheme->isChecked() != ui.overrideSystemIconTheme->property("storedValue").toBool();
204
205     auto iconTheme = selectedIconTheme();
206     if (iconTheme.isEmpty()) {
207         styleSettings.remove("Icons/FallbackTheme");
208     }
209     else {
210         styleSettings.setValue("Icons/FallbackTheme", iconTheme);
211     }
212     ui.iconThemeComboBox->setProperty("storedValue", ui.iconThemeComboBox->currentIndex());
213
214     bool needsStyleReload = ui.useCustomStyleSheet->isChecked() != ui.useCustomStyleSheet->property("storedValue").toBool()
215                             || (ui.useCustomStyleSheet->isChecked()
216                                 && ui.customStyleSheetPath->text() != ui.customStyleSheetPath->property("storedValue").toString());
217
218     BufferSettings bufferSettings;
219     int redirectTarget = 0;
220     if (ui.userNoticesInDefaultBuffer->isChecked())
221         redirectTarget |= BufferSettings::DefaultBuffer;
222     if (ui.userNoticesInStatusBuffer->isChecked())
223         redirectTarget |= BufferSettings::StatusBuffer;
224     if (ui.userNoticesInCurrentBuffer->isChecked())
225         redirectTarget |= BufferSettings::CurrentBuffer;
226     bufferSettings.setUserNoticesTarget(redirectTarget);
227
228     redirectTarget = 0;
229     if (ui.serverNoticesInDefaultBuffer->isChecked())
230         redirectTarget |= BufferSettings::DefaultBuffer;
231     if (ui.serverNoticesInStatusBuffer->isChecked())
232         redirectTarget |= BufferSettings::StatusBuffer;
233     if (ui.serverNoticesInCurrentBuffer->isChecked())
234         redirectTarget |= BufferSettings::CurrentBuffer;
235     bufferSettings.setServerNoticesTarget(redirectTarget);
236
237     redirectTarget = 0;
238     if (ui.errorMsgsInDefaultBuffer->isChecked())
239         redirectTarget |= BufferSettings::DefaultBuffer;
240     if (ui.errorMsgsInStatusBuffer->isChecked())
241         redirectTarget |= BufferSettings::StatusBuffer;
242     if (ui.errorMsgsInCurrentBuffer->isChecked())
243         redirectTarget |= BufferSettings::CurrentBuffer;
244     bufferSettings.setErrorMsgsTarget(redirectTarget);
245
246     SettingsPage::save();
247     setChangedState(false);
248     if (needsStyleReload)
249         QtUi::style()->reload();
250     if (needsIconThemeRefresh)
251         QtUi::instance()->refreshIconTheme();
252 }
253
254 QLocale AppearanceSettingsPage::selectedLocale() const
255 {
256     QLocale locale;
257     int index = ui.languageComboBox->currentIndex();
258     if (index == 1)
259         locale = QLocale::system();
260     else if (index == 0)
261         locale = QLocale::c();
262     else if (index > 1)
263         locale = _locales.values()[index - 2];
264
265     return locale;
266 }
267
268 QString AppearanceSettingsPage::selectedIconTheme() const
269 {
270     return ui.iconThemeComboBox->itemData(ui.iconThemeComboBox->currentIndex()).toString();
271 }
272
273 void AppearanceSettingsPage::chooseStyleSheet()
274 {
275     QString dir = ui.customStyleSheetPath->property("storedValue").toString();
276     if (!dir.isEmpty() && QFile(dir).exists())
277         dir = QDir(dir).absolutePath();
278     else
279         dir = QDir(Quassel::findDataFilePath("default.qss")).absolutePath();
280
281     QString name = QFileDialog::getOpenFileName(this, tr("Please choose a stylesheet file"), dir, "*.qss");
282     if (!name.isEmpty())
283         ui.customStyleSheetPath->setText(name);
284 }
285
286 void AppearanceSettingsPage::widgetHasChanged()
287 {
288     setChangedState(testHasChanged());
289 }
290
291 bool AppearanceSettingsPage::testHasChanged()
292 {
293     if (ui.styleComboBox->currentIndex() != ui.styleComboBox->property("storedValue").toInt())
294         return true;
295     if (ui.languageComboBox->currentIndex() != ui.languageComboBox->property("storedValue").toInt())
296         return true;
297     if (ui.iconThemeComboBox->currentIndex() != ui.iconThemeComboBox->property("storedValue").toInt())
298         return true;
299
300     if (SettingsPage::hasChanged(ui.userNoticesInStatusBuffer))
301         return true;
302     if (SettingsPage::hasChanged(ui.userNoticesInDefaultBuffer))
303         return true;
304     if (SettingsPage::hasChanged(ui.userNoticesInCurrentBuffer))
305         return true;
306
307     if (SettingsPage::hasChanged(ui.serverNoticesInStatusBuffer))
308         return true;
309     if (SettingsPage::hasChanged(ui.serverNoticesInDefaultBuffer))
310         return true;
311     if (SettingsPage::hasChanged(ui.serverNoticesInCurrentBuffer))
312         return true;
313
314     if (SettingsPage::hasChanged(ui.errorMsgsInStatusBuffer))
315         return true;
316     if (SettingsPage::hasChanged(ui.errorMsgsInDefaultBuffer))
317         return true;
318     if (SettingsPage::hasChanged(ui.errorMsgsInCurrentBuffer))
319         return true;
320
321     return false;
322 }