]> sigrok.org Git - pulseview.git/blame - pv/globalsettings.cpp
Main: Create human-readable stack trace and notify user
[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
20#include "globalsettings.hpp"
21
daa54986 22#include <QApplication>
daa54986 23#include <QFontMetrics>
d3feec23
SA
24#include <QString>
25
6f925ba9 26using std::map;
d0c0573b 27using std::vector;
6f925ba9 28
bf9f1268
SA
29namespace pv {
30
e91fb166 31const QString GlobalSettings::Key_View_ZoomToFitDuringAcq = "View_ZoomToFitDuringAcq";
28ceff25 32const QString GlobalSettings::Key_View_ZoomToFitAfterAcq = "View_ZoomToFitAfterAcq";
ffc00fdd 33const QString GlobalSettings::Key_View_TriggerIsZeroTime = "View_TriggerIsZeroTime";
bf9f1268 34const QString GlobalSettings::Key_View_ColouredBG = "View_ColouredBG";
87a97d8a 35const QString GlobalSettings::Key_View_StickyScrolling = "View_StickyScrolling";
051ba3b3 36const QString GlobalSettings::Key_View_ShowSamplingPoints = "View_ShowSamplingPoints";
8ad61f40 37const QString GlobalSettings::Key_View_ShowAnalogMinorGrid = "View_ShowAnalogMinorGrid";
90ee1ed9 38const QString GlobalSettings::Key_View_ConversionThresholdDispMode = "View_ConversionThresholdDispMode";
daa54986 39const QString GlobalSettings::Key_View_DefaultDivHeight = "View_DefaultDivHeight";
48051ccb 40const QString GlobalSettings::Key_View_DefaultLogicHeight = "View_DefaultLogicHeight";
1cc1c8de 41const QString GlobalSettings::Key_Dec_InitialStateConfigurable = "Dec_InitialStateConfigurable";
bcb4c327 42const QString GlobalSettings::Key_Log_BufferSize = "Log_BufferSize";
c5d6200c 43const QString GlobalSettings::Key_Log_NotifyOfStacktrace = "Log_NotifyOfStacktrace";
bf9f1268 44
d0c0573b 45vector<GlobalSettingsInterface*> GlobalSettings::callbacks_;
2cca9ebf 46bool GlobalSettings::tracking_ = false;
6f925ba9 47map<QString, QVariant> GlobalSettings::tracked_changes_;
bf9f1268
SA
48
49GlobalSettings::GlobalSettings() :
50 QSettings()
51{
52 beginGroup("Settings");
53}
54
c031de4b
SA
55void GlobalSettings::set_defaults_where_needed()
56{
28ceff25
SA
57 // Enable zoom-to-fit after acquisition by default
58 if (!contains(Key_View_ZoomToFitAfterAcq))
59 setValue(Key_View_ZoomToFitAfterAcq, true);
60
c031de4b
SA
61 // Enable coloured trace backgrounds by default
62 if (!contains(Key_View_ColouredBG))
63 setValue(Key_View_ColouredBG, true);
df842d4f
SA
64
65 // Enable showing sampling points by default
66 if (!contains(Key_View_ShowSamplingPoints))
67 setValue(Key_View_ShowSamplingPoints, true);
daa54986
SA
68
69 if (!contains(Key_View_DefaultDivHeight))
70 setValue(Key_View_DefaultDivHeight,
71 3 * QFontMetrics(QApplication::font()).height());
48051ccb
SA
72
73 if (!contains(Key_View_DefaultLogicHeight))
74 setValue(Key_View_DefaultLogicHeight,
75 2 * QFontMetrics(QApplication::font()).height());
bcb4c327
SA
76
77 // Default to 500 lines of backlog
78 if (!contains(Key_Log_BufferSize))
79 setValue(Key_Log_BufferSize, 500);
c5d6200c
SA
80
81 // Notify user of existing stack trace by default
82 if (!contains(Key_Log_NotifyOfStacktrace))
83 setValue(Key_Log_NotifyOfStacktrace, true);
c031de4b
SA
84}
85
d0c0573b 86void GlobalSettings::add_change_handler(GlobalSettingsInterface *cb)
bf9f1268 87{
d0c0573b
SA
88 callbacks_.push_back(cb);
89}
90
91void GlobalSettings::remove_change_handler(GlobalSettingsInterface *cb)
92{
93 for (auto cb_it = callbacks_.begin(); cb_it != callbacks_.end(); cb_it++)
94 if (*cb_it == cb) {
95 callbacks_.erase(cb_it);
96 break;
97 }
bf9f1268
SA
98}
99
100void GlobalSettings::setValue(const QString &key, const QVariant &value)
101{
2cca9ebf
SA
102 // Save previous value if we're tracking changes,
103 // not altering an already-existing saved setting
104 if (tracking_)
105 tracked_changes_.emplace(key, QSettings::value(key));
106
bf9f1268
SA
107 QSettings::setValue(key, value);
108
d0c0573b
SA
109 // Call all registered callbacks
110 for (GlobalSettingsInterface *cb : callbacks_)
111 cb->on_setting_changed(key, value);
bf9f1268
SA
112}
113
2cca9ebf
SA
114void GlobalSettings::start_tracking()
115{
116 tracking_ = true;
117 tracked_changes_.clear();
118}
119
120void GlobalSettings::stop_tracking()
121{
122 tracking_ = false;
123 tracked_changes_.clear();
124}
125
126void GlobalSettings::undo_tracked_changes()
127{
128 tracking_ = false;
129
130 for (auto entry : tracked_changes_)
131 setValue(entry.first, entry.second);
132
133 tracked_changes_.clear();
134}
bf9f1268 135
d3feec23
SA
136void GlobalSettings::store_gvariant(QSettings &settings, GVariant *v)
137{
138 const GVariantType *var_type = g_variant_get_type(v);
139 char *var_type_str = g_variant_type_dup_string(var_type);
140
141 QByteArray var_data = QByteArray((const char*)g_variant_get_data(v),
142 g_variant_get_size(v));
143
144 settings.setValue("value", var_data);
145 settings.setValue("type", var_type_str);
146
147 g_free(var_type_str);
148}
149
150GVariant* GlobalSettings::restore_gvariant(QSettings &settings)
151{
152 QString raw_type = settings.value("type").toString();
153 GVariantType *var_type = g_variant_type_new(raw_type.toUtf8());
154
155 QByteArray data = settings.value("value").toByteArray();
156
157 gpointer var_data = g_memdup((gconstpointer)data.constData(),
158 (guint)data.size());
159
160 GVariant *value = g_variant_new_from_data(var_type, var_data,
161 data.size(), false, g_free, var_data);
162
163 g_variant_type_free(var_type);
164
165 return value;
166}
167
168
bf9f1268 169} // namespace pv