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