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