0b33f0f4e0066d27de55cd5f9a0e6e7f2cb350c5
[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 #include <QFile>
31 #include <QDir>
32
33 AppearanceSettingsPage::AppearanceSettingsPage(QWidget *parent)
34   : SettingsPage(tr("Interface"), QString(), parent)
35 {
36   ui.setupUi(this);
37   initAutoWidgets();
38   initStyleComboBox();
39   initLanguageComboBox();
40
41   foreach(QComboBox *comboBox, findChildren<QComboBox *>()) {
42     connect(comboBox, SIGNAL(currentIndexChanged(QString)), this, SLOT(widgetHasChanged()));
43   }
44   foreach(QCheckBox *checkBox, findChildren<QCheckBox *>()) {
45     connect(checkBox, SIGNAL(clicked()), this, SLOT(widgetHasChanged()));
46   }
47
48   connect(ui.chooseStyleSheet, SIGNAL(clicked()), SLOT(chooseStyleSheet()));
49 }
50
51 void AppearanceSettingsPage::initStyleComboBox() {
52   QStringList styleList = QStyleFactory::keys();
53   ui.styleComboBox->addItem(tr("<System Default>"));
54   foreach(QString style, styleList) {
55     ui.styleComboBox->addItem(style);
56   }
57 }
58
59 void AppearanceSettingsPage::initLanguageComboBox() {
60   QDir i18nDir(Quassel::translationDirPath(), "quassel_*.qm");
61
62   foreach(QString translationFile, i18nDir.entryList()) {
63     QString localeName(translationFile.mid(8));
64     localeName.chop(3);
65     QLocale locale(localeName);
66     _locales << locale;
67     ui.languageComboBox->addItem(QLocale::languageToString(locale.language()));
68   }
69 }
70
71 void AppearanceSettingsPage::defaults() {
72   ui.styleComboBox->setCurrentIndex(0);
73
74   SettingsPage::defaults();
75   widgetHasChanged();
76 }
77
78 void AppearanceSettingsPage::load() {
79   QtUiSettings uiSettings;
80
81   // Gui Style
82   QString style = uiSettings.value("Style", QString("")).toString();
83   if(style.isEmpty()) {
84     ui.styleComboBox->setCurrentIndex(0);
85   } else {
86     ui.styleComboBox->setCurrentIndex(ui.styleComboBox->findText(style, Qt::MatchExactly));
87     QApplication::setStyle(style);
88   }
89   ui.styleComboBox->setProperty("storedValue", ui.styleComboBox->currentIndex());
90
91   // Language
92   QLocale locale = uiSettings.value("Locale", QLocale::system()).value<QLocale>();
93   if(locale == QLocale::system())
94     ui.languageComboBox->setCurrentIndex(0);
95   else if(locale.language() == QLocale::C)
96     ui.languageComboBox->setCurrentIndex(1);
97   else
98     ui.languageComboBox->setCurrentIndex(ui.languageComboBox->findText(QLocale::languageToString(locale.language()), Qt::MatchExactly));
99   ui.languageComboBox->setProperty("storedValue", ui.languageComboBox->currentIndex());
100   Quassel::loadTranslation(selectedLocale());
101
102   SettingsPage::load();
103   setChangedState(false);
104 }
105
106 void AppearanceSettingsPage::save() {
107   QtUiSettings uiSettings;
108
109   if(ui.styleComboBox->currentIndex() < 1) {
110     uiSettings.setValue("Style", QString(""));
111   } else {
112     uiSettings.setValue("Style", ui.styleComboBox->currentText());
113   }
114
115   if(ui.languageComboBox->currentIndex() == 0) {
116     uiSettings.remove("Locale"); // force the default (QLocale::system())
117   } else {
118     uiSettings.setValue("Locale", selectedLocale());
119   }
120
121   bool needsStyleReload =
122         ui.useCustomStyleSheet->isChecked() != ui.useCustomStyleSheet->property("storedValue").toBool()
123     || (ui.useCustomStyleSheet->isChecked() && ui.customStyleSheetPath->text() != ui.customStyleSheetPath->property("storedValue").toString());
124
125   SettingsPage::save();
126   setChangedState(false);
127   if(needsStyleReload)
128     QtUi::style()->reload();
129 }
130
131 QLocale AppearanceSettingsPage::selectedLocale() const {
132   QLocale locale;
133   int index = ui.languageComboBox->currentIndex();
134   if(index == 0)
135     locale = QLocale::system();
136   else if(index == 1)
137     locale = QLocale::c();
138   else if(index > 1)
139     locale = _locales[index - 2];
140
141   return locale;
142 }
143
144 void AppearanceSettingsPage::chooseStyleSheet() {
145   QString dir = ui.customStyleSheetPath->property("storedValue").toString();
146   if(!dir.isEmpty() && QFile(dir).exists())
147     dir = QDir(dir).absolutePath();
148   else
149     dir = QDir(Quassel::findDataFilePath("default.qss")).absolutePath();
150
151   QString name = QFileDialog::getOpenFileName(this, tr("Please choose a stylesheet file"), dir, "*.qss");
152   if(!name.isEmpty())
153     ui.customStyleSheetPath->setText(name);
154 }
155
156 void AppearanceSettingsPage::widgetHasChanged() {
157   setChangedState(testHasChanged());
158 }
159
160 bool AppearanceSettingsPage::testHasChanged() {
161   if(ui.styleComboBox->currentIndex() != ui.styleComboBox->property("storedValue").toInt()) return true;
162
163   if(selectedLocale() != QLocale()) return true; // QLocale() returns the default locale (manipulated via loadTranslation())
164
165   return false;
166 }