Move font settings to general appearance settingspage
[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 {
38   ui.setupUi(this);
39   initStyleComboBox();
40   initLanguageComboBox();
41
42 #ifndef HAVE_WEBKIT
43   ui.showWebPreview->hide();
44   ui.showWebPreview->setEnabled(false);
45 #endif
46
47   foreach(QComboBox *comboBox, findChildren<QComboBox *>()) {
48     connect(comboBox, SIGNAL(currentIndexChanged(QString)), this, SLOT(widgetHasChanged()));
49   }
50   foreach(QCheckBox *checkBox, findChildren<QCheckBox *>()) {
51     connect(checkBox, SIGNAL(clicked()), this, SLOT(widgetHasChanged()));
52   }
53
54   mapper = new QSignalMapper(this);
55   connect(mapper, SIGNAL(mapped(QWidget *)), this, SLOT(chooseFont(QWidget *)));
56
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()));
60
61   mapper->setMapping(ui.chooseChatView, ui.demoChatView);
62   mapper->setMapping(ui.chooseBufferView, ui.demoBufferView);
63   mapper->setMapping(ui.chooseInputLine, ui.demoInputLine);
64 }
65
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);
71   }
72 }
73
74 void AppearanceSettingsPage::initLanguageComboBox() {
75   QDir i18nDir(Quassel::translationDirPath(), "quassel_*.qm");
76
77   foreach(QString translationFile, i18nDir.entryList()) {
78     QString localeName(translationFile.mid(8));
79     localeName.chop(3);
80     QLocale locale(localeName);
81     _locales << locale;
82     ui.languageComboBox->addItem(QLocale::languageToString(locale.language()));
83   }
84 }
85
86 void AppearanceSettingsPage::defaults() {
87   ui.styleComboBox->setCurrentIndex(0);
88
89   loadFonts(Settings::Default);
90   _fontsChanged = true;
91   widgetHasChanged();
92 }
93
94 void AppearanceSettingsPage::load() {
95   QtUiSettings uiSettings;
96
97   // Gui Style
98   settings["Style"] = uiSettings.value("Style", QString(""));
99   if(settings["Style"].toString() == "") {
100     ui.styleComboBox->setCurrentIndex(0);
101   } else {
102     ui.styleComboBox->setCurrentIndex(ui.styleComboBox->findText(settings["Style"].toString(), Qt::MatchExactly));
103     QApplication::setStyle(settings["Style"].toString());
104   }
105
106   // Language
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);
112   else
113     ui.languageComboBox->setCurrentIndex(ui.languageComboBox->findText(QLocale::languageToString(locale.language()), Qt::MatchExactly));
114   Quassel::loadTranslation(selectedLocale());
115
116   ChatViewSettings chatViewSettings;
117   SettingsPage::load(ui.showWebPreview, chatViewSettings.showWebPreview());
118
119   BufferSettings bufferSettings;
120   SettingsPage::load(ui.showUserStateIcons, bufferSettings.showUserStateIcons());
121
122   loadFonts(Settings::Custom);
123
124   setChangedState(false);
125 }
126
127 void AppearanceSettingsPage::loadFonts(Settings::Mode mode) {
128   QtUiStyleSettings s("Fonts");
129
130   QFont inputLineFont;
131   if(mode == Settings::Custom)
132     inputLineFont = s.value("InputLine", QFont()).value<QFont>();
133   setFont(ui.demoInputLine, inputLineFont);
134
135   QFont bufferViewFont;
136   if(mode == Settings::Custom)
137     bufferViewFont = s.value("BufferView", QFont()).value<QFont>();
138   setFont(ui.demoBufferView, bufferViewFont);
139
140   QTextCharFormat chatFormat = QtUi::style()->format(UiStyle::None, mode);
141   setFont(ui.demoChatView, chatFormat.font());
142
143   _fontsChanged = false;
144 }
145
146 void AppearanceSettingsPage::save() {
147   QtUiSettings uiSettings;
148
149   if(ui.styleComboBox->currentIndex() < 1) {
150     uiSettings.setValue("Style", QString(""));
151   } else {
152     uiSettings.setValue("Style", ui.styleComboBox->currentText());
153   }
154
155   if(ui.languageComboBox->currentIndex() == 0) {
156     uiSettings.remove("Locale"); // force the default (QLocale::system())
157   } else {
158     uiSettings.setValue("Locale", selectedLocale());
159   }
160
161   ChatViewSettings chatViewSettings;
162   chatViewSettings.enableWebPreview(ui.showWebPreview->isChecked());
163
164   BufferSettings bufferSettings;
165   bufferSettings.enableUserStateIcons(ui.showUserStateIcons->isChecked());
166
167   // Fonts
168   QtUiStyleSettings fontSettings("Fonts");
169   if(ui.demoInputLine->font() != QApplication::font())
170     fontSettings.setValue("InputLine", ui.demoInputLine->font());
171   else
172     fontSettings.setValue("InputLine", "");
173
174   if(ui.demoBufferView->font() != QApplication::font())
175     fontSettings.setValue("BufferView", ui.demoBufferView->font());
176   else
177     fontSettings.setValue("BufferView", "");
178
179   QTextCharFormat chatFormat = QtUi::style()->format(UiStyle::None);
180   chatFormat.setFont(ui.demoChatView->font());
181   QtUi::style()->setFormat(UiStyle::None, chatFormat, Settings::Custom);
182
183   _fontsChanged = false;
184
185   load();
186   setChangedState(false);
187 }
188
189 QLocale AppearanceSettingsPage::selectedLocale() const {
190   QLocale locale;
191   int index = ui.languageComboBox->currentIndex();
192   if(index == 0)
193     locale = QLocale::system();
194   else if(index == 1)
195     locale = QLocale::c();
196   else if(index > 1)
197     locale = _locales[index - 2];
198
199   return locale;
200 }
201
202 void AppearanceSettingsPage::setFont(QLabel *label, const QFont &font_) {
203   QFont font = 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()));
208   widgetHasChanged();
209 }
210
211 void AppearanceSettingsPage::chooseFont(QWidget *widget) {
212   QLabel *label = qobject_cast<QLabel *>(widget);
213   Q_ASSERT(label);
214   bool ok;
215   QFont font = QFontDialog::getFont(&ok, label->font());
216   if(ok) {
217     setFont(label, font);
218     _fontsChanged = true;
219   }
220 }
221
222 void AppearanceSettingsPage::widgetHasChanged() {
223   setChangedState(testHasChanged());
224 }
225
226 bool AppearanceSettingsPage::testHasChanged() {
227   if(_fontsChanged) return true; // comparisons are nasty for now
228
229   if(settings["Style"].toString() != ui.styleComboBox->currentText()) return true;
230   if(selectedLocale() != QLocale()) return true; // QLocale() returns the default locale (manipulated via loadTranslation())
231
232   if(SettingsPage::hasChanged(ui.showWebPreview)) return true;
233   if(SettingsPage::hasChanged(ui.showUserStateIcons)) return true;
234
235   return false;
236 }