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 // TODO Replace by runtime detection
105 #if defined WITH_OXYGEN || defined WITH_BREEZE || defined WITH_BREEZE_DARK
106 # if defined WITH_BREEZE
107 ui.iconthemeComboBox->addItem(tr("Breeze Light"), QVariant("breeze"));
109 # if defined WITH_BREEZE_DARK
110 ui.iconthemeComboBox->addItem(tr("Breeze Dark"), QVariant("breezedark"));
112 # if defined WITH_OXYGEN
113 ui.iconthemeComboBox->addItem(tr("Oxygen"), QVariant("oxygen"));
116 ui.iconthemeLabel->hide();
117 ui.iconthemeComboBox->hide();
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 = uiSettings.value("IconTheme", QVariant("")).toString();
160 ui.iconthemeComboBox->setCurrentIndex(0);
162 ui.iconthemeComboBox->setCurrentIndex(ui.iconthemeComboBox->findData(icontheme));
163 ui.iconthemeComboBox->setProperty("storedValue", ui.iconthemeComboBox->currentIndex());
166 BufferSettings bufferSettings;
167 int redirectTarget = bufferSettings.userNoticesTarget();
168 SettingsPage::load(ui.userNoticesInDefaultBuffer, redirectTarget & BufferSettings::DefaultBuffer);
169 SettingsPage::load(ui.userNoticesInStatusBuffer, redirectTarget & BufferSettings::StatusBuffer);
170 SettingsPage::load(ui.userNoticesInCurrentBuffer, redirectTarget & BufferSettings::CurrentBuffer);
172 redirectTarget = bufferSettings.serverNoticesTarget();
173 SettingsPage::load(ui.serverNoticesInDefaultBuffer, redirectTarget & BufferSettings::DefaultBuffer);
174 SettingsPage::load(ui.serverNoticesInStatusBuffer, redirectTarget & BufferSettings::StatusBuffer);
175 SettingsPage::load(ui.serverNoticesInCurrentBuffer, redirectTarget & BufferSettings::CurrentBuffer);
177 redirectTarget = bufferSettings.errorMsgsTarget();
178 SettingsPage::load(ui.errorMsgsInDefaultBuffer, redirectTarget & BufferSettings::DefaultBuffer);
179 SettingsPage::load(ui.errorMsgsInStatusBuffer, redirectTarget & BufferSettings::StatusBuffer);
180 SettingsPage::load(ui.errorMsgsInCurrentBuffer, redirectTarget & BufferSettings::CurrentBuffer);
182 SettingsPage::load();
183 setChangedState(false);
187 void AppearanceSettingsPage::save()
189 QtUiSettings uiSettings;
191 if (ui.styleComboBox->currentIndex() < 1) {
192 uiSettings.setValue("Style", QString(""));
195 uiSettings.setValue("Style", ui.styleComboBox->currentText());
196 QApplication::setStyle(ui.styleComboBox->currentText());
198 ui.styleComboBox->setProperty("storedValue", ui.styleComboBox->currentIndex());
200 if (ui.languageComboBox->currentIndex() == 1) {
201 uiSettings.remove("Locale"); // force the default (QLocale::system())
204 uiSettings.setValue("Locale", selectedLocale());
206 ui.languageComboBox->setProperty("storedValue", ui.languageComboBox->currentIndex());
208 if (selectedIconTheme()=="") {
209 uiSettings.remove("IconTheme");
212 uiSettings.setValue("IconTheme", selectedIconTheme());
213 QIcon::setThemeName(selectedIconTheme());
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();
256 QLocale AppearanceSettingsPage::selectedLocale() const
259 int index = ui.languageComboBox->currentIndex();
261 locale = QLocale::system();
263 locale = QLocale::c();
265 locale = _locales.values()[index - 2];
270 QString AppearanceSettingsPage::selectedIconTheme() const
272 return ui.iconthemeComboBox->itemData(ui.iconthemeComboBox->currentIndex()).toString();
275 void AppearanceSettingsPage::chooseStyleSheet()
277 QString dir = ui.customStyleSheetPath->property("storedValue").toString();
278 if (!dir.isEmpty() && QFile(dir).exists())
279 dir = QDir(dir).absolutePath();
281 dir = QDir(Quassel::findDataFilePath("default.qss")).absolutePath();
283 QString name = QFileDialog::getOpenFileName(this, tr("Please choose a stylesheet file"), dir, "*.qss");
285 ui.customStyleSheetPath->setText(name);
289 void AppearanceSettingsPage::widgetHasChanged()
291 setChangedState(testHasChanged());
295 bool AppearanceSettingsPage::testHasChanged()
297 if (ui.styleComboBox->currentIndex() != ui.styleComboBox->property("storedValue").toInt()) return true;
298 if (ui.languageComboBox->currentIndex() != ui.languageComboBox->property("storedValue").toInt()) return true;
299 if (ui.iconthemeComboBox->currentIndex() != ui.iconthemeComboBox->property("storedValue").toInt()) return true;
301 if (SettingsPage::hasChanged(ui.userNoticesInStatusBuffer)) return true;
302 if (SettingsPage::hasChanged(ui.userNoticesInDefaultBuffer)) return true;
303 if (SettingsPage::hasChanged(ui.userNoticesInCurrentBuffer)) return true;
305 if (SettingsPage::hasChanged(ui.serverNoticesInStatusBuffer)) return true;
306 if (SettingsPage::hasChanged(ui.serverNoticesInDefaultBuffer)) return true;
307 if (SettingsPage::hasChanged(ui.serverNoticesInCurrentBuffer)) return true;
309 if (SettingsPage::hasChanged(ui.errorMsgsInStatusBuffer)) return true;
310 if (SettingsPage::hasChanged(ui.errorMsgsInDefaultBuffer)) return true;
311 if (SettingsPage::hasChanged(ui.errorMsgsInCurrentBuffer)) return true;