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"
23 #include "buffersettings.h"
24 #include "chatviewsettings.h"
26 #include "qtuisettings.h"
27 #include "qtuistyle.h"
32 #include <QFileDialog>
33 #include <QFontDialog>
34 #include <QSignalMapper>
35 #include <QStyleFactory>
37 AppearanceSettingsPage::AppearanceSettingsPage(QWidget *parent)
38 : SettingsPage(tr("Appearance"), QString(), parent),
44 initLanguageComboBox();
47 ui.showWebPreview->hide();
48 ui.showWebPreview->setEnabled(false);
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 mapper = new QSignalMapper(this);
59 connect(mapper, SIGNAL(mapped(QWidget *)), this, SLOT(chooseFont(QWidget *)));
61 connect(ui.chooseInputLine, SIGNAL(clicked()), mapper, SLOT(map()));
63 mapper->setMapping(ui.chooseInputLine, ui.demoInputLine);
65 connect(ui.chooseStyleSheet, SIGNAL(clicked()), SLOT(chooseStyleSheet()));
68 void AppearanceSettingsPage::initStyleComboBox() {
69 QStringList styleList = QStyleFactory::keys();
70 ui.styleComboBox->addItem(tr("<System Default>"));
71 foreach(QString style, styleList) {
72 ui.styleComboBox->addItem(style);
76 void AppearanceSettingsPage::initLanguageComboBox() {
77 QDir i18nDir(Quassel::translationDirPath(), "quassel_*.qm");
79 foreach(QString translationFile, i18nDir.entryList()) {
80 QString localeName(translationFile.mid(8));
82 QLocale locale(localeName);
84 ui.languageComboBox->addItem(QLocale::languageToString(locale.language()));
88 void AppearanceSettingsPage::defaults() {
89 ui.styleComboBox->setCurrentIndex(0);
91 loadFonts(Settings::Default);
94 SettingsPage::defaults();
98 void AppearanceSettingsPage::load() {
99 QtUiSettings uiSettings;
102 QString style = uiSettings.value("Style", QString("")).toString();
103 if(style.isEmpty()) {
104 ui.styleComboBox->setCurrentIndex(0);
106 ui.styleComboBox->setCurrentIndex(ui.styleComboBox->findText(style, Qt::MatchExactly));
107 QApplication::setStyle(style);
109 ui.styleComboBox->setProperty("storedValue", ui.styleComboBox->currentIndex());
112 QLocale locale = uiSettings.value("Locale", QLocale::system()).value<QLocale>();
113 if(locale == QLocale::system())
114 ui.languageComboBox->setCurrentIndex(0);
115 else if(locale.language() == QLocale::C)
116 ui.languageComboBox->setCurrentIndex(1);
118 ui.languageComboBox->setCurrentIndex(ui.languageComboBox->findText(QLocale::languageToString(locale.language()), Qt::MatchExactly));
119 ui.languageComboBox->setProperty("storedValue", ui.languageComboBox->currentIndex());
120 Quassel::loadTranslation(selectedLocale());
122 loadFonts(Settings::Custom);
124 SettingsPage::load();
125 setChangedState(false);
128 void AppearanceSettingsPage::loadFonts(Settings::Mode mode) {
129 QtUiStyleSettings s("Fonts");
132 if(mode == Settings::Custom)
133 inputLineFont = s.value("InputLine", QFont()).value<QFont>();
134 setFont(ui.demoInputLine, inputLineFont);
136 _fontsChanged = false;
139 void AppearanceSettingsPage::save() {
140 QtUiSettings uiSettings;
142 if(ui.styleComboBox->currentIndex() < 1) {
143 uiSettings.setValue("Style", QString(""));
145 uiSettings.setValue("Style", ui.styleComboBox->currentText());
148 if(ui.languageComboBox->currentIndex() == 0) {
149 uiSettings.remove("Locale"); // force the default (QLocale::system())
151 uiSettings.setValue("Locale", selectedLocale());
155 QtUiStyleSettings fontSettings("Fonts");
156 if(ui.demoInputLine->font() != QApplication::font())
157 fontSettings.setValue("InputLine", ui.demoInputLine->font());
159 fontSettings.setValue("InputLine", "");
161 _fontsChanged = false;
163 bool needsStyleReload =
164 ui.useCustomStyleSheet->isChecked() != ui.useCustomStyleSheet->property("storedValue").toBool()
165 || (ui.useCustomStyleSheet->isChecked() && ui.customStyleSheetPath->text() != ui.customStyleSheetPath->property("storedValue").toString());
167 SettingsPage::save();
168 setChangedState(false);
170 QtUi::style()->reload();
173 QLocale AppearanceSettingsPage::selectedLocale() const {
175 int index = ui.languageComboBox->currentIndex();
177 locale = QLocale::system();
179 locale = QLocale::c();
181 locale = _locales[index - 2];
186 void AppearanceSettingsPage::setFont(QLabel *label, const QFont &font_) {
188 if(font.family().isEmpty())
189 font = QApplication::font();
190 label->setFont(font);
191 label->setText(QString("%1 %2").arg(font.family()).arg(font.pointSize()));
195 void AppearanceSettingsPage::chooseFont(QWidget *widget) {
196 QLabel *label = qobject_cast<QLabel *>(widget);
199 QFont font = QFontDialog::getFont(&ok, label->font());
201 _fontsChanged = true;
202 setFont(label, font);
206 void AppearanceSettingsPage::chooseStyleSheet() {
207 QString name = QFileDialog::getOpenFileName(this, tr("Please choose a stylesheet file"), QString(), "*.qss");
209 ui.customStyleSheetPath->setText(name);
212 void AppearanceSettingsPage::widgetHasChanged() {
213 setChangedState(testHasChanged());
216 bool AppearanceSettingsPage::testHasChanged() {
217 if(_fontsChanged) return true; // comparisons are nasty for now
219 if(ui.styleComboBox->currentIndex() != ui.styleComboBox->property("storedValue").toInt()) return true;
221 if(selectedLocale() != QLocale()) return true; // QLocale() returns the default locale (manipulated via loadTranslation())