7132f6835fcfa2e1ce7dcf24b4828cf1fd1a7c39
[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 "qtui.h"
25 #include "qtuisettings.h"
26 #include "qtuistyle.h"
27
28 #include <QCheckBox>
29 #include <QFileDialog>
30 #include <QStyleFactory>
31 #include <QFile>
32 #include <QDir>
33
34 AppearanceSettingsPage::AppearanceSettingsPage(QWidget *parent)
35   : SettingsPage(tr("Interface"), QString(), parent)
36 {
37   ui.setupUi(this);
38
39 #ifdef Q_WS_MAC
40   ui.minimizeOnClose->hide();
41 #endif
42
43   initAutoWidgets();
44   initStyleComboBox();
45   initLanguageComboBox();
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   connect(ui.chooseStyleSheet, SIGNAL(clicked()), SLOT(chooseStyleSheet()));
55
56   connect(ui.userNoticesInDefaultBuffer, SIGNAL(clicked(bool)), this, SLOT(widgetHasChanged()));
57   connect(ui.userNoticesInStatusBuffer, SIGNAL(clicked(bool)), this, SLOT(widgetHasChanged()));
58   connect(ui.userNoticesInCurrentBuffer, SIGNAL(clicked(bool)), this, SLOT(widgetHasChanged()));
59
60   connect(ui.serverNoticesInDefaultBuffer, SIGNAL(clicked(bool)), this, SLOT(widgetHasChanged()));
61   connect(ui.serverNoticesInStatusBuffer, SIGNAL(clicked(bool)), this, SLOT(widgetHasChanged()));
62   connect(ui.serverNoticesInCurrentBuffer, SIGNAL(clicked(bool)), this, SLOT(widgetHasChanged()));
63
64   connect(ui.errorMsgsInDefaultBuffer, SIGNAL(clicked(bool)), this, SLOT(widgetHasChanged()));
65   connect(ui.errorMsgsInStatusBuffer, SIGNAL(clicked(bool)), this, SLOT(widgetHasChanged()));
66   connect(ui.errorMsgsInCurrentBuffer, SIGNAL(clicked(bool)), this, SLOT(widgetHasChanged()));
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(), "*.qm");
79
80   QRegExp rx("(qt_)?([a-zA-Z_]+)\\.qm");
81   foreach(QString translationFile, i18nDir.entryList()) {
82     if(!rx.exactMatch(translationFile))
83       continue;
84     if(!rx.cap(1).isEmpty())
85       continue;
86     QLocale locale(rx.cap(2));
87     _locales << locale;
88     ui.languageComboBox->addItem(QLocale::languageToString(locale.language()));
89   }
90 }
91
92 void AppearanceSettingsPage::defaults() {
93   ui.styleComboBox->setCurrentIndex(0);
94   ui.languageComboBox->setCurrentIndex(1);
95
96   SettingsPage::defaults();
97   widgetHasChanged();
98 }
99
100 void AppearanceSettingsPage::load() {
101   QtUiSettings uiSettings;
102
103   // Gui Style
104   QString style = uiSettings.value("Style", QString("")).toString();
105   if(style.isEmpty()) {
106     ui.styleComboBox->setCurrentIndex(0);
107   } else {
108     ui.styleComboBox->setCurrentIndex(ui.styleComboBox->findText(style, Qt::MatchExactly));
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(1);
116   else if(locale.language() == QLocale::C)  // we use C for "untranslated"
117     ui.languageComboBox->setCurrentIndex(0);
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   // bufferSettings:
124   BufferSettings bufferSettings;
125   int redirectTarget = bufferSettings.userNoticesTarget();
126   SettingsPage::load(ui.userNoticesInDefaultBuffer, redirectTarget & BufferSettings::DefaultBuffer);
127   SettingsPage::load(ui.userNoticesInStatusBuffer, redirectTarget & BufferSettings::StatusBuffer);
128   SettingsPage::load(ui.userNoticesInCurrentBuffer, redirectTarget & BufferSettings::CurrentBuffer);
129
130   redirectTarget = bufferSettings.serverNoticesTarget();
131   SettingsPage::load(ui.serverNoticesInDefaultBuffer, redirectTarget & BufferSettings::DefaultBuffer);
132   SettingsPage::load(ui.serverNoticesInStatusBuffer, redirectTarget & BufferSettings::StatusBuffer);
133   SettingsPage::load(ui.serverNoticesInCurrentBuffer, redirectTarget & BufferSettings::CurrentBuffer);
134
135   redirectTarget = bufferSettings.errorMsgsTarget();
136   SettingsPage::load(ui.errorMsgsInDefaultBuffer, redirectTarget & BufferSettings::DefaultBuffer);
137   SettingsPage::load(ui.errorMsgsInStatusBuffer, redirectTarget & BufferSettings::StatusBuffer);
138   SettingsPage::load(ui.errorMsgsInCurrentBuffer, redirectTarget & BufferSettings::CurrentBuffer);
139
140   SettingsPage::load();
141   setChangedState(false);
142 }
143
144 void AppearanceSettingsPage::save() {
145   QtUiSettings uiSettings;
146
147   if(ui.styleComboBox->currentIndex() < 1) {
148     uiSettings.setValue("Style", QString(""));
149   } else {
150     uiSettings.setValue("Style", ui.styleComboBox->currentText());
151     QApplication::setStyle(ui.styleComboBox->currentText());
152   }
153   ui.styleComboBox->setProperty("storedValue", ui.styleComboBox->currentIndex());
154
155   if(ui.languageComboBox->currentIndex() == 1) {
156     uiSettings.remove("Locale"); // force the default (QLocale::system())
157   } else {
158     uiSettings.setValue("Locale", selectedLocale());
159   }
160   ui.languageComboBox->setProperty("storedValue", ui.languageComboBox->currentIndex());
161
162   bool needsStyleReload =
163         ui.useCustomStyleSheet->isChecked() != ui.useCustomStyleSheet->property("storedValue").toBool()
164     || (ui.useCustomStyleSheet->isChecked() && ui.customStyleSheetPath->text() != ui.customStyleSheetPath->property("storedValue").toString());
165
166   BufferSettings bufferSettings;
167   int redirectTarget = 0;
168   if(ui.userNoticesInDefaultBuffer->isChecked())
169     redirectTarget |= BufferSettings::DefaultBuffer;
170   if(ui.userNoticesInStatusBuffer->isChecked())
171     redirectTarget |= BufferSettings::StatusBuffer;
172   if(ui.userNoticesInCurrentBuffer->isChecked())
173     redirectTarget |= BufferSettings::CurrentBuffer;
174   bufferSettings.setUserNoticesTarget(redirectTarget);
175
176   redirectTarget = 0;
177   if(ui.serverNoticesInDefaultBuffer->isChecked())
178     redirectTarget |= BufferSettings::DefaultBuffer;
179   if(ui.serverNoticesInStatusBuffer->isChecked())
180     redirectTarget |= BufferSettings::StatusBuffer;
181   if(ui.serverNoticesInCurrentBuffer->isChecked())
182     redirectTarget |= BufferSettings::CurrentBuffer;
183   bufferSettings.setServerNoticesTarget(redirectTarget);
184
185   redirectTarget = 0;
186   if(ui.errorMsgsInDefaultBuffer->isChecked())
187     redirectTarget |= BufferSettings::DefaultBuffer;
188   if(ui.errorMsgsInStatusBuffer->isChecked())
189     redirectTarget |= BufferSettings::StatusBuffer;
190   if(ui.errorMsgsInCurrentBuffer->isChecked())
191     redirectTarget |= BufferSettings::CurrentBuffer;
192   bufferSettings.setErrorMsgsTarget(redirectTarget);
193
194   SettingsPage::save();
195   setChangedState(false);
196   if(needsStyleReload)
197     QtUi::style()->reload();
198 }
199
200 QLocale AppearanceSettingsPage::selectedLocale() const {
201   QLocale locale;
202   int index = ui.languageComboBox->currentIndex();
203   if(index == 1)
204     locale = QLocale::system();
205   else if(index == 0)
206     locale = QLocale::c();
207   else if(index > 1)
208     locale = _locales[index - 2];
209
210   return locale;
211 }
212
213 void AppearanceSettingsPage::chooseStyleSheet() {
214   QString dir = ui.customStyleSheetPath->property("storedValue").toString();
215   if(!dir.isEmpty() && QFile(dir).exists())
216     dir = QDir(dir).absolutePath();
217   else
218     dir = QDir(Quassel::findDataFilePath("default.qss")).absolutePath();
219
220   QString name = QFileDialog::getOpenFileName(this, tr("Please choose a stylesheet file"), dir, "*.qss");
221   if(!name.isEmpty())
222     ui.customStyleSheetPath->setText(name);
223 }
224
225 void AppearanceSettingsPage::widgetHasChanged() {
226   setChangedState(testHasChanged());
227 }
228
229 bool AppearanceSettingsPage::testHasChanged() {
230   if(ui.styleComboBox->currentIndex() != ui.styleComboBox->property("storedValue").toInt()) return true;
231   if(ui.languageComboBox->currentIndex() != ui.languageComboBox->property("storedValue").toInt()) return true;
232
233   if(SettingsPage::hasChanged(ui.userNoticesInStatusBuffer)) return true;
234   if(SettingsPage::hasChanged(ui.userNoticesInDefaultBuffer)) return true;
235   if(SettingsPage::hasChanged(ui.userNoticesInCurrentBuffer)) return true;
236
237   if(SettingsPage::hasChanged(ui.serverNoticesInStatusBuffer)) return true;
238   if(SettingsPage::hasChanged(ui.serverNoticesInDefaultBuffer)) return true;
239   if(SettingsPage::hasChanged(ui.serverNoticesInCurrentBuffer)) return true;
240
241   if(SettingsPage::hasChanged(ui.errorMsgsInStatusBuffer)) return true;
242   if(SettingsPage::hasChanged(ui.errorMsgsInDefaultBuffer)) return true;
243   if(SettingsPage::hasChanged(ui.errorMsgsInCurrentBuffer)) return true;
244
245   return false;
246 }