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