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