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 <QFontDialog>
32 #include <QSignalMapper>
33 #include <QStyleFactory>
35 AppearanceSettingsPage::AppearanceSettingsPage(QWidget *parent)
36 : SettingsPage(tr("Appearance"), QString(), parent),
41 initLanguageComboBox();
44 ui.showWebPreview->hide();
45 ui.showWebPreview->setEnabled(false);
48 foreach(QComboBox *comboBox, findChildren<QComboBox *>()) {
49 connect(comboBox, SIGNAL(currentIndexChanged(QString)), this, SLOT(widgetHasChanged()));
51 foreach(QCheckBox *checkBox, findChildren<QCheckBox *>()) {
52 connect(checkBox, SIGNAL(clicked()), this, SLOT(widgetHasChanged()));
55 mapper = new QSignalMapper(this);
56 connect(mapper, SIGNAL(mapped(QWidget *)), this, SLOT(chooseFont(QWidget *)));
58 connect(ui.chooseChatView, SIGNAL(clicked()), mapper, SLOT(map()));
59 connect(ui.chooseBufferView, SIGNAL(clicked()), mapper, SLOT(map()));
60 connect(ui.chooseInputLine, SIGNAL(clicked()), mapper, SLOT(map()));
62 mapper->setMapping(ui.chooseChatView, ui.demoChatView);
63 mapper->setMapping(ui.chooseBufferView, ui.demoBufferView);
64 mapper->setMapping(ui.chooseInputLine, ui.demoInputLine);
67 void AppearanceSettingsPage::initStyleComboBox() {
68 QStringList styleList = QStyleFactory::keys();
69 ui.styleComboBox->addItem(tr("<System Default>"));
70 foreach(QString style, styleList) {
71 ui.styleComboBox->addItem(style);
75 void AppearanceSettingsPage::initLanguageComboBox() {
76 QDir i18nDir(Quassel::translationDirPath(), "quassel_*.qm");
78 foreach(QString translationFile, i18nDir.entryList()) {
79 QString localeName(translationFile.mid(8));
81 QLocale locale(localeName);
83 ui.languageComboBox->addItem(QLocale::languageToString(locale.language()));
87 void AppearanceSettingsPage::defaults() {
88 ui.styleComboBox->setCurrentIndex(0);
90 loadFonts(Settings::Default);
93 ui.showWebPreview->setChecked(true);
94 ui.showUserStateIcons->setChecked(true);
99 void AppearanceSettingsPage::load() {
100 QtUiSettings uiSettings;
103 QString style = uiSettings.value("Style", QString("")).toString();
104 if(style.isEmpty()) {
105 ui.styleComboBox->setCurrentIndex(0);
107 ui.styleComboBox->setCurrentIndex(ui.styleComboBox->findText(style, Qt::MatchExactly));
108 QApplication::setStyle(style);
110 ui.styleComboBox->setProperty("storedValue", ui.styleComboBox->currentIndex());
113 QLocale locale = uiSettings.value("Locale", QLocale::system()).value<QLocale>();
114 if(locale == QLocale::system())
115 ui.languageComboBox->setCurrentIndex(0);
116 else if(locale.language() == QLocale::C)
117 ui.languageComboBox->setCurrentIndex(1);
119 ui.languageComboBox->setCurrentIndex(ui.languageComboBox->findText(QLocale::languageToString(locale.language()), Qt::MatchExactly));
120 ui.languageComboBox->setProperty("storedValue", ui.languageComboBox->currentIndex());
121 Quassel::loadTranslation(selectedLocale());
123 ChatViewSettings chatViewSettings;
124 SettingsPage::load(ui.showWebPreview, chatViewSettings.showWebPreview());
126 BufferSettings bufferSettings;
127 SettingsPage::load(ui.showUserStateIcons, bufferSettings.showUserStateIcons());
129 loadFonts(Settings::Custom);
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()->format(UiStyle::None, mode);
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 ChatViewSettings chatViewSettings;
169 chatViewSettings.enableWebPreview(ui.showWebPreview->isChecked());
171 BufferSettings bufferSettings;
172 bufferSettings.enableUserStateIcons(ui.showUserStateIcons->isChecked());
175 QtUiStyleSettings fontSettings("Fonts");
176 if(ui.demoInputLine->font() != QApplication::font())
177 fontSettings.setValue("InputLine", ui.demoInputLine->font());
179 fontSettings.setValue("InputLine", "");
181 if(ui.demoBufferView->font() != QApplication::font())
182 fontSettings.setValue("BufferView", ui.demoBufferView->font());
184 fontSettings.setValue("BufferView", "");
186 QTextCharFormat chatFormat = QtUi::style()->format(UiStyle::None);
187 chatFormat.setFont(ui.demoChatView->font());
188 QtUi::style()->setFormat(UiStyle::None, chatFormat, Settings::Custom);
190 _fontsChanged = false;
193 setChangedState(false);
196 QLocale AppearanceSettingsPage::selectedLocale() const {
198 int index = ui.languageComboBox->currentIndex();
200 locale = QLocale::system();
202 locale = QLocale::c();
204 locale = _locales[index - 2];
209 void AppearanceSettingsPage::setFont(QLabel *label, const QFont &font_) {
211 if(font.family().isEmpty())
212 font = QApplication::font();
213 label->setFont(font);
214 label->setText(QString("%1 %2").arg(font.family()).arg(font.pointSize()));
218 void AppearanceSettingsPage::chooseFont(QWidget *widget) {
219 QLabel *label = qobject_cast<QLabel *>(widget);
222 QFont font = QFontDialog::getFont(&ok, label->font());
224 setFont(label, font);
225 _fontsChanged = true;
229 void AppearanceSettingsPage::widgetHasChanged() {
230 setChangedState(testHasChanged());
233 bool AppearanceSettingsPage::testHasChanged() {
234 if(_fontsChanged) return true; // comparisons are nasty for now
236 if(ui.styleComboBox->currentIndex() != ui.styleComboBox->property("storedValue").toInt()) return true;
238 if(selectedLocale() != QLocale()) return true; // QLocale() returns the default locale (manipulated via loadTranslation())
240 if(SettingsPage::hasChanged(ui.showWebPreview)) return true;
241 if(SettingsPage::hasChanged(ui.showUserStateIcons)) return true;