qtui: Fix icon loading and improve icon theme support
[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 #if QT_VERSION < 0x050000
44     // We don't support overriding the system icon theme with Qt4
45     ui.overrideSystemIconTheme->hide();
46 #endif
47
48     initAutoWidgets();
49     initStyleComboBox();
50     initLanguageComboBox();
51     initIconThemeComboBox();
52
53     foreach(QComboBox *comboBox, findChildren<QComboBox *>()) {
54         connect(comboBox, SIGNAL(currentIndexChanged(QString)), this, SLOT(widgetHasChanged()));
55     }
56     foreach(QCheckBox *checkBox, findChildren<QCheckBox *>()) {
57         connect(checkBox, SIGNAL(clicked()), this, SLOT(widgetHasChanged()));
58     }
59
60     connect(ui.chooseStyleSheet, SIGNAL(clicked()), SLOT(chooseStyleSheet()));
61
62     connect(ui.userNoticesInDefaultBuffer, SIGNAL(clicked(bool)), this, SLOT(widgetHasChanged()));
63     connect(ui.userNoticesInStatusBuffer, SIGNAL(clicked(bool)), this, SLOT(widgetHasChanged()));
64     connect(ui.userNoticesInCurrentBuffer, SIGNAL(clicked(bool)), this, SLOT(widgetHasChanged()));
65
66     connect(ui.serverNoticesInDefaultBuffer, SIGNAL(clicked(bool)), this, SLOT(widgetHasChanged()));
67     connect(ui.serverNoticesInStatusBuffer, SIGNAL(clicked(bool)), this, SLOT(widgetHasChanged()));
68     connect(ui.serverNoticesInCurrentBuffer, SIGNAL(clicked(bool)), this, SLOT(widgetHasChanged()));
69
70     connect(ui.errorMsgsInDefaultBuffer, SIGNAL(clicked(bool)), this, SLOT(widgetHasChanged()));
71     connect(ui.errorMsgsInStatusBuffer, SIGNAL(clicked(bool)), this, SLOT(widgetHasChanged()));
72     connect(ui.errorMsgsInCurrentBuffer, SIGNAL(clicked(bool)), this, SLOT(widgetHasChanged()));
73 }
74
75
76 void AppearanceSettingsPage::initStyleComboBox()
77 {
78     QStringList styleList = QStyleFactory::keys();
79     ui.styleComboBox->addItem(tr("<System Default>"));
80     foreach(QString style, styleList) {
81         ui.styleComboBox->addItem(style);
82     }
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
115 void AppearanceSettingsPage::defaults()
116 {
117     ui.styleComboBox->setCurrentIndex(0);
118     ui.languageComboBox->setCurrentIndex(1);
119
120     SettingsPage::defaults();
121     widgetHasChanged();
122 }
123
124
125 void AppearanceSettingsPage::load()
126 {
127     QtUiSettings uiSettings;
128
129     // Gui Style
130     QString style = uiSettings.value("Style", QString("")).toString();
131     if (style.isEmpty()) {
132         ui.styleComboBox->setCurrentIndex(0);
133     }
134     else {
135         ui.styleComboBox->setCurrentIndex(ui.styleComboBox->findText(style, Qt::MatchExactly));
136     }
137     ui.styleComboBox->setProperty("storedValue", ui.styleComboBox->currentIndex());
138
139     // Language
140     QLocale locale = uiSettings.value("Locale", QLocale::system()).value<QLocale>();
141     if (locale == QLocale::system())
142         ui.languageComboBox->setCurrentIndex(1);
143     else if (locale.language() == QLocale::C) // we use C for "untranslated"
144         ui.languageComboBox->setCurrentIndex(0);
145     else
146         ui.languageComboBox->setCurrentIndex(ui.languageComboBox->findText(QLocale::languageToString(locale.language()), Qt::MatchExactly));
147     ui.languageComboBox->setProperty("storedValue", ui.languageComboBox->currentIndex());
148     Quassel::loadTranslation(selectedLocale());
149
150     // IconTheme
151     QString icontheme = UiStyleSettings{}.value("Icons/FallbackTheme", QString{}).toString();
152     if (icontheme.isEmpty()) {
153         ui.iconThemeComboBox->setCurrentIndex(0);
154     }
155     else {
156         auto idx = ui.iconThemeComboBox->findData(icontheme);
157         ui.iconThemeComboBox->setCurrentIndex(idx > 0 ? idx : 0);
158     }
159     ui.iconThemeComboBox->setProperty("storedValue", ui.iconThemeComboBox->currentIndex());
160
161     // bufferSettings:
162     BufferSettings bufferSettings;
163     int redirectTarget = bufferSettings.userNoticesTarget();
164     SettingsPage::load(ui.userNoticesInDefaultBuffer, redirectTarget & BufferSettings::DefaultBuffer);
165     SettingsPage::load(ui.userNoticesInStatusBuffer, redirectTarget & BufferSettings::StatusBuffer);
166     SettingsPage::load(ui.userNoticesInCurrentBuffer, redirectTarget & BufferSettings::CurrentBuffer);
167
168     redirectTarget = bufferSettings.serverNoticesTarget();
169     SettingsPage::load(ui.serverNoticesInDefaultBuffer, redirectTarget & BufferSettings::DefaultBuffer);
170     SettingsPage::load(ui.serverNoticesInStatusBuffer, redirectTarget & BufferSettings::StatusBuffer);
171     SettingsPage::load(ui.serverNoticesInCurrentBuffer, redirectTarget & BufferSettings::CurrentBuffer);
172
173     redirectTarget = bufferSettings.errorMsgsTarget();
174     SettingsPage::load(ui.errorMsgsInDefaultBuffer, redirectTarget & BufferSettings::DefaultBuffer);
175     SettingsPage::load(ui.errorMsgsInStatusBuffer, redirectTarget & BufferSettings::StatusBuffer);
176     SettingsPage::load(ui.errorMsgsInCurrentBuffer, redirectTarget & BufferSettings::CurrentBuffer);
177
178     SettingsPage::load();
179     setChangedState(false);
180 }
181
182
183 void AppearanceSettingsPage::save()
184 {
185     QtUiSettings uiSettings;
186     UiStyleSettings styleSettings;
187
188     if (ui.styleComboBox->currentIndex() < 1) {
189         uiSettings.setValue("Style", QString(""));
190     }
191     else {
192         uiSettings.setValue("Style", ui.styleComboBox->currentText());
193         QApplication::setStyle(ui.styleComboBox->currentText());
194     }
195     ui.styleComboBox->setProperty("storedValue", ui.styleComboBox->currentIndex());
196
197     if (ui.languageComboBox->currentIndex() == 1) {
198         uiSettings.remove("Locale"); // force the default (QLocale::system())
199     }
200     else {
201         uiSettings.setValue("Locale", selectedLocale());
202     }
203     ui.languageComboBox->setProperty("storedValue", ui.languageComboBox->currentIndex());
204
205     bool needsIconThemeRefresh = ui.iconThemeComboBox->currentIndex() != ui.iconThemeComboBox->property("storedValue").toInt()
206                               || ui.overrideSystemIconTheme->isChecked() != ui.overrideSystemIconTheme->property("storedValue").toBool();
207
208     auto iconTheme = selectedIconTheme();
209     if (iconTheme.isEmpty()) {
210         styleSettings.remove("Icons/FallbackTheme");
211     }
212     else {
213         styleSettings.setValue("Icons/FallbackTheme", iconTheme);
214     }
215     ui.iconThemeComboBox->setProperty("storedValue", ui.iconThemeComboBox->currentIndex());
216
217     bool needsStyleReload =
218         ui.useCustomStyleSheet->isChecked() != ui.useCustomStyleSheet->property("storedValue").toBool()
219         || (ui.useCustomStyleSheet->isChecked() && ui.customStyleSheetPath->text() != ui.customStyleSheetPath->property("storedValue").toString());
220
221     BufferSettings bufferSettings;
222     int redirectTarget = 0;
223     if (ui.userNoticesInDefaultBuffer->isChecked())
224         redirectTarget |= BufferSettings::DefaultBuffer;
225     if (ui.userNoticesInStatusBuffer->isChecked())
226         redirectTarget |= BufferSettings::StatusBuffer;
227     if (ui.userNoticesInCurrentBuffer->isChecked())
228         redirectTarget |= BufferSettings::CurrentBuffer;
229     bufferSettings.setUserNoticesTarget(redirectTarget);
230
231     redirectTarget = 0;
232     if (ui.serverNoticesInDefaultBuffer->isChecked())
233         redirectTarget |= BufferSettings::DefaultBuffer;
234     if (ui.serverNoticesInStatusBuffer->isChecked())
235         redirectTarget |= BufferSettings::StatusBuffer;
236     if (ui.serverNoticesInCurrentBuffer->isChecked())
237         redirectTarget |= BufferSettings::CurrentBuffer;
238     bufferSettings.setServerNoticesTarget(redirectTarget);
239
240     redirectTarget = 0;
241     if (ui.errorMsgsInDefaultBuffer->isChecked())
242         redirectTarget |= BufferSettings::DefaultBuffer;
243     if (ui.errorMsgsInStatusBuffer->isChecked())
244         redirectTarget |= BufferSettings::StatusBuffer;
245     if (ui.errorMsgsInCurrentBuffer->isChecked())
246         redirectTarget |= BufferSettings::CurrentBuffer;
247     bufferSettings.setErrorMsgsTarget(redirectTarget);
248
249     SettingsPage::save();
250     setChangedState(false);
251     if (needsStyleReload)
252         QtUi::style()->reload();
253     if (needsIconThemeRefresh)
254         QtUi::instance()->refreshIconTheme();
255 }
256
257
258 QLocale AppearanceSettingsPage::selectedLocale() const
259 {
260     QLocale locale;
261     int index = ui.languageComboBox->currentIndex();
262     if (index == 1)
263         locale = QLocale::system();
264     else if (index == 0)
265         locale = QLocale::c();
266     else if (index > 1)
267         locale = _locales.values()[index - 2];
268
269     return locale;
270 }
271
272
273 QString AppearanceSettingsPage::selectedIconTheme() const
274 {
275     return ui.iconThemeComboBox->itemData(ui.iconThemeComboBox->currentIndex()).toString();
276 }
277
278
279 void AppearanceSettingsPage::chooseStyleSheet()
280 {
281     QString dir = ui.customStyleSheetPath->property("storedValue").toString();
282     if (!dir.isEmpty() && QFile(dir).exists())
283         dir = QDir(dir).absolutePath();
284     else
285         dir = QDir(Quassel::findDataFilePath("default.qss")).absolutePath();
286
287     QString name = QFileDialog::getOpenFileName(this, tr("Please choose a stylesheet file"), dir, "*.qss");
288     if (!name.isEmpty())
289         ui.customStyleSheetPath->setText(name);
290 }
291
292
293 void AppearanceSettingsPage::widgetHasChanged()
294 {
295     setChangedState(testHasChanged());
296 }
297
298
299 bool AppearanceSettingsPage::testHasChanged()
300 {
301     if (ui.styleComboBox->currentIndex() != ui.styleComboBox->property("storedValue").toInt()) return true;
302     if (ui.languageComboBox->currentIndex() != ui.languageComboBox->property("storedValue").toInt()) return true;
303     if (ui.iconThemeComboBox->currentIndex() != ui.iconThemeComboBox->property("storedValue").toInt()) return true;
304
305     if (SettingsPage::hasChanged(ui.userNoticesInStatusBuffer)) return true;
306     if (SettingsPage::hasChanged(ui.userNoticesInDefaultBuffer)) return true;
307     if (SettingsPage::hasChanged(ui.userNoticesInCurrentBuffer)) return true;
308
309     if (SettingsPage::hasChanged(ui.serverNoticesInStatusBuffer)) return true;
310     if (SettingsPage::hasChanged(ui.serverNoticesInDefaultBuffer)) return true;
311     if (SettingsPage::hasChanged(ui.serverNoticesInCurrentBuffer)) return true;
312
313     if (SettingsPage::hasChanged(ui.errorMsgsInStatusBuffer)) return true;
314     if (SettingsPage::hasChanged(ui.errorMsgsInDefaultBuffer)) return true;
315     if (SettingsPage::hasChanged(ui.errorMsgsInCurrentBuffer)) return true;
316
317     return false;
318 }