4fcaa0d84a5a9edbe8e91046afdef5ef653b745d
[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 "qtui.h"
24 #include "uisettings.h"
25 #include "util.h"
26
27 #include <QDir>
28 #include <QStyleFactory>
29
30 AppearanceSettingsPage::AppearanceSettingsPage(QWidget *parent)
31   : SettingsPage(tr("Appearance"), tr("General"), parent) {
32   ui.setupUi(this);
33   initStyleComboBox();
34   initLanguageComboBox();
35
36   connect(ui.styleComboBox, SIGNAL(currentIndexChanged(QString)), this, SLOT(widgetHasChanged())); 
37   connect(ui.languageComboBox, SIGNAL(currentIndexChanged(QString)), this, SLOT(widgetHasChanged())); 
38 }
39
40 void AppearanceSettingsPage::initStyleComboBox() {
41   QStringList styleList = QStyleFactory::keys();
42   ui.styleComboBox->addItem("<default>");
43   foreach(QString style, styleList) {
44     ui.styleComboBox->addItem(style);
45   }
46 }
47
48 void AppearanceSettingsPage::initLanguageComboBox() {
49   QDir i18nDir(":/i18n", "quassel_*.qm");
50
51   foreach(QString translationFile, i18nDir.entryList()) {
52     QString localeName(translationFile.mid(8));
53     localeName.chop(3);
54     QLocale locale(localeName);
55     _locales << locale;
56     ui.languageComboBox->addItem(QLocale::languageToString(locale.language()));
57   }
58
59 }
60
61 void AppearanceSettingsPage::defaults() {
62   ui.styleComboBox->setCurrentIndex(0);
63
64   widgetHasChanged();
65 }
66
67 void AppearanceSettingsPage::load() {
68   UiSettings uiSettings;
69
70   settings["Style"] = uiSettings.value("Style", QString(""));
71   if(settings["Style"].toString() == "") {
72     ui.styleComboBox->setCurrentIndex(0);
73   } else {
74     ui.styleComboBox->setCurrentIndex(ui.styleComboBox->findText(settings["Style"].toString(), Qt::MatchExactly));
75     QApplication::setStyle(settings["Style"].toString());
76   }
77
78   QLocale locale = uiSettings.value("Locale", QLocale::system()).value<QLocale>();
79   if(locale == QLocale::system())
80     ui.languageComboBox->setCurrentIndex(0);
81   else if(locale.language() == QLocale::C)
82     ui.languageComboBox->setCurrentIndex(1);
83   else
84     ui.languageComboBox->setCurrentIndex(ui.languageComboBox->findText(QLocale::languageToString(locale.language()), Qt::MatchExactly));
85   loadTranslation(selectedLocale());
86
87   setChangedState(false);
88 }
89
90 void AppearanceSettingsPage::save() {
91   UiSettings uiSettings;
92
93   if(ui.styleComboBox->currentIndex() < 1) {
94     uiSettings.setValue("Style", QString(""));
95   } else {
96     uiSettings.setValue("Style", ui.styleComboBox->currentText());
97   }
98
99   if(ui.languageComboBox->currentIndex() == 0) {
100     uiSettings.remove("Locale"); // force the default (QLocale::system())
101   } else {
102     uiSettings.setValue("Locale", selectedLocale());
103   }
104   
105   load();
106   setChangedState(false);
107 }
108
109 QLocale AppearanceSettingsPage::selectedLocale() const {
110   QLocale locale;
111   int index = ui.languageComboBox->currentIndex();
112   if(index == 0)
113     locale = QLocale::system();
114   else if(index == 1)
115     locale = QLocale::c();
116   else if(index > 1)
117     locale = _locales[index - 2];
118
119   return locale;
120 }
121
122 void AppearanceSettingsPage::widgetHasChanged() {
123   setChangedState(testHasChanged());
124 }
125
126 bool AppearanceSettingsPage::testHasChanged() {
127   if(settings["Style"].toString() != ui.styleComboBox->currentText()) return true;
128   if(selectedLocale() != QLocale()) return true; // QLocale() returns the default locale (manipulated via loadTranslation())
129
130   return false;
131 }
132
133
134
135