6891c7441ecc96e99074668e2a485d64f5190b3d
[quassel.git] / src / qtui / settingspages / appearancesettingspage.cpp
1 /***************************************************************************
2  *   Copyright (C) 2005-2016 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) version 3.                                           *
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  *   51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, 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_OS_MAC
40     ui.minimizeOnClose->hide();
41 #endif
42 #ifdef QT_NO_SYSTEMTRAYICON
43     ui.useSystemTrayIcon->hide();
44 #endif
45
46     initAutoWidgets();
47     initStyleComboBox();
48     initLanguageComboBox();
49     initIconThemeComboBox();
50
51     foreach(QComboBox *comboBox, findChildren<QComboBox *>()) {
52         connect(comboBox, SIGNAL(currentIndexChanged(QString)), this, SLOT(widgetHasChanged()));
53     }
54     foreach(QCheckBox *checkBox, findChildren<QCheckBox *>()) {
55         connect(checkBox, SIGNAL(clicked()), this, SLOT(widgetHasChanged()));
56     }
57
58     connect(ui.chooseStyleSheet, SIGNAL(clicked()), SLOT(chooseStyleSheet()));
59
60     connect(ui.userNoticesInDefaultBuffer, SIGNAL(clicked(bool)), this, SLOT(widgetHasChanged()));
61     connect(ui.userNoticesInStatusBuffer, SIGNAL(clicked(bool)), this, SLOT(widgetHasChanged()));
62     connect(ui.userNoticesInCurrentBuffer, SIGNAL(clicked(bool)), this, SLOT(widgetHasChanged()));
63
64     connect(ui.serverNoticesInDefaultBuffer, SIGNAL(clicked(bool)), this, SLOT(widgetHasChanged()));
65     connect(ui.serverNoticesInStatusBuffer, SIGNAL(clicked(bool)), this, SLOT(widgetHasChanged()));
66     connect(ui.serverNoticesInCurrentBuffer, SIGNAL(clicked(bool)), this, SLOT(widgetHasChanged()));
67
68     connect(ui.errorMsgsInDefaultBuffer, SIGNAL(clicked(bool)), this, SLOT(widgetHasChanged()));
69     connect(ui.errorMsgsInStatusBuffer, SIGNAL(clicked(bool)), this, SLOT(widgetHasChanged()));
70     connect(ui.errorMsgsInCurrentBuffer, SIGNAL(clicked(bool)), this, SLOT(widgetHasChanged()));
71 }
72
73
74 void AppearanceSettingsPage::initStyleComboBox()
75 {
76     QStringList styleList = QStyleFactory::keys();
77     ui.styleComboBox->addItem(tr("<System Default>"));
78     foreach(QString style, styleList) {
79         ui.styleComboBox->addItem(style);
80     }
81 }
82
83
84 void AppearanceSettingsPage::initLanguageComboBox()
85 {
86     QDir i18nDir(Quassel::translationDirPath(), "*.qm");
87
88     QRegExp rx("(qt_)?([a-zA-Z_]+)\\.qm");
89     foreach(QString translationFile, i18nDir.entryList()) {
90         if (!rx.exactMatch(translationFile))
91             continue;
92         if (!rx.cap(1).isEmpty())
93             continue;
94         QLocale locale(rx.cap(2));
95         _locales[QLocale::languageToString(locale.language())] = locale;
96     }
97     foreach(QString language, _locales.keys()) {
98         ui.languageComboBox->addItem(language);
99     }
100 }
101
102 void AppearanceSettingsPage::initIconThemeComboBox()
103 {
104 #if defined WITH_OXYGEN || defined WITH_BREEZE || defined WITH_BREEZE_DARK
105 # if defined WITH_OXYGEN
106     ui.iconthemeComboBox->addItem(tr("Oxygen"), QVariant("oxygen"));
107 # endif
108 # if defined WITH_BREEZE
109     ui.iconthemeComboBox->addItem(tr("Breeze Light"), QVariant("breeze"));
110 # endif
111 # if defined WITH_BREEZE_DARK
112     ui.iconthemeComboBox->addItem(tr("Breeze Dark"), QVariant("breezedark"));
113 # endif
114 #else
115     ui.iconthemeComboBox->hide();
116 #endif
117 }
118
119
120 void AppearanceSettingsPage::defaults()
121 {
122     ui.styleComboBox->setCurrentIndex(0);
123     ui.languageComboBox->setCurrentIndex(1);
124
125     SettingsPage::defaults();
126     widgetHasChanged();
127 }
128
129
130 void AppearanceSettingsPage::load()
131 {
132     QtUiSettings uiSettings;
133
134     // Gui Style
135     QString style = uiSettings.value("Style", QString("")).toString();
136     if (style.isEmpty()) {
137         ui.styleComboBox->setCurrentIndex(0);
138     }
139     else {
140         ui.styleComboBox->setCurrentIndex(ui.styleComboBox->findText(style, Qt::MatchExactly));
141     }
142     ui.styleComboBox->setProperty("storedValue", ui.styleComboBox->currentIndex());
143
144     // Language
145     QLocale locale = uiSettings.value("Locale", QLocale::system()).value<QLocale>();
146     if (locale == QLocale::system())
147         ui.languageComboBox->setCurrentIndex(1);
148     else if (locale.language() == QLocale::C) // we use C for "untranslated"
149         ui.languageComboBox->setCurrentIndex(0);
150     else
151         ui.languageComboBox->setCurrentIndex(ui.languageComboBox->findText(QLocale::languageToString(locale.language()), Qt::MatchExactly));
152     ui.languageComboBox->setProperty("storedValue", ui.languageComboBox->currentIndex());
153     Quassel::loadTranslation(selectedLocale());
154
155     // IconTheme
156     QString icontheme = uiSettings.value("IconTheme", QVariant("")).toString();
157     if (icontheme == "")
158         ui.iconthemeComboBox->setCurrentIndex(0);
159     else
160         ui.iconthemeComboBox->setCurrentIndex(ui.iconthemeComboBox->findData(icontheme));
161     ui.iconthemeComboBox->setProperty("storedValue", ui.iconthemeComboBox->currentIndex());
162
163     // bufferSettings:
164     BufferSettings bufferSettings;
165     int redirectTarget = bufferSettings.userNoticesTarget();
166     SettingsPage::load(ui.userNoticesInDefaultBuffer, redirectTarget & BufferSettings::DefaultBuffer);
167     SettingsPage::load(ui.userNoticesInStatusBuffer, redirectTarget & BufferSettings::StatusBuffer);
168     SettingsPage::load(ui.userNoticesInCurrentBuffer, redirectTarget & BufferSettings::CurrentBuffer);
169
170     redirectTarget = bufferSettings.serverNoticesTarget();
171     SettingsPage::load(ui.serverNoticesInDefaultBuffer, redirectTarget & BufferSettings::DefaultBuffer);
172     SettingsPage::load(ui.serverNoticesInStatusBuffer, redirectTarget & BufferSettings::StatusBuffer);
173     SettingsPage::load(ui.serverNoticesInCurrentBuffer, redirectTarget & BufferSettings::CurrentBuffer);
174
175     redirectTarget = bufferSettings.errorMsgsTarget();
176     SettingsPage::load(ui.errorMsgsInDefaultBuffer, redirectTarget & BufferSettings::DefaultBuffer);
177     SettingsPage::load(ui.errorMsgsInStatusBuffer, redirectTarget & BufferSettings::StatusBuffer);
178     SettingsPage::load(ui.errorMsgsInCurrentBuffer, redirectTarget & BufferSettings::CurrentBuffer);
179
180     SettingsPage::load();
181     setChangedState(false);
182 }
183
184
185 void AppearanceSettingsPage::save()
186 {
187     QtUiSettings uiSettings;
188
189     if (ui.styleComboBox->currentIndex() < 1) {
190         uiSettings.setValue("Style", QString(""));
191     }
192     else {
193         uiSettings.setValue("Style", ui.styleComboBox->currentText());
194         QApplication::setStyle(ui.styleComboBox->currentText());
195     }
196     ui.styleComboBox->setProperty("storedValue", ui.styleComboBox->currentIndex());
197
198     if (ui.languageComboBox->currentIndex() == 1) {
199         uiSettings.remove("Locale"); // force the default (QLocale::system())
200     }
201     else {
202         uiSettings.setValue("Locale", selectedLocale());
203     }
204     ui.languageComboBox->setProperty("storedValue", ui.languageComboBox->currentIndex());
205
206     if (selectedIconTheme()=="") {
207         uiSettings.remove("IconTheme");
208     }
209     else {
210         uiSettings.setValue("IconTheme", selectedIconTheme());
211         QIcon::setThemeName(selectedIconTheme());
212     }
213     ui.iconthemeComboBox->setProperty("storedValue", ui.iconthemeComboBox->currentIndex());
214
215     bool needsStyleReload =
216         ui.useCustomStyleSheet->isChecked() != ui.useCustomStyleSheet->property("storedValue").toBool()
217         || (ui.useCustomStyleSheet->isChecked() && ui.customStyleSheetPath->text() != ui.customStyleSheetPath->property("storedValue").toString());
218
219     BufferSettings bufferSettings;
220     int redirectTarget = 0;
221     if (ui.userNoticesInDefaultBuffer->isChecked())
222         redirectTarget |= BufferSettings::DefaultBuffer;
223     if (ui.userNoticesInStatusBuffer->isChecked())
224         redirectTarget |= BufferSettings::StatusBuffer;
225     if (ui.userNoticesInCurrentBuffer->isChecked())
226         redirectTarget |= BufferSettings::CurrentBuffer;
227     bufferSettings.setUserNoticesTarget(redirectTarget);
228
229     redirectTarget = 0;
230     if (ui.serverNoticesInDefaultBuffer->isChecked())
231         redirectTarget |= BufferSettings::DefaultBuffer;
232     if (ui.serverNoticesInStatusBuffer->isChecked())
233         redirectTarget |= BufferSettings::StatusBuffer;
234     if (ui.serverNoticesInCurrentBuffer->isChecked())
235         redirectTarget |= BufferSettings::CurrentBuffer;
236     bufferSettings.setServerNoticesTarget(redirectTarget);
237
238     redirectTarget = 0;
239     if (ui.errorMsgsInDefaultBuffer->isChecked())
240         redirectTarget |= BufferSettings::DefaultBuffer;
241     if (ui.errorMsgsInStatusBuffer->isChecked())
242         redirectTarget |= BufferSettings::StatusBuffer;
243     if (ui.errorMsgsInCurrentBuffer->isChecked())
244         redirectTarget |= BufferSettings::CurrentBuffer;
245     bufferSettings.setErrorMsgsTarget(redirectTarget);
246
247     SettingsPage::save();
248     setChangedState(false);
249     if (needsStyleReload)
250         QtUi::style()->reload();
251 }
252
253
254 QLocale AppearanceSettingsPage::selectedLocale() const
255 {
256     QLocale locale;
257     int index = ui.languageComboBox->currentIndex();
258     if (index == 1)
259         locale = QLocale::system();
260     else if (index == 0)
261         locale = QLocale::c();
262     else if (index > 1)
263         locale = _locales.values()[index - 2];
264
265     return locale;
266 }
267
268 QString AppearanceSettingsPage::selectedIconTheme() const
269 {
270     return ui.iconthemeComboBox->itemData(ui.iconthemeComboBox->currentIndex()).toString();
271 }
272
273 void AppearanceSettingsPage::chooseStyleSheet()
274 {
275     QString dir = ui.customStyleSheetPath->property("storedValue").toString();
276     if (!dir.isEmpty() && QFile(dir).exists())
277         dir = QDir(dir).absolutePath();
278     else
279         dir = QDir(Quassel::findDataFilePath("default.qss")).absolutePath();
280
281     QString name = QFileDialog::getOpenFileName(this, tr("Please choose a stylesheet file"), dir, "*.qss");
282     if (!name.isEmpty())
283         ui.customStyleSheetPath->setText(name);
284 }
285
286
287 void AppearanceSettingsPage::widgetHasChanged()
288 {
289     setChangedState(testHasChanged());
290 }
291
292
293 bool AppearanceSettingsPage::testHasChanged()
294 {
295     if (ui.styleComboBox->currentIndex() != ui.styleComboBox->property("storedValue").toInt()) return true;
296     if (ui.languageComboBox->currentIndex() != ui.languageComboBox->property("storedValue").toInt()) return true;
297     if (ui.iconthemeComboBox->currentIndex() != ui.iconthemeComboBox->property("storedValue").toInt()) return true;
298
299     if (SettingsPage::hasChanged(ui.userNoticesInStatusBuffer)) return true;
300     if (SettingsPage::hasChanged(ui.userNoticesInDefaultBuffer)) return true;
301     if (SettingsPage::hasChanged(ui.userNoticesInCurrentBuffer)) return true;
302
303     if (SettingsPage::hasChanged(ui.serverNoticesInStatusBuffer)) return true;
304     if (SettingsPage::hasChanged(ui.serverNoticesInDefaultBuffer)) return true;
305     if (SettingsPage::hasChanged(ui.serverNoticesInCurrentBuffer)) return true;
306
307     if (SettingsPage::hasChanged(ui.errorMsgsInStatusBuffer)) return true;
308     if (SettingsPage::hasChanged(ui.errorMsgsInDefaultBuffer)) return true;
309     if (SettingsPage::hasChanged(ui.errorMsgsInCurrentBuffer)) return true;
310
311     return false;
312 }