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)
40 initLanguageComboBox();
43 ui.showWebPreview->hide();
44 ui.showWebPreview->setEnabled(false);
47 foreach(QComboBox *comboBox, findChildren<QComboBox *>()) {
48 connect(comboBox, SIGNAL(currentIndexChanged(QString)), this, SLOT(widgetHasChanged()));
50 foreach(QCheckBox *checkBox, findChildren<QCheckBox *>()) {
51 connect(checkBox, SIGNAL(clicked()), this, SLOT(widgetHasChanged()));
54 mapper = new QSignalMapper(this);
55 connect(mapper, SIGNAL(mapped(QWidget *)), this, SLOT(chooseFont(QWidget *)));
57 connect(ui.chooseChatView, SIGNAL(clicked()), mapper, SLOT(map()));
58 connect(ui.chooseBufferView, SIGNAL(clicked()), mapper, SLOT(map()));
59 connect(ui.chooseInputLine, SIGNAL(clicked()), mapper, SLOT(map()));
61 mapper->setMapping(ui.chooseChatView, ui.demoChatView);
62 mapper->setMapping(ui.chooseBufferView, ui.demoBufferView);
63 mapper->setMapping(ui.chooseInputLine, ui.demoInputLine);
66 void AppearanceSettingsPage::initStyleComboBox() {
67 QStringList styleList = QStyleFactory::keys();
68 ui.styleComboBox->addItem(tr("<System Default>"));
69 foreach(QString style, styleList) {
70 ui.styleComboBox->addItem(style);
74 void AppearanceSettingsPage::initLanguageComboBox() {
75 QDir i18nDir(Quassel::translationDirPath(), "quassel_*.qm");
77 foreach(QString translationFile, i18nDir.entryList()) {
78 QString localeName(translationFile.mid(8));
80 QLocale locale(localeName);
82 ui.languageComboBox->addItem(QLocale::languageToString(locale.language()));
86 void AppearanceSettingsPage::defaults() {
87 ui.styleComboBox->setCurrentIndex(0);
89 loadFonts(Settings::Default);
94 void AppearanceSettingsPage::load() {
95 QtUiSettings uiSettings;
98 settings["Style"] = uiSettings.value("Style", QString(""));
99 if(settings["Style"].toString() == "") {
100 ui.styleComboBox->setCurrentIndex(0);
102 ui.styleComboBox->setCurrentIndex(ui.styleComboBox->findText(settings["Style"].toString(), Qt::MatchExactly));
103 QApplication::setStyle(settings["Style"].toString());
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 Quassel::loadTranslation(selectedLocale());
116 ChatViewSettings chatViewSettings;
117 SettingsPage::load(ui.showWebPreview, chatViewSettings.showWebPreview());
119 BufferSettings bufferSettings;
120 SettingsPage::load(ui.showUserStateIcons, bufferSettings.showUserStateIcons());
122 loadFonts(Settings::Custom);
124 setChangedState(false);
127 void AppearanceSettingsPage::loadFonts(Settings::Mode mode) {
128 QtUiStyleSettings s("Fonts");
131 if(mode == Settings::Custom)
132 inputLineFont = s.value("InputLine", QFont()).value<QFont>();
133 setFont(ui.demoInputLine, inputLineFont);
135 QFont bufferViewFont;
136 if(mode == Settings::Custom)
137 bufferViewFont = s.value("BufferView", QFont()).value<QFont>();
138 setFont(ui.demoBufferView, bufferViewFont);
140 QTextCharFormat chatFormat = QtUi::style()->format(UiStyle::None, mode);
141 setFont(ui.demoChatView, chatFormat.font());
143 _fontsChanged = false;
146 void AppearanceSettingsPage::save() {
147 QtUiSettings uiSettings;
149 if(ui.styleComboBox->currentIndex() < 1) {
150 uiSettings.setValue("Style", QString(""));
152 uiSettings.setValue("Style", ui.styleComboBox->currentText());
155 if(ui.languageComboBox->currentIndex() == 0) {
156 uiSettings.remove("Locale"); // force the default (QLocale::system())
158 uiSettings.setValue("Locale", selectedLocale());
161 ChatViewSettings chatViewSettings;
162 chatViewSettings.enableWebPreview(ui.showWebPreview->isChecked());
164 BufferSettings bufferSettings;
165 bufferSettings.enableUserStateIcons(ui.showUserStateIcons->isChecked());
168 QtUiStyleSettings fontSettings("Fonts");
169 if(ui.demoInputLine->font() != QApplication::font())
170 fontSettings.setValue("InputLine", ui.demoInputLine->font());
172 fontSettings.setValue("InputLine", "");
174 if(ui.demoBufferView->font() != QApplication::font())
175 fontSettings.setValue("BufferView", ui.demoBufferView->font());
177 fontSettings.setValue("BufferView", "");
179 QTextCharFormat chatFormat = QtUi::style()->format(UiStyle::None);
180 chatFormat.setFont(ui.demoChatView->font());
181 QtUi::style()->setFormat(UiStyle::None, chatFormat, Settings::Custom);
183 _fontsChanged = false;
186 setChangedState(false);
189 QLocale AppearanceSettingsPage::selectedLocale() const {
191 int index = ui.languageComboBox->currentIndex();
193 locale = QLocale::system();
195 locale = QLocale::c();
197 locale = _locales[index - 2];
202 void AppearanceSettingsPage::setFont(QLabel *label, const QFont &font_) {
204 if(font.family().isEmpty())
205 font = QApplication::font();
206 label->setFont(font);
207 label->setText(QString("%1 %2").arg(font.family()).arg(font.pointSize()));
211 void AppearanceSettingsPage::chooseFont(QWidget *widget) {
212 QLabel *label = qobject_cast<QLabel *>(widget);
215 QFont font = QFontDialog::getFont(&ok, label->font());
217 setFont(label, font);
218 _fontsChanged = true;
222 void AppearanceSettingsPage::widgetHasChanged() {
223 setChangedState(testHasChanged());
226 bool AppearanceSettingsPage::testHasChanged() {
227 if(_fontsChanged) return true; // comparisons are nasty for now
229 if(settings["Style"].toString() != ui.styleComboBox->currentText()) return true;
230 if(selectedLocale() != QLocale()) return true; // QLocale() returns the default locale (manipulated via loadTranslation())
232 if(SettingsPage::hasChanged(ui.showWebPreview)) return true;
233 if(SettingsPage::hasChanged(ui.showUserStateIcons)) return true;