Add configuration option for a custom stylesheet
[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 <QDir>
31 #include <QFileDialog>
32 #include <QFontDialog>
33 #include <QSignalMapper>
34 #include <QStyleFactory>
35
36 AppearanceSettingsPage::AppearanceSettingsPage(QWidget *parent)
37   : SettingsPage(tr("Appearance"), QString(), parent),
38   _fontsChanged(false)
39 {
40   ui.setupUi(this);
41   initAutoWidgets();
42   initStyleComboBox();
43   initLanguageComboBox();
44
45 #ifndef HAVE_WEBKIT
46   ui.showWebPreview->hide();
47   ui.showWebPreview->setEnabled(false);
48 #endif
49
50   foreach(QComboBox *comboBox, findChildren<QComboBox *>()) {
51     connect(comboBox, SIGNAL(currentIndexChanged(QString)), this, SLOT(widgetHasChanged()));
52   }
53   foreach(QCheckBox *checkBox, findChildren<QCheckBox *>()) {
54     connect(checkBox, SIGNAL(clicked()), this, SLOT(widgetHasChanged()));
55   }
56
57   mapper = new QSignalMapper(this);
58   connect(mapper, SIGNAL(mapped(QWidget *)), this, SLOT(chooseFont(QWidget *)));
59
60   connect(ui.chooseBufferView, SIGNAL(clicked()), mapper, SLOT(map()));
61   connect(ui.chooseInputLine, SIGNAL(clicked()), mapper, SLOT(map()));
62
63   mapper->setMapping(ui.chooseBufferView, ui.demoBufferView);
64   mapper->setMapping(ui.chooseInputLine, ui.demoInputLine);
65
66   connect(ui.chooseStyleSheet, SIGNAL(clicked()), SLOT(chooseStyleSheet()));
67 }
68
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);
74   }
75 }
76
77 void AppearanceSettingsPage::initLanguageComboBox() {
78   QDir i18nDir(Quassel::translationDirPath(), "quassel_*.qm");
79
80   foreach(QString translationFile, i18nDir.entryList()) {
81     QString localeName(translationFile.mid(8));
82     localeName.chop(3);
83     QLocale locale(localeName);
84     _locales << locale;
85     ui.languageComboBox->addItem(QLocale::languageToString(locale.language()));
86   }
87 }
88
89 void AppearanceSettingsPage::defaults() {
90   ui.styleComboBox->setCurrentIndex(0);
91
92   loadFonts(Settings::Default);
93   _fontsChanged = true;
94
95   ui.showUserStateIcons->setChecked(true);
96
97   SettingsPage::defaults();
98   widgetHasChanged();
99 }
100
101 void AppearanceSettingsPage::load() {
102   QtUiSettings uiSettings;
103
104   // Gui Style
105   QString style = uiSettings.value("Style", QString("")).toString();
106   if(style.isEmpty()) {
107     ui.styleComboBox->setCurrentIndex(0);
108   } else {
109     ui.styleComboBox->setCurrentIndex(ui.styleComboBox->findText(style, Qt::MatchExactly));
110     QApplication::setStyle(style);
111   }
112   ui.styleComboBox->setProperty("storedValue", ui.styleComboBox->currentIndex());
113
114   // Language
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);
120   else
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());
124
125   BufferSettings bufferSettings;
126   SettingsPage::load(ui.showUserStateIcons, bufferSettings.showUserStateIcons());
127
128   loadFonts(Settings::Custom);
129
130   SettingsPage::load();
131   setChangedState(false);
132 }
133
134 void AppearanceSettingsPage::loadFonts(Settings::Mode mode) {
135   QtUiStyleSettings s("Fonts");
136
137   QFont inputLineFont;
138   if(mode == Settings::Custom)
139     inputLineFont = s.value("InputLine", QFont()).value<QFont>();
140   setFont(ui.demoInputLine, inputLineFont);
141
142   QFont bufferViewFont;
143   if(mode == Settings::Custom)
144     bufferViewFont = s.value("BufferView", QFont()).value<QFont>();
145   setFont(ui.demoBufferView, bufferViewFont);
146
147   //QTextCharFormat chatFormat = QtUi::style()->cachedFormat(UiStyle::None, 0); // FIXME
148   //setFont(ui.demoChatView, chatFormat.font());
149
150   _fontsChanged = false;
151 }
152
153 void AppearanceSettingsPage::save() {
154   QtUiSettings uiSettings;
155
156   if(ui.styleComboBox->currentIndex() < 1) {
157     uiSettings.setValue("Style", QString(""));
158   } else {
159     uiSettings.setValue("Style", ui.styleComboBox->currentText());
160   }
161
162   if(ui.languageComboBox->currentIndex() == 0) {
163     uiSettings.remove("Locale"); // force the default (QLocale::system())
164   } else {
165     uiSettings.setValue("Locale", selectedLocale());
166   }
167
168   BufferSettings bufferSettings;
169   bufferSettings.enableUserStateIcons(ui.showUserStateIcons->isChecked());
170
171   // Fonts
172   QtUiStyleSettings fontSettings("Fonts");
173   if(ui.demoInputLine->font() != QApplication::font())
174     fontSettings.setValue("InputLine", ui.demoInputLine->font());
175   else
176     fontSettings.setValue("InputLine", "");
177
178   if(ui.demoBufferView->font() != QApplication::font())
179     fontSettings.setValue("BufferView", ui.demoBufferView->font());
180   else
181     fontSettings.setValue("BufferView", "");
182
183   //QTextCharFormat chatFormat = QtUi::style()->format(UiStyle::None);
184   //chatFormat.setFont(ui.demoChatView->font());
185   //QtUi::style()->setFormat(UiStyle::None, chatFormat, Settings::Custom);
186
187   _fontsChanged = false;
188
189   SettingsPage::save();
190   setChangedState(false);
191 }
192
193 QLocale AppearanceSettingsPage::selectedLocale() const {
194   QLocale locale;
195   int index = ui.languageComboBox->currentIndex();
196   if(index == 0)
197     locale = QLocale::system();
198   else if(index == 1)
199     locale = QLocale::c();
200   else if(index > 1)
201     locale = _locales[index - 2];
202
203   return locale;
204 }
205
206 void AppearanceSettingsPage::setFont(QLabel *label, const QFont &font_) {
207   QFont font = font_;
208   if(font.family().isEmpty())
209     font = QApplication::font();
210   label->setFont(font);
211   label->setText(QString("%1 %2").arg(font.family()).arg(font.pointSize()));
212   widgetHasChanged();
213 }
214
215 void AppearanceSettingsPage::chooseFont(QWidget *widget) {
216   QLabel *label = qobject_cast<QLabel *>(widget);
217   Q_ASSERT(label);
218   bool ok;
219   QFont font = QFontDialog::getFont(&ok, label->font());
220   if(ok) {
221     _fontsChanged = true;
222     setFont(label, font);
223   }
224 }
225
226 void AppearanceSettingsPage::chooseStyleSheet() {
227   QString name = QFileDialog::getOpenFileName(this, tr("Please choose a stylesheet file"), QString(), "*.qss");
228   if(!name.isEmpty())
229     ui.customStyleSheetPath->setText(name);
230 }
231
232 void AppearanceSettingsPage::widgetHasChanged() {
233   setChangedState(testHasChanged());
234 }
235
236 bool AppearanceSettingsPage::testHasChanged() {
237   if(_fontsChanged) return true; // comparisons are nasty for now
238
239   if(ui.styleComboBox->currentIndex() != ui.styleComboBox->property("storedValue").toInt()) return true;
240
241   if(selectedLocale() != QLocale()) return true; // QLocale() returns the default locale (manipulated via loadTranslation())
242
243   if(SettingsPage::hasChanged(ui.showUserStateIcons)) return true;
244
245   return false;
246 }