8ab7ce39bab249047532b4b9fa30a892f595eaaa
[quassel.git] / src / qtui / settingspages / appearancesettingspage.cpp
1 /***************************************************************************
2  *   Copyright (C) 2005-09 by the Quassel Project                          *
3  *   devel@quassel-irc.org                                                 *
4  *                                                                         *
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.                                   *
9  *                                                                         *
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.                          *
14  *                                                                         *
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  ***************************************************************************/
20
21 #include "appearancesettingspage.h"
22
23 #include "buffersettings.h"
24 #include "chatviewsettings.h"
25 #include "qtui.h"
26 #include "qtuisettings.h"
27 #include "qtuistyle.h"
28 #include "util.h"
29
30 #include <QCheckBox>
31 #include <QDir>
32 #include <QFileDialog>
33 #include <QFontDialog>
34 #include <QSignalMapper>
35 #include <QStyleFactory>
36
37 AppearanceSettingsPage::AppearanceSettingsPage(QWidget *parent)
38   : SettingsPage(tr("Appearance"), QString(), parent),
39   _fontsChanged(false)
40 {
41   ui.setupUi(this);
42   initAutoWidgets();
43   initStyleComboBox();
44   initLanguageComboBox();
45
46 #ifndef HAVE_WEBKIT
47   ui.showWebPreview->hide();
48   ui.showWebPreview->setEnabled(false);
49 #endif
50
51   foreach(QComboBox *comboBox, findChildren<QComboBox *>()) {
52     connect(comboBox, SIGNAL(currentIndexChanged(QString)), this, SLOT(widgetHasChanged()));
53   }
54   foreach(QCheckBox *checkBox, findChildren<QCheckBox *>()) {
55     connect(checkBox, SIGNAL(clicked()), this, SLOT(widgetHasChanged()));
56   }
57
58   mapper = new QSignalMapper(this);
59   connect(mapper, SIGNAL(mapped(QWidget *)), this, SLOT(chooseFont(QWidget *)));
60
61   connect(ui.chooseInputLine, SIGNAL(clicked()), mapper, SLOT(map()));
62
63   mapper->setMapping(ui.chooseInputLine, ui.demoInputLine);
64
65   connect(ui.chooseStyleSheet, SIGNAL(clicked()), SLOT(chooseStyleSheet()));
66 }
67
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);
73   }
74 }
75
76 void AppearanceSettingsPage::initLanguageComboBox() {
77   QDir i18nDir(Quassel::translationDirPath(), "quassel_*.qm");
78
79   foreach(QString translationFile, i18nDir.entryList()) {
80     QString localeName(translationFile.mid(8));
81     localeName.chop(3);
82     QLocale locale(localeName);
83     _locales << locale;
84     ui.languageComboBox->addItem(QLocale::languageToString(locale.language()));
85   }
86 }
87
88 void AppearanceSettingsPage::defaults() {
89   ui.styleComboBox->setCurrentIndex(0);
90
91   loadFonts(Settings::Default);
92   _fontsChanged = true;
93
94   SettingsPage::defaults();
95   widgetHasChanged();
96 }
97
98 void AppearanceSettingsPage::load() {
99   QtUiSettings uiSettings;
100
101   // Gui Style
102   QString style = uiSettings.value("Style", QString("")).toString();
103   if(style.isEmpty()) {
104     ui.styleComboBox->setCurrentIndex(0);
105   } else {
106     ui.styleComboBox->setCurrentIndex(ui.styleComboBox->findText(style, Qt::MatchExactly));
107     QApplication::setStyle(style);
108   }
109   ui.styleComboBox->setProperty("storedValue", ui.styleComboBox->currentIndex());
110
111   // Language
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);
117   else
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());
121
122   loadFonts(Settings::Custom);
123
124   SettingsPage::load();
125   setChangedState(false);
126 }
127
128 void AppearanceSettingsPage::loadFonts(Settings::Mode mode) {
129   QtUiStyleSettings s("Fonts");
130
131   QFont inputLineFont;
132   if(mode == Settings::Custom)
133     inputLineFont = s.value("InputLine", QFont()).value<QFont>();
134   setFont(ui.demoInputLine, inputLineFont);
135
136   _fontsChanged = false;
137 }
138
139 void AppearanceSettingsPage::save() {
140   QtUiSettings uiSettings;
141
142   if(ui.styleComboBox->currentIndex() < 1) {
143     uiSettings.setValue("Style", QString(""));
144   } else {
145     uiSettings.setValue("Style", ui.styleComboBox->currentText());
146   }
147
148   if(ui.languageComboBox->currentIndex() == 0) {
149     uiSettings.remove("Locale"); // force the default (QLocale::system())
150   } else {
151     uiSettings.setValue("Locale", selectedLocale());
152   }
153
154   // Fonts
155   QtUiStyleSettings fontSettings("Fonts");
156   if(ui.demoInputLine->font() != QApplication::font())
157     fontSettings.setValue("InputLine", ui.demoInputLine->font());
158   else
159     fontSettings.setValue("InputLine", "");
160
161   _fontsChanged = false;
162
163   bool needsStyleReload =
164         ui.useCustomStyleSheet->isChecked() != ui.useCustomStyleSheet->property("storedValue").toBool()
165     || (ui.useCustomStyleSheet->isChecked() && ui.customStyleSheetPath->text() != ui.customStyleSheetPath->property("storedValue").toString());
166
167   SettingsPage::save();
168   setChangedState(false);
169   if(needsStyleReload)
170     QtUi::style()->reload();
171 }
172
173 QLocale AppearanceSettingsPage::selectedLocale() const {
174   QLocale locale;
175   int index = ui.languageComboBox->currentIndex();
176   if(index == 0)
177     locale = QLocale::system();
178   else if(index == 1)
179     locale = QLocale::c();
180   else if(index > 1)
181     locale = _locales[index - 2];
182
183   return locale;
184 }
185
186 void AppearanceSettingsPage::setFont(QLabel *label, const QFont &font_) {
187   QFont font = 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()));
192   widgetHasChanged();
193 }
194
195 void AppearanceSettingsPage::chooseFont(QWidget *widget) {
196   QLabel *label = qobject_cast<QLabel *>(widget);
197   Q_ASSERT(label);
198   bool ok;
199   QFont font = QFontDialog::getFont(&ok, label->font());
200   if(ok) {
201     _fontsChanged = true;
202     setFont(label, font);
203   }
204 }
205
206 void AppearanceSettingsPage::chooseStyleSheet() {
207   QString name = QFileDialog::getOpenFileName(this, tr("Please choose a stylesheet file"), QString(), "*.qss");
208   if(!name.isEmpty())
209     ui.customStyleSheetPath->setText(name);
210 }
211
212 void AppearanceSettingsPage::widgetHasChanged() {
213   setChangedState(testHasChanged());
214 }
215
216 bool AppearanceSettingsPage::testHasChanged() {
217   if(_fontsChanged) return true; // comparisons are nasty for now
218
219   if(ui.styleComboBox->currentIndex() != ui.styleComboBox->property("storedValue").toInt()) return true;
220
221   if(selectedLocale() != QLocale()) return true; // QLocale() returns the default locale (manipulated via loadTranslation())
222
223   return false;
224 }