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();
43 #if QT_VERSION < 0x050000
44 // We don't support overriding the system icon theme with Qt4
45 ui.overrideSystemIconTheme->hide();
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();
57 initLanguageComboBox();
58 initIconThemeComboBox();
60 foreach(QComboBox *comboBox, findChildren<QComboBox *>()) {
61 connect(comboBox, SIGNAL(currentIndexChanged(QString)), this, SLOT(widgetHasChanged()));
63 foreach(QCheckBox *checkBox, findChildren<QCheckBox *>()) {
64 connect(checkBox, SIGNAL(clicked()), this, SLOT(widgetHasChanged()));
67 connect(ui.chooseStyleSheet, SIGNAL(clicked()), SLOT(chooseStyleSheet()));
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()));
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()));
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()));
83 void AppearanceSettingsPage::initStyleComboBox()
85 QStringList styleList = QStyleFactory::keys();
86 ui.styleComboBox->addItem(tr("<System Default>"));
87 foreach(QString style, styleList) {
88 ui.styleComboBox->addItem(style);
93 void AppearanceSettingsPage::initLanguageComboBox()
95 QDir i18nDir(Quassel::translationDirPath(), "*.qm");
97 QRegExp rx("(qt_)?([a-zA-Z_]+)\\.qm");
98 foreach(QString translationFile, i18nDir.entryList()) {
99 if (!rx.exactMatch(translationFile))
101 if (!rx.cap(1).isEmpty())
103 QLocale locale(rx.cap(2));
104 _locales[QLocale::languageToString(locale.language())] = locale;
106 foreach(QString language, _locales.keys()) {
107 ui.languageComboBox->addItem(language);
111 void AppearanceSettingsPage::initIconThemeComboBox()
113 auto availableThemes = QtUi::instance()->availableIconThemes();
115 ui.iconThemeComboBox->addItem(tr("Automatic"), QString{});
116 for (auto &&p : QtUi::instance()->availableIconThemes()) {
117 ui.iconThemeComboBox->addItem(p.second, p.first);
122 void AppearanceSettingsPage::defaults()
124 ui.styleComboBox->setCurrentIndex(0);
125 ui.languageComboBox->setCurrentIndex(1);
127 SettingsPage::defaults();
132 void AppearanceSettingsPage::load()
134 QtUiSettings uiSettings;
137 QString style = uiSettings.value("Style", QString("")).toString();
138 if (style.isEmpty()) {
139 ui.styleComboBox->setCurrentIndex(0);
142 ui.styleComboBox->setCurrentIndex(ui.styleComboBox->findText(style, Qt::MatchExactly));
144 ui.styleComboBox->setProperty("storedValue", ui.styleComboBox->currentIndex());
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);
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());
158 QString icontheme = UiStyleSettings{}.value("Icons/FallbackTheme", QString{}).toString();
159 if (icontheme.isEmpty()) {
160 ui.iconThemeComboBox->setCurrentIndex(0);
163 auto idx = ui.iconThemeComboBox->findData(icontheme);
164 ui.iconThemeComboBox->setCurrentIndex(idx > 0 ? idx : 0);
166 ui.iconThemeComboBox->setProperty("storedValue", ui.iconThemeComboBox->currentIndex());
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);
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);
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);
185 SettingsPage::load();
186 setChangedState(false);
190 void AppearanceSettingsPage::save()
192 QtUiSettings uiSettings;
193 UiStyleSettings styleSettings;
195 if (ui.styleComboBox->currentIndex() < 1) {
196 uiSettings.setValue("Style", QString(""));
199 uiSettings.setValue("Style", ui.styleComboBox->currentText());
200 QApplication::setStyle(ui.styleComboBox->currentText());
202 ui.styleComboBox->setProperty("storedValue", ui.styleComboBox->currentIndex());
204 if (ui.languageComboBox->currentIndex() == 1) {
205 uiSettings.remove("Locale"); // force the default (QLocale::system())
208 uiSettings.setValue("Locale", selectedLocale());
210 ui.languageComboBox->setProperty("storedValue", ui.languageComboBox->currentIndex());
212 bool needsIconThemeRefresh = ui.iconThemeComboBox->currentIndex() != ui.iconThemeComboBox->property("storedValue").toInt()
213 || ui.overrideSystemIconTheme->isChecked() != ui.overrideSystemIconTheme->property("storedValue").toBool();
215 auto iconTheme = selectedIconTheme();
216 if (iconTheme.isEmpty()) {
217 styleSettings.remove("Icons/FallbackTheme");
220 styleSettings.setValue("Icons/FallbackTheme", iconTheme);
222 ui.iconThemeComboBox->setProperty("storedValue", ui.iconThemeComboBox->currentIndex());
224 bool needsStyleReload =
225 ui.useCustomStyleSheet->isChecked() != ui.useCustomStyleSheet->property("storedValue").toBool()
226 || (ui.useCustomStyleSheet->isChecked() && ui.customStyleSheetPath->text() != ui.customStyleSheetPath->property("storedValue").toString());
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);
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);
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);
256 SettingsPage::save();
257 setChangedState(false);
258 if (needsStyleReload)
259 QtUi::style()->reload();
260 if (needsIconThemeRefresh)
261 QtUi::instance()->refreshIconTheme();
265 QLocale AppearanceSettingsPage::selectedLocale() const
268 int index = ui.languageComboBox->currentIndex();
270 locale = QLocale::system();
272 locale = QLocale::c();
274 locale = _locales.values()[index - 2];
280 QString AppearanceSettingsPage::selectedIconTheme() const
282 return ui.iconThemeComboBox->itemData(ui.iconThemeComboBox->currentIndex()).toString();
286 void AppearanceSettingsPage::chooseStyleSheet()
288 QString dir = ui.customStyleSheetPath->property("storedValue").toString();
289 if (!dir.isEmpty() && QFile(dir).exists())
290 dir = QDir(dir).absolutePath();
292 dir = QDir(Quassel::findDataFilePath("default.qss")).absolutePath();
294 QString name = QFileDialog::getOpenFileName(this, tr("Please choose a stylesheet file"), dir, "*.qss");
296 ui.customStyleSheetPath->setText(name);
300 void AppearanceSettingsPage::widgetHasChanged()
302 setChangedState(testHasChanged());
306 bool AppearanceSettingsPage::testHasChanged()
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;
312 if (SettingsPage::hasChanged(ui.userNoticesInStatusBuffer)) return true;
313 if (SettingsPage::hasChanged(ui.userNoticesInDefaultBuffer)) return true;
314 if (SettingsPage::hasChanged(ui.userNoticesInCurrentBuffer)) return true;
316 if (SettingsPage::hasChanged(ui.serverNoticesInStatusBuffer)) return true;
317 if (SettingsPage::hasChanged(ui.serverNoticesInDefaultBuffer)) return true;
318 if (SettingsPage::hasChanged(ui.serverNoticesInCurrentBuffer)) return true;
320 if (SettingsPage::hasChanged(ui.errorMsgsInStatusBuffer)) return true;
321 if (SettingsPage::hasChanged(ui.errorMsgsInDefaultBuffer)) return true;
322 if (SettingsPage::hasChanged(ui.errorMsgsInCurrentBuffer)) return true;