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