Make Breeze the default icon theme
[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     // TODO Replace by runtime detection
105 #if defined WITH_OXYGEN || defined WITH_BREEZE || defined WITH_BREEZE_DARK
106 # if defined WITH_BREEZE
107     ui.iconthemeComboBox->addItem(tr("Breeze Light"), QVariant("breeze"));
108 # endif
109 # if defined WITH_BREEZE_DARK
110     ui.iconthemeComboBox->addItem(tr("Breeze Dark"), QVariant("breezedark"));
111 # endif
112 # if defined WITH_OXYGEN
113     ui.iconthemeComboBox->addItem(tr("Oxygen"), QVariant("oxygen"));
114 # endif
115 #else
116     ui.iconthemeLabel->hide();
117     ui.iconthemeComboBox->hide();
118 #endif
119 }
120
121
122 void AppearanceSettingsPage::defaults()
123 {
124     ui.styleComboBox->setCurrentIndex(0);
125     ui.languageComboBox->setCurrentIndex(1);
126
127     SettingsPage::defaults();
128     widgetHasChanged();
129 }
130
131
132 void AppearanceSettingsPage::load()
133 {
134     QtUiSettings uiSettings;
135
136     // Gui Style
137     QString style = uiSettings.value("Style", QString("")).toString();
138     if (style.isEmpty()) {
139         ui.styleComboBox->setCurrentIndex(0);
140     }
141     else {
142         ui.styleComboBox->setCurrentIndex(ui.styleComboBox->findText(style, Qt::MatchExactly));
143     }
144     ui.styleComboBox->setProperty("storedValue", ui.styleComboBox->currentIndex());
145
146     // Language
147     QLocale locale = uiSettings.value("Locale", QLocale::system()).value<QLocale>();
148     if (locale == QLocale::system())
149         ui.languageComboBox->setCurrentIndex(1);
150     else if (locale.language() == QLocale::C) // we use C for "untranslated"
151         ui.languageComboBox->setCurrentIndex(0);
152     else
153         ui.languageComboBox->setCurrentIndex(ui.languageComboBox->findText(QLocale::languageToString(locale.language()), Qt::MatchExactly));
154     ui.languageComboBox->setProperty("storedValue", ui.languageComboBox->currentIndex());
155     Quassel::loadTranslation(selectedLocale());
156
157     // IconTheme
158     QString icontheme = uiSettings.value("IconTheme", QVariant("")).toString();
159     if (icontheme == "")
160         ui.iconthemeComboBox->setCurrentIndex(0);
161     else
162         ui.iconthemeComboBox->setCurrentIndex(ui.iconthemeComboBox->findData(icontheme));
163     ui.iconthemeComboBox->setProperty("storedValue", ui.iconthemeComboBox->currentIndex());
164
165     // bufferSettings:
166     BufferSettings bufferSettings;
167     int redirectTarget = bufferSettings.userNoticesTarget();
168     SettingsPage::load(ui.userNoticesInDefaultBuffer, redirectTarget & BufferSettings::DefaultBuffer);
169     SettingsPage::load(ui.userNoticesInStatusBuffer, redirectTarget & BufferSettings::StatusBuffer);
170     SettingsPage::load(ui.userNoticesInCurrentBuffer, redirectTarget & BufferSettings::CurrentBuffer);
171
172     redirectTarget = bufferSettings.serverNoticesTarget();
173     SettingsPage::load(ui.serverNoticesInDefaultBuffer, redirectTarget & BufferSettings::DefaultBuffer);
174     SettingsPage::load(ui.serverNoticesInStatusBuffer, redirectTarget & BufferSettings::StatusBuffer);
175     SettingsPage::load(ui.serverNoticesInCurrentBuffer, redirectTarget & BufferSettings::CurrentBuffer);
176
177     redirectTarget = bufferSettings.errorMsgsTarget();
178     SettingsPage::load(ui.errorMsgsInDefaultBuffer, redirectTarget & BufferSettings::DefaultBuffer);
179     SettingsPage::load(ui.errorMsgsInStatusBuffer, redirectTarget & BufferSettings::StatusBuffer);
180     SettingsPage::load(ui.errorMsgsInCurrentBuffer, redirectTarget & BufferSettings::CurrentBuffer);
181
182     SettingsPage::load();
183     setChangedState(false);
184 }
185
186
187 void AppearanceSettingsPage::save()
188 {
189     QtUiSettings uiSettings;
190
191     if (ui.styleComboBox->currentIndex() < 1) {
192         uiSettings.setValue("Style", QString(""));
193     }
194     else {
195         uiSettings.setValue("Style", ui.styleComboBox->currentText());
196         QApplication::setStyle(ui.styleComboBox->currentText());
197     }
198     ui.styleComboBox->setProperty("storedValue", ui.styleComboBox->currentIndex());
199
200     if (ui.languageComboBox->currentIndex() == 1) {
201         uiSettings.remove("Locale"); // force the default (QLocale::system())
202     }
203     else {
204         uiSettings.setValue("Locale", selectedLocale());
205     }
206     ui.languageComboBox->setProperty("storedValue", ui.languageComboBox->currentIndex());
207
208     if (selectedIconTheme()=="") {
209         uiSettings.remove("IconTheme");
210     }
211     else {
212         uiSettings.setValue("IconTheme", selectedIconTheme());
213         QIcon::setThemeName(selectedIconTheme());
214     }
215     ui.iconthemeComboBox->setProperty("storedValue", ui.iconthemeComboBox->currentIndex());
216
217     bool needsStyleReload =
218         ui.useCustomStyleSheet->isChecked() != ui.useCustomStyleSheet->property("storedValue").toBool()
219         || (ui.useCustomStyleSheet->isChecked() && ui.customStyleSheetPath->text() != ui.customStyleSheetPath->property("storedValue").toString());
220
221     BufferSettings bufferSettings;
222     int redirectTarget = 0;
223     if (ui.userNoticesInDefaultBuffer->isChecked())
224         redirectTarget |= BufferSettings::DefaultBuffer;
225     if (ui.userNoticesInStatusBuffer->isChecked())
226         redirectTarget |= BufferSettings::StatusBuffer;
227     if (ui.userNoticesInCurrentBuffer->isChecked())
228         redirectTarget |= BufferSettings::CurrentBuffer;
229     bufferSettings.setUserNoticesTarget(redirectTarget);
230
231     redirectTarget = 0;
232     if (ui.serverNoticesInDefaultBuffer->isChecked())
233         redirectTarget |= BufferSettings::DefaultBuffer;
234     if (ui.serverNoticesInStatusBuffer->isChecked())
235         redirectTarget |= BufferSettings::StatusBuffer;
236     if (ui.serverNoticesInCurrentBuffer->isChecked())
237         redirectTarget |= BufferSettings::CurrentBuffer;
238     bufferSettings.setServerNoticesTarget(redirectTarget);
239
240     redirectTarget = 0;
241     if (ui.errorMsgsInDefaultBuffer->isChecked())
242         redirectTarget |= BufferSettings::DefaultBuffer;
243     if (ui.errorMsgsInStatusBuffer->isChecked())
244         redirectTarget |= BufferSettings::StatusBuffer;
245     if (ui.errorMsgsInCurrentBuffer->isChecked())
246         redirectTarget |= BufferSettings::CurrentBuffer;
247     bufferSettings.setErrorMsgsTarget(redirectTarget);
248
249     SettingsPage::save();
250     setChangedState(false);
251     if (needsStyleReload)
252         QtUi::style()->reload();
253 }
254
255
256 QLocale AppearanceSettingsPage::selectedLocale() const
257 {
258     QLocale locale;
259     int index = ui.languageComboBox->currentIndex();
260     if (index == 1)
261         locale = QLocale::system();
262     else if (index == 0)
263         locale = QLocale::c();
264     else if (index > 1)
265         locale = _locales.values()[index - 2];
266
267     return locale;
268 }
269
270 QString AppearanceSettingsPage::selectedIconTheme() const
271 {
272     return ui.iconthemeComboBox->itemData(ui.iconthemeComboBox->currentIndex()).toString();
273 }
274
275 void AppearanceSettingsPage::chooseStyleSheet()
276 {
277     QString dir = ui.customStyleSheetPath->property("storedValue").toString();
278     if (!dir.isEmpty() && QFile(dir).exists())
279         dir = QDir(dir).absolutePath();
280     else
281         dir = QDir(Quassel::findDataFilePath("default.qss")).absolutePath();
282
283     QString name = QFileDialog::getOpenFileName(this, tr("Please choose a stylesheet file"), dir, "*.qss");
284     if (!name.isEmpty())
285         ui.customStyleSheetPath->setText(name);
286 }
287
288
289 void AppearanceSettingsPage::widgetHasChanged()
290 {
291     setChangedState(testHasChanged());
292 }
293
294
295 bool AppearanceSettingsPage::testHasChanged()
296 {
297     if (ui.styleComboBox->currentIndex() != ui.styleComboBox->property("storedValue").toInt()) return true;
298     if (ui.languageComboBox->currentIndex() != ui.languageComboBox->property("storedValue").toInt()) return true;
299     if (ui.iconthemeComboBox->currentIndex() != ui.iconthemeComboBox->property("storedValue").toInt()) return true;
300
301     if (SettingsPage::hasChanged(ui.userNoticesInStatusBuffer)) return true;
302     if (SettingsPage::hasChanged(ui.userNoticesInDefaultBuffer)) return true;
303     if (SettingsPage::hasChanged(ui.userNoticesInCurrentBuffer)) return true;
304
305     if (SettingsPage::hasChanged(ui.serverNoticesInStatusBuffer)) return true;
306     if (SettingsPage::hasChanged(ui.serverNoticesInDefaultBuffer)) return true;
307     if (SettingsPage::hasChanged(ui.serverNoticesInCurrentBuffer)) return true;
308
309     if (SettingsPage::hasChanged(ui.errorMsgsInStatusBuffer)) return true;
310     if (SettingsPage::hasChanged(ui.errorMsgsInDefaultBuffer)) return true;
311     if (SettingsPage::hasChanged(ui.errorMsgsInCurrentBuffer)) return true;
312
313     return false;
314 }