ab39344aa43dd46a2f8d39a6975cbaefadc70db2
[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("Interface"), QString(), parent),
39   _fontsChanged(false)
40 {
41   ui.setupUi(this);
42   initAutoWidgets();
43   initStyleComboBox();
44   initLanguageComboBox();
45
46   foreach(QComboBox *comboBox, findChildren<QComboBox *>()) {
47     connect(comboBox, SIGNAL(currentIndexChanged(QString)), this, SLOT(widgetHasChanged()));
48   }
49   foreach(QCheckBox *checkBox, findChildren<QCheckBox *>()) {
50     connect(checkBox, SIGNAL(clicked()), this, SLOT(widgetHasChanged()));
51   }
52
53   mapper = new QSignalMapper(this);
54   connect(mapper, SIGNAL(mapped(QWidget *)), this, SLOT(chooseFont(QWidget *)));
55
56   connect(ui.chooseInputLine, SIGNAL(clicked()), mapper, SLOT(map()));
57
58   mapper->setMapping(ui.chooseInputLine, ui.demoInputLine);
59
60   connect(ui.chooseStyleSheet, SIGNAL(clicked()), SLOT(chooseStyleSheet()));
61 }
62
63 void AppearanceSettingsPage::initStyleComboBox() {
64   QStringList styleList = QStyleFactory::keys();
65   ui.styleComboBox->addItem(tr("<System Default>"));
66   foreach(QString style, styleList) {
67     ui.styleComboBox->addItem(style);
68   }
69 }
70
71 void AppearanceSettingsPage::initLanguageComboBox() {
72   QDir i18nDir(Quassel::translationDirPath(), "quassel_*.qm");
73
74   foreach(QString translationFile, i18nDir.entryList()) {
75     QString localeName(translationFile.mid(8));
76     localeName.chop(3);
77     QLocale locale(localeName);
78     _locales << locale;
79     ui.languageComboBox->addItem(QLocale::languageToString(locale.language()));
80   }
81 }
82
83 void AppearanceSettingsPage::defaults() {
84   ui.styleComboBox->setCurrentIndex(0);
85
86   loadFonts(Settings::Default);
87   _fontsChanged = true;
88
89   SettingsPage::defaults();
90   widgetHasChanged();
91 }
92
93 void AppearanceSettingsPage::load() {
94   QtUiSettings uiSettings;
95
96   // Gui Style
97   QString style = uiSettings.value("Style", QString("")).toString();
98   if(style.isEmpty()) {
99     ui.styleComboBox->setCurrentIndex(0);
100   } else {
101     ui.styleComboBox->setCurrentIndex(ui.styleComboBox->findText(style, Qt::MatchExactly));
102     QApplication::setStyle(style);
103   }
104   ui.styleComboBox->setProperty("storedValue", ui.styleComboBox->currentIndex());
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   ui.languageComboBox->setProperty("storedValue", ui.languageComboBox->currentIndex());
115   Quassel::loadTranslation(selectedLocale());
116
117   loadFonts(Settings::Custom);
118
119   SettingsPage::load();
120   setChangedState(false);
121 }
122
123 void AppearanceSettingsPage::loadFonts(Settings::Mode mode) {
124   QtUiStyleSettings s("Fonts");
125
126   QFont inputLineFont;
127   if(mode == Settings::Custom)
128     inputLineFont = s.value("InputLine", QFont()).value<QFont>();
129   setFont(ui.demoInputLine, inputLineFont);
130
131   _fontsChanged = false;
132 }
133
134 void AppearanceSettingsPage::save() {
135   QtUiSettings uiSettings;
136
137   if(ui.styleComboBox->currentIndex() < 1) {
138     uiSettings.setValue("Style", QString(""));
139   } else {
140     uiSettings.setValue("Style", ui.styleComboBox->currentText());
141   }
142
143   if(ui.languageComboBox->currentIndex() == 0) {
144     uiSettings.remove("Locale"); // force the default (QLocale::system())
145   } else {
146     uiSettings.setValue("Locale", selectedLocale());
147   }
148
149   // Fonts
150   QtUiStyleSettings fontSettings("Fonts");
151   if(ui.demoInputLine->font() != QApplication::font())
152     fontSettings.setValue("InputLine", ui.demoInputLine->font());
153   else
154     fontSettings.setValue("InputLine", "");
155
156   _fontsChanged = false;
157
158   bool needsStyleReload =
159         ui.useCustomStyleSheet->isChecked() != ui.useCustomStyleSheet->property("storedValue").toBool()
160     || (ui.useCustomStyleSheet->isChecked() && ui.customStyleSheetPath->text() != ui.customStyleSheetPath->property("storedValue").toString());
161
162   SettingsPage::save();
163   setChangedState(false);
164   if(needsStyleReload)
165     QtUi::style()->reload();
166 }
167
168 QLocale AppearanceSettingsPage::selectedLocale() const {
169   QLocale locale;
170   int index = ui.languageComboBox->currentIndex();
171   if(index == 0)
172     locale = QLocale::system();
173   else if(index == 1)
174     locale = QLocale::c();
175   else if(index > 1)
176     locale = _locales[index - 2];
177
178   return locale;
179 }
180
181 void AppearanceSettingsPage::setFont(QLabel *label, const QFont &font_) {
182   QFont font = font_;
183   if(font.family().isEmpty())
184     font = QApplication::font();
185   label->setFont(font);
186   label->setText(QString("%1 %2").arg(font.family()).arg(font.pointSize()));
187   widgetHasChanged();
188 }
189
190 void AppearanceSettingsPage::chooseFont(QWidget *widget) {
191   QLabel *label = qobject_cast<QLabel *>(widget);
192   Q_ASSERT(label);
193   bool ok;
194   QFont font = QFontDialog::getFont(&ok, label->font());
195   if(ok) {
196     _fontsChanged = true;
197     setFont(label, font);
198   }
199 }
200
201 void AppearanceSettingsPage::chooseStyleSheet() {
202   QString name = QFileDialog::getOpenFileName(this, tr("Please choose a stylesheet file"), QString(), "*.qss");
203   if(!name.isEmpty())
204     ui.customStyleSheetPath->setText(name);
205 }
206
207 void AppearanceSettingsPage::widgetHasChanged() {
208   setChangedState(testHasChanged());
209 }
210
211 bool AppearanceSettingsPage::testHasChanged() {
212   if(_fontsChanged) return true; // comparisons are nasty for now
213
214   if(ui.styleComboBox->currentIndex() != ui.styleComboBox->property("storedValue").toInt()) return true;
215
216   if(selectedLocale() != QLocale()) return true; // QLocale() returns the default locale (manipulated via loadTranslation())
217
218   return false;
219 }