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)
39 #ifdef QT_NO_SYSTEMTRAYICON
40 ui.useSystemTrayIcon->hide();
45 initLanguageComboBox();
46 initIconThemeComboBox();
48 foreach(QComboBox *comboBox, findChildren<QComboBox *>()) {
49 connect(comboBox, SIGNAL(currentIndexChanged(QString)), this, SLOT(widgetHasChanged()));
51 foreach(QCheckBox *checkBox, findChildren<QCheckBox *>()) {
52 connect(checkBox, SIGNAL(clicked()), this, SLOT(widgetHasChanged()));
55 connect(ui.chooseStyleSheet, SIGNAL(clicked()), SLOT(chooseStyleSheet()));
57 connect(ui.userNoticesInDefaultBuffer, SIGNAL(clicked(bool)), this, SLOT(widgetHasChanged()));
58 connect(ui.userNoticesInStatusBuffer, SIGNAL(clicked(bool)), this, SLOT(widgetHasChanged()));
59 connect(ui.userNoticesInCurrentBuffer, SIGNAL(clicked(bool)), this, SLOT(widgetHasChanged()));
61 connect(ui.serverNoticesInDefaultBuffer, SIGNAL(clicked(bool)), this, SLOT(widgetHasChanged()));
62 connect(ui.serverNoticesInStatusBuffer, SIGNAL(clicked(bool)), this, SLOT(widgetHasChanged()));
63 connect(ui.serverNoticesInCurrentBuffer, SIGNAL(clicked(bool)), this, SLOT(widgetHasChanged()));
65 connect(ui.errorMsgsInDefaultBuffer, SIGNAL(clicked(bool)), this, SLOT(widgetHasChanged()));
66 connect(ui.errorMsgsInStatusBuffer, SIGNAL(clicked(bool)), this, SLOT(widgetHasChanged()));
67 connect(ui.errorMsgsInCurrentBuffer, SIGNAL(clicked(bool)), this, SLOT(widgetHasChanged()));
71 void AppearanceSettingsPage::initStyleComboBox()
73 QStringList styleList = QStyleFactory::keys();
74 ui.styleComboBox->addItem(tr("<System Default>"));
75 foreach(QString style, styleList) {
76 ui.styleComboBox->addItem(style);
81 void AppearanceSettingsPage::initLanguageComboBox()
83 QDir i18nDir(Quassel::translationDirPath(), "*.qm");
85 QRegExp rx("(qt_)?([a-zA-Z_]+)\\.qm");
86 foreach(QString translationFile, i18nDir.entryList()) {
87 if (!rx.exactMatch(translationFile))
89 if (!rx.cap(1).isEmpty())
91 QLocale locale(rx.cap(2));
92 _locales[QLocale::languageToString(locale.language())] = locale;
94 foreach(QString language, _locales.keys()) {
95 ui.languageComboBox->addItem(language);
99 void AppearanceSettingsPage::initIconThemeComboBox()
101 // TODO Replace by runtime detection
102 #if defined WITH_OXYGEN || defined WITH_BREEZE || defined WITH_BREEZE_DARK
103 # if defined WITH_BREEZE
104 ui.iconthemeComboBox->addItem(tr("Breeze Light"), QVariant("breeze"));
106 # if defined WITH_BREEZE_DARK
107 ui.iconthemeComboBox->addItem(tr("Breeze Dark"), QVariant("breezedark"));
109 # if defined WITH_OXYGEN
110 ui.iconthemeComboBox->addItem(tr("Oxygen"), QVariant("oxygen"));
113 ui.iconthemeLabel->hide();
114 ui.iconthemeComboBox->hide();
119 void AppearanceSettingsPage::defaults()
121 ui.styleComboBox->setCurrentIndex(0);
122 ui.languageComboBox->setCurrentIndex(1);
124 SettingsPage::defaults();
129 void AppearanceSettingsPage::load()
131 QtUiSettings uiSettings;
134 QString style = uiSettings.value("Style", QString("")).toString();
135 if (style.isEmpty()) {
136 ui.styleComboBox->setCurrentIndex(0);
139 ui.styleComboBox->setCurrentIndex(ui.styleComboBox->findText(style, Qt::MatchExactly));
141 ui.styleComboBox->setProperty("storedValue", ui.styleComboBox->currentIndex());
144 QLocale locale = uiSettings.value("Locale", QLocale::system()).value<QLocale>();
145 if (locale == QLocale::system())
146 ui.languageComboBox->setCurrentIndex(1);
147 else if (locale.language() == QLocale::C) // we use C for "untranslated"
148 ui.languageComboBox->setCurrentIndex(0);
150 ui.languageComboBox->setCurrentIndex(ui.languageComboBox->findText(QLocale::languageToString(locale.language()), Qt::MatchExactly));
151 ui.languageComboBox->setProperty("storedValue", ui.languageComboBox->currentIndex());
152 Quassel::loadTranslation(selectedLocale());
155 QString icontheme = uiSettings.value("IconTheme", QVariant("")).toString();
157 ui.iconthemeComboBox->setCurrentIndex(0);
159 ui.iconthemeComboBox->setCurrentIndex(ui.iconthemeComboBox->findData(icontheme));
160 ui.iconthemeComboBox->setProperty("storedValue", ui.iconthemeComboBox->currentIndex());
163 BufferSettings bufferSettings;
164 int redirectTarget = bufferSettings.userNoticesTarget();
165 SettingsPage::load(ui.userNoticesInDefaultBuffer, redirectTarget & BufferSettings::DefaultBuffer);
166 SettingsPage::load(ui.userNoticesInStatusBuffer, redirectTarget & BufferSettings::StatusBuffer);
167 SettingsPage::load(ui.userNoticesInCurrentBuffer, redirectTarget & BufferSettings::CurrentBuffer);
169 redirectTarget = bufferSettings.serverNoticesTarget();
170 SettingsPage::load(ui.serverNoticesInDefaultBuffer, redirectTarget & BufferSettings::DefaultBuffer);
171 SettingsPage::load(ui.serverNoticesInStatusBuffer, redirectTarget & BufferSettings::StatusBuffer);
172 SettingsPage::load(ui.serverNoticesInCurrentBuffer, redirectTarget & BufferSettings::CurrentBuffer);
174 redirectTarget = bufferSettings.errorMsgsTarget();
175 SettingsPage::load(ui.errorMsgsInDefaultBuffer, redirectTarget & BufferSettings::DefaultBuffer);
176 SettingsPage::load(ui.errorMsgsInStatusBuffer, redirectTarget & BufferSettings::StatusBuffer);
177 SettingsPage::load(ui.errorMsgsInCurrentBuffer, redirectTarget & BufferSettings::CurrentBuffer);
179 SettingsPage::load();
180 setChangedState(false);
184 void AppearanceSettingsPage::save()
186 QtUiSettings uiSettings;
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 if (selectedIconTheme()=="") {
206 uiSettings.remove("IconTheme");
209 uiSettings.setValue("IconTheme", selectedIconTheme());
210 QIcon::setThemeName(selectedIconTheme());
212 ui.iconthemeComboBox->setProperty("storedValue", ui.iconthemeComboBox->currentIndex());
214 bool needsStyleReload =
215 ui.useCustomStyleSheet->isChecked() != ui.useCustomStyleSheet->property("storedValue").toBool()
216 || (ui.useCustomStyleSheet->isChecked() && 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();
253 QLocale AppearanceSettingsPage::selectedLocale() const
256 int index = ui.languageComboBox->currentIndex();
258 locale = QLocale::system();
260 locale = QLocale::c();
262 locale = _locales.values()[index - 2];
267 QString AppearanceSettingsPage::selectedIconTheme() const
269 return ui.iconthemeComboBox->itemData(ui.iconthemeComboBox->currentIndex()).toString();
272 void AppearanceSettingsPage::chooseStyleSheet()
274 QString dir = ui.customStyleSheetPath->property("storedValue").toString();
275 if (!dir.isEmpty() && QFile(dir).exists())
276 dir = QDir(dir).absolutePath();
278 dir = QDir(Quassel::findDataFilePath("default.qss")).absolutePath();
280 QString name = QFileDialog::getOpenFileName(this, tr("Please choose a stylesheet file"), dir, "*.qss");
282 ui.customStyleSheetPath->setText(name);
286 void AppearanceSettingsPage::widgetHasChanged()
288 setChangedState(testHasChanged());
292 bool AppearanceSettingsPage::testHasChanged()
294 if (ui.styleComboBox->currentIndex() != ui.styleComboBox->property("storedValue").toInt()) return true;
295 if (ui.languageComboBox->currentIndex() != ui.languageComboBox->property("storedValue").toInt()) return true;
296 if (ui.iconthemeComboBox->currentIndex() != ui.iconthemeComboBox->property("storedValue").toInt()) return true;
298 if (SettingsPage::hasChanged(ui.userNoticesInStatusBuffer)) return true;
299 if (SettingsPage::hasChanged(ui.userNoticesInDefaultBuffer)) return true;
300 if (SettingsPage::hasChanged(ui.userNoticesInCurrentBuffer)) return true;
302 if (SettingsPage::hasChanged(ui.serverNoticesInStatusBuffer)) return true;
303 if (SettingsPage::hasChanged(ui.serverNoticesInDefaultBuffer)) return true;
304 if (SettingsPage::hasChanged(ui.serverNoticesInCurrentBuffer)) return true;
306 if (SettingsPage::hasChanged(ui.errorMsgsInStatusBuffer)) return true;
307 if (SettingsPage::hasChanged(ui.errorMsgsInDefaultBuffer)) return true;
308 if (SettingsPage::hasChanged(ui.errorMsgsInCurrentBuffer)) return true;