Make fonts/sizes changeable again Fixes #664
[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 <QFontDialog>
32 #include <QSignalMapper>
33 #include <QStyleFactory>
34
35 AppearanceSettingsPage::AppearanceSettingsPage(QWidget *parent)
36   : SettingsPage(tr("Appearance"), QString(), parent),
37   _fontsChanged(false)
38 {
39   ui.setupUi(this);
40   initStyleComboBox();
41   initLanguageComboBox();
42
43 #ifndef HAVE_WEBKIT
44   ui.showWebPreview->hide();
45   ui.showWebPreview->setEnabled(false);
46 #endif
47
48   foreach(QComboBox *comboBox, findChildren<QComboBox *>()) {
49     connect(comboBox, SIGNAL(currentIndexChanged(QString)), this, SLOT(widgetHasChanged()));
50   }
51   foreach(QCheckBox *checkBox, findChildren<QCheckBox *>()) {
52     connect(checkBox, SIGNAL(clicked()), this, SLOT(widgetHasChanged()));
53   }
54
55   mapper = new QSignalMapper(this);
56   connect(mapper, SIGNAL(mapped(QWidget *)), this, SLOT(chooseFont(QWidget *)));
57
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()));
61
62   mapper->setMapping(ui.chooseChatView, ui.demoChatView);
63   mapper->setMapping(ui.chooseBufferView, ui.demoBufferView);
64   mapper->setMapping(ui.chooseInputLine, ui.demoInputLine);
65 }
66
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);
72   }
73 }
74
75 void AppearanceSettingsPage::initLanguageComboBox() {
76   QDir i18nDir(Quassel::translationDirPath(), "quassel_*.qm");
77
78   foreach(QString translationFile, i18nDir.entryList()) {
79     QString localeName(translationFile.mid(8));
80     localeName.chop(3);
81     QLocale locale(localeName);
82     _locales << locale;
83     ui.languageComboBox->addItem(QLocale::languageToString(locale.language()));
84   }
85 }
86
87 void AppearanceSettingsPage::defaults() {
88   ui.styleComboBox->setCurrentIndex(0);
89
90   loadFonts(Settings::Default);
91   _fontsChanged = true;
92
93   ui.showWebPreview->setChecked(true);
94   ui.showUserStateIcons->setChecked(true);
95
96   widgetHasChanged();
97 }
98
99 void AppearanceSettingsPage::load() {
100   QtUiSettings uiSettings;
101
102   // Gui Style
103   QString style = uiSettings.value("Style", QString("")).toString();
104   if(style.isEmpty()) {
105     ui.styleComboBox->setCurrentIndex(0);
106   } else {
107     ui.styleComboBox->setCurrentIndex(ui.styleComboBox->findText(style, Qt::MatchExactly));
108     QApplication::setStyle(style);
109   }
110   ui.styleComboBox->setProperty("storedValue", ui.styleComboBox->currentIndex());
111
112   // Language
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);
118   else
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());
122
123   ChatViewSettings chatViewSettings;
124   SettingsPage::load(ui.showWebPreview, chatViewSettings.showWebPreview());
125
126   BufferSettings bufferSettings;
127   SettingsPage::load(ui.showUserStateIcons, bufferSettings.showUserStateIcons());
128
129   loadFonts(Settings::Custom);
130
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()->format(UiStyle::None, mode);
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   ChatViewSettings chatViewSettings;
169   chatViewSettings.enableWebPreview(ui.showWebPreview->isChecked());
170
171   BufferSettings bufferSettings;
172   bufferSettings.enableUserStateIcons(ui.showUserStateIcons->isChecked());
173
174   // Fonts
175   QtUiStyleSettings fontSettings("Fonts");
176   if(ui.demoInputLine->font() != QApplication::font())
177     fontSettings.setValue("InputLine", ui.demoInputLine->font());
178   else
179     fontSettings.setValue("InputLine", "");
180
181   if(ui.demoBufferView->font() != QApplication::font())
182     fontSettings.setValue("BufferView", ui.demoBufferView->font());
183   else
184     fontSettings.setValue("BufferView", "");
185
186   QTextCharFormat chatFormat = QtUi::style()->format(UiStyle::None);
187   chatFormat.setFont(ui.demoChatView->font());
188   QtUi::style()->setFormat(UiStyle::None, chatFormat, Settings::Custom);
189
190   _fontsChanged = false;
191
192   load();
193   setChangedState(false);
194 }
195
196 QLocale AppearanceSettingsPage::selectedLocale() const {
197   QLocale locale;
198   int index = ui.languageComboBox->currentIndex();
199   if(index == 0)
200     locale = QLocale::system();
201   else if(index == 1)
202     locale = QLocale::c();
203   else if(index > 1)
204     locale = _locales[index - 2];
205
206   return locale;
207 }
208
209 void AppearanceSettingsPage::setFont(QLabel *label, const QFont &font_) {
210   QFont font = 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()));
215   widgetHasChanged();
216 }
217
218 void AppearanceSettingsPage::chooseFont(QWidget *widget) {
219   QLabel *label = qobject_cast<QLabel *>(widget);
220   Q_ASSERT(label);
221   bool ok;
222   QFont font = QFontDialog::getFont(&ok, label->font());
223   if(ok) {
224     _fontsChanged = true;
225     setFont(label, font);
226   }
227 }
228
229 void AppearanceSettingsPage::widgetHasChanged() {
230   setChangedState(testHasChanged());
231 }
232
233 bool AppearanceSettingsPage::testHasChanged() {
234   if(_fontsChanged) return true; // comparisons are nasty for now
235
236   if(ui.styleComboBox->currentIndex() != ui.styleComboBox->property("storedValue").toInt()) return true;
237
238   if(selectedLocale() != QLocale()) return true; // QLocale() returns the default locale (manipulated via loadTranslation())
239
240   if(SettingsPage::hasChanged(ui.showWebPreview)) return true;
241   if(SettingsPage::hasChanged(ui.showUserStateIcons)) return true;
242
243   return false;
244 }