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"
31 #include <QFileDialog>
32 #include <QFontDialog>
33 #include <QSignalMapper>
34 #include <QStyleFactory>
36 AppearanceSettingsPage::AppearanceSettingsPage(QWidget *parent)
37 : SettingsPage(tr("Appearance"), QString(), parent),
43 initLanguageComboBox();
46 ui.showWebPreview->hide();
47 ui.showWebPreview->setEnabled(false);
50 foreach(QComboBox *comboBox, findChildren<QComboBox *>()) {
51 connect(comboBox, SIGNAL(currentIndexChanged(QString)), this, SLOT(widgetHasChanged()));
53 foreach(QCheckBox *checkBox, findChildren<QCheckBox *>()) {
54 connect(checkBox, SIGNAL(clicked()), this, SLOT(widgetHasChanged()));
57 mapper = new QSignalMapper(this);
58 connect(mapper, SIGNAL(mapped(QWidget *)), this, SLOT(chooseFont(QWidget *)));
60 connect(ui.chooseBufferView, SIGNAL(clicked()), mapper, SLOT(map()));
61 connect(ui.chooseInputLine, SIGNAL(clicked()), mapper, SLOT(map()));
63 mapper->setMapping(ui.chooseBufferView, ui.demoBufferView);
64 mapper->setMapping(ui.chooseInputLine, ui.demoInputLine);
66 connect(ui.chooseStyleSheet, SIGNAL(clicked()), SLOT(chooseStyleSheet()));
69 void AppearanceSettingsPage::initStyleComboBox() {
70 QStringList styleList = QStyleFactory::keys();
71 ui.styleComboBox->addItem(tr("<System Default>"));
72 foreach(QString style, styleList) {
73 ui.styleComboBox->addItem(style);
77 void AppearanceSettingsPage::initLanguageComboBox() {
78 QDir i18nDir(Quassel::translationDirPath(), "quassel_*.qm");
80 foreach(QString translationFile, i18nDir.entryList()) {
81 QString localeName(translationFile.mid(8));
83 QLocale locale(localeName);
85 ui.languageComboBox->addItem(QLocale::languageToString(locale.language()));
89 void AppearanceSettingsPage::defaults() {
90 ui.styleComboBox->setCurrentIndex(0);
92 loadFonts(Settings::Default);
95 ui.showUserStateIcons->setChecked(true);
97 SettingsPage::defaults();
101 void AppearanceSettingsPage::load() {
102 QtUiSettings uiSettings;
105 QString style = uiSettings.value("Style", QString("")).toString();
106 if(style.isEmpty()) {
107 ui.styleComboBox->setCurrentIndex(0);
109 ui.styleComboBox->setCurrentIndex(ui.styleComboBox->findText(style, Qt::MatchExactly));
110 QApplication::setStyle(style);
112 ui.styleComboBox->setProperty("storedValue", ui.styleComboBox->currentIndex());
115 QLocale locale = uiSettings.value("Locale", QLocale::system()).value<QLocale>();
116 if(locale == QLocale::system())
117 ui.languageComboBox->setCurrentIndex(0);
118 else if(locale.language() == QLocale::C)
119 ui.languageComboBox->setCurrentIndex(1);
121 ui.languageComboBox->setCurrentIndex(ui.languageComboBox->findText(QLocale::languageToString(locale.language()), Qt::MatchExactly));
122 ui.languageComboBox->setProperty("storedValue", ui.languageComboBox->currentIndex());
123 Quassel::loadTranslation(selectedLocale());
125 BufferSettings bufferSettings;
126 SettingsPage::load(ui.showUserStateIcons, bufferSettings.showUserStateIcons());
128 loadFonts(Settings::Custom);
130 SettingsPage::load();
131 setChangedState(false);
134 void AppearanceSettingsPage::loadFonts(Settings::Mode mode) {
135 QtUiStyleSettings s("Fonts");
138 if(mode == Settings::Custom)
139 inputLineFont = s.value("InputLine", QFont()).value<QFont>();
140 setFont(ui.demoInputLine, inputLineFont);
142 QFont bufferViewFont;
143 if(mode == Settings::Custom)
144 bufferViewFont = s.value("BufferView", QFont()).value<QFont>();
145 setFont(ui.demoBufferView, bufferViewFont);
147 //QTextCharFormat chatFormat = QtUi::style()->cachedFormat(UiStyle::None, 0); // FIXME
148 //setFont(ui.demoChatView, chatFormat.font());
150 _fontsChanged = false;
153 void AppearanceSettingsPage::save() {
154 QtUiSettings uiSettings;
156 if(ui.styleComboBox->currentIndex() < 1) {
157 uiSettings.setValue("Style", QString(""));
159 uiSettings.setValue("Style", ui.styleComboBox->currentText());
162 if(ui.languageComboBox->currentIndex() == 0) {
163 uiSettings.remove("Locale"); // force the default (QLocale::system())
165 uiSettings.setValue("Locale", selectedLocale());
168 BufferSettings bufferSettings;
169 bufferSettings.enableUserStateIcons(ui.showUserStateIcons->isChecked());
172 QtUiStyleSettings fontSettings("Fonts");
173 if(ui.demoInputLine->font() != QApplication::font())
174 fontSettings.setValue("InputLine", ui.demoInputLine->font());
176 fontSettings.setValue("InputLine", "");
178 if(ui.demoBufferView->font() != QApplication::font())
179 fontSettings.setValue("BufferView", ui.demoBufferView->font());
181 fontSettings.setValue("BufferView", "");
183 //QTextCharFormat chatFormat = QtUi::style()->format(UiStyle::None);
184 //chatFormat.setFont(ui.demoChatView->font());
185 //QtUi::style()->setFormat(UiStyle::None, chatFormat, Settings::Custom);
187 _fontsChanged = false;
189 bool needsStyleReload =
190 ui.useCustomStyleSheet->isChecked() != ui.useCustomStyleSheet->property("storedValue").toBool()
191 || (ui.useCustomStyleSheet->isChecked() && ui.customStyleSheetPath->text() != ui.customStyleSheetPath->property("storedValue").toString());
193 SettingsPage::save();
194 setChangedState(false);
196 QtUi::style()->reload();
199 QLocale AppearanceSettingsPage::selectedLocale() const {
201 int index = ui.languageComboBox->currentIndex();
203 locale = QLocale::system();
205 locale = QLocale::c();
207 locale = _locales[index - 2];
212 void AppearanceSettingsPage::setFont(QLabel *label, const QFont &font_) {
214 if(font.family().isEmpty())
215 font = QApplication::font();
216 label->setFont(font);
217 label->setText(QString("%1 %2").arg(font.family()).arg(font.pointSize()));
221 void AppearanceSettingsPage::chooseFont(QWidget *widget) {
222 QLabel *label = qobject_cast<QLabel *>(widget);
225 QFont font = QFontDialog::getFont(&ok, label->font());
227 _fontsChanged = true;
228 setFont(label, font);
232 void AppearanceSettingsPage::chooseStyleSheet() {
233 QString name = QFileDialog::getOpenFileName(this, tr("Please choose a stylesheet file"), QString(), "*.qss");
235 ui.customStyleSheetPath->setText(name);
238 void AppearanceSettingsPage::widgetHasChanged() {
239 setChangedState(testHasChanged());
242 bool AppearanceSettingsPage::testHasChanged() {
243 if(_fontsChanged) return true; // comparisons are nasty for now
245 if(ui.styleComboBox->currentIndex() != ui.styleComboBox->property("storedValue").toInt()) return true;
247 if(selectedLocale() != QLocale()) return true; // QLocale() returns the default locale (manipulated via loadTranslation())
249 if(SettingsPage::hasChanged(ui.showUserStateIcons)) return true;