]>
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 | ||
daa54986 | 22 | #include <QApplication> |
4521022b | 23 | #include <QColor> |
7ba25e4f | 24 | #include <QDebug> |
daa54986 | 25 | #include <QFontMetrics> |
d3feec23 SA |
26 | #include <QString> |
27 | ||
6f925ba9 | 28 | using std::map; |
1325ce42 | 29 | using std::string; |
d0c0573b | 30 | using std::vector; |
6f925ba9 | 31 | |
bf9f1268 SA |
32 | namespace pv { |
33 | ||
e91fb166 | 34 | const QString GlobalSettings::Key_View_ZoomToFitDuringAcq = "View_ZoomToFitDuringAcq"; |
28ceff25 | 35 | const QString GlobalSettings::Key_View_ZoomToFitAfterAcq = "View_ZoomToFitAfterAcq"; |
ffc00fdd | 36 | const QString GlobalSettings::Key_View_TriggerIsZeroTime = "View_TriggerIsZeroTime"; |
641574bc | 37 | const QString GlobalSettings::Key_View_ColoredBG = "View_ColoredBG"; |
87a97d8a | 38 | const QString GlobalSettings::Key_View_StickyScrolling = "View_StickyScrolling"; |
051ba3b3 | 39 | const QString GlobalSettings::Key_View_ShowSamplingPoints = "View_ShowSamplingPoints"; |
4521022b SA |
40 | const QString GlobalSettings::Key_View_FillSignalHighAreas = "View_FillSignalHighAreas"; |
41 | const QString GlobalSettings::Key_View_FillSignalHighAreaColor = "View_FillSignalHighAreaColor"; | |
8ad61f40 | 42 | const QString GlobalSettings::Key_View_ShowAnalogMinorGrid = "View_ShowAnalogMinorGrid"; |
90ee1ed9 | 43 | const QString GlobalSettings::Key_View_ConversionThresholdDispMode = "View_ConversionThresholdDispMode"; |
daa54986 | 44 | const QString GlobalSettings::Key_View_DefaultDivHeight = "View_DefaultDivHeight"; |
48051ccb | 45 | const QString GlobalSettings::Key_View_DefaultLogicHeight = "View_DefaultLogicHeight"; |
1931b5f9 | 46 | const QString GlobalSettings::Key_View_ShowHoverMarker = "View_ShowHoverMarker"; |
fb641801 | 47 | const QString GlobalSettings::Key_View_SnapDistance = "View_SnapDistance"; |
1cc1c8de | 48 | const QString GlobalSettings::Key_Dec_InitialStateConfigurable = "Dec_InitialStateConfigurable"; |
1ed996b4 | 49 | const QString GlobalSettings::Key_Dec_ExportFormat = "Dec_ExportFormat"; |
bcb4c327 | 50 | const QString GlobalSettings::Key_Log_BufferSize = "Log_BufferSize"; |
c5d6200c | 51 | const QString GlobalSettings::Key_Log_NotifyOfStacktrace = "Log_NotifyOfStacktrace"; |
bf9f1268 | 52 | |
d0c0573b | 53 | vector<GlobalSettingsInterface*> GlobalSettings::callbacks_; |
2cca9ebf | 54 | bool GlobalSettings::tracking_ = false; |
6f925ba9 | 55 | map<QString, QVariant> GlobalSettings::tracked_changes_; |
bf9f1268 SA |
56 | |
57 | GlobalSettings::GlobalSettings() : | |
58 | QSettings() | |
59 | { | |
60 | beginGroup("Settings"); | |
61 | } | |
62 | ||
c031de4b SA |
63 | void GlobalSettings::set_defaults_where_needed() |
64 | { | |
28ceff25 SA |
65 | // Enable zoom-to-fit after acquisition by default |
66 | if (!contains(Key_View_ZoomToFitAfterAcq)) | |
67 | setValue(Key_View_ZoomToFitAfterAcq, true); | |
68 | ||
641574bc SA |
69 | // Enable colored trace backgrounds by default |
70 | if (!contains(Key_View_ColoredBG)) | |
71 | setValue(Key_View_ColoredBG, true); | |
df842d4f SA |
72 | |
73 | // Enable showing sampling points by default | |
74 | if (!contains(Key_View_ShowSamplingPoints)) | |
75 | setValue(Key_View_ShowSamplingPoints, true); | |
daa54986 | 76 | |
4521022b SA |
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 | ||
daa54986 SA |
84 | if (!contains(Key_View_DefaultDivHeight)) |
85 | setValue(Key_View_DefaultDivHeight, | |
86 | 3 * QFontMetrics(QApplication::font()).height()); | |
48051ccb SA |
87 | |
88 | if (!contains(Key_View_DefaultLogicHeight)) | |
89 | setValue(Key_View_DefaultLogicHeight, | |
90 | 2 * QFontMetrics(QApplication::font()).height()); | |
bcb4c327 | 91 | |
6fff12ac SA |
92 | if (!contains(Key_View_ShowHoverMarker)) |
93 | setValue(Key_View_ShowHoverMarker, true); | |
94 | ||
fb641801 SA |
95 | if (!contains(Key_View_SnapDistance)) |
96 | setValue(Key_View_SnapDistance, 15); | |
97 | ||
1ed996b4 SA |
98 | if (!contains(Key_Dec_ExportFormat)) |
99 | setValue(Key_Dec_ExportFormat, "%s %d: %c: %1"); | |
100 | ||
bcb4c327 SA |
101 | // Default to 500 lines of backlog |
102 | if (!contains(Key_Log_BufferSize)) | |
103 | setValue(Key_Log_BufferSize, 500); | |
c5d6200c SA |
104 | |
105 | // Notify user of existing stack trace by default | |
106 | if (!contains(Key_Log_NotifyOfStacktrace)) | |
107 | setValue(Key_Log_NotifyOfStacktrace, true); | |
c031de4b SA |
108 | } |
109 | ||
d0c0573b | 110 | void GlobalSettings::add_change_handler(GlobalSettingsInterface *cb) |
bf9f1268 | 111 | { |
d0c0573b SA |
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 | } | |
bf9f1268 SA |
122 | } |
123 | ||
124 | void GlobalSettings::setValue(const QString &key, const QVariant &value) | |
125 | { | |
2cca9ebf SA |
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 | ||
bf9f1268 SA |
131 | QSettings::setValue(key, value); |
132 | ||
fe060a48 SA |
133 | // TODO Emulate noquote() |
134 | qDebug() << "Setting" << key << "changed to" << value; | |
7ba25e4f | 135 | |
d0c0573b SA |
136 | // Call all registered callbacks |
137 | for (GlobalSettingsInterface *cb : callbacks_) | |
138 | cb->on_setting_changed(key, value); | |
bf9f1268 SA |
139 | } |
140 | ||
2cca9ebf SA |
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 | } | |
bf9f1268 | 162 | |
d3feec23 SA |
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 | ||
1325ce42 SA |
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 | } | |
d3feec23 | 223 | |
bf9f1268 | 224 | } // namespace pv |