1 /***************************************************************************
2 * Copyright (C) 2005-2020 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"
34 AppearanceSettingsPage::AppearanceSettingsPage(QWidget* parent)
35 : SettingsPage(tr("Interface"), QString(), parent)
39 #ifdef QT_NO_SYSTEMTRAYICON
40 ui.useSystemTrayIcon->hide();
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();
52 initLanguageComboBox();
53 initIconThemeComboBox();
55 foreach (QComboBox* comboBox, findChildren<QComboBox*>()) {
56 connect(comboBox, &QComboBox::currentTextChanged, this, &AppearanceSettingsPage::widgetHasChanged);
58 foreach (QCheckBox* checkBox, findChildren<QCheckBox*>()) {
59 connect(checkBox, &QAbstractButton::clicked, this, &AppearanceSettingsPage::widgetHasChanged);
62 connect(ui.chooseStyleSheet, &QAbstractButton::clicked, this, &AppearanceSettingsPage::chooseStyleSheet);
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);
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);
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);
77 void AppearanceSettingsPage::initStyleComboBox()
79 QStringList styleList = QStyleFactory::keys();
80 ui.styleComboBox->addItem(tr("<System Default>"));
81 foreach (QString style, styleList) {
82 ui.styleComboBox->addItem(style);
86 void AppearanceSettingsPage::initLanguageComboBox()
88 QDir i18nDir(Quassel::translationDirPath(), "*.qm");
90 QRegExp rx("(qt_)?([a-zA-Z_]+)\\.qm");
91 foreach (QString translationFile, i18nDir.entryList()) {
92 if (!rx.exactMatch(translationFile))
94 if (!rx.cap(1).isEmpty())
96 QLocale locale(rx.cap(2));
97 _locales[QLocale::languageToString(locale.language())] = locale;
99 foreach (QString language, _locales.keys()) {
100 ui.languageComboBox->addItem(language);
104 void AppearanceSettingsPage::initIconThemeComboBox()
106 auto availableThemes = QtUi::instance()->availableIconThemes();
108 ui.iconThemeComboBox->addItem(tr("Automatic"), QString{});
109 for (auto&& p : QtUi::instance()->availableIconThemes()) {
110 ui.iconThemeComboBox->addItem(p.second, p.first);
114 void AppearanceSettingsPage::defaults()
116 ui.styleComboBox->setCurrentIndex(0);
117 ui.languageComboBox->setCurrentIndex(1);
119 SettingsPage::defaults();
123 void AppearanceSettingsPage::load()
125 QtUiSettings uiSettings;
128 QString style = uiSettings.value("Style", QString("")).toString();
129 if (style.isEmpty()) {
130 ui.styleComboBox->setCurrentIndex(0);
133 ui.styleComboBox->setCurrentIndex(ui.styleComboBox->findText(style, Qt::MatchExactly));
135 ui.styleComboBox->setProperty("storedValue", ui.styleComboBox->currentIndex());
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);
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());
149 QString icontheme = UiStyleSettings{}.value("Icons/FallbackTheme", QString{}).toString();
150 if (icontheme.isEmpty()) {
151 ui.iconThemeComboBox->setCurrentIndex(0);
154 auto idx = ui.iconThemeComboBox->findData(icontheme);
155 ui.iconThemeComboBox->setCurrentIndex(idx > 0 ? idx : 0);
157 ui.iconThemeComboBox->setProperty("storedValue", ui.iconThemeComboBox->currentIndex());
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);
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);
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);
176 SettingsPage::load();
177 setChangedState(false);
180 void AppearanceSettingsPage::save()
182 QtUiSettings uiSettings;
183 UiStyleSettings styleSettings;
185 if (ui.styleComboBox->currentIndex() < 1) {
186 uiSettings.setValue("Style", QString(""));
189 uiSettings.setValue("Style", ui.styleComboBox->currentText());
190 QApplication::setStyle(ui.styleComboBox->currentText());
192 ui.styleComboBox->setProperty("storedValue", ui.styleComboBox->currentIndex());
194 if (ui.languageComboBox->currentIndex() == 1) {
195 uiSettings.remove("Locale"); // force the default (QLocale::system())
198 uiSettings.setValue("Locale", selectedLocale());
200 ui.languageComboBox->setProperty("storedValue", ui.languageComboBox->currentIndex());
202 bool needsIconThemeRefresh = ui.iconThemeComboBox->currentIndex() != ui.iconThemeComboBox->property("storedValue").toInt()
203 || ui.overrideSystemIconTheme->isChecked() != ui.overrideSystemIconTheme->property("storedValue").toBool();
205 auto iconTheme = selectedIconTheme();
206 if (iconTheme.isEmpty()) {
207 styleSettings.remove("Icons/FallbackTheme");
210 styleSettings.setValue("Icons/FallbackTheme", iconTheme);
212 ui.iconThemeComboBox->setProperty("storedValue", ui.iconThemeComboBox->currentIndex());
214 bool needsStyleReload = ui.useCustomStyleSheet->isChecked() != ui.useCustomStyleSheet->property("storedValue").toBool()
215 || (ui.useCustomStyleSheet->isChecked()
216 && ui.customStyleSheetPath->text() != ui.customStyleSheetPath->property("storedValue").toString());
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);
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);
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);
246 SettingsPage::save();
247 setChangedState(false);
248 if (needsStyleReload)
249 QtUi::style()->reload();
250 if (needsIconThemeRefresh)
251 QtUi::instance()->refreshIconTheme();
254 QLocale AppearanceSettingsPage::selectedLocale() const
257 int index = ui.languageComboBox->currentIndex();
259 locale = QLocale::system();
261 locale = QLocale::c();
263 locale = _locales.values()[index - 2];
268 QString AppearanceSettingsPage::selectedIconTheme() const
270 return ui.iconThemeComboBox->itemData(ui.iconThemeComboBox->currentIndex()).toString();
273 void AppearanceSettingsPage::chooseStyleSheet()
275 QString dir = ui.customStyleSheetPath->property("storedValue").toString();
276 if (!dir.isEmpty() && QFile(dir).exists())
277 dir = QDir(dir).absolutePath();
279 dir = QDir(Quassel::findDataFilePath("default.qss")).absolutePath();
281 QString name = QFileDialog::getOpenFileName(this, tr("Please choose a stylesheet file"), dir, "*.qss");
283 ui.customStyleSheetPath->setText(name);
286 void AppearanceSettingsPage::widgetHasChanged()
288 setChangedState(testHasChanged());
291 bool AppearanceSettingsPage::testHasChanged()
293 if (ui.styleComboBox->currentIndex() != ui.styleComboBox->property("storedValue").toInt())
295 if (ui.languageComboBox->currentIndex() != ui.languageComboBox->property("storedValue").toInt())
297 if (ui.iconThemeComboBox->currentIndex() != ui.iconThemeComboBox->property("storedValue").toInt())
300 if (SettingsPage::hasChanged(ui.userNoticesInStatusBuffer))
302 if (SettingsPage::hasChanged(ui.userNoticesInDefaultBuffer))
304 if (SettingsPage::hasChanged(ui.userNoticesInCurrentBuffer))
307 if (SettingsPage::hasChanged(ui.serverNoticesInStatusBuffer))
309 if (SettingsPage::hasChanged(ui.serverNoticesInDefaultBuffer))
311 if (SettingsPage::hasChanged(ui.serverNoticesInCurrentBuffer))
314 if (SettingsPage::hasChanged(ui.errorMsgsInStatusBuffer))
316 if (SettingsPage::hasChanged(ui.errorMsgsInDefaultBuffer))
318 if (SettingsPage::hasChanged(ui.errorMsgsInCurrentBuffer))