]>
Commit | Line | Data |
---|---|---|
bf9f1268 SA |
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 | ||
e887fe9e SA |
20 | #include <boost/archive/text_iarchive.hpp> |
21 | #include <boost/archive/text_oarchive.hpp> | |
22 | #include <boost/serialization/serialization.hpp> | |
bf9f1268 | 23 | |
daa54986 | 24 | #include <QApplication> |
4521022b | 25 | #include <QColor> |
7ba25e4f | 26 | #include <QDebug> |
37b0bd35 | 27 | #include <QFile> |
daa54986 | 28 | #include <QFontMetrics> |
37b0bd35 | 29 | #include <QPixmapCache> |
d3feec23 | 30 | #include <QString> |
374c697f | 31 | #include <QStyle> |
43be386b | 32 | #include <QtGlobal> |
d3feec23 | 33 | |
e887fe9e SA |
34 | #include "globalsettings.hpp" |
35 | #include "application.hpp" | |
36 | ||
6f925ba9 | 37 | using std::map; |
37b0bd35 | 38 | using std::pair; |
1325ce42 | 39 | using std::string; |
e887fe9e | 40 | using std::stringstream; |
d0c0573b | 41 | using std::vector; |
6f925ba9 | 42 | |
bf9f1268 SA |
43 | namespace pv { |
44 | ||
37b0bd35 SA |
45 | const vector< pair<QString, QString> > Themes { |
46 | {"None" , ""}, | |
47 | {"QDarkStyleSheet", ":/themes/qdarkstyle/style.qss"}, | |
48 | {"DarkStyle", ":/themes/darkstyle/darkstyle.qss"} | |
49 | }; | |
50 | ||
0466001b | 51 | const QString GlobalSettings::Key_General_Language = "General_Language"; |
37b0bd35 | 52 | const QString GlobalSettings::Key_General_Theme = "General_Theme"; |
374c697f | 53 | const QString GlobalSettings::Key_General_Style = "General_Style"; |
8962d7b3 | 54 | const QString GlobalSettings::Key_General_SaveWithSetup = "General_SaveWithSetup"; |
5d58e6ce | 55 | const QString GlobalSettings::Key_General_StartAllSessions = "General_StartAllSessions"; |
e91fb166 | 56 | const QString GlobalSettings::Key_View_ZoomToFitDuringAcq = "View_ZoomToFitDuringAcq"; |
28ceff25 | 57 | const QString GlobalSettings::Key_View_ZoomToFitAfterAcq = "View_ZoomToFitAfterAcq"; |
ffc00fdd | 58 | const QString GlobalSettings::Key_View_TriggerIsZeroTime = "View_TriggerIsZeroTime"; |
641574bc | 59 | const QString GlobalSettings::Key_View_ColoredBG = "View_ColoredBG"; |
87a97d8a | 60 | const QString GlobalSettings::Key_View_StickyScrolling = "View_StickyScrolling"; |
e6c8d58b | 61 | const QString GlobalSettings::Key_View_AllowVerticalDragging = "View_AllowVerticalDragging"; |
051ba3b3 | 62 | const QString GlobalSettings::Key_View_ShowSamplingPoints = "View_ShowSamplingPoints"; |
4521022b SA |
63 | const QString GlobalSettings::Key_View_FillSignalHighAreas = "View_FillSignalHighAreas"; |
64 | const QString GlobalSettings::Key_View_FillSignalHighAreaColor = "View_FillSignalHighAreaColor"; | |
8ad61f40 | 65 | const QString GlobalSettings::Key_View_ShowAnalogMinorGrid = "View_ShowAnalogMinorGrid"; |
90ee1ed9 | 66 | const QString GlobalSettings::Key_View_ConversionThresholdDispMode = "View_ConversionThresholdDispMode"; |
daa54986 | 67 | const QString GlobalSettings::Key_View_DefaultDivHeight = "View_DefaultDivHeight"; |
48051ccb | 68 | const QString GlobalSettings::Key_View_DefaultLogicHeight = "View_DefaultLogicHeight"; |
1931b5f9 | 69 | const QString GlobalSettings::Key_View_ShowHoverMarker = "View_ShowHoverMarker"; |
fb641801 | 70 | const QString GlobalSettings::Key_View_SnapDistance = "View_SnapDistance"; |
c04f5a29 | 71 | const QString GlobalSettings::Key_View_CursorFillColor = "View_CursorFillColor"; |
79a37ed8 MM |
72 | const QString GlobalSettings::Key_View_CursorShowFrequency = "View_CursorShowFrequency"; |
73 | const QString GlobalSettings::Key_View_CursorShowInterval = "View_CursorShowInterval"; | |
a3c57f44 | 74 | const QString GlobalSettings::Key_View_CursorShowSamples = "View_CursorShowSamples"; |
1cc1c8de | 75 | const QString GlobalSettings::Key_Dec_InitialStateConfigurable = "Dec_InitialStateConfigurable"; |
1ed996b4 | 76 | const QString GlobalSettings::Key_Dec_ExportFormat = "Dec_ExportFormat"; |
ab185f78 | 77 | const QString GlobalSettings::Key_Dec_AlwaysShowAllRows = "Dec_AlwaysShowAllRows"; |
bcb4c327 | 78 | const QString GlobalSettings::Key_Log_BufferSize = "Log_BufferSize"; |
c5d6200c | 79 | const QString GlobalSettings::Key_Log_NotifyOfStacktrace = "Log_NotifyOfStacktrace"; |
bf9f1268 | 80 | |
d0c0573b | 81 | vector<GlobalSettingsInterface*> GlobalSettings::callbacks_; |
2cca9ebf | 82 | bool GlobalSettings::tracking_ = false; |
c6b4e925 | 83 | bool GlobalSettings::is_dark_theme_ = false; |
6f925ba9 | 84 | map<QString, QVariant> GlobalSettings::tracked_changes_; |
374c697f | 85 | QString GlobalSettings::default_style_; |
37b0bd35 | 86 | QPalette GlobalSettings::default_palette_; |
bf9f1268 SA |
87 | |
88 | GlobalSettings::GlobalSettings() : | |
c6b4e925 | 89 | QSettings() |
bf9f1268 SA |
90 | { |
91 | beginGroup("Settings"); | |
92 | } | |
93 | ||
a42d2514 SA |
94 | void GlobalSettings::save_internal_defaults() |
95 | { | |
96 | default_style_ = qApp->style()->objectName(); | |
97 | if (default_style_.isEmpty()) | |
98 | default_style_ = "fusion"; | |
99 | ||
100 | default_palette_ = QApplication::palette(); | |
101 | } | |
102 | ||
c031de4b SA |
103 | void GlobalSettings::set_defaults_where_needed() |
104 | { | |
0466001b SA |
105 | if (!contains(Key_General_Language)) { |
106 | // Determine and set default UI language | |
49fee853 | 107 | QString language = QLocale().uiLanguages().first(); // May return e.g. en-Latn-US // clazy:exclude=detaching-temporary |
0466001b SA |
108 | language = language.split("-").first(); |
109 | ||
110 | setValue(Key_General_Language, language); | |
d8036995 | 111 | apply_language(); |
0466001b SA |
112 | } |
113 | ||
37b0bd35 SA |
114 | // Use no theme by default |
115 | if (!contains(Key_General_Theme)) | |
116 | setValue(Key_General_Theme, 0); | |
374c697f SA |
117 | if (!contains(Key_General_Style)) |
118 | setValue(Key_General_Style, ""); | |
37b0bd35 | 119 | |
8962d7b3 SA |
120 | // Save setup with .sr files by default |
121 | if (!contains(Key_General_SaveWithSetup)) | |
122 | setValue(Key_General_SaveWithSetup, true); | |
123 | ||
28ceff25 SA |
124 | // Enable zoom-to-fit after acquisition by default |
125 | if (!contains(Key_View_ZoomToFitAfterAcq)) | |
126 | setValue(Key_View_ZoomToFitAfterAcq, true); | |
127 | ||
e6c8d58b SA |
128 | // Allow vertical dragging by default |
129 | if (!contains(Key_View_AllowVerticalDragging)) | |
130 | setValue(Key_View_AllowVerticalDragging, true); | |
131 | ||
641574bc SA |
132 | // Enable colored trace backgrounds by default |
133 | if (!contains(Key_View_ColoredBG)) | |
134 | setValue(Key_View_ColoredBG, true); | |
df842d4f SA |
135 | |
136 | // Enable showing sampling points by default | |
137 | if (!contains(Key_View_ShowSamplingPoints)) | |
138 | setValue(Key_View_ShowSamplingPoints, true); | |
daa54986 | 139 | |
4521022b SA |
140 | // Enable filling logic signal high areas by default |
141 | if (!contains(Key_View_FillSignalHighAreas)) | |
142 | setValue(Key_View_FillSignalHighAreas, true); | |
4521022b | 143 | |
daa54986 SA |
144 | if (!contains(Key_View_DefaultDivHeight)) |
145 | setValue(Key_View_DefaultDivHeight, | |
146 | 3 * QFontMetrics(QApplication::font()).height()); | |
48051ccb SA |
147 | |
148 | if (!contains(Key_View_DefaultLogicHeight)) | |
149 | setValue(Key_View_DefaultLogicHeight, | |
150 | 2 * QFontMetrics(QApplication::font()).height()); | |
bcb4c327 | 151 | |
6fff12ac SA |
152 | if (!contains(Key_View_ShowHoverMarker)) |
153 | setValue(Key_View_ShowHoverMarker, true); | |
154 | ||
fb641801 SA |
155 | if (!contains(Key_View_SnapDistance)) |
156 | setValue(Key_View_SnapDistance, 15); | |
157 | ||
79a37ed8 MM |
158 | if (!contains(Key_View_CursorShowInterval)) |
159 | setValue(Key_View_CursorShowInterval, true); | |
160 | ||
161 | if (!contains(Key_View_CursorShowFrequency)) | |
162 | setValue(Key_View_CursorShowFrequency, true); | |
163 | ||
6a26fc44 SA |
164 | // %c was used for the row name in the past so we need to transition such users |
165 | if (!contains(Key_Dec_ExportFormat) || | |
166 | value(Key_Dec_ExportFormat).toString() == "%s %d: %c: %1") | |
167 | setValue(Key_Dec_ExportFormat, "%s %d: %r: %1"); | |
1ed996b4 | 168 | |
bcb4c327 SA |
169 | // Default to 500 lines of backlog |
170 | if (!contains(Key_Log_BufferSize)) | |
171 | setValue(Key_Log_BufferSize, 500); | |
c5d6200c SA |
172 | |
173 | // Notify user of existing stack trace by default | |
174 | if (!contains(Key_Log_NotifyOfStacktrace)) | |
175 | setValue(Key_Log_NotifyOfStacktrace, true); | |
a42d2514 | 176 | |
c04f5a29 SA |
177 | // Default theme is bright, so use its color scheme if undefined |
178 | if (!contains(Key_View_CursorFillColor)) | |
179 | set_bright_theme_default_colors(); | |
c031de4b SA |
180 | } |
181 | ||
a42d2514 | 182 | void GlobalSettings::set_bright_theme_default_colors() |
37b0bd35 | 183 | { |
a42d2514 SA |
184 | setValue(Key_View_FillSignalHighAreaColor, |
185 | QColor(0, 0, 0, 5 * 256 / 100).rgba()); | |
c04f5a29 SA |
186 | |
187 | setValue(Key_View_CursorFillColor, | |
188 | QColor(220, 231, 243).rgba()); | |
a42d2514 | 189 | } |
374c697f | 190 | |
a42d2514 SA |
191 | void GlobalSettings::set_dark_theme_default_colors() |
192 | { | |
193 | setValue(Key_View_FillSignalHighAreaColor, | |
194 | QColor(188, 188, 188, 9 * 256 / 100).rgba()); | |
c04f5a29 SA |
195 | |
196 | setValue(Key_View_CursorFillColor, | |
197 | QColor(60, 60, 60).rgba()); | |
a42d2514 SA |
198 | } |
199 | ||
200 | bool GlobalSettings::current_theme_is_dark() | |
201 | { | |
202 | return is_dark_theme_; | |
37b0bd35 SA |
203 | } |
204 | ||
205 | void GlobalSettings::apply_theme() | |
206 | { | |
207 | QString theme_name = Themes.at(value(Key_General_Theme).toInt()).first; | |
208 | QString resource_name = Themes.at(value(Key_General_Theme).toInt()).second; | |
209 | ||
210 | if (!resource_name.isEmpty()) { | |
211 | QFile file(resource_name); | |
212 | file.open(QFile::ReadOnly | QFile::Text); | |
213 | qApp->setStyleSheet(file.readAll()); | |
214 | } else | |
215 | qApp->setStyleSheet(""); | |
216 | ||
217 | qApp->setPalette(default_palette_); | |
218 | ||
374c697f SA |
219 | const QString style = value(Key_General_Style).toString(); |
220 | if (style.isEmpty()) | |
221 | qApp->setStyle(default_style_); | |
222 | else | |
223 | qApp->setStyle(style); | |
224 | ||
a42d2514 SA |
225 | is_dark_theme_ = false; |
226 | ||
37b0bd35 SA |
227 | if (theme_name.compare("QDarkStyleSheet") == 0) { |
228 | QPalette dark_palette; | |
229 | dark_palette.setColor(QPalette::Window, QColor(53, 53, 53)); | |
230 | dark_palette.setColor(QPalette::WindowText, Qt::white); | |
231 | dark_palette.setColor(QPalette::Base, QColor(42, 42, 42)); | |
232 | dark_palette.setColor(QPalette::Dark, QColor(35, 35, 35)); | |
233 | dark_palette.setColor(QPalette::Highlight, QColor(42, 130, 218)); | |
234 | qApp->setPalette(dark_palette); | |
a42d2514 | 235 | is_dark_theme_ = true; |
37b0bd35 SA |
236 | } else if (theme_name.compare("DarkStyle") == 0) { |
237 | QPalette dark_palette; | |
238 | dark_palette.setColor(QPalette::Window, QColor(53, 53, 53)); | |
239 | dark_palette.setColor(QPalette::WindowText, Qt::white); | |
240 | dark_palette.setColor(QPalette::Disabled, QPalette::WindowText, QColor(127, 127, 127)); | |
241 | dark_palette.setColor(QPalette::Base, QColor(42, 42, 42)); | |
242 | dark_palette.setColor(QPalette::AlternateBase, QColor(66, 66, 66)); | |
243 | dark_palette.setColor(QPalette::ToolTipBase, Qt::white); | |
244 | dark_palette.setColor(QPalette::ToolTipText, QColor(53, 53, 53)); | |
245 | dark_palette.setColor(QPalette::Text, Qt::white); | |
246 | dark_palette.setColor(QPalette::Disabled, QPalette::Text, QColor(127, 127, 127)); | |
247 | dark_palette.setColor(QPalette::Dark, QColor(35, 35, 35)); | |
248 | dark_palette.setColor(QPalette::Shadow, QColor(20, 20, 20)); | |
249 | dark_palette.setColor(QPalette::Button, QColor(53, 53, 53)); | |
250 | dark_palette.setColor(QPalette::ButtonText, Qt::white); | |
251 | dark_palette.setColor(QPalette::Disabled, QPalette::ButtonText, QColor(127, 127, 127)); | |
252 | dark_palette.setColor(QPalette::BrightText, Qt::red); | |
253 | dark_palette.setColor(QPalette::Link, QColor(42, 130, 218)); | |
254 | dark_palette.setColor(QPalette::Highlight, QColor(42, 130, 218)); | |
255 | dark_palette.setColor(QPalette::Disabled, QPalette::Highlight, QColor(80, 80, 80)); | |
256 | dark_palette.setColor(QPalette::HighlightedText, Qt::white); | |
257 | dark_palette.setColor(QPalette::Disabled, QPalette::HighlightedText, QColor(127, 127, 127)); | |
258 | qApp->setPalette(dark_palette); | |
a42d2514 | 259 | is_dark_theme_ = true; |
37b0bd35 SA |
260 | } |
261 | ||
262 | QPixmapCache::clear(); | |
263 | } | |
264 | ||
0466001b SA |
265 | void GlobalSettings::apply_language() |
266 | { | |
267 | Application* a = qobject_cast<Application*>(QApplication::instance()); | |
268 | a->switch_language(value(Key_General_Language).toString()); | |
269 | } | |
270 | ||
d0c0573b | 271 | void GlobalSettings::add_change_handler(GlobalSettingsInterface *cb) |
bf9f1268 | 272 | { |
d0c0573b SA |
273 | callbacks_.push_back(cb); |
274 | } | |
275 | ||
276 | void GlobalSettings::remove_change_handler(GlobalSettingsInterface *cb) | |
277 | { | |
278 | for (auto cb_it = callbacks_.begin(); cb_it != callbacks_.end(); cb_it++) | |
279 | if (*cb_it == cb) { | |
280 | callbacks_.erase(cb_it); | |
281 | break; | |
282 | } | |
bf9f1268 SA |
283 | } |
284 | ||
285 | void GlobalSettings::setValue(const QString &key, const QVariant &value) | |
286 | { | |
2cca9ebf SA |
287 | // Save previous value if we're tracking changes, |
288 | // not altering an already-existing saved setting | |
289 | if (tracking_) | |
290 | tracked_changes_.emplace(key, QSettings::value(key)); | |
291 | ||
bf9f1268 SA |
292 | QSettings::setValue(key, value); |
293 | ||
fe060a48 SA |
294 | // TODO Emulate noquote() |
295 | qDebug() << "Setting" << key << "changed to" << value; | |
7ba25e4f | 296 | |
d0c0573b SA |
297 | // Call all registered callbacks |
298 | for (GlobalSettingsInterface *cb : callbacks_) | |
299 | cb->on_setting_changed(key, value); | |
bf9f1268 SA |
300 | } |
301 | ||
2cca9ebf SA |
302 | void GlobalSettings::start_tracking() |
303 | { | |
304 | tracking_ = true; | |
305 | tracked_changes_.clear(); | |
306 | } | |
307 | ||
308 | void GlobalSettings::stop_tracking() | |
309 | { | |
310 | tracking_ = false; | |
311 | tracked_changes_.clear(); | |
312 | } | |
313 | ||
314 | void GlobalSettings::undo_tracked_changes() | |
315 | { | |
316 | tracking_ = false; | |
317 | ||
f4ab4b5c | 318 | for (auto& entry : tracked_changes_) |
2cca9ebf SA |
319 | setValue(entry.first, entry.second); |
320 | ||
321 | tracked_changes_.clear(); | |
322 | } | |
bf9f1268 | 323 | |
d3feec23 SA |
324 | void GlobalSettings::store_gvariant(QSettings &settings, GVariant *v) |
325 | { | |
326 | const GVariantType *var_type = g_variant_get_type(v); | |
327 | char *var_type_str = g_variant_type_dup_string(var_type); | |
328 | ||
329 | QByteArray var_data = QByteArray((const char*)g_variant_get_data(v), | |
330 | g_variant_get_size(v)); | |
331 | ||
332 | settings.setValue("value", var_data); | |
333 | settings.setValue("type", var_type_str); | |
334 | ||
335 | g_free(var_type_str); | |
336 | } | |
337 | ||
338 | GVariant* GlobalSettings::restore_gvariant(QSettings &settings) | |
339 | { | |
340 | QString raw_type = settings.value("type").toString(); | |
341 | GVariantType *var_type = g_variant_type_new(raw_type.toUtf8()); | |
342 | ||
343 | QByteArray data = settings.value("value").toByteArray(); | |
344 | ||
cf010911 SA |
345 | #if GLIB_CHECK_VERSION(2, 67, 3) // See https://discourse.gnome.org/t/port-your-module-from-g-memdup-to-g-memdup2-now/5538 |
346 | gpointer var_data = g_memdup2((gconstpointer)data.constData(), (guint)data.size()); | |
347 | #else | |
348 | gpointer var_data = g_memdup((gconstpointer)data.constData(), (guint)data.size()); | |
349 | #endif | |
d3feec23 SA |
350 | |
351 | GVariant *value = g_variant_new_from_data(var_type, var_data, | |
352 | data.size(), false, g_free, var_data); | |
353 | ||
354 | g_variant_type_free(var_type); | |
355 | ||
356 | return value; | |
357 | } | |
358 | ||
1325ce42 SA |
359 | void GlobalSettings::store_variantbase(QSettings &settings, Glib::VariantBase v) |
360 | { | |
361 | const QByteArray var_data = QByteArray((const char*)v.get_data(), v.get_size()); | |
362 | ||
363 | settings.setValue("value", var_data); | |
364 | settings.setValue("type", QString::fromStdString(v.get_type_string())); | |
365 | } | |
366 | ||
367 | Glib::VariantBase GlobalSettings::restore_variantbase(QSettings &settings) | |
368 | { | |
369 | QString raw_type = settings.value("type").toString(); | |
370 | GVariantType *var_type = g_variant_type_new(raw_type.toUtf8()); | |
371 | ||
372 | QByteArray data = settings.value("value").toByteArray(); | |
373 | ||
cf010911 SA |
374 | #if GLIB_CHECK_VERSION(2, 67, 3) // See https://discourse.gnome.org/t/port-your-module-from-g-memdup-to-g-memdup2-now/5538 |
375 | gpointer var_data = g_memdup2((gconstpointer)data.constData(), (guint)data.size()); | |
376 | #else | |
377 | gpointer var_data = g_memdup((gconstpointer)data.constData(), (guint)data.size()); | |
378 | #endif | |
1325ce42 SA |
379 | |
380 | GVariant *value = g_variant_new_from_data(var_type, var_data, | |
381 | data.size(), false, g_free, var_data); | |
382 | ||
383 | Glib::VariantBase ret_val = Glib::VariantBase(value, true); | |
384 | ||
385 | g_variant_type_free(var_type); | |
386 | g_variant_unref(value); | |
387 | ||
388 | return ret_val; | |
389 | } | |
d3feec23 | 390 | |
e887fe9e SA |
391 | void GlobalSettings::store_timestamp(QSettings &settings, const char *name, const pv::util::Timestamp &ts) |
392 | { | |
393 | stringstream ss; | |
394 | boost::archive::text_oarchive oa(ss); | |
395 | oa << boost::serialization::make_nvp(name, ts); | |
396 | settings.setValue(name, QString::fromStdString(ss.str())); | |
397 | } | |
398 | ||
399 | pv::util::Timestamp GlobalSettings::restore_timestamp(QSettings &settings, const char *name) | |
400 | { | |
401 | util::Timestamp result; | |
402 | stringstream ss; | |
403 | ss << settings.value(name).toString().toStdString(); | |
404 | ||
405 | try { | |
406 | boost::archive::text_iarchive ia(ss); | |
407 | ia >> boost::serialization::make_nvp(name, result); | |
408 | } catch (boost::archive::archive_exception&) { | |
409 | qDebug() << "Could not restore setting" << name; | |
410 | } | |
411 | ||
412 | return result; | |
413 | } | |
414 | ||
bf9f1268 | 415 | } // namespace pv |