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