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