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