]> sigrok.org Git - pulseview.git/blame_incremental - pv/globalsettings.cpp
Adjust unit tests
[pulseview.git] / pv / globalsettings.cpp
... / ...
CommitLineData
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 <QByteArray>
24#include <QFontMetrics>
25#include <QString>
26
27using std::function;
28using std::map;
29using std::multimap;
30
31namespace pv {
32
33const QString GlobalSettings::Key_View_ZoomToFitDuringAcq = "View_ZoomToFitDuringAcq";
34const QString GlobalSettings::Key_View_ZoomToFitAfterAcq = "View_ZoomToFitAfterAcq";
35const QString GlobalSettings::Key_View_TriggerIsZeroTime = "View_TriggerIsZeroTime";
36const QString GlobalSettings::Key_View_ColouredBG = "View_ColouredBG";
37const QString GlobalSettings::Key_View_StickyScrolling = "View_StickyScrolling";
38const QString GlobalSettings::Key_View_ShowSamplingPoints = "View_ShowSamplingPoints";
39const QString GlobalSettings::Key_View_ShowAnalogMinorGrid = "View_ShowAnalogMinorGrid";
40const QString GlobalSettings::Key_View_ConversionThresholdDispMode = "View_ConversionThresholdDispMode";
41const QString GlobalSettings::Key_View_DefaultDivHeight = "View_DefaultDivHeight";
42const QString GlobalSettings::Key_View_DefaultLogicHeight = "View_DefaultLogicHeight";
43const QString GlobalSettings::Key_Dec_InitialStateConfigurable = "Dec_InitialStateConfigurable";
44
45multimap< QString, function<void(QVariant)> > GlobalSettings::callbacks_;
46bool GlobalSettings::tracking_ = false;
47map<QString, QVariant> GlobalSettings::tracked_changes_;
48
49GlobalSettings::GlobalSettings() :
50 QSettings()
51{
52 beginGroup("Settings");
53}
54
55void GlobalSettings::set_defaults_where_needed()
56{
57 // Enable zoom-to-fit after acquisition by default
58 if (!contains(Key_View_ZoomToFitAfterAcq))
59 setValue(Key_View_ZoomToFitAfterAcq, true);
60
61 // Enable coloured trace backgrounds by default
62 if (!contains(Key_View_ColouredBG))
63 setValue(Key_View_ColouredBG, true);
64
65 // Enable showing sampling points by default
66 if (!contains(Key_View_ShowSamplingPoints))
67 setValue(Key_View_ShowSamplingPoints, true);
68
69 if (!contains(Key_View_DefaultDivHeight))
70 setValue(Key_View_DefaultDivHeight,
71 3 * QFontMetrics(QApplication::font()).height());
72
73 if (!contains(Key_View_DefaultLogicHeight))
74 setValue(Key_View_DefaultLogicHeight,
75 2 * QFontMetrics(QApplication::font()).height());
76}
77
78void GlobalSettings::register_change_handler(const QString key,
79 function<void(QVariant)> cb)
80{
81 callbacks_.emplace(key, cb);
82}
83
84void GlobalSettings::setValue(const QString &key, const QVariant &value)
85{
86 // Save previous value if we're tracking changes,
87 // not altering an already-existing saved setting
88 if (tracking_)
89 tracked_changes_.emplace(key, QSettings::value(key));
90
91 QSettings::setValue(key, value);
92
93 // Call all registered callbacks for this key
94 auto range = callbacks_.equal_range(key);
95
96 for (auto it = range.first; it != range.second; it++)
97 it->second(value);
98}
99
100void GlobalSettings::start_tracking()
101{
102 tracking_ = true;
103 tracked_changes_.clear();
104}
105
106void GlobalSettings::stop_tracking()
107{
108 tracking_ = false;
109 tracked_changes_.clear();
110}
111
112void GlobalSettings::undo_tracked_changes()
113{
114 tracking_ = false;
115
116 for (auto entry : tracked_changes_)
117 setValue(entry.first, entry.second);
118
119 tracked_changes_.clear();
120}
121
122void GlobalSettings::store_gvariant(QSettings &settings, GVariant *v)
123{
124 const GVariantType *var_type = g_variant_get_type(v);
125 char *var_type_str = g_variant_type_dup_string(var_type);
126
127 QByteArray var_data = QByteArray((const char*)g_variant_get_data(v),
128 g_variant_get_size(v));
129
130 settings.setValue("value", var_data);
131 settings.setValue("type", var_type_str);
132
133 g_free(var_type_str);
134}
135
136GVariant* GlobalSettings::restore_gvariant(QSettings &settings)
137{
138 QString raw_type = settings.value("type").toString();
139 GVariantType *var_type = g_variant_type_new(raw_type.toUtf8());
140
141 QByteArray data = settings.value("value").toByteArray();
142
143 gpointer var_data = g_memdup((gconstpointer)data.constData(),
144 (guint)data.size());
145
146 GVariant *value = g_variant_new_from_data(var_type, var_data,
147 data.size(), false, g_free, var_data);
148
149 g_variant_type_free(var_type);
150
151 return value;
152}
153
154
155} // namespace pv