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