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("Interface"), QString(), parent),
44 initLanguageComboBox();
46 foreach(QComboBox *comboBox, findChildren<QComboBox *>()) {
47 connect(comboBox, SIGNAL(currentIndexChanged(QString)), this, SLOT(widgetHasChanged()));
49 foreach(QCheckBox *checkBox, findChildren<QCheckBox *>()) {
50 connect(checkBox, SIGNAL(clicked()), this, SLOT(widgetHasChanged()));
53 mapper = new QSignalMapper(this);
54 connect(mapper, SIGNAL(mapped(QWidget *)), this, SLOT(chooseFont(QWidget *)));
56 connect(ui.chooseInputLine, SIGNAL(clicked()), mapper, SLOT(map()));
58 mapper->setMapping(ui.chooseInputLine, ui.demoInputLine);
60 connect(ui.chooseStyleSheet, SIGNAL(clicked()), SLOT(chooseStyleSheet()));
63 void AppearanceSettingsPage::initStyleComboBox() {
64 QStringList styleList = QStyleFactory::keys();
65 ui.styleComboBox->addItem(tr("<System Default>"));
66 foreach(QString style, styleList) {
67 ui.styleComboBox->addItem(style);
71 void AppearanceSettingsPage::initLanguageComboBox() {
72 QDir i18nDir(Quassel::translationDirPath(), "quassel_*.qm");
74 foreach(QString translationFile, i18nDir.entryList()) {
75 QString localeName(translationFile.mid(8));
77 QLocale locale(localeName);
79 ui.languageComboBox->addItem(QLocale::languageToString(locale.language()));
83 void AppearanceSettingsPage::defaults() {
84 ui.styleComboBox->setCurrentIndex(0);
86 loadFonts(Settings::Default);
89 SettingsPage::defaults();
93 void AppearanceSettingsPage::load() {
94 QtUiSettings uiSettings;
97 QString style = uiSettings.value("Style", QString("")).toString();
99 ui.styleComboBox->setCurrentIndex(0);
101 ui.styleComboBox->setCurrentIndex(ui.styleComboBox->findText(style, Qt::MatchExactly));
102 QApplication::setStyle(style);
104 ui.styleComboBox->setProperty("storedValue", ui.styleComboBox->currentIndex());
107 QLocale locale = uiSettings.value("Locale", QLocale::system()).value<QLocale>();
108 if(locale == QLocale::system())
109 ui.languageComboBox->setCurrentIndex(0);
110 else if(locale.language() == QLocale::C)
111 ui.languageComboBox->setCurrentIndex(1);
113 ui.languageComboBox->setCurrentIndex(ui.languageComboBox->findText(QLocale::languageToString(locale.language()), Qt::MatchExactly));
114 ui.languageComboBox->setProperty("storedValue", ui.languageComboBox->currentIndex());
115 Quassel::loadTranslation(selectedLocale());
117 loadFonts(Settings::Custom);
119 SettingsPage::load();
120 setChangedState(false);
123 void AppearanceSettingsPage::loadFonts(Settings::Mode mode) {
124 QtUiStyleSettings s("Fonts");
127 if(mode == Settings::Custom)
128 inputLineFont = s.value("InputLine", QFont()).value<QFont>();
129 setFont(ui.demoInputLine, inputLineFont);
131 _fontsChanged = false;
134 void AppearanceSettingsPage::save() {
135 QtUiSettings uiSettings;
137 if(ui.styleComboBox->currentIndex() < 1) {
138 uiSettings.setValue("Style", QString(""));
140 uiSettings.setValue("Style", ui.styleComboBox->currentText());
143 if(ui.languageComboBox->currentIndex() == 0) {
144 uiSettings.remove("Locale"); // force the default (QLocale::system())
146 uiSettings.setValue("Locale", selectedLocale());
150 QtUiStyleSettings fontSettings("Fonts");
151 if(ui.demoInputLine->font() != QApplication::font())
152 fontSettings.setValue("InputLine", ui.demoInputLine->font());
154 fontSettings.setValue("InputLine", "");
156 _fontsChanged = false;
158 bool needsStyleReload =
159 ui.useCustomStyleSheet->isChecked() != ui.useCustomStyleSheet->property("storedValue").toBool()
160 || (ui.useCustomStyleSheet->isChecked() && ui.customStyleSheetPath->text() != ui.customStyleSheetPath->property("storedValue").toString());
162 SettingsPage::save();
163 setChangedState(false);
165 QtUi::style()->reload();
168 QLocale AppearanceSettingsPage::selectedLocale() const {
170 int index = ui.languageComboBox->currentIndex();
172 locale = QLocale::system();
174 locale = QLocale::c();
176 locale = _locales[index - 2];
181 void AppearanceSettingsPage::setFont(QLabel *label, const QFont &font_) {
183 if(font.family().isEmpty())
184 font = QApplication::font();
185 label->setFont(font);
186 label->setText(QString("%1 %2").arg(font.family()).arg(font.pointSize()));
190 void AppearanceSettingsPage::chooseFont(QWidget *widget) {
191 QLabel *label = qobject_cast<QLabel *>(widget);
194 QFont font = QFontDialog::getFont(&ok, label->font());
196 _fontsChanged = true;
197 setFont(label, font);
201 void AppearanceSettingsPage::chooseStyleSheet() {
202 QString name = QFileDialog::getOpenFileName(this, tr("Please choose a stylesheet file"), QString(), "*.qss");
204 ui.customStyleSheetPath->setText(name);
207 void AppearanceSettingsPage::widgetHasChanged() {
208 setChangedState(testHasChanged());
211 bool AppearanceSettingsPage::testHasChanged() {
212 if(_fontsChanged) return true; // comparisons are nasty for now
214 if(ui.styleComboBox->currentIndex() != ui.styleComboBox->property("storedValue").toInt()) return true;
216 if(selectedLocale() != QLocale()) return true; // QLocale() returns the default locale (manipulated via loadTranslation())