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