1 /***************************************************************************
2 * Copyright (C) 2005-2018 by the Quassel Project *
3 * devel@quassel-irc.org *
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. *
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. *
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 ***************************************************************************/
21 #include "appearancesettingspage.h"
26 #include <QFileDialog>
27 #include <QStyleFactory>
29 #include "buffersettings.h"
31 #include "qtuisettings.h"
32 #include "qtuistyle.h"
35 AppearanceSettingsPage::AppearanceSettingsPage(QWidget *parent)
36 : SettingsPage(tr("Interface"), QString(), parent)
40 #ifdef QT_NO_SYSTEMTRAYICON
41 ui.useSystemTrayIcon->hide();
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();
53 initLanguageComboBox();
54 initIconThemeComboBox();
56 foreach(QComboBox *comboBox, findChildren<QComboBox *>()) {
57 connect(comboBox, SIGNAL(currentIndexChanged(QString)), this, SLOT(widgetHasChanged()));
59 foreach(QCheckBox *checkBox, findChildren<QCheckBox *>()) {
60 connect(checkBox, &QAbstractButton::clicked, this, &AppearanceSettingsPage::widgetHasChanged);
63 connect(ui.chooseStyleSheet, &QAbstractButton::clicked, this, &AppearanceSettingsPage::chooseStyleSheet);
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);
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);
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);
79 void AppearanceSettingsPage::initStyleComboBox()
81 QStringList styleList = QStyleFactory::keys();
82 ui.styleComboBox->addItem(tr("<System Default>"));
83 foreach(QString style, styleList) {
84 ui.styleComboBox->addItem(style);
89 void AppearanceSettingsPage::initLanguageComboBox()
91 QDir i18nDir(Quassel::translationDirPath(), "*.qm");
93 QRegExp rx("(qt_)?([a-zA-Z_]+)\\.qm");
94 foreach(QString translationFile, i18nDir.entryList()) {
95 if (!rx.exactMatch(translationFile))
97 if (!rx.cap(1).isEmpty())
99 QLocale locale(rx.cap(2));
100 _locales[QLocale::languageToString(locale.language())] = locale;
102 foreach(QString language, _locales.keys()) {
103 ui.languageComboBox->addItem(language);
107 void AppearanceSettingsPage::initIconThemeComboBox()
109 auto availableThemes = QtUi::instance()->availableIconThemes();
111 ui.iconThemeComboBox->addItem(tr("Automatic"), QString{});
112 for (auto &&p : QtUi::instance()->availableIconThemes()) {
113 ui.iconThemeComboBox->addItem(p.second, p.first);
118 void AppearanceSettingsPage::defaults()
120 ui.styleComboBox->setCurrentIndex(0);
121 ui.languageComboBox->setCurrentIndex(1);
123 SettingsPage::defaults();
128 void AppearanceSettingsPage::load()
130 QtUiSettings uiSettings;
133 QString style = uiSettings.value("Style", QString("")).toString();
134 if (style.isEmpty()) {
135 ui.styleComboBox->setCurrentIndex(0);
138 ui.styleComboBox->setCurrentIndex(ui.styleComboBox->findText(style, Qt::MatchExactly));
140 ui.styleComboBox->setProperty("storedValue", ui.styleComboBox->currentIndex());
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);
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());
154 QString icontheme = UiStyleSettings{}.value("Icons/FallbackTheme", QString{}).toString();
155 if (icontheme.isEmpty()) {
156 ui.iconThemeComboBox->setCurrentIndex(0);
159 auto idx = ui.iconThemeComboBox->findData(icontheme);
160 ui.iconThemeComboBox->setCurrentIndex(idx > 0 ? idx : 0);
162 ui.iconThemeComboBox->setProperty("storedValue", ui.iconThemeComboBox->currentIndex());
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);
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);
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);
181 SettingsPage::load();
182 setChangedState(false);
186 void AppearanceSettingsPage::save()
188 QtUiSettings uiSettings;
189 UiStyleSettings styleSettings;
191 if (ui.styleComboBox->currentIndex() < 1) {
192 uiSettings.setValue("Style", QString(""));
195 uiSettings.setValue("Style", ui.styleComboBox->currentText());
196 QApplication::setStyle(ui.styleComboBox->currentText());
198 ui.styleComboBox->setProperty("storedValue", ui.styleComboBox->currentIndex());
200 if (ui.languageComboBox->currentIndex() == 1) {
201 uiSettings.remove("Locale"); // force the default (QLocale::system())
204 uiSettings.setValue("Locale", selectedLocale());
206 ui.languageComboBox->setProperty("storedValue", ui.languageComboBox->currentIndex());
208 bool needsIconThemeRefresh = ui.iconThemeComboBox->currentIndex() != ui.iconThemeComboBox->property("storedValue").toInt()
209 || ui.overrideSystemIconTheme->isChecked() != ui.overrideSystemIconTheme->property("storedValue").toBool();
211 auto iconTheme = selectedIconTheme();
212 if (iconTheme.isEmpty()) {
213 styleSettings.remove("Icons/FallbackTheme");
216 styleSettings.setValue("Icons/FallbackTheme", iconTheme);
218 ui.iconThemeComboBox->setProperty("storedValue", ui.iconThemeComboBox->currentIndex());
220 bool needsStyleReload =
221 ui.useCustomStyleSheet->isChecked() != ui.useCustomStyleSheet->property("storedValue").toBool()
222 || (ui.useCustomStyleSheet->isChecked() && ui.customStyleSheetPath->text() != ui.customStyleSheetPath->property("storedValue").toString());
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);
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);
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);
252 SettingsPage::save();
253 setChangedState(false);
254 if (needsStyleReload)
255 QtUi::style()->reload();
256 if (needsIconThemeRefresh)
257 QtUi::instance()->refreshIconTheme();
261 QLocale AppearanceSettingsPage::selectedLocale() const
264 int index = ui.languageComboBox->currentIndex();
266 locale = QLocale::system();
268 locale = QLocale::c();
270 locale = _locales.values()[index - 2];
276 QString AppearanceSettingsPage::selectedIconTheme() const
278 return ui.iconThemeComboBox->itemData(ui.iconThemeComboBox->currentIndex()).toString();
282 void AppearanceSettingsPage::chooseStyleSheet()
284 QString dir = ui.customStyleSheetPath->property("storedValue").toString();
285 if (!dir.isEmpty() && QFile(dir).exists())
286 dir = QDir(dir).absolutePath();
288 dir = QDir(Quassel::findDataFilePath("default.qss")).absolutePath();
290 QString name = QFileDialog::getOpenFileName(this, tr("Please choose a stylesheet file"), dir, "*.qss");
292 ui.customStyleSheetPath->setText(name);
296 void AppearanceSettingsPage::widgetHasChanged()
298 setChangedState(testHasChanged());
302 bool AppearanceSettingsPage::testHasChanged()
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;
308 if (SettingsPage::hasChanged(ui.userNoticesInStatusBuffer)) return true;
309 if (SettingsPage::hasChanged(ui.userNoticesInDefaultBuffer)) return true;
310 if (SettingsPage::hasChanged(ui.userNoticesInCurrentBuffer)) return true;
312 if (SettingsPage::hasChanged(ui.serverNoticesInStatusBuffer)) return true;
313 if (SettingsPage::hasChanged(ui.serverNoticesInDefaultBuffer)) return true;
314 if (SettingsPage::hasChanged(ui.serverNoticesInCurrentBuffer)) return true;
316 if (SettingsPage::hasChanged(ui.errorMsgsInStatusBuffer)) return true;
317 if (SettingsPage::hasChanged(ui.errorMsgsInDefaultBuffer)) return true;
318 if (SettingsPage::hasChanged(ui.errorMsgsInCurrentBuffer)) return true;