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