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