]>
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 | ||
e887fe9e SA |
20 | #include <boost/archive/text_iarchive.hpp> |
21 | #include <boost/archive/text_oarchive.hpp> | |
22 | #include <boost/serialization/serialization.hpp> | |
bf9f1268 | 23 | |
daa54986 | 24 | #include <QApplication> |
4521022b | 25 | #include <QColor> |
7ba25e4f | 26 | #include <QDebug> |
37b0bd35 | 27 | #include <QFile> |
daa54986 | 28 | #include <QFontMetrics> |
37b0bd35 | 29 | #include <QPixmapCache> |
d3feec23 | 30 | #include <QString> |
374c697f | 31 | #include <QStyle> |
43be386b | 32 | #include <QtGlobal> |
d3feec23 | 33 | |
e887fe9e SA |
34 | #include "globalsettings.hpp" |
35 | #include "application.hpp" | |
36 | ||
6f925ba9 | 37 | using std::map; |
37b0bd35 | 38 | using std::pair; |
1325ce42 | 39 | using std::string; |
e887fe9e | 40 | using std::stringstream; |
d0c0573b | 41 | using std::vector; |
6f925ba9 | 42 | |
bf9f1268 SA |
43 | namespace pv { |
44 | ||
37b0bd35 SA |
45 | const vector< pair<QString, QString> > Themes { |
46 | {"None" , ""}, | |
47 | {"QDarkStyleSheet", ":/themes/qdarkstyle/style.qss"}, | |
48 | {"DarkStyle", ":/themes/darkstyle/darkstyle.qss"} | |
49 | }; | |
50 | ||
0466001b | 51 | const QString GlobalSettings::Key_General_Language = "General_Language"; |
37b0bd35 | 52 | const QString GlobalSettings::Key_General_Theme = "General_Theme"; |
374c697f | 53 | const QString GlobalSettings::Key_General_Style = "General_Style"; |
8962d7b3 | 54 | const QString GlobalSettings::Key_General_SaveWithSetup = "General_SaveWithSetup"; |
e91fb166 | 55 | const QString GlobalSettings::Key_View_ZoomToFitDuringAcq = "View_ZoomToFitDuringAcq"; |
28ceff25 | 56 | const QString GlobalSettings::Key_View_ZoomToFitAfterAcq = "View_ZoomToFitAfterAcq"; |
ffc00fdd | 57 | const QString GlobalSettings::Key_View_TriggerIsZeroTime = "View_TriggerIsZeroTime"; |
641574bc | 58 | const QString GlobalSettings::Key_View_ColoredBG = "View_ColoredBG"; |
87a97d8a | 59 | const QString GlobalSettings::Key_View_StickyScrolling = "View_StickyScrolling"; |
051ba3b3 | 60 | const QString GlobalSettings::Key_View_ShowSamplingPoints = "View_ShowSamplingPoints"; |
4521022b SA |
61 | const QString GlobalSettings::Key_View_FillSignalHighAreas = "View_FillSignalHighAreas"; |
62 | const QString GlobalSettings::Key_View_FillSignalHighAreaColor = "View_FillSignalHighAreaColor"; | |
8ad61f40 | 63 | const QString GlobalSettings::Key_View_ShowAnalogMinorGrid = "View_ShowAnalogMinorGrid"; |
90ee1ed9 | 64 | const QString GlobalSettings::Key_View_ConversionThresholdDispMode = "View_ConversionThresholdDispMode"; |
daa54986 | 65 | const QString GlobalSettings::Key_View_DefaultDivHeight = "View_DefaultDivHeight"; |
48051ccb | 66 | const QString GlobalSettings::Key_View_DefaultLogicHeight = "View_DefaultLogicHeight"; |
1931b5f9 | 67 | const QString GlobalSettings::Key_View_ShowHoverMarker = "View_ShowHoverMarker"; |
fb641801 | 68 | const QString GlobalSettings::Key_View_SnapDistance = "View_SnapDistance"; |
c04f5a29 | 69 | const QString GlobalSettings::Key_View_CursorFillColor = "View_CursorFillColor"; |
79a37ed8 MM |
70 | const QString GlobalSettings::Key_View_CursorShowFrequency = "View_CursorShowFrequency"; |
71 | const QString GlobalSettings::Key_View_CursorShowInterval = "View_CursorShowInterval"; | |
a3c57f44 | 72 | const QString GlobalSettings::Key_View_CursorShowSamples = "View_CursorShowSamples"; |
1cc1c8de | 73 | const QString GlobalSettings::Key_Dec_InitialStateConfigurable = "Dec_InitialStateConfigurable"; |
1ed996b4 | 74 | const QString GlobalSettings::Key_Dec_ExportFormat = "Dec_ExportFormat"; |
ab185f78 | 75 | const QString GlobalSettings::Key_Dec_AlwaysShowAllRows = "Dec_AlwaysShowAllRows"; |
bcb4c327 | 76 | const QString GlobalSettings::Key_Log_BufferSize = "Log_BufferSize"; |
c5d6200c | 77 | const QString GlobalSettings::Key_Log_NotifyOfStacktrace = "Log_NotifyOfStacktrace"; |
bf9f1268 | 78 | |
d0c0573b | 79 | vector<GlobalSettingsInterface*> GlobalSettings::callbacks_; |
2cca9ebf | 80 | bool GlobalSettings::tracking_ = false; |
c6b4e925 | 81 | bool GlobalSettings::is_dark_theme_ = false; |
6f925ba9 | 82 | map<QString, QVariant> GlobalSettings::tracked_changes_; |
374c697f | 83 | QString GlobalSettings::default_style_; |
37b0bd35 | 84 | QPalette GlobalSettings::default_palette_; |
bf9f1268 SA |
85 | |
86 | GlobalSettings::GlobalSettings() : | |
c6b4e925 | 87 | QSettings() |
bf9f1268 SA |
88 | { |
89 | beginGroup("Settings"); | |
90 | } | |
91 | ||
a42d2514 SA |
92 | void GlobalSettings::save_internal_defaults() |
93 | { | |
94 | default_style_ = qApp->style()->objectName(); | |
95 | if (default_style_.isEmpty()) | |
96 | default_style_ = "fusion"; | |
97 | ||
98 | default_palette_ = QApplication::palette(); | |
99 | } | |
100 | ||
c031de4b SA |
101 | void GlobalSettings::set_defaults_where_needed() |
102 | { | |
0466001b SA |
103 | if (!contains(Key_General_Language)) { |
104 | // Determine and set default UI language | |
105 | QString language = QLocale().uiLanguages().first(); // May return e.g. en-Latn-US | |
106 | language = language.split("-").first(); | |
107 | ||
108 | setValue(Key_General_Language, language); | |
d8036995 | 109 | apply_language(); |
0466001b SA |
110 | } |
111 | ||
37b0bd35 SA |
112 | // Use no theme by default |
113 | if (!contains(Key_General_Theme)) | |
114 | setValue(Key_General_Theme, 0); | |
374c697f SA |
115 | if (!contains(Key_General_Style)) |
116 | setValue(Key_General_Style, ""); | |
37b0bd35 | 117 | |
8962d7b3 SA |
118 | // Save setup with .sr files by default |
119 | if (!contains(Key_General_SaveWithSetup)) | |
120 | setValue(Key_General_SaveWithSetup, true); | |
121 | ||
28ceff25 SA |
122 | // Enable zoom-to-fit after acquisition by default |
123 | if (!contains(Key_View_ZoomToFitAfterAcq)) | |
124 | setValue(Key_View_ZoomToFitAfterAcq, true); | |
125 | ||
641574bc SA |
126 | // Enable colored trace backgrounds by default |
127 | if (!contains(Key_View_ColoredBG)) | |
128 | setValue(Key_View_ColoredBG, true); | |
df842d4f SA |
129 | |
130 | // Enable showing sampling points by default | |
131 | if (!contains(Key_View_ShowSamplingPoints)) | |
132 | setValue(Key_View_ShowSamplingPoints, true); | |
daa54986 | 133 | |
4521022b SA |
134 | // Enable filling logic signal high areas by default |
135 | if (!contains(Key_View_FillSignalHighAreas)) | |
136 | setValue(Key_View_FillSignalHighAreas, true); | |
4521022b | 137 | |
daa54986 SA |
138 | if (!contains(Key_View_DefaultDivHeight)) |
139 | setValue(Key_View_DefaultDivHeight, | |
140 | 3 * QFontMetrics(QApplication::font()).height()); | |
48051ccb SA |
141 | |
142 | if (!contains(Key_View_DefaultLogicHeight)) | |
143 | setValue(Key_View_DefaultLogicHeight, | |
144 | 2 * QFontMetrics(QApplication::font()).height()); | |
bcb4c327 | 145 | |
6fff12ac SA |
146 | if (!contains(Key_View_ShowHoverMarker)) |
147 | setValue(Key_View_ShowHoverMarker, true); | |
148 | ||
fb641801 SA |
149 | if (!contains(Key_View_SnapDistance)) |
150 | setValue(Key_View_SnapDistance, 15); | |
151 | ||
79a37ed8 MM |
152 | if (!contains(Key_View_CursorShowInterval)) |
153 | setValue(Key_View_CursorShowInterval, true); | |
154 | ||
155 | if (!contains(Key_View_CursorShowFrequency)) | |
156 | setValue(Key_View_CursorShowFrequency, true); | |
157 | ||
6a26fc44 SA |
158 | // %c was used for the row name in the past so we need to transition such users |
159 | if (!contains(Key_Dec_ExportFormat) || | |
160 | value(Key_Dec_ExportFormat).toString() == "%s %d: %c: %1") | |
161 | setValue(Key_Dec_ExportFormat, "%s %d: %r: %1"); | |
1ed996b4 | 162 | |
bcb4c327 SA |
163 | // Default to 500 lines of backlog |
164 | if (!contains(Key_Log_BufferSize)) | |
165 | setValue(Key_Log_BufferSize, 500); | |
c5d6200c SA |
166 | |
167 | // Notify user of existing stack trace by default | |
168 | if (!contains(Key_Log_NotifyOfStacktrace)) | |
169 | setValue(Key_Log_NotifyOfStacktrace, true); | |
a42d2514 | 170 | |
c04f5a29 SA |
171 | // Default theme is bright, so use its color scheme if undefined |
172 | if (!contains(Key_View_CursorFillColor)) | |
173 | set_bright_theme_default_colors(); | |
c031de4b SA |
174 | } |
175 | ||
a42d2514 | 176 | void GlobalSettings::set_bright_theme_default_colors() |
37b0bd35 | 177 | { |
a42d2514 SA |
178 | setValue(Key_View_FillSignalHighAreaColor, |
179 | QColor(0, 0, 0, 5 * 256 / 100).rgba()); | |
c04f5a29 SA |
180 | |
181 | setValue(Key_View_CursorFillColor, | |
182 | QColor(220, 231, 243).rgba()); | |
a42d2514 | 183 | } |
374c697f | 184 | |
a42d2514 SA |
185 | void GlobalSettings::set_dark_theme_default_colors() |
186 | { | |
187 | setValue(Key_View_FillSignalHighAreaColor, | |
188 | QColor(188, 188, 188, 9 * 256 / 100).rgba()); | |
c04f5a29 SA |
189 | |
190 | setValue(Key_View_CursorFillColor, | |
191 | QColor(60, 60, 60).rgba()); | |
a42d2514 SA |
192 | } |
193 | ||
194 | bool GlobalSettings::current_theme_is_dark() | |
195 | { | |
196 | return is_dark_theme_; | |
37b0bd35 SA |
197 | } |
198 | ||
199 | void GlobalSettings::apply_theme() | |
200 | { | |
201 | QString theme_name = Themes.at(value(Key_General_Theme).toInt()).first; | |
202 | QString resource_name = Themes.at(value(Key_General_Theme).toInt()).second; | |
203 | ||
204 | if (!resource_name.isEmpty()) { | |
205 | QFile file(resource_name); | |
206 | file.open(QFile::ReadOnly | QFile::Text); | |
207 | qApp->setStyleSheet(file.readAll()); | |
208 | } else | |
209 | qApp->setStyleSheet(""); | |
210 | ||
211 | qApp->setPalette(default_palette_); | |
212 | ||
374c697f SA |
213 | const QString style = value(Key_General_Style).toString(); |
214 | if (style.isEmpty()) | |
215 | qApp->setStyle(default_style_); | |
216 | else | |
217 | qApp->setStyle(style); | |
218 | ||
a42d2514 SA |
219 | is_dark_theme_ = false; |
220 | ||
37b0bd35 SA |
221 | if (theme_name.compare("QDarkStyleSheet") == 0) { |
222 | QPalette dark_palette; | |
223 | dark_palette.setColor(QPalette::Window, QColor(53, 53, 53)); | |
224 | dark_palette.setColor(QPalette::WindowText, Qt::white); | |
225 | dark_palette.setColor(QPalette::Base, QColor(42, 42, 42)); | |
226 | dark_palette.setColor(QPalette::Dark, QColor(35, 35, 35)); | |
227 | dark_palette.setColor(QPalette::Highlight, QColor(42, 130, 218)); | |
228 | qApp->setPalette(dark_palette); | |
a42d2514 | 229 | is_dark_theme_ = true; |
37b0bd35 SA |
230 | } else if (theme_name.compare("DarkStyle") == 0) { |
231 | QPalette dark_palette; | |
232 | dark_palette.setColor(QPalette::Window, QColor(53, 53, 53)); | |
233 | dark_palette.setColor(QPalette::WindowText, Qt::white); | |
234 | dark_palette.setColor(QPalette::Disabled, QPalette::WindowText, QColor(127, 127, 127)); | |
235 | dark_palette.setColor(QPalette::Base, QColor(42, 42, 42)); | |
236 | dark_palette.setColor(QPalette::AlternateBase, QColor(66, 66, 66)); | |
237 | dark_palette.setColor(QPalette::ToolTipBase, Qt::white); | |
238 | dark_palette.setColor(QPalette::ToolTipText, QColor(53, 53, 53)); | |
239 | dark_palette.setColor(QPalette::Text, Qt::white); | |
240 | dark_palette.setColor(QPalette::Disabled, QPalette::Text, QColor(127, 127, 127)); | |
241 | dark_palette.setColor(QPalette::Dark, QColor(35, 35, 35)); | |
242 | dark_palette.setColor(QPalette::Shadow, QColor(20, 20, 20)); | |
243 | dark_palette.setColor(QPalette::Button, QColor(53, 53, 53)); | |
244 | dark_palette.setColor(QPalette::ButtonText, Qt::white); | |
245 | dark_palette.setColor(QPalette::Disabled, QPalette::ButtonText, QColor(127, 127, 127)); | |
246 | dark_palette.setColor(QPalette::BrightText, Qt::red); | |
247 | dark_palette.setColor(QPalette::Link, QColor(42, 130, 218)); | |
248 | dark_palette.setColor(QPalette::Highlight, QColor(42, 130, 218)); | |
249 | dark_palette.setColor(QPalette::Disabled, QPalette::Highlight, QColor(80, 80, 80)); | |
250 | dark_palette.setColor(QPalette::HighlightedText, Qt::white); | |
251 | dark_palette.setColor(QPalette::Disabled, QPalette::HighlightedText, QColor(127, 127, 127)); | |
252 | qApp->setPalette(dark_palette); | |
a42d2514 | 253 | is_dark_theme_ = true; |
37b0bd35 SA |
254 | } |
255 | ||
256 | QPixmapCache::clear(); | |
257 | } | |
258 | ||
0466001b SA |
259 | void GlobalSettings::apply_language() |
260 | { | |
261 | Application* a = qobject_cast<Application*>(QApplication::instance()); | |
262 | a->switch_language(value(Key_General_Language).toString()); | |
263 | } | |
264 | ||
d0c0573b | 265 | void GlobalSettings::add_change_handler(GlobalSettingsInterface *cb) |
bf9f1268 | 266 | { |
d0c0573b SA |
267 | callbacks_.push_back(cb); |
268 | } | |
269 | ||
270 | void GlobalSettings::remove_change_handler(GlobalSettingsInterface *cb) | |
271 | { | |
272 | for (auto cb_it = callbacks_.begin(); cb_it != callbacks_.end(); cb_it++) | |
273 | if (*cb_it == cb) { | |
274 | callbacks_.erase(cb_it); | |
275 | break; | |
276 | } | |
bf9f1268 SA |
277 | } |
278 | ||
279 | void GlobalSettings::setValue(const QString &key, const QVariant &value) | |
280 | { | |
2cca9ebf SA |
281 | // Save previous value if we're tracking changes, |
282 | // not altering an already-existing saved setting | |
283 | if (tracking_) | |
284 | tracked_changes_.emplace(key, QSettings::value(key)); | |
285 | ||
bf9f1268 SA |
286 | QSettings::setValue(key, value); |
287 | ||
fe060a48 SA |
288 | // TODO Emulate noquote() |
289 | qDebug() << "Setting" << key << "changed to" << value; | |
7ba25e4f | 290 | |
d0c0573b SA |
291 | // Call all registered callbacks |
292 | for (GlobalSettingsInterface *cb : callbacks_) | |
293 | cb->on_setting_changed(key, value); | |
bf9f1268 SA |
294 | } |
295 | ||
2cca9ebf SA |
296 | void GlobalSettings::start_tracking() |
297 | { | |
298 | tracking_ = true; | |
299 | tracked_changes_.clear(); | |
300 | } | |
301 | ||
302 | void GlobalSettings::stop_tracking() | |
303 | { | |
304 | tracking_ = false; | |
305 | tracked_changes_.clear(); | |
306 | } | |
307 | ||
308 | void GlobalSettings::undo_tracked_changes() | |
309 | { | |
310 | tracking_ = false; | |
311 | ||
f4ab4b5c | 312 | for (auto& entry : tracked_changes_) |
2cca9ebf SA |
313 | setValue(entry.first, entry.second); |
314 | ||
315 | tracked_changes_.clear(); | |
316 | } | |
bf9f1268 | 317 | |
d3feec23 SA |
318 | void GlobalSettings::store_gvariant(QSettings &settings, GVariant *v) |
319 | { | |
320 | const GVariantType *var_type = g_variant_get_type(v); | |
321 | char *var_type_str = g_variant_type_dup_string(var_type); | |
322 | ||
323 | QByteArray var_data = QByteArray((const char*)g_variant_get_data(v), | |
324 | g_variant_get_size(v)); | |
325 | ||
326 | settings.setValue("value", var_data); | |
327 | settings.setValue("type", var_type_str); | |
328 | ||
329 | g_free(var_type_str); | |
330 | } | |
331 | ||
332 | GVariant* GlobalSettings::restore_gvariant(QSettings &settings) | |
333 | { | |
334 | QString raw_type = settings.value("type").toString(); | |
335 | GVariantType *var_type = g_variant_type_new(raw_type.toUtf8()); | |
336 | ||
337 | QByteArray data = settings.value("value").toByteArray(); | |
338 | ||
339 | gpointer var_data = g_memdup((gconstpointer)data.constData(), | |
340 | (guint)data.size()); | |
341 | ||
342 | GVariant *value = g_variant_new_from_data(var_type, var_data, | |
343 | data.size(), false, g_free, var_data); | |
344 | ||
345 | g_variant_type_free(var_type); | |
346 | ||
347 | return value; | |
348 | } | |
349 | ||
1325ce42 SA |
350 | void GlobalSettings::store_variantbase(QSettings &settings, Glib::VariantBase v) |
351 | { | |
352 | const QByteArray var_data = QByteArray((const char*)v.get_data(), v.get_size()); | |
353 | ||
354 | settings.setValue("value", var_data); | |
355 | settings.setValue("type", QString::fromStdString(v.get_type_string())); | |
356 | } | |
357 | ||
358 | Glib::VariantBase GlobalSettings::restore_variantbase(QSettings &settings) | |
359 | { | |
360 | QString raw_type = settings.value("type").toString(); | |
361 | GVariantType *var_type = g_variant_type_new(raw_type.toUtf8()); | |
362 | ||
363 | QByteArray data = settings.value("value").toByteArray(); | |
364 | ||
365 | gpointer var_data = g_memdup((gconstpointer)data.constData(), | |
366 | (guint)data.size()); | |
367 | ||
368 | GVariant *value = g_variant_new_from_data(var_type, var_data, | |
369 | data.size(), false, g_free, var_data); | |
370 | ||
371 | Glib::VariantBase ret_val = Glib::VariantBase(value, true); | |
372 | ||
373 | g_variant_type_free(var_type); | |
374 | g_variant_unref(value); | |
375 | ||
376 | return ret_val; | |
377 | } | |
d3feec23 | 378 | |
e887fe9e SA |
379 | void GlobalSettings::store_timestamp(QSettings &settings, const char *name, const pv::util::Timestamp &ts) |
380 | { | |
381 | stringstream ss; | |
382 | boost::archive::text_oarchive oa(ss); | |
383 | oa << boost::serialization::make_nvp(name, ts); | |
384 | settings.setValue(name, QString::fromStdString(ss.str())); | |
385 | } | |
386 | ||
387 | pv::util::Timestamp GlobalSettings::restore_timestamp(QSettings &settings, const char *name) | |
388 | { | |
389 | util::Timestamp result; | |
390 | stringstream ss; | |
391 | ss << settings.value(name).toString().toStdString(); | |
392 | ||
393 | try { | |
394 | boost::archive::text_iarchive ia(ss); | |
395 | ia >> boost::serialization::make_nvp(name, result); | |
396 | } catch (boost::archive::archive_exception&) { | |
397 | qDebug() << "Could not restore setting" << name; | |
398 | } | |
399 | ||
400 | return result; | |
401 | } | |
402 | ||
bf9f1268 | 403 | } // namespace pv |