]>
Commit | Line | Data |
---|---|---|
1 | /* | |
2 | * This file is part of the PulseView project. | |
3 | * | |
4 | * Copyright (C) 2017 Soeren Apel <soeren@apelpie.net> | |
5 | * | |
6 | * This program is free software; you can redistribute it and/or modify | |
7 | * it under the terms of the GNU General Public License as published by | |
8 | * the Free Software Foundation; either version 2 of the License, or | |
9 | * (at your option) any later version. | |
10 | * | |
11 | * This program is distributed in the hope that it will be useful, | |
12 | * but WITHOUT ANY WARRANTY; without even the implied warranty of | |
13 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
14 | * GNU General Public License for more details. | |
15 | * | |
16 | * You should have received a copy of the GNU General Public License | |
17 | * along with this program; if not, see <http://www.gnu.org/licenses/>. | |
18 | */ | |
19 | ||
20 | #include "globalsettings.hpp" | |
21 | #include "application.hpp" | |
22 | ||
23 | #include <QApplication> | |
24 | #include <QColor> | |
25 | #include <QDebug> | |
26 | #include <QFile> | |
27 | #include <QFontMetrics> | |
28 | #include <QPixmapCache> | |
29 | #include <QString> | |
30 | #include <QStyle> | |
31 | #include <QtGlobal> | |
32 | ||
33 | using std::map; | |
34 | using std::pair; | |
35 | using std::string; | |
36 | using std::vector; | |
37 | ||
38 | namespace pv { | |
39 | ||
40 | const vector< pair<QString, QString> > Themes { | |
41 | {"None" , ""}, | |
42 | {"QDarkStyleSheet", ":/themes/qdarkstyle/style.qss"}, | |
43 | {"DarkStyle", ":/themes/darkstyle/darkstyle.qss"} | |
44 | }; | |
45 | ||
46 | const QString GlobalSettings::Key_General_Language = "General_Language"; | |
47 | const QString GlobalSettings::Key_General_Theme = "General_Theme"; | |
48 | const QString GlobalSettings::Key_General_Style = "General_Style"; | |
49 | const QString GlobalSettings::Key_General_SaveWithSetup = "General_SaveWithSetup"; | |
50 | const QString GlobalSettings::Key_View_ZoomToFitDuringAcq = "View_ZoomToFitDuringAcq"; | |
51 | const QString GlobalSettings::Key_View_ZoomToFitAfterAcq = "View_ZoomToFitAfterAcq"; | |
52 | const QString GlobalSettings::Key_View_TriggerIsZeroTime = "View_TriggerIsZeroTime"; | |
53 | const QString GlobalSettings::Key_View_ColoredBG = "View_ColoredBG"; | |
54 | const QString GlobalSettings::Key_View_StickyScrolling = "View_StickyScrolling"; | |
55 | const QString GlobalSettings::Key_View_ShowSamplingPoints = "View_ShowSamplingPoints"; | |
56 | const QString GlobalSettings::Key_View_FillSignalHighAreas = "View_FillSignalHighAreas"; | |
57 | const QString GlobalSettings::Key_View_FillSignalHighAreaColor = "View_FillSignalHighAreaColor"; | |
58 | const QString GlobalSettings::Key_View_ShowAnalogMinorGrid = "View_ShowAnalogMinorGrid"; | |
59 | const QString GlobalSettings::Key_View_ConversionThresholdDispMode = "View_ConversionThresholdDispMode"; | |
60 | const QString GlobalSettings::Key_View_DefaultDivHeight = "View_DefaultDivHeight"; | |
61 | const QString GlobalSettings::Key_View_DefaultLogicHeight = "View_DefaultLogicHeight"; | |
62 | const QString GlobalSettings::Key_View_ShowHoverMarker = "View_ShowHoverMarker"; | |
63 | const QString GlobalSettings::Key_View_SnapDistance = "View_SnapDistance"; | |
64 | const QString GlobalSettings::Key_View_CursorFillColor = "View_CursorFillColor"; | |
65 | const QString GlobalSettings::Key_View_CursorShowFrequency = "View_CursorShowFrequency"; | |
66 | const QString GlobalSettings::Key_View_CursorShowInterval = "View_CursorShowInterval"; | |
67 | const QString GlobalSettings::Key_Dec_InitialStateConfigurable = "Dec_InitialStateConfigurable"; | |
68 | const QString GlobalSettings::Key_Dec_ExportFormat = "Dec_ExportFormat"; | |
69 | const QString GlobalSettings::Key_Dec_AlwaysShowAllRows = "Dec_AlwaysShowAllRows"; | |
70 | const QString GlobalSettings::Key_Log_BufferSize = "Log_BufferSize"; | |
71 | const QString GlobalSettings::Key_Log_NotifyOfStacktrace = "Log_NotifyOfStacktrace"; | |
72 | ||
73 | vector<GlobalSettingsInterface*> GlobalSettings::callbacks_; | |
74 | bool GlobalSettings::tracking_ = false; | |
75 | bool GlobalSettings::is_dark_theme_ = false; | |
76 | map<QString, QVariant> GlobalSettings::tracked_changes_; | |
77 | QString GlobalSettings::default_style_; | |
78 | QPalette GlobalSettings::default_palette_; | |
79 | ||
80 | GlobalSettings::GlobalSettings() : | |
81 | QSettings() | |
82 | { | |
83 | beginGroup("Settings"); | |
84 | } | |
85 | ||
86 | void GlobalSettings::save_internal_defaults() | |
87 | { | |
88 | default_style_ = qApp->style()->objectName(); | |
89 | if (default_style_.isEmpty()) | |
90 | default_style_ = "fusion"; | |
91 | ||
92 | default_palette_ = QApplication::palette(); | |
93 | } | |
94 | ||
95 | void GlobalSettings::set_defaults_where_needed() | |
96 | { | |
97 | if (!contains(Key_General_Language)) { | |
98 | // Determine and set default UI language | |
99 | QString language = QLocale().uiLanguages().first(); // May return e.g. en-Latn-US | |
100 | language = language.split("-").first(); | |
101 | ||
102 | setValue(Key_General_Language, language); | |
103 | } | |
104 | ||
105 | // Use no theme by default | |
106 | if (!contains(Key_General_Theme)) | |
107 | setValue(Key_General_Theme, 0); | |
108 | if (!contains(Key_General_Style)) | |
109 | setValue(Key_General_Style, ""); | |
110 | ||
111 | // Save setup with .sr files by default | |
112 | if (!contains(Key_General_SaveWithSetup)) | |
113 | setValue(Key_General_SaveWithSetup, true); | |
114 | ||
115 | // Enable zoom-to-fit after acquisition by default | |
116 | if (!contains(Key_View_ZoomToFitAfterAcq)) | |
117 | setValue(Key_View_ZoomToFitAfterAcq, true); | |
118 | ||
119 | // Enable colored trace backgrounds by default | |
120 | if (!contains(Key_View_ColoredBG)) | |
121 | setValue(Key_View_ColoredBG, true); | |
122 | ||
123 | // Enable showing sampling points by default | |
124 | if (!contains(Key_View_ShowSamplingPoints)) | |
125 | setValue(Key_View_ShowSamplingPoints, true); | |
126 | ||
127 | // Enable filling logic signal high areas by default | |
128 | if (!contains(Key_View_FillSignalHighAreas)) | |
129 | setValue(Key_View_FillSignalHighAreas, true); | |
130 | ||
131 | if (!contains(Key_View_DefaultDivHeight)) | |
132 | setValue(Key_View_DefaultDivHeight, | |
133 | 3 * QFontMetrics(QApplication::font()).height()); | |
134 | ||
135 | if (!contains(Key_View_DefaultLogicHeight)) | |
136 | setValue(Key_View_DefaultLogicHeight, | |
137 | 2 * QFontMetrics(QApplication::font()).height()); | |
138 | ||
139 | if (!contains(Key_View_ShowHoverMarker)) | |
140 | setValue(Key_View_ShowHoverMarker, true); | |
141 | ||
142 | if (!contains(Key_View_SnapDistance)) | |
143 | setValue(Key_View_SnapDistance, 15); | |
144 | ||
145 | if (!contains(Key_View_CursorShowInterval)) | |
146 | setValue(Key_View_CursorShowInterval, true); | |
147 | ||
148 | if (!contains(Key_View_CursorShowFrequency)) | |
149 | setValue(Key_View_CursorShowFrequency, true); | |
150 | ||
151 | // %c was used for the row name in the past so we need to transition such users | |
152 | if (!contains(Key_Dec_ExportFormat) || | |
153 | value(Key_Dec_ExportFormat).toString() == "%s %d: %c: %1") | |
154 | setValue(Key_Dec_ExportFormat, "%s %d: %r: %1"); | |
155 | ||
156 | // Default to 500 lines of backlog | |
157 | if (!contains(Key_Log_BufferSize)) | |
158 | setValue(Key_Log_BufferSize, 500); | |
159 | ||
160 | // Notify user of existing stack trace by default | |
161 | if (!contains(Key_Log_NotifyOfStacktrace)) | |
162 | setValue(Key_Log_NotifyOfStacktrace, true); | |
163 | ||
164 | // Default theme is bright, so use its color scheme if undefined | |
165 | if (!contains(Key_View_CursorFillColor)) | |
166 | set_bright_theme_default_colors(); | |
167 | } | |
168 | ||
169 | void GlobalSettings::set_bright_theme_default_colors() | |
170 | { | |
171 | setValue(Key_View_FillSignalHighAreaColor, | |
172 | QColor(0, 0, 0, 5 * 256 / 100).rgba()); | |
173 | ||
174 | setValue(Key_View_CursorFillColor, | |
175 | QColor(220, 231, 243).rgba()); | |
176 | } | |
177 | ||
178 | void GlobalSettings::set_dark_theme_default_colors() | |
179 | { | |
180 | setValue(Key_View_FillSignalHighAreaColor, | |
181 | QColor(188, 188, 188, 9 * 256 / 100).rgba()); | |
182 | ||
183 | setValue(Key_View_CursorFillColor, | |
184 | QColor(60, 60, 60).rgba()); | |
185 | } | |
186 | ||
187 | bool GlobalSettings::current_theme_is_dark() | |
188 | { | |
189 | return is_dark_theme_; | |
190 | } | |
191 | ||
192 | void GlobalSettings::apply_theme() | |
193 | { | |
194 | QString theme_name = Themes.at(value(Key_General_Theme).toInt()).first; | |
195 | QString resource_name = Themes.at(value(Key_General_Theme).toInt()).second; | |
196 | ||
197 | if (!resource_name.isEmpty()) { | |
198 | QFile file(resource_name); | |
199 | file.open(QFile::ReadOnly | QFile::Text); | |
200 | qApp->setStyleSheet(file.readAll()); | |
201 | } else | |
202 | qApp->setStyleSheet(""); | |
203 | ||
204 | qApp->setPalette(default_palette_); | |
205 | ||
206 | const QString style = value(Key_General_Style).toString(); | |
207 | if (style.isEmpty()) | |
208 | qApp->setStyle(default_style_); | |
209 | else | |
210 | qApp->setStyle(style); | |
211 | ||
212 | is_dark_theme_ = false; | |
213 | ||
214 | if (theme_name.compare("QDarkStyleSheet") == 0) { | |
215 | QPalette dark_palette; | |
216 | dark_palette.setColor(QPalette::Window, QColor(53, 53, 53)); | |
217 | dark_palette.setColor(QPalette::WindowText, Qt::white); | |
218 | dark_palette.setColor(QPalette::Base, QColor(42, 42, 42)); | |
219 | dark_palette.setColor(QPalette::Dark, QColor(35, 35, 35)); | |
220 | dark_palette.setColor(QPalette::Highlight, QColor(42, 130, 218)); | |
221 | qApp->setPalette(dark_palette); | |
222 | is_dark_theme_ = true; | |
223 | } else if (theme_name.compare("DarkStyle") == 0) { | |
224 | QPalette dark_palette; | |
225 | dark_palette.setColor(QPalette::Window, QColor(53, 53, 53)); | |
226 | dark_palette.setColor(QPalette::WindowText, Qt::white); | |
227 | dark_palette.setColor(QPalette::Disabled, QPalette::WindowText, QColor(127, 127, 127)); | |
228 | dark_palette.setColor(QPalette::Base, QColor(42, 42, 42)); | |
229 | dark_palette.setColor(QPalette::AlternateBase, QColor(66, 66, 66)); | |
230 | dark_palette.setColor(QPalette::ToolTipBase, Qt::white); | |
231 | dark_palette.setColor(QPalette::ToolTipText, QColor(53, 53, 53)); | |
232 | dark_palette.setColor(QPalette::Text, Qt::white); | |
233 | dark_palette.setColor(QPalette::Disabled, QPalette::Text, QColor(127, 127, 127)); | |
234 | dark_palette.setColor(QPalette::Dark, QColor(35, 35, 35)); | |
235 | dark_palette.setColor(QPalette::Shadow, QColor(20, 20, 20)); | |
236 | dark_palette.setColor(QPalette::Button, QColor(53, 53, 53)); | |
237 | dark_palette.setColor(QPalette::ButtonText, Qt::white); | |
238 | dark_palette.setColor(QPalette::Disabled, QPalette::ButtonText, QColor(127, 127, 127)); | |
239 | dark_palette.setColor(QPalette::BrightText, Qt::red); | |
240 | dark_palette.setColor(QPalette::Link, QColor(42, 130, 218)); | |
241 | dark_palette.setColor(QPalette::Highlight, QColor(42, 130, 218)); | |
242 | dark_palette.setColor(QPalette::Disabled, QPalette::Highlight, QColor(80, 80, 80)); | |
243 | dark_palette.setColor(QPalette::HighlightedText, Qt::white); | |
244 | dark_palette.setColor(QPalette::Disabled, QPalette::HighlightedText, QColor(127, 127, 127)); | |
245 | qApp->setPalette(dark_palette); | |
246 | is_dark_theme_ = true; | |
247 | } | |
248 | ||
249 | QPixmapCache::clear(); | |
250 | } | |
251 | ||
252 | void GlobalSettings::apply_language() | |
253 | { | |
254 | Application* a = qobject_cast<Application*>(QApplication::instance()); | |
255 | a->switch_language(value(Key_General_Language).toString()); | |
256 | } | |
257 | ||
258 | void GlobalSettings::add_change_handler(GlobalSettingsInterface *cb) | |
259 | { | |
260 | callbacks_.push_back(cb); | |
261 | } | |
262 | ||
263 | void GlobalSettings::remove_change_handler(GlobalSettingsInterface *cb) | |
264 | { | |
265 | for (auto cb_it = callbacks_.begin(); cb_it != callbacks_.end(); cb_it++) | |
266 | if (*cb_it == cb) { | |
267 | callbacks_.erase(cb_it); | |
268 | break; | |
269 | } | |
270 | } | |
271 | ||
272 | void GlobalSettings::setValue(const QString &key, const QVariant &value) | |
273 | { | |
274 | // Save previous value if we're tracking changes, | |
275 | // not altering an already-existing saved setting | |
276 | if (tracking_) | |
277 | tracked_changes_.emplace(key, QSettings::value(key)); | |
278 | ||
279 | QSettings::setValue(key, value); | |
280 | ||
281 | // TODO Emulate noquote() | |
282 | qDebug() << "Setting" << key << "changed to" << value; | |
283 | ||
284 | // Call all registered callbacks | |
285 | for (GlobalSettingsInterface *cb : callbacks_) | |
286 | cb->on_setting_changed(key, value); | |
287 | } | |
288 | ||
289 | void GlobalSettings::start_tracking() | |
290 | { | |
291 | tracking_ = true; | |
292 | tracked_changes_.clear(); | |
293 | } | |
294 | ||
295 | void GlobalSettings::stop_tracking() | |
296 | { | |
297 | tracking_ = false; | |
298 | tracked_changes_.clear(); | |
299 | } | |
300 | ||
301 | void GlobalSettings::undo_tracked_changes() | |
302 | { | |
303 | tracking_ = false; | |
304 | ||
305 | for (auto& entry : tracked_changes_) | |
306 | setValue(entry.first, entry.second); | |
307 | ||
308 | tracked_changes_.clear(); | |
309 | } | |
310 | ||
311 | void GlobalSettings::store_gvariant(QSettings &settings, GVariant *v) | |
312 | { | |
313 | const GVariantType *var_type = g_variant_get_type(v); | |
314 | char *var_type_str = g_variant_type_dup_string(var_type); | |
315 | ||
316 | QByteArray var_data = QByteArray((const char*)g_variant_get_data(v), | |
317 | g_variant_get_size(v)); | |
318 | ||
319 | settings.setValue("value", var_data); | |
320 | settings.setValue("type", var_type_str); | |
321 | ||
322 | g_free(var_type_str); | |
323 | } | |
324 | ||
325 | GVariant* GlobalSettings::restore_gvariant(QSettings &settings) | |
326 | { | |
327 | QString raw_type = settings.value("type").toString(); | |
328 | GVariantType *var_type = g_variant_type_new(raw_type.toUtf8()); | |
329 | ||
330 | QByteArray data = settings.value("value").toByteArray(); | |
331 | ||
332 | gpointer var_data = g_memdup((gconstpointer)data.constData(), | |
333 | (guint)data.size()); | |
334 | ||
335 | GVariant *value = g_variant_new_from_data(var_type, var_data, | |
336 | data.size(), false, g_free, var_data); | |
337 | ||
338 | g_variant_type_free(var_type); | |
339 | ||
340 | return value; | |
341 | } | |
342 | ||
343 | void GlobalSettings::store_variantbase(QSettings &settings, Glib::VariantBase v) | |
344 | { | |
345 | const QByteArray var_data = QByteArray((const char*)v.get_data(), v.get_size()); | |
346 | ||
347 | settings.setValue("value", var_data); | |
348 | settings.setValue("type", QString::fromStdString(v.get_type_string())); | |
349 | } | |
350 | ||
351 | Glib::VariantBase GlobalSettings::restore_variantbase(QSettings &settings) | |
352 | { | |
353 | QString raw_type = settings.value("type").toString(); | |
354 | GVariantType *var_type = g_variant_type_new(raw_type.toUtf8()); | |
355 | ||
356 | QByteArray data = settings.value("value").toByteArray(); | |
357 | ||
358 | gpointer var_data = g_memdup((gconstpointer)data.constData(), | |
359 | (guint)data.size()); | |
360 | ||
361 | GVariant *value = g_variant_new_from_data(var_type, var_data, | |
362 | data.size(), false, g_free, var_data); | |
363 | ||
364 | Glib::VariantBase ret_val = Glib::VariantBase(value, true); | |
365 | ||
366 | g_variant_type_free(var_type); | |
367 | g_variant_unref(value); | |
368 | ||
369 | return ret_val; | |
370 | } | |
371 | ||
372 | } // namespace pv |