1 /***************************************************************************
2 * Copyright (C) 2005-09 by the Quassel Project *
3 * devel@quassel-irc.org *
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. *
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. *
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 ***************************************************************************/
21 #include "appearancesettingspage.h"
23 #include "buffersettings.h"
25 #include "qtuisettings.h"
26 #include "qtuistyle.h"
29 #include <QFileDialog>
30 #include <QStyleFactory>
34 AppearanceSettingsPage::AppearanceSettingsPage(QWidget *parent)
35 : SettingsPage(tr("Interface"), QString(), parent)
40 ui.minimizeOnClose->hide();
42 #ifdef QT_NO_SYSTEMTRAYICON
43 ui.useSystemTrayIcon->hide();
48 initLanguageComboBox();
50 foreach(QComboBox *comboBox, findChildren<QComboBox *>()) {
51 connect(comboBox, SIGNAL(currentIndexChanged(QString)), this, SLOT(widgetHasChanged()));
53 foreach(QCheckBox *checkBox, findChildren<QCheckBox *>()) {
54 connect(checkBox, SIGNAL(clicked()), this, SLOT(widgetHasChanged()));
57 connect(ui.chooseStyleSheet, SIGNAL(clicked()), SLOT(chooseStyleSheet()));
59 connect(ui.userNoticesInDefaultBuffer, SIGNAL(clicked(bool)), this, SLOT(widgetHasChanged()));
60 connect(ui.userNoticesInStatusBuffer, SIGNAL(clicked(bool)), this, SLOT(widgetHasChanged()));
61 connect(ui.userNoticesInCurrentBuffer, SIGNAL(clicked(bool)), this, SLOT(widgetHasChanged()));
63 connect(ui.serverNoticesInDefaultBuffer, SIGNAL(clicked(bool)), this, SLOT(widgetHasChanged()));
64 connect(ui.serverNoticesInStatusBuffer, SIGNAL(clicked(bool)), this, SLOT(widgetHasChanged()));
65 connect(ui.serverNoticesInCurrentBuffer, SIGNAL(clicked(bool)), this, SLOT(widgetHasChanged()));
67 connect(ui.errorMsgsInDefaultBuffer, SIGNAL(clicked(bool)), this, SLOT(widgetHasChanged()));
68 connect(ui.errorMsgsInStatusBuffer, SIGNAL(clicked(bool)), this, SLOT(widgetHasChanged()));
69 connect(ui.errorMsgsInCurrentBuffer, SIGNAL(clicked(bool)), this, SLOT(widgetHasChanged()));
72 void AppearanceSettingsPage::initStyleComboBox() {
73 QStringList styleList = QStyleFactory::keys();
74 ui.styleComboBox->addItem(tr("<System Default>"));
75 foreach(QString style, styleList) {
76 ui.styleComboBox->addItem(style);
80 void AppearanceSettingsPage::initLanguageComboBox() {
81 QDir i18nDir(Quassel::translationDirPath(), "*.qm");
83 QRegExp rx("(qt_)?([a-zA-Z_]+)\\.qm");
84 foreach(QString translationFile, i18nDir.entryList()) {
85 if(!rx.exactMatch(translationFile))
87 if(!rx.cap(1).isEmpty())
89 QLocale locale(rx.cap(2));
91 ui.languageComboBox->addItem(QLocale::languageToString(locale.language()));
95 void AppearanceSettingsPage::defaults() {
96 ui.styleComboBox->setCurrentIndex(0);
97 ui.languageComboBox->setCurrentIndex(1);
99 SettingsPage::defaults();
103 void AppearanceSettingsPage::load() {
104 QtUiSettings uiSettings;
107 QString style = uiSettings.value("Style", QString("")).toString();
108 if(style.isEmpty()) {
109 ui.styleComboBox->setCurrentIndex(0);
111 ui.styleComboBox->setCurrentIndex(ui.styleComboBox->findText(style, Qt::MatchExactly));
113 ui.styleComboBox->setProperty("storedValue", ui.styleComboBox->currentIndex());
116 QLocale locale = uiSettings.value("Locale", QLocale::system()).value<QLocale>();
117 if(locale == QLocale::system())
118 ui.languageComboBox->setCurrentIndex(1);
119 else if(locale.language() == QLocale::C) // we use C for "untranslated"
120 ui.languageComboBox->setCurrentIndex(0);
122 ui.languageComboBox->setCurrentIndex(ui.languageComboBox->findText(QLocale::languageToString(locale.language()), Qt::MatchExactly));
123 ui.languageComboBox->setProperty("storedValue", ui.languageComboBox->currentIndex());
124 Quassel::loadTranslation(selectedLocale());
127 BufferSettings bufferSettings;
128 int redirectTarget = bufferSettings.userNoticesTarget();
129 SettingsPage::load(ui.userNoticesInDefaultBuffer, redirectTarget & BufferSettings::DefaultBuffer);
130 SettingsPage::load(ui.userNoticesInStatusBuffer, redirectTarget & BufferSettings::StatusBuffer);
131 SettingsPage::load(ui.userNoticesInCurrentBuffer, redirectTarget & BufferSettings::CurrentBuffer);
133 redirectTarget = bufferSettings.serverNoticesTarget();
134 SettingsPage::load(ui.serverNoticesInDefaultBuffer, redirectTarget & BufferSettings::DefaultBuffer);
135 SettingsPage::load(ui.serverNoticesInStatusBuffer, redirectTarget & BufferSettings::StatusBuffer);
136 SettingsPage::load(ui.serverNoticesInCurrentBuffer, redirectTarget & BufferSettings::CurrentBuffer);
138 redirectTarget = bufferSettings.errorMsgsTarget();
139 SettingsPage::load(ui.errorMsgsInDefaultBuffer, redirectTarget & BufferSettings::DefaultBuffer);
140 SettingsPage::load(ui.errorMsgsInStatusBuffer, redirectTarget & BufferSettings::StatusBuffer);
141 SettingsPage::load(ui.errorMsgsInCurrentBuffer, redirectTarget & BufferSettings::CurrentBuffer);
143 SettingsPage::load();
144 setChangedState(false);
147 void AppearanceSettingsPage::save() {
148 QtUiSettings uiSettings;
150 if(ui.styleComboBox->currentIndex() < 1) {
151 uiSettings.setValue("Style", QString(""));
153 uiSettings.setValue("Style", ui.styleComboBox->currentText());
154 QApplication::setStyle(ui.styleComboBox->currentText());
156 ui.styleComboBox->setProperty("storedValue", ui.styleComboBox->currentIndex());
158 if(ui.languageComboBox->currentIndex() == 1) {
159 uiSettings.remove("Locale"); // force the default (QLocale::system())
161 uiSettings.setValue("Locale", selectedLocale());
163 ui.languageComboBox->setProperty("storedValue", ui.languageComboBox->currentIndex());
165 bool needsStyleReload =
166 ui.useCustomStyleSheet->isChecked() != ui.useCustomStyleSheet->property("storedValue").toBool()
167 || (ui.useCustomStyleSheet->isChecked() && ui.customStyleSheetPath->text() != ui.customStyleSheetPath->property("storedValue").toString());
169 BufferSettings bufferSettings;
170 int redirectTarget = 0;
171 if(ui.userNoticesInDefaultBuffer->isChecked())
172 redirectTarget |= BufferSettings::DefaultBuffer;
173 if(ui.userNoticesInStatusBuffer->isChecked())
174 redirectTarget |= BufferSettings::StatusBuffer;
175 if(ui.userNoticesInCurrentBuffer->isChecked())
176 redirectTarget |= BufferSettings::CurrentBuffer;
177 bufferSettings.setUserNoticesTarget(redirectTarget);
180 if(ui.serverNoticesInDefaultBuffer->isChecked())
181 redirectTarget |= BufferSettings::DefaultBuffer;
182 if(ui.serverNoticesInStatusBuffer->isChecked())
183 redirectTarget |= BufferSettings::StatusBuffer;
184 if(ui.serverNoticesInCurrentBuffer->isChecked())
185 redirectTarget |= BufferSettings::CurrentBuffer;
186 bufferSettings.setServerNoticesTarget(redirectTarget);
189 if(ui.errorMsgsInDefaultBuffer->isChecked())
190 redirectTarget |= BufferSettings::DefaultBuffer;
191 if(ui.errorMsgsInStatusBuffer->isChecked())
192 redirectTarget |= BufferSettings::StatusBuffer;
193 if(ui.errorMsgsInCurrentBuffer->isChecked())
194 redirectTarget |= BufferSettings::CurrentBuffer;
195 bufferSettings.setErrorMsgsTarget(redirectTarget);
197 SettingsPage::save();
198 setChangedState(false);
200 QtUi::style()->reload();
203 QLocale AppearanceSettingsPage::selectedLocale() const {
205 int index = ui.languageComboBox->currentIndex();
207 locale = QLocale::system();
209 locale = QLocale::c();
211 locale = _locales[index - 2];
216 void AppearanceSettingsPage::chooseStyleSheet() {
217 QString dir = ui.customStyleSheetPath->property("storedValue").toString();
218 if(!dir.isEmpty() && QFile(dir).exists())
219 dir = QDir(dir).absolutePath();
221 dir = QDir(Quassel::findDataFilePath("default.qss")).absolutePath();
223 QString name = QFileDialog::getOpenFileName(this, tr("Please choose a stylesheet file"), dir, "*.qss");
225 ui.customStyleSheetPath->setText(name);
228 void AppearanceSettingsPage::widgetHasChanged() {
229 setChangedState(testHasChanged());
232 bool AppearanceSettingsPage::testHasChanged() {
233 if(ui.styleComboBox->currentIndex() != ui.styleComboBox->property("storedValue").toInt()) return true;
234 if(ui.languageComboBox->currentIndex() != ui.languageComboBox->property("storedValue").toInt()) return true;
236 if(SettingsPage::hasChanged(ui.userNoticesInStatusBuffer)) return true;
237 if(SettingsPage::hasChanged(ui.userNoticesInDefaultBuffer)) return true;
238 if(SettingsPage::hasChanged(ui.userNoticesInCurrentBuffer)) return true;
240 if(SettingsPage::hasChanged(ui.serverNoticesInStatusBuffer)) return true;
241 if(SettingsPage::hasChanged(ui.serverNoticesInDefaultBuffer)) return true;
242 if(SettingsPage::hasChanged(ui.serverNoticesInCurrentBuffer)) return true;
244 if(SettingsPage::hasChanged(ui.errorMsgsInStatusBuffer)) return true;
245 if(SettingsPage::hasChanged(ui.errorMsgsInDefaultBuffer)) return true;
246 if(SettingsPage::hasChanged(ui.errorMsgsInCurrentBuffer)) return true;