]> sigrok.org Git - pulseview.git/blob - pv/globalsettings.cpp
GlobalSettings: Enable mouse hover marker by default
[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 <QColor>
24 #include <QDebug>
25 #include <QFontMetrics>
26 #include <QString>
27
28 using std::map;
29 using std::string;
30 using std::vector;
31
32 namespace pv {
33
34 const QString GlobalSettings::Key_View_ZoomToFitDuringAcq = "View_ZoomToFitDuringAcq";
35 const QString GlobalSettings::Key_View_ZoomToFitAfterAcq = "View_ZoomToFitAfterAcq";
36 const QString GlobalSettings::Key_View_TriggerIsZeroTime = "View_TriggerIsZeroTime";
37 const QString GlobalSettings::Key_View_ColoredBG = "View_ColoredBG";
38 const QString GlobalSettings::Key_View_StickyScrolling = "View_StickyScrolling";
39 const QString GlobalSettings::Key_View_ShowSamplingPoints = "View_ShowSamplingPoints";
40 const QString GlobalSettings::Key_View_FillSignalHighAreas = "View_FillSignalHighAreas";
41 const QString GlobalSettings::Key_View_FillSignalHighAreaColor = "View_FillSignalHighAreaColor";
42 const QString GlobalSettings::Key_View_ShowAnalogMinorGrid = "View_ShowAnalogMinorGrid";
43 const QString GlobalSettings::Key_View_ConversionThresholdDispMode = "View_ConversionThresholdDispMode";
44 const QString GlobalSettings::Key_View_DefaultDivHeight = "View_DefaultDivHeight";
45 const QString GlobalSettings::Key_View_DefaultLogicHeight = "View_DefaultLogicHeight";
46 const QString GlobalSettings::Key_View_ShowHoverMarker = "View_ShowHoverMarker";
47 const QString GlobalSettings::Key_View_SnapDistance = "View_SnapDistance";
48 const QString GlobalSettings::Key_Dec_InitialStateConfigurable = "Dec_InitialStateConfigurable";
49 const QString GlobalSettings::Key_Dec_ExportFormat = "Dec_ExportFormat";
50 const QString GlobalSettings::Key_Log_BufferSize = "Log_BufferSize";
51 const QString GlobalSettings::Key_Log_NotifyOfStacktrace = "Log_NotifyOfStacktrace";
52
53 vector<GlobalSettingsInterface*> GlobalSettings::callbacks_;
54 bool GlobalSettings::tracking_ = false;
55 map<QString, QVariant> GlobalSettings::tracked_changes_;
56
57 GlobalSettings::GlobalSettings() :
58         QSettings()
59 {
60         beginGroup("Settings");
61 }
62
63 void GlobalSettings::set_defaults_where_needed()
64 {
65         // Enable zoom-to-fit after acquisition by default
66         if (!contains(Key_View_ZoomToFitAfterAcq))
67                 setValue(Key_View_ZoomToFitAfterAcq, true);
68
69         // Enable colored trace backgrounds by default
70         if (!contains(Key_View_ColoredBG))
71                 setValue(Key_View_ColoredBG, true);
72
73         // Enable showing sampling points by default
74         if (!contains(Key_View_ShowSamplingPoints))
75                 setValue(Key_View_ShowSamplingPoints, true);
76
77         // Enable filling logic signal high areas by default
78         if (!contains(Key_View_FillSignalHighAreas))
79                 setValue(Key_View_FillSignalHighAreas, true);
80         if (!contains(Key_View_FillSignalHighAreaColor))
81                 setValue(Key_View_FillSignalHighAreaColor,
82                         QColor(0, 0, 0, 5 * 256 / 100).rgba());
83
84         if (!contains(Key_View_DefaultDivHeight))
85                 setValue(Key_View_DefaultDivHeight,
86                 3 * QFontMetrics(QApplication::font()).height());
87
88         if (!contains(Key_View_DefaultLogicHeight))
89                 setValue(Key_View_DefaultLogicHeight,
90                 2 * QFontMetrics(QApplication::font()).height());
91
92         if (!contains(Key_View_ShowHoverMarker))
93                 setValue(Key_View_ShowHoverMarker, true);
94
95         if (!contains(Key_View_SnapDistance))
96                 setValue(Key_View_SnapDistance, 15);
97
98         if (!contains(Key_Dec_ExportFormat))
99                 setValue(Key_Dec_ExportFormat, "%s %d: %c: %1");
100
101         // Default to 500 lines of backlog
102         if (!contains(Key_Log_BufferSize))
103                 setValue(Key_Log_BufferSize, 500);
104
105         // Notify user of existing stack trace by default
106         if (!contains(Key_Log_NotifyOfStacktrace))
107                 setValue(Key_Log_NotifyOfStacktrace, true);
108 }
109
110 void GlobalSettings::add_change_handler(GlobalSettingsInterface *cb)
111 {
112         callbacks_.push_back(cb);
113 }
114
115 void GlobalSettings::remove_change_handler(GlobalSettingsInterface *cb)
116 {
117         for (auto cb_it = callbacks_.begin(); cb_it != callbacks_.end(); cb_it++)
118                 if (*cb_it == cb) {
119                         callbacks_.erase(cb_it);
120                         break;
121                 }
122 }
123
124 void GlobalSettings::setValue(const QString &key, const QVariant &value)
125 {
126         // Save previous value if we're tracking changes,
127         // not altering an already-existing saved setting
128         if (tracking_)
129                 tracked_changes_.emplace(key, QSettings::value(key));
130
131         QSettings::setValue(key, value);
132
133         // TODO Emulate noquote()
134         qDebug() << "Setting" << key << "changed to" << value;
135
136         // Call all registered callbacks
137         for (GlobalSettingsInterface *cb : callbacks_)
138                 cb->on_setting_changed(key, value);
139 }
140
141 void GlobalSettings::start_tracking()
142 {
143         tracking_ = true;
144         tracked_changes_.clear();
145 }
146
147 void GlobalSettings::stop_tracking()
148 {
149         tracking_ = false;
150         tracked_changes_.clear();
151 }
152
153 void GlobalSettings::undo_tracked_changes()
154 {
155         tracking_ = false;
156
157         for (auto entry : tracked_changes_)
158                 setValue(entry.first, entry.second);
159
160         tracked_changes_.clear();
161 }
162
163 void GlobalSettings::store_gvariant(QSettings &settings, GVariant *v)
164 {
165         const GVariantType *var_type = g_variant_get_type(v);
166         char *var_type_str = g_variant_type_dup_string(var_type);
167
168         QByteArray var_data = QByteArray((const char*)g_variant_get_data(v),
169                 g_variant_get_size(v));
170
171         settings.setValue("value", var_data);
172         settings.setValue("type", var_type_str);
173
174         g_free(var_type_str);
175 }
176
177 GVariant* GlobalSettings::restore_gvariant(QSettings &settings)
178 {
179         QString raw_type = settings.value("type").toString();
180         GVariantType *var_type = g_variant_type_new(raw_type.toUtf8());
181
182         QByteArray data = settings.value("value").toByteArray();
183
184         gpointer var_data = g_memdup((gconstpointer)data.constData(),
185                 (guint)data.size());
186
187         GVariant *value = g_variant_new_from_data(var_type, var_data,
188                 data.size(), false, g_free, var_data);
189
190         g_variant_type_free(var_type);
191
192         return value;
193 }
194
195 void GlobalSettings::store_variantbase(QSettings &settings, Glib::VariantBase v)
196 {
197         const QByteArray var_data = QByteArray((const char*)v.get_data(), v.get_size());
198
199         settings.setValue("value", var_data);
200         settings.setValue("type", QString::fromStdString(v.get_type_string()));
201 }
202
203 Glib::VariantBase GlobalSettings::restore_variantbase(QSettings &settings)
204 {
205         QString raw_type = settings.value("type").toString();
206         GVariantType *var_type = g_variant_type_new(raw_type.toUtf8());
207
208         QByteArray data = settings.value("value").toByteArray();
209
210         gpointer var_data = g_memdup((gconstpointer)data.constData(),
211                 (guint)data.size());
212
213         GVariant *value = g_variant_new_from_data(var_type, var_data,
214                 data.size(), false, g_free, var_data);
215
216         Glib::VariantBase ret_val = Glib::VariantBase(value, true);
217
218         g_variant_type_free(var_type);
219         g_variant_unref(value);
220
221         return ret_val;
222 }
223
224 } // namespace pv