]>
Commit | Line | Data |
---|---|---|
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_SnapDistance)) | |
93 | setValue(Key_View_SnapDistance, 15); | |
94 | ||
95 | if (!contains(Key_Dec_ExportFormat)) | |
96 | setValue(Key_Dec_ExportFormat, "%s %d: %c: %1"); | |
97 | ||
98 | // Default to 500 lines of backlog | |
99 | if (!contains(Key_Log_BufferSize)) | |
100 | setValue(Key_Log_BufferSize, 500); | |
101 | ||
102 | // Notify user of existing stack trace by default | |
103 | if (!contains(Key_Log_NotifyOfStacktrace)) | |
104 | setValue(Key_Log_NotifyOfStacktrace, true); | |
105 | } | |
106 | ||
107 | void GlobalSettings::add_change_handler(GlobalSettingsInterface *cb) | |
108 | { | |
109 | callbacks_.push_back(cb); | |
110 | } | |
111 | ||
112 | void GlobalSettings::remove_change_handler(GlobalSettingsInterface *cb) | |
113 | { | |
114 | for (auto cb_it = callbacks_.begin(); cb_it != callbacks_.end(); cb_it++) | |
115 | if (*cb_it == cb) { | |
116 | callbacks_.erase(cb_it); | |
117 | break; | |
118 | } | |
119 | } | |
120 | ||
121 | void GlobalSettings::setValue(const QString &key, const QVariant &value) | |
122 | { | |
123 | // Save previous value if we're tracking changes, | |
124 | // not altering an already-existing saved setting | |
125 | if (tracking_) | |
126 | tracked_changes_.emplace(key, QSettings::value(key)); | |
127 | ||
128 | QSettings::setValue(key, value); | |
129 | ||
130 | // TODO Emulate noquote() | |
131 | qDebug() << "Setting" << key << "changed to" << value; | |
132 | ||
133 | // Call all registered callbacks | |
134 | for (GlobalSettingsInterface *cb : callbacks_) | |
135 | cb->on_setting_changed(key, value); | |
136 | } | |
137 | ||
138 | void GlobalSettings::start_tracking() | |
139 | { | |
140 | tracking_ = true; | |
141 | tracked_changes_.clear(); | |
142 | } | |
143 | ||
144 | void GlobalSettings::stop_tracking() | |
145 | { | |
146 | tracking_ = false; | |
147 | tracked_changes_.clear(); | |
148 | } | |
149 | ||
150 | void GlobalSettings::undo_tracked_changes() | |
151 | { | |
152 | tracking_ = false; | |
153 | ||
154 | for (auto entry : tracked_changes_) | |
155 | setValue(entry.first, entry.second); | |
156 | ||
157 | tracked_changes_.clear(); | |
158 | } | |
159 | ||
160 | void GlobalSettings::store_gvariant(QSettings &settings, GVariant *v) | |
161 | { | |
162 | const GVariantType *var_type = g_variant_get_type(v); | |
163 | char *var_type_str = g_variant_type_dup_string(var_type); | |
164 | ||
165 | QByteArray var_data = QByteArray((const char*)g_variant_get_data(v), | |
166 | g_variant_get_size(v)); | |
167 | ||
168 | settings.setValue("value", var_data); | |
169 | settings.setValue("type", var_type_str); | |
170 | ||
171 | g_free(var_type_str); | |
172 | } | |
173 | ||
174 | GVariant* GlobalSettings::restore_gvariant(QSettings &settings) | |
175 | { | |
176 | QString raw_type = settings.value("type").toString(); | |
177 | GVariantType *var_type = g_variant_type_new(raw_type.toUtf8()); | |
178 | ||
179 | QByteArray data = settings.value("value").toByteArray(); | |
180 | ||
181 | gpointer var_data = g_memdup((gconstpointer)data.constData(), | |
182 | (guint)data.size()); | |
183 | ||
184 | GVariant *value = g_variant_new_from_data(var_type, var_data, | |
185 | data.size(), false, g_free, var_data); | |
186 | ||
187 | g_variant_type_free(var_type); | |
188 | ||
189 | return value; | |
190 | } | |
191 | ||
192 | void GlobalSettings::store_variantbase(QSettings &settings, Glib::VariantBase v) | |
193 | { | |
194 | const QByteArray var_data = QByteArray((const char*)v.get_data(), v.get_size()); | |
195 | ||
196 | settings.setValue("value", var_data); | |
197 | settings.setValue("type", QString::fromStdString(v.get_type_string())); | |
198 | } | |
199 | ||
200 | Glib::VariantBase GlobalSettings::restore_variantbase(QSettings &settings) | |
201 | { | |
202 | QString raw_type = settings.value("type").toString(); | |
203 | GVariantType *var_type = g_variant_type_new(raw_type.toUtf8()); | |
204 | ||
205 | QByteArray data = settings.value("value").toByteArray(); | |
206 | ||
207 | gpointer var_data = g_memdup((gconstpointer)data.constData(), | |
208 | (guint)data.size()); | |
209 | ||
210 | GVariant *value = g_variant_new_from_data(var_type, var_data, | |
211 | data.size(), false, g_free, var_data); | |
212 | ||
213 | Glib::VariantBase ret_val = Glib::VariantBase(value, true); | |
214 | ||
215 | g_variant_type_free(var_type); | |
216 | g_variant_unref(value); | |
217 | ||
218 | return ret_val; | |
219 | } | |
220 | ||
221 | } // namespace pv |