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