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();
50 initLanguageComboBox();
51 initIconThemeComboBox();
53 foreach(QComboBox *comboBox, findChildren<QComboBox *>()) {
54 connect(comboBox, SIGNAL(currentIndexChanged(QString)), this, SLOT(widgetHasChanged()));
56 foreach(QCheckBox *checkBox, findChildren<QCheckBox *>()) {
57 connect(checkBox, SIGNAL(clicked()), this, SLOT(widgetHasChanged()));
60 connect(ui.chooseStyleSheet, SIGNAL(clicked()), SLOT(chooseStyleSheet()));
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()));
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()));
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()));
76 void AppearanceSettingsPage::initStyleComboBox()
78 QStringList styleList = QStyleFactory::keys();
79 ui.styleComboBox->addItem(tr("<System Default>"));
80 foreach(QString style, styleList) {
81 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);
115 void AppearanceSettingsPage::defaults()
117 ui.styleComboBox->setCurrentIndex(0);
118 ui.languageComboBox->setCurrentIndex(1);
120 SettingsPage::defaults();
125 void AppearanceSettingsPage::load()
127 QtUiSettings uiSettings;
130 QString style = uiSettings.value("Style", QString("")).toString();
131 if (style.isEmpty()) {
132 ui.styleComboBox->setCurrentIndex(0);
135 ui.styleComboBox->setCurrentIndex(ui.styleComboBox->findText(style, Qt::MatchExactly));
137 ui.styleComboBox->setProperty("storedValue", ui.styleComboBox->currentIndex());
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);
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());
151 QString icontheme = UiStyleSettings{}.value("Icons/FallbackTheme", QString{}).toString();
152 if (icontheme.isEmpty()) {
153 ui.iconThemeComboBox->setCurrentIndex(0);
156 auto idx = ui.iconThemeComboBox->findData(icontheme);
157 ui.iconThemeComboBox->setCurrentIndex(idx > 0 ? idx : 0);
159 ui.iconThemeComboBox->setProperty("storedValue", ui.iconThemeComboBox->currentIndex());
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);
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);
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);
178 SettingsPage::load();
179 setChangedState(false);
183 void AppearanceSettingsPage::save()
185 QtUiSettings uiSettings;
186 UiStyleSettings styleSettings;
188 if (ui.styleComboBox->currentIndex() < 1) {
189 uiSettings.setValue("Style", QString(""));
192 uiSettings.setValue("Style", ui.styleComboBox->currentText());
193 QApplication::setStyle(ui.styleComboBox->currentText());
195 ui.styleComboBox->setProperty("storedValue", ui.styleComboBox->currentIndex());
197 if (ui.languageComboBox->currentIndex() == 1) {
198 uiSettings.remove("Locale"); // force the default (QLocale::system())
201 uiSettings.setValue("Locale", selectedLocale());
203 ui.languageComboBox->setProperty("storedValue", ui.languageComboBox->currentIndex());
205 bool needsIconThemeRefresh = ui.iconThemeComboBox->currentIndex() != ui.iconThemeComboBox->property("storedValue").toInt()
206 || ui.overrideSystemIconTheme->isChecked() != ui.overrideSystemIconTheme->property("storedValue").toBool();
208 auto iconTheme = selectedIconTheme();
209 if (iconTheme.isEmpty()) {
210 styleSettings.remove("Icons/FallbackTheme");
213 styleSettings.setValue("Icons/FallbackTheme", iconTheme);
215 ui.iconThemeComboBox->setProperty("storedValue", ui.iconThemeComboBox->currentIndex());
217 bool needsStyleReload =
218 ui.useCustomStyleSheet->isChecked() != ui.useCustomStyleSheet->property("storedValue").toBool()
219 || (ui.useCustomStyleSheet->isChecked() && ui.customStyleSheetPath->text() != ui.customStyleSheetPath->property("storedValue").toString());
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);
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);
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);
249 SettingsPage::save();
250 setChangedState(false);
251 if (needsStyleReload)
252 QtUi::style()->reload();
253 if (needsIconThemeRefresh)
254 QtUi::instance()->refreshIconTheme();
258 QLocale AppearanceSettingsPage::selectedLocale() const
261 int index = ui.languageComboBox->currentIndex();
263 locale = QLocale::system();
265 locale = QLocale::c();
267 locale = _locales.values()[index - 2];
273 QString AppearanceSettingsPage::selectedIconTheme() const
275 return ui.iconThemeComboBox->itemData(ui.iconThemeComboBox->currentIndex()).toString();
279 void AppearanceSettingsPage::chooseStyleSheet()
281 QString dir = ui.customStyleSheetPath->property("storedValue").toString();
282 if (!dir.isEmpty() && QFile(dir).exists())
283 dir = QDir(dir).absolutePath();
285 dir = QDir(Quassel::findDataFilePath("default.qss")).absolutePath();
287 QString name = QFileDialog::getOpenFileName(this, tr("Please choose a stylesheet file"), dir, "*.qss");
289 ui.customStyleSheetPath->setText(name);
293 void AppearanceSettingsPage::widgetHasChanged()
295 setChangedState(testHasChanged());
299 bool AppearanceSettingsPage::testHasChanged()
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;
305 if (SettingsPage::hasChanged(ui.userNoticesInStatusBuffer)) return true;
306 if (SettingsPage::hasChanged(ui.userNoticesInDefaultBuffer)) return true;
307 if (SettingsPage::hasChanged(ui.userNoticesInCurrentBuffer)) return true;
309 if (SettingsPage::hasChanged(ui.serverNoticesInStatusBuffer)) return true;
310 if (SettingsPage::hasChanged(ui.serverNoticesInDefaultBuffer)) return true;
311 if (SettingsPage::hasChanged(ui.serverNoticesInCurrentBuffer)) return true;
313 if (SettingsPage::hasChanged(ui.errorMsgsInStatusBuffer)) return true;
314 if (SettingsPage::hasChanged(ui.errorMsgsInDefaultBuffer)) return true;
315 if (SettingsPage::hasChanged(ui.errorMsgsInCurrentBuffer)) return true;