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