1 /***************************************************************************
2 * Copyright (C) 2005-2016 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"
23 #include "buffersettings.h"
25 #include "qtuisettings.h"
26 #include "qtuistyle.h"
29 #include <QFileDialog>
30 #include <QStyleFactory>
34 AppearanceSettingsPage::AppearanceSettingsPage(QWidget *parent)
35 : SettingsPage(tr("Interface"), QString(), parent)
40 ui.minimizeOnClose->hide();
42 #ifdef QT_NO_SYSTEMTRAYICON
43 ui.useSystemTrayIcon->hide();
48 initLanguageComboBox();
49 initIconThemeComboBox();
51 foreach(QComboBox *comboBox, findChildren<QComboBox *>()) {
52 connect(comboBox, SIGNAL(currentIndexChanged(QString)), this, SLOT(widgetHasChanged()));
54 foreach(QCheckBox *checkBox, findChildren<QCheckBox *>()) {
55 connect(checkBox, SIGNAL(clicked()), this, SLOT(widgetHasChanged()));
58 connect(ui.chooseStyleSheet, SIGNAL(clicked()), SLOT(chooseStyleSheet()));
60 connect(ui.userNoticesInDefaultBuffer, SIGNAL(clicked(bool)), this, SLOT(widgetHasChanged()));
61 connect(ui.userNoticesInStatusBuffer, SIGNAL(clicked(bool)), this, SLOT(widgetHasChanged()));
62 connect(ui.userNoticesInCurrentBuffer, SIGNAL(clicked(bool)), this, SLOT(widgetHasChanged()));
64 connect(ui.serverNoticesInDefaultBuffer, SIGNAL(clicked(bool)), this, SLOT(widgetHasChanged()));
65 connect(ui.serverNoticesInStatusBuffer, SIGNAL(clicked(bool)), this, SLOT(widgetHasChanged()));
66 connect(ui.serverNoticesInCurrentBuffer, SIGNAL(clicked(bool)), this, SLOT(widgetHasChanged()));
68 connect(ui.errorMsgsInDefaultBuffer, SIGNAL(clicked(bool)), this, SLOT(widgetHasChanged()));
69 connect(ui.errorMsgsInStatusBuffer, SIGNAL(clicked(bool)), this, SLOT(widgetHasChanged()));
70 connect(ui.errorMsgsInCurrentBuffer, SIGNAL(clicked(bool)), this, SLOT(widgetHasChanged()));
74 void AppearanceSettingsPage::initStyleComboBox()
76 QStringList styleList = QStyleFactory::keys();
77 ui.styleComboBox->addItem(tr("<System Default>"));
78 foreach(QString style, styleList) {
79 ui.styleComboBox->addItem(style);
84 void AppearanceSettingsPage::initLanguageComboBox()
86 QDir i18nDir(Quassel::translationDirPath(), "*.qm");
88 QRegExp rx("(qt_)?([a-zA-Z_]+)\\.qm");
89 foreach(QString translationFile, i18nDir.entryList()) {
90 if (!rx.exactMatch(translationFile))
92 if (!rx.cap(1).isEmpty())
94 QLocale locale(rx.cap(2));
95 _locales[QLocale::languageToString(locale.language())] = locale;
97 foreach(QString language, _locales.keys()) {
98 ui.languageComboBox->addItem(language);
102 void AppearanceSettingsPage::initIconThemeComboBox()
104 #if defined WITH_OXYGEN || defined WITH_BREEZE || defined WITH_BREEZE_DARK
105 # if defined WITH_OXYGEN
106 ui.iconthemeComboBox->addItem(tr("Oxygen"), QVariant("oxygen"));
108 # if defined WITH_BREEZE
109 ui.iconthemeComboBox->addItem(tr("Breeze Light"), QVariant("breeze"));
111 # if defined WITH_BREEZE_DARK
112 ui.iconthemeComboBox->addItem(tr("Breeze Dark"), QVariant("breezedark"));
115 ui.iconthemeComboBox->hide();
120 void AppearanceSettingsPage::defaults()
122 ui.styleComboBox->setCurrentIndex(0);
123 ui.languageComboBox->setCurrentIndex(1);
125 SettingsPage::defaults();
130 void AppearanceSettingsPage::load()
132 QtUiSettings uiSettings;
135 QString style = uiSettings.value("Style", QString("")).toString();
136 if (style.isEmpty()) {
137 ui.styleComboBox->setCurrentIndex(0);
140 ui.styleComboBox->setCurrentIndex(ui.styleComboBox->findText(style, Qt::MatchExactly));
142 ui.styleComboBox->setProperty("storedValue", ui.styleComboBox->currentIndex());
145 QLocale locale = uiSettings.value("Locale", QLocale::system()).value<QLocale>();
146 if (locale == QLocale::system())
147 ui.languageComboBox->setCurrentIndex(1);
148 else if (locale.language() == QLocale::C) // we use C for "untranslated"
149 ui.languageComboBox->setCurrentIndex(0);
151 ui.languageComboBox->setCurrentIndex(ui.languageComboBox->findText(QLocale::languageToString(locale.language()), Qt::MatchExactly));
152 ui.languageComboBox->setProperty("storedValue", ui.languageComboBox->currentIndex());
153 Quassel::loadTranslation(selectedLocale());
156 QString icontheme = uiSettings.value("IconTheme", QVariant("")).toString();
158 ui.iconthemeComboBox->setCurrentIndex(0);
160 ui.iconthemeComboBox->setCurrentIndex(ui.iconthemeComboBox->findData(icontheme));
161 ui.iconthemeComboBox->setProperty("storedValue", ui.iconthemeComboBox->currentIndex());
164 BufferSettings bufferSettings;
165 int redirectTarget = bufferSettings.userNoticesTarget();
166 SettingsPage::load(ui.userNoticesInDefaultBuffer, redirectTarget & BufferSettings::DefaultBuffer);
167 SettingsPage::load(ui.userNoticesInStatusBuffer, redirectTarget & BufferSettings::StatusBuffer);
168 SettingsPage::load(ui.userNoticesInCurrentBuffer, redirectTarget & BufferSettings::CurrentBuffer);
170 redirectTarget = bufferSettings.serverNoticesTarget();
171 SettingsPage::load(ui.serverNoticesInDefaultBuffer, redirectTarget & BufferSettings::DefaultBuffer);
172 SettingsPage::load(ui.serverNoticesInStatusBuffer, redirectTarget & BufferSettings::StatusBuffer);
173 SettingsPage::load(ui.serverNoticesInCurrentBuffer, redirectTarget & BufferSettings::CurrentBuffer);
175 redirectTarget = bufferSettings.errorMsgsTarget();
176 SettingsPage::load(ui.errorMsgsInDefaultBuffer, redirectTarget & BufferSettings::DefaultBuffer);
177 SettingsPage::load(ui.errorMsgsInStatusBuffer, redirectTarget & BufferSettings::StatusBuffer);
178 SettingsPage::load(ui.errorMsgsInCurrentBuffer, redirectTarget & BufferSettings::CurrentBuffer);
180 SettingsPage::load();
181 setChangedState(false);
185 void AppearanceSettingsPage::save()
187 QtUiSettings uiSettings;
189 if (ui.styleComboBox->currentIndex() < 1) {
190 uiSettings.setValue("Style", QString(""));
193 uiSettings.setValue("Style", ui.styleComboBox->currentText());
194 QApplication::setStyle(ui.styleComboBox->currentText());
196 ui.styleComboBox->setProperty("storedValue", ui.styleComboBox->currentIndex());
198 if (ui.languageComboBox->currentIndex() == 1) {
199 uiSettings.remove("Locale"); // force the default (QLocale::system())
202 uiSettings.setValue("Locale", selectedLocale());
204 ui.languageComboBox->setProperty("storedValue", ui.languageComboBox->currentIndex());
206 if (selectedIconTheme()=="") {
207 uiSettings.remove("IconTheme");
210 uiSettings.setValue("IconTheme", selectedIconTheme());
211 QIcon::setThemeName(selectedIconTheme());
213 ui.iconthemeComboBox->setProperty("storedValue", ui.iconthemeComboBox->currentIndex());
215 bool needsStyleReload =
216 ui.useCustomStyleSheet->isChecked() != ui.useCustomStyleSheet->property("storedValue").toBool()
217 || (ui.useCustomStyleSheet->isChecked() && ui.customStyleSheetPath->text() != ui.customStyleSheetPath->property("storedValue").toString());
219 BufferSettings bufferSettings;
220 int redirectTarget = 0;
221 if (ui.userNoticesInDefaultBuffer->isChecked())
222 redirectTarget |= BufferSettings::DefaultBuffer;
223 if (ui.userNoticesInStatusBuffer->isChecked())
224 redirectTarget |= BufferSettings::StatusBuffer;
225 if (ui.userNoticesInCurrentBuffer->isChecked())
226 redirectTarget |= BufferSettings::CurrentBuffer;
227 bufferSettings.setUserNoticesTarget(redirectTarget);
230 if (ui.serverNoticesInDefaultBuffer->isChecked())
231 redirectTarget |= BufferSettings::DefaultBuffer;
232 if (ui.serverNoticesInStatusBuffer->isChecked())
233 redirectTarget |= BufferSettings::StatusBuffer;
234 if (ui.serverNoticesInCurrentBuffer->isChecked())
235 redirectTarget |= BufferSettings::CurrentBuffer;
236 bufferSettings.setServerNoticesTarget(redirectTarget);
239 if (ui.errorMsgsInDefaultBuffer->isChecked())
240 redirectTarget |= BufferSettings::DefaultBuffer;
241 if (ui.errorMsgsInStatusBuffer->isChecked())
242 redirectTarget |= BufferSettings::StatusBuffer;
243 if (ui.errorMsgsInCurrentBuffer->isChecked())
244 redirectTarget |= BufferSettings::CurrentBuffer;
245 bufferSettings.setErrorMsgsTarget(redirectTarget);
247 SettingsPage::save();
248 setChangedState(false);
249 if (needsStyleReload)
250 QtUi::style()->reload();
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);
287 void AppearanceSettingsPage::widgetHasChanged()
289 setChangedState(testHasChanged());
293 bool AppearanceSettingsPage::testHasChanged()
295 if (ui.styleComboBox->currentIndex() != ui.styleComboBox->property("storedValue").toInt()) return true;
296 if (ui.languageComboBox->currentIndex() != ui.languageComboBox->property("storedValue").toInt()) return true;
297 if (ui.iconthemeComboBox->currentIndex() != ui.iconthemeComboBox->property("storedValue").toInt()) return true;
299 if (SettingsPage::hasChanged(ui.userNoticesInStatusBuffer)) return true;
300 if (SettingsPage::hasChanged(ui.userNoticesInDefaultBuffer)) return true;
301 if (SettingsPage::hasChanged(ui.userNoticesInCurrentBuffer)) return true;
303 if (SettingsPage::hasChanged(ui.serverNoticesInStatusBuffer)) return true;
304 if (SettingsPage::hasChanged(ui.serverNoticesInDefaultBuffer)) return true;
305 if (SettingsPage::hasChanged(ui.serverNoticesInCurrentBuffer)) return true;
307 if (SettingsPage::hasChanged(ui.errorMsgsInStatusBuffer)) return true;
308 if (SettingsPage::hasChanged(ui.errorMsgsInDefaultBuffer)) return true;
309 if (SettingsPage::hasChanged(ui.errorMsgsInCurrentBuffer)) return true;