ChatMonitorSettingspage ported to 0.3.x
[quassel.git] / src / qtui / settingspages / appearancesettingspage.cpp
1 /***************************************************************************
2  *   Copyright (C) 2005-08 by the Quassel IRC Team                         *
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 "util.h"
28
29 #include <QDir>
30 #include <QStyleFactory>
31
32 AppearanceSettingsPage::AppearanceSettingsPage(QWidget *parent)
33   : SettingsPage(tr("Appearance"), tr("General"), parent) {
34   ui.setupUi(this);
35   initStyleComboBox();
36   initLanguageComboBox();
37
38   foreach(QComboBox *comboBox, findChildren<QComboBox *>()) {
39     connect(comboBox, SIGNAL(currentIndexChanged(QString)), this, SLOT(widgetHasChanged()));
40   }
41   foreach(QCheckBox *checkBox, findChildren<QCheckBox *>()) {
42     connect(checkBox, SIGNAL(clicked()), this, SLOT(widgetHasChanged()));
43   }
44 }
45
46 void AppearanceSettingsPage::initStyleComboBox() {
47   QStringList styleList = QStyleFactory::keys();
48   ui.styleComboBox->addItem(tr("<System Default>"));
49   foreach(QString style, styleList) {
50     ui.styleComboBox->addItem(style);
51   }
52 }
53
54 void AppearanceSettingsPage::initLanguageComboBox() {
55   QDir i18nDir(":/i18n", "quassel_*.qm");
56
57   foreach(QString translationFile, i18nDir.entryList()) {
58     QString localeName(translationFile.mid(8));
59     localeName.chop(3);
60     QLocale locale(localeName);
61     _locales << locale;
62     ui.languageComboBox->addItem(QLocale::languageToString(locale.language()));
63   }
64
65 }
66
67 void AppearanceSettingsPage::defaults() {
68   ui.styleComboBox->setCurrentIndex(0);
69
70   widgetHasChanged();
71 }
72
73 void AppearanceSettingsPage::load() {
74   QtUiSettings uiSettings;
75
76   // Gui Style
77   settings["Style"] = uiSettings.value("Style", QString(""));
78   if(settings["Style"].toString() == "") {
79     ui.styleComboBox->setCurrentIndex(0);
80   } else {
81     ui.styleComboBox->setCurrentIndex(ui.styleComboBox->findText(settings["Style"].toString(), Qt::MatchExactly));
82     QApplication::setStyle(settings["Style"].toString());
83   }
84
85   // Language
86   QLocale locale = uiSettings.value("Locale", QLocale::system()).value<QLocale>();
87   if(locale == QLocale::system())
88     ui.languageComboBox->setCurrentIndex(0);
89   else if(locale.language() == QLocale::C)
90     ui.languageComboBox->setCurrentIndex(1);
91   else
92     ui.languageComboBox->setCurrentIndex(ui.languageComboBox->findText(QLocale::languageToString(locale.language()), Qt::MatchExactly));
93   loadTranslation(selectedLocale());
94
95   ChatViewSettings chatViewSettings;
96   SettingsPage::load(ui.showWebPreview, chatViewSettings.showWebPreview());
97
98   BufferSettings bufferSettings;
99   SettingsPage::load(ui.showUserStateIcons, bufferSettings.showUserStateIcons());
100
101   setChangedState(false);
102 }
103
104 void AppearanceSettingsPage::save() {
105   QtUiSettings uiSettings;
106
107   if(ui.styleComboBox->currentIndex() < 1) {
108     uiSettings.setValue("Style", QString(""));
109   } else {
110     uiSettings.setValue("Style", ui.styleComboBox->currentText());
111   }
112
113   if(ui.languageComboBox->currentIndex() == 0) {
114     uiSettings.remove("Locale"); // force the default (QLocale::system())
115   } else {
116     uiSettings.setValue("Locale", selectedLocale());
117   }
118
119   ChatViewSettings chatViewSettings;
120   chatViewSettings.enableWebPreview(ui.showWebPreview->isChecked());
121
122   BufferSettings bufferSettings;
123   bufferSettings.enableUserStateIcons(ui.showUserStateIcons->isChecked());
124
125   load();
126   setChangedState(false);
127 }
128
129 QLocale AppearanceSettingsPage::selectedLocale() const {
130   QLocale locale;
131   int index = ui.languageComboBox->currentIndex();
132   if(index == 0)
133     locale = QLocale::system();
134   else if(index == 1)
135     locale = QLocale::c();
136   else if(index > 1)
137     locale = _locales[index - 2];
138
139   return locale;
140 }
141
142 void AppearanceSettingsPage::widgetHasChanged() {
143   setChangedState(testHasChanged());
144 }
145
146 bool AppearanceSettingsPage::testHasChanged() {
147   if(settings["Style"].toString() != ui.styleComboBox->currentText()) return true;
148   if(selectedLocale() != QLocale()) return true; // QLocale() returns the default locale (manipulated via loadTranslation())
149
150   if(SettingsPage::hasChanged(ui.showWebPreview)) return true;
151   if(SettingsPage::hasChanged(ui.showUserStateIcons)) return true;
152
153   return false;
154 }
155
156
157
158