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