2 * This file is part of the PulseView project.
4 * Copyright (C) 2017 Soeren Apel <soeren@apelpie.net>
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.
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.
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/>.
20 #include "globalsettings.hpp"
22 #include <QApplication>
24 #include <QFontMetrics>
33 const QString GlobalSettings::Key_View_ZoomToFitDuringAcq = "View_ZoomToFitDuringAcq";
34 const QString GlobalSettings::Key_View_ZoomToFitAfterAcq = "View_ZoomToFitAfterAcq";
35 const QString GlobalSettings::Key_View_TriggerIsZeroTime = "View_TriggerIsZeroTime";
36 const QString GlobalSettings::Key_View_ColoredBG = "View_ColoredBG";
37 const QString GlobalSettings::Key_View_StickyScrolling = "View_StickyScrolling";
38 const QString GlobalSettings::Key_View_ShowSamplingPoints = "View_ShowSamplingPoints";
39 const QString GlobalSettings::Key_View_ShowAnalogMinorGrid = "View_ShowAnalogMinorGrid";
40 const QString GlobalSettings::Key_View_ConversionThresholdDispMode = "View_ConversionThresholdDispMode";
41 const QString GlobalSettings::Key_View_DefaultDivHeight = "View_DefaultDivHeight";
42 const QString GlobalSettings::Key_View_DefaultLogicHeight = "View_DefaultLogicHeight";
43 const QString GlobalSettings::Key_View_ShowHoverMarker = "View_ShowHoverMarker";
44 const QString GlobalSettings::Key_View_SnapDistance = "View_SnapDistance";
45 const QString GlobalSettings::Key_Dec_InitialStateConfigurable = "Dec_InitialStateConfigurable";
46 const QString GlobalSettings::Key_Dec_ExportFormat = "Dec_ExportFormat";
47 const QString GlobalSettings::Key_Log_BufferSize = "Log_BufferSize";
48 const QString GlobalSettings::Key_Log_NotifyOfStacktrace = "Log_NotifyOfStacktrace";
50 vector<GlobalSettingsInterface*> GlobalSettings::callbacks_;
51 bool GlobalSettings::tracking_ = false;
52 map<QString, QVariant> GlobalSettings::tracked_changes_;
54 GlobalSettings::GlobalSettings() :
57 beginGroup("Settings");
60 void GlobalSettings::set_defaults_where_needed()
62 // Enable zoom-to-fit after acquisition by default
63 if (!contains(Key_View_ZoomToFitAfterAcq))
64 setValue(Key_View_ZoomToFitAfterAcq, true);
66 // Enable colored trace backgrounds by default
67 if (!contains(Key_View_ColoredBG))
68 setValue(Key_View_ColoredBG, true);
70 // Enable showing sampling points by default
71 if (!contains(Key_View_ShowSamplingPoints))
72 setValue(Key_View_ShowSamplingPoints, true);
74 if (!contains(Key_View_DefaultDivHeight))
75 setValue(Key_View_DefaultDivHeight,
76 3 * QFontMetrics(QApplication::font()).height());
78 if (!contains(Key_View_DefaultLogicHeight))
79 setValue(Key_View_DefaultLogicHeight,
80 2 * QFontMetrics(QApplication::font()).height());
82 if (!contains(Key_View_SnapDistance))
83 setValue(Key_View_SnapDistance, 15);
85 if (!contains(Key_Dec_ExportFormat))
86 setValue(Key_Dec_ExportFormat, "%s %d: %c: %1");
88 // Default to 500 lines of backlog
89 if (!contains(Key_Log_BufferSize))
90 setValue(Key_Log_BufferSize, 500);
92 // Notify user of existing stack trace by default
93 if (!contains(Key_Log_NotifyOfStacktrace))
94 setValue(Key_Log_NotifyOfStacktrace, true);
97 void GlobalSettings::add_change_handler(GlobalSettingsInterface *cb)
99 callbacks_.push_back(cb);
102 void GlobalSettings::remove_change_handler(GlobalSettingsInterface *cb)
104 for (auto cb_it = callbacks_.begin(); cb_it != callbacks_.end(); cb_it++)
106 callbacks_.erase(cb_it);
111 void GlobalSettings::setValue(const QString &key, const QVariant &value)
113 // Save previous value if we're tracking changes,
114 // not altering an already-existing saved setting
116 tracked_changes_.emplace(key, QSettings::value(key));
118 QSettings::setValue(key, value);
120 // TODO Emulate noquote()
121 qDebug() << "Setting" << key << "changed to" << value;
123 // Call all registered callbacks
124 for (GlobalSettingsInterface *cb : callbacks_)
125 cb->on_setting_changed(key, value);
128 void GlobalSettings::start_tracking()
131 tracked_changes_.clear();
134 void GlobalSettings::stop_tracking()
137 tracked_changes_.clear();
140 void GlobalSettings::undo_tracked_changes()
144 for (auto entry : tracked_changes_)
145 setValue(entry.first, entry.second);
147 tracked_changes_.clear();
150 void GlobalSettings::store_gvariant(QSettings &settings, GVariant *v)
152 const GVariantType *var_type = g_variant_get_type(v);
153 char *var_type_str = g_variant_type_dup_string(var_type);
155 QByteArray var_data = QByteArray((const char*)g_variant_get_data(v),
156 g_variant_get_size(v));
158 settings.setValue("value", var_data);
159 settings.setValue("type", var_type_str);
161 g_free(var_type_str);
164 GVariant* GlobalSettings::restore_gvariant(QSettings &settings)
166 QString raw_type = settings.value("type").toString();
167 GVariantType *var_type = g_variant_type_new(raw_type.toUtf8());
169 QByteArray data = settings.value("value").toByteArray();
171 gpointer var_data = g_memdup((gconstpointer)data.constData(),
174 GVariant *value = g_variant_new_from_data(var_type, var_data,
175 data.size(), false, g_free, var_data);
177 g_variant_type_free(var_type);
182 void GlobalSettings::store_variantbase(QSettings &settings, Glib::VariantBase v)
184 const QByteArray var_data = QByteArray((const char*)v.get_data(), v.get_size());
186 settings.setValue("value", var_data);
187 settings.setValue("type", QString::fromStdString(v.get_type_string()));
190 Glib::VariantBase GlobalSettings::restore_variantbase(QSettings &settings)
192 QString raw_type = settings.value("type").toString();
193 GVariantType *var_type = g_variant_type_new(raw_type.toUtf8());
195 QByteArray data = settings.value("value").toByteArray();
197 gpointer var_data = g_memdup((gconstpointer)data.constData(),
200 GVariant *value = g_variant_new_from_data(var_type, var_data,
201 data.size(), false, g_free, var_data);
203 Glib::VariantBase ret_val = Glib::VariantBase(value, true);
205 g_variant_type_free(var_type);
206 g_variant_unref(value);