]> sigrok.org Git - pulseview.git/blob - pv/globalsettings.cpp
Session: Fix issue #67 by improving error handling
[pulseview.git] / pv / globalsettings.cpp
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 <boost/archive/text_iarchive.hpp>
21 #include <boost/archive/text_oarchive.hpp>
22 #include <boost/serialization/serialization.hpp>
23
24 #include <QApplication>
25 #include <QColor>
26 #include <QDebug>
27 #include <QFile>
28 #include <QFontMetrics>
29 #include <QPixmapCache>
30 #include <QString>
31 #include <QStyle>
32 #include <QtGlobal>
33
34 #include "globalsettings.hpp"
35 #include "application.hpp"
36
37 using std::map;
38 using std::pair;
39 using std::string;
40 using std::stringstream;
41 using std::vector;
42
43 namespace pv {
44
45 const vector< pair<QString, QString> > Themes {
46         {"None" , ""},
47         {"QDarkStyleSheet", ":/themes/qdarkstyle/style.qss"},
48         {"DarkStyle", ":/themes/darkstyle/darkstyle.qss"}
49 };
50
51 const QString GlobalSettings::Key_General_Language = "General_Language";
52 const QString GlobalSettings::Key_General_Theme = "General_Theme";
53 const QString GlobalSettings::Key_General_Style = "General_Style";
54 const QString GlobalSettings::Key_General_SaveWithSetup = "General_SaveWithSetup";
55 const QString GlobalSettings::Key_General_StartAllSessions = "General_StartAllSessions";
56 const QString GlobalSettings::Key_View_ZoomToFitDuringAcq = "View_ZoomToFitDuringAcq";
57 const QString GlobalSettings::Key_View_ZoomToFitAfterAcq = "View_ZoomToFitAfterAcq";
58 const QString GlobalSettings::Key_View_TriggerIsZeroTime = "View_TriggerIsZeroTime";
59 const QString GlobalSettings::Key_View_ColoredBG = "View_ColoredBG";
60 const QString GlobalSettings::Key_View_StickyScrolling = "View_StickyScrolling";
61 const QString GlobalSettings::Key_View_AllowVerticalDragging = "View_AllowVerticalDragging";
62 const QString GlobalSettings::Key_View_ShowSamplingPoints = "View_ShowSamplingPoints";
63 const QString GlobalSettings::Key_View_FillSignalHighAreas = "View_FillSignalHighAreas";
64 const QString GlobalSettings::Key_View_FillSignalHighAreaColor = "View_FillSignalHighAreaColor";
65 const QString GlobalSettings::Key_View_ShowAnalogMinorGrid = "View_ShowAnalogMinorGrid";
66 const QString GlobalSettings::Key_View_ConversionThresholdDispMode = "View_ConversionThresholdDispMode";
67 const QString GlobalSettings::Key_View_DefaultDivHeight = "View_DefaultDivHeight";
68 const QString GlobalSettings::Key_View_DefaultLogicHeight = "View_DefaultLogicHeight";
69 const QString GlobalSettings::Key_View_ShowHoverMarker = "View_ShowHoverMarker";
70 const QString GlobalSettings::Key_View_SnapDistance = "View_SnapDistance";
71 const QString GlobalSettings::Key_View_CursorFillColor = "View_CursorFillColor";
72 const QString GlobalSettings::Key_View_CursorShowFrequency = "View_CursorShowFrequency";
73 const QString GlobalSettings::Key_View_CursorShowInterval = "View_CursorShowInterval";
74 const QString GlobalSettings::Key_View_CursorShowSamples = "View_CursorShowSamples";
75 const QString GlobalSettings::Key_Dec_InitialStateConfigurable = "Dec_InitialStateConfigurable";
76 const QString GlobalSettings::Key_Dec_ExportFormat = "Dec_ExportFormat";
77 const QString GlobalSettings::Key_Dec_AlwaysShowAllRows = "Dec_AlwaysShowAllRows";
78 const QString GlobalSettings::Key_Log_BufferSize = "Log_BufferSize";
79 const QString GlobalSettings::Key_Log_NotifyOfStacktrace = "Log_NotifyOfStacktrace";
80
81 vector<GlobalSettingsInterface*> GlobalSettings::callbacks_;
82 bool GlobalSettings::tracking_ = false;
83 bool GlobalSettings::is_dark_theme_ = false;
84 map<QString, QVariant> GlobalSettings::tracked_changes_;
85 QString GlobalSettings::default_style_;
86 QPalette GlobalSettings::default_palette_;
87
88 GlobalSettings::GlobalSettings() :
89         QSettings()
90 {
91         beginGroup("Settings");
92 }
93
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
103 void GlobalSettings::set_defaults_where_needed()
104 {
105         if (!contains(Key_General_Language)) {
106                 // Determine and set default UI language
107                 QString language = QLocale().uiLanguages().first();  // May return e.g. en-Latn-US  // clazy:exclude=detaching-temporary
108                 language = language.split("-").first();
109
110                 setValue(Key_General_Language, language);
111                 apply_language();
112         }
113
114         // Use no theme by default
115         if (!contains(Key_General_Theme))
116                 setValue(Key_General_Theme, 0);
117         if (!contains(Key_General_Style))
118                 setValue(Key_General_Style, "");
119
120         // Save setup with .sr files by default
121         if (!contains(Key_General_SaveWithSetup))
122                 setValue(Key_General_SaveWithSetup, true);
123
124         // Enable zoom-to-fit after acquisition by default
125         if (!contains(Key_View_ZoomToFitAfterAcq))
126                 setValue(Key_View_ZoomToFitAfterAcq, true);
127
128         // Allow vertical dragging by default
129         if (!contains(Key_View_AllowVerticalDragging))
130                 setValue(Key_View_AllowVerticalDragging, true);
131
132         // Enable colored trace backgrounds by default
133         if (!contains(Key_View_ColoredBG))
134                 setValue(Key_View_ColoredBG, true);
135
136         // Enable showing sampling points by default
137         if (!contains(Key_View_ShowSamplingPoints))
138                 setValue(Key_View_ShowSamplingPoints, true);
139
140         // Enable filling logic signal high areas by default
141         if (!contains(Key_View_FillSignalHighAreas))
142                 setValue(Key_View_FillSignalHighAreas, true);
143
144         if (!contains(Key_View_DefaultDivHeight))
145                 setValue(Key_View_DefaultDivHeight,
146                 3 * QFontMetrics(QApplication::font()).height());
147
148         if (!contains(Key_View_DefaultLogicHeight))
149                 setValue(Key_View_DefaultLogicHeight,
150                 2 * QFontMetrics(QApplication::font()).height());
151
152         if (!contains(Key_View_ShowHoverMarker))
153                 setValue(Key_View_ShowHoverMarker, true);
154
155         if (!contains(Key_View_SnapDistance))
156                 setValue(Key_View_SnapDistance, 15);
157
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
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");
168
169         // Default to 500 lines of backlog
170         if (!contains(Key_Log_BufferSize))
171                 setValue(Key_Log_BufferSize, 500);
172
173         // Notify user of existing stack trace by default
174         if (!contains(Key_Log_NotifyOfStacktrace))
175                 setValue(Key_Log_NotifyOfStacktrace, true);
176
177         // Default theme is bright, so use its color scheme if undefined
178         if (!contains(Key_View_CursorFillColor))
179                 set_bright_theme_default_colors();
180 }
181
182 void GlobalSettings::set_bright_theme_default_colors()
183 {
184         setValue(Key_View_FillSignalHighAreaColor,
185                 QColor(0, 0, 0, 5 * 256 / 100).rgba());
186
187         setValue(Key_View_CursorFillColor,
188                 QColor(220, 231, 243).rgba());
189 }
190
191 void GlobalSettings::set_dark_theme_default_colors()
192 {
193         setValue(Key_View_FillSignalHighAreaColor,
194                 QColor(188, 188, 188, 9 * 256 / 100).rgba());
195
196         setValue(Key_View_CursorFillColor,
197                 QColor(60, 60, 60).rgba());
198 }
199
200 bool GlobalSettings::current_theme_is_dark()
201 {
202         return is_dark_theme_;
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
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
225         is_dark_theme_ = false;
226
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);
235                 is_dark_theme_ = true;
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);
259                 is_dark_theme_ = true;
260         }
261
262         QPixmapCache::clear();
263 }
264
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
271 void GlobalSettings::add_change_handler(GlobalSettingsInterface *cb)
272 {
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                 }
283 }
284
285 void GlobalSettings::setValue(const QString &key, const QVariant &value)
286 {
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
292         QSettings::setValue(key, value);
293
294         // TODO Emulate noquote()
295         qDebug() << "Setting" << key << "changed to" << value;
296
297         // Call all registered callbacks
298         for (GlobalSettingsInterface *cb : callbacks_)
299                 cb->on_setting_changed(key, value);
300 }
301
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
318         for (auto& entry : tracked_changes_)
319                 setValue(entry.first, entry.second);
320
321         tracked_changes_.clear();
322 }
323
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
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(), (gsize)data.size());
347 #else
348         gpointer var_data = g_memdup((gconstpointer)data.constData(), (guint)data.size());
349 #endif
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
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
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(), (gsize)data.size());
376 #else
377         gpointer var_data = g_memdup((gconstpointer)data.constData(), (guint)data.size());
378 #endif
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 }
390
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
415 } // namespace pv