228fe40391888a51fe6fb7ccd75179dadef16ec9
[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 "qtui.h"
24 #include "qtuisettings.h"
25 #include "qtuistyle.h"
26
27 #include <QCheckBox>
28 #include <QFileDialog>
29 #include <QStyleFactory>
30
31 AppearanceSettingsPage::AppearanceSettingsPage(QWidget *parent)
32   : SettingsPage(tr("Interface"), QString(), parent)
33 {
34   ui.setupUi(this);
35   initAutoWidgets();
36   initStyleComboBox();
37   initLanguageComboBox();
38
39   foreach(QComboBox *comboBox, findChildren<QComboBox *>()) {
40     connect(comboBox, SIGNAL(currentIndexChanged(QString)), this, SLOT(widgetHasChanged()));
41   }
42   foreach(QCheckBox *checkBox, findChildren<QCheckBox *>()) {
43     connect(checkBox, SIGNAL(clicked()), this, SLOT(widgetHasChanged()));
44   }
45
46   connect(ui.chooseStyleSheet, SIGNAL(clicked()), SLOT(chooseStyleSheet()));
47 }
48
49 void AppearanceSettingsPage::initStyleComboBox() {
50   QStringList styleList = QStyleFactory::keys();
51   ui.styleComboBox->addItem(tr("<System Default>"));
52   foreach(QString style, styleList) {
53     ui.styleComboBox->addItem(style);
54   }
55 }
56
57 void AppearanceSettingsPage::initLanguageComboBox() {
58   QDir i18nDir(Quassel::translationDirPath(), "quassel_*.qm");
59
60   foreach(QString translationFile, i18nDir.entryList()) {
61     QString localeName(translationFile.mid(8));
62     localeName.chop(3);
63     QLocale locale(localeName);
64     _locales << locale;
65     ui.languageComboBox->addItem(QLocale::languageToString(locale.language()));
66   }
67 }
68
69 void AppearanceSettingsPage::defaults() {
70   ui.styleComboBox->setCurrentIndex(0);
71
72   SettingsPage::defaults();
73   widgetHasChanged();
74 }
75
76 void AppearanceSettingsPage::load() {
77   QtUiSettings uiSettings;
78
79   // Gui Style
80   QString style = uiSettings.value("Style", QString("")).toString();
81   if(style.isEmpty()) {
82     ui.styleComboBox->setCurrentIndex(0);
83   } else {
84     ui.styleComboBox->setCurrentIndex(ui.styleComboBox->findText(style, Qt::MatchExactly));
85     QApplication::setStyle(style);
86   }
87   ui.styleComboBox->setProperty("storedValue", ui.styleComboBox->currentIndex());
88
89   // Language
90   QLocale locale = uiSettings.value("Locale", QLocale::system()).value<QLocale>();
91   if(locale == QLocale::system())
92     ui.languageComboBox->setCurrentIndex(0);
93   else if(locale.language() == QLocale::C)
94     ui.languageComboBox->setCurrentIndex(1);
95   else
96     ui.languageComboBox->setCurrentIndex(ui.languageComboBox->findText(QLocale::languageToString(locale.language()), Qt::MatchExactly));
97   ui.languageComboBox->setProperty("storedValue", ui.languageComboBox->currentIndex());
98   Quassel::loadTranslation(selectedLocale());
99
100   SettingsPage::load();
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   bool needsStyleReload =
120         ui.useCustomStyleSheet->isChecked() != ui.useCustomStyleSheet->property("storedValue").toBool()
121     || (ui.useCustomStyleSheet->isChecked() && ui.customStyleSheetPath->text() != ui.customStyleSheetPath->property("storedValue").toString());
122
123   SettingsPage::save();
124   setChangedState(false);
125   if(needsStyleReload)
126     QtUi::style()->reload();
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::chooseStyleSheet() {
143   QString name = QFileDialog::getOpenFileName(this, tr("Please choose a stylesheet file"), QString(), "*.qss");
144   if(!name.isEmpty())
145     ui.customStyleSheetPath->setText(name);
146 }
147
148 void AppearanceSettingsPage::widgetHasChanged() {
149   setChangedState(testHasChanged());
150 }
151
152 bool AppearanceSettingsPage::testHasChanged() {
153   if(ui.styleComboBox->currentIndex() != ui.styleComboBox->property("storedValue").toInt()) return true;
154
155   if(selectedLocale() != QLocale()) return true; // QLocale() returns the default locale (manipulated via loadTranslation())
156
157   return false;
158 }