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