1 /***************************************************************************
2 * Copyright (C) 2005-09 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) any later version. *
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 * 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. *
19 ***************************************************************************/
21 #include "appearancesettingspage.h"
24 #include "qtuisettings.h"
25 #include "qtuistyle.h"
28 #include <QFileDialog>
29 #include <QStyleFactory>
33 AppearanceSettingsPage::AppearanceSettingsPage(QWidget *parent)
34 : SettingsPage(tr("Interface"), QString(), parent)
39 initLanguageComboBox();
41 foreach(QComboBox *comboBox, findChildren<QComboBox *>()) {
42 connect(comboBox, SIGNAL(currentIndexChanged(QString)), this, SLOT(widgetHasChanged()));
44 foreach(QCheckBox *checkBox, findChildren<QCheckBox *>()) {
45 connect(checkBox, SIGNAL(clicked()), this, SLOT(widgetHasChanged()));
48 connect(ui.chooseStyleSheet, SIGNAL(clicked()), SLOT(chooseStyleSheet()));
51 void AppearanceSettingsPage::initStyleComboBox() {
52 QStringList styleList = QStyleFactory::keys();
53 ui.styleComboBox->addItem(tr("<System Default>"));
54 foreach(QString style, styleList) {
55 ui.styleComboBox->addItem(style);
59 void AppearanceSettingsPage::initLanguageComboBox() {
60 QDir i18nDir(Quassel::translationDirPath(), "quassel_*.qm");
62 foreach(QString translationFile, i18nDir.entryList()) {
63 QString localeName(translationFile.mid(8));
65 QLocale locale(localeName);
67 ui.languageComboBox->addItem(QLocale::languageToString(locale.language()));
71 void AppearanceSettingsPage::defaults() {
72 ui.styleComboBox->setCurrentIndex(0);
74 SettingsPage::defaults();
78 void AppearanceSettingsPage::load() {
79 QtUiSettings uiSettings;
82 QString style = uiSettings.value("Style", QString("")).toString();
84 ui.styleComboBox->setCurrentIndex(0);
86 ui.styleComboBox->setCurrentIndex(ui.styleComboBox->findText(style, Qt::MatchExactly));
87 QApplication::setStyle(style);
89 ui.styleComboBox->setProperty("storedValue", ui.styleComboBox->currentIndex());
92 QLocale locale = uiSettings.value("Locale", QLocale::system()).value<QLocale>();
93 if(locale == QLocale::system())
94 ui.languageComboBox->setCurrentIndex(0);
95 else if(locale.language() == QLocale::C)
96 ui.languageComboBox->setCurrentIndex(1);
98 ui.languageComboBox->setCurrentIndex(ui.languageComboBox->findText(QLocale::languageToString(locale.language()), Qt::MatchExactly));
99 ui.languageComboBox->setProperty("storedValue", ui.languageComboBox->currentIndex());
100 Quassel::loadTranslation(selectedLocale());
102 SettingsPage::load();
103 setChangedState(false);
106 void AppearanceSettingsPage::save() {
107 QtUiSettings uiSettings;
109 if(ui.styleComboBox->currentIndex() < 1) {
110 uiSettings.setValue("Style", QString(""));
112 uiSettings.setValue("Style", ui.styleComboBox->currentText());
115 if(ui.languageComboBox->currentIndex() == 0) {
116 uiSettings.remove("Locale"); // force the default (QLocale::system())
118 uiSettings.setValue("Locale", selectedLocale());
121 bool needsStyleReload =
122 ui.useCustomStyleSheet->isChecked() != ui.useCustomStyleSheet->property("storedValue").toBool()
123 || (ui.useCustomStyleSheet->isChecked() && ui.customStyleSheetPath->text() != ui.customStyleSheetPath->property("storedValue").toString());
125 SettingsPage::save();
126 setChangedState(false);
128 QtUi::style()->reload();
131 QLocale AppearanceSettingsPage::selectedLocale() const {
133 int index = ui.languageComboBox->currentIndex();
135 locale = QLocale::system();
137 locale = QLocale::c();
139 locale = _locales[index - 2];
144 void AppearanceSettingsPage::chooseStyleSheet() {
145 QString dir = ui.customStyleSheetPath->property("storedValue").toString();
146 if(!dir.isEmpty() && QFile(dir).exists())
147 dir = QDir(dir).absolutePath();
149 dir = QDir(Quassel::findDataFilePath("default.qss")).absolutePath();
151 QString name = QFileDialog::getOpenFileName(this, tr("Please choose a stylesheet file"), dir, "*.qss");
153 ui.customStyleSheetPath->setText(name);
156 void AppearanceSettingsPage::widgetHasChanged() {
157 setChangedState(testHasChanged());
160 bool AppearanceSettingsPage::testHasChanged() {
161 if(ui.styleComboBox->currentIndex() != ui.styleComboBox->property("storedValue").toInt()) return true;
163 if(selectedLocale() != QLocale()) return true; // QLocale() returns the default locale (manipulated via loadTranslation())