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>
32 const QString GlobalSettings::Key_View_ZoomToFitDuringAcq = "View_ZoomToFitDuringAcq";
33 const QString GlobalSettings::Key_View_ZoomToFitAfterAcq = "View_ZoomToFitAfterAcq";
34 const QString GlobalSettings::Key_View_TriggerIsZeroTime = "View_TriggerIsZeroTime";
35 const QString GlobalSettings::Key_View_ColoredBG = "View_ColoredBG";
36 const QString GlobalSettings::Key_View_StickyScrolling = "View_StickyScrolling";
37 const QString GlobalSettings::Key_View_ShowSamplingPoints = "View_ShowSamplingPoints";
38 const QString GlobalSettings::Key_View_ShowAnalogMinorGrid = "View_ShowAnalogMinorGrid";
39 const QString GlobalSettings::Key_View_ConversionThresholdDispMode = "View_ConversionThresholdDispMode";
40 const QString GlobalSettings::Key_View_DefaultDivHeight = "View_DefaultDivHeight";
41 const QString GlobalSettings::Key_View_DefaultLogicHeight = "View_DefaultLogicHeight";
42 const QString GlobalSettings::Key_Dec_InitialStateConfigurable = "Dec_InitialStateConfigurable";
43 const QString GlobalSettings::Key_Log_BufferSize = "Log_BufferSize";
44 const QString GlobalSettings::Key_Log_NotifyOfStacktrace = "Log_NotifyOfStacktrace";
46 vector<GlobalSettingsInterface*> GlobalSettings::callbacks_;
47 bool GlobalSettings::tracking_ = false;
48 map<QString, QVariant> GlobalSettings::tracked_changes_;
50 GlobalSettings::GlobalSettings() :
53 beginGroup("Settings");
56 void GlobalSettings::set_defaults_where_needed()
58 // Enable zoom-to-fit after acquisition by default
59 if (!contains(Key_View_ZoomToFitAfterAcq))
60 setValue(Key_View_ZoomToFitAfterAcq, true);
62 // Enable colored trace backgrounds by default
63 if (!contains(Key_View_ColoredBG))
64 setValue(Key_View_ColoredBG, true);
66 // Enable showing sampling points by default
67 if (!contains(Key_View_ShowSamplingPoints))
68 setValue(Key_View_ShowSamplingPoints, true);
70 if (!contains(Key_View_DefaultDivHeight))
71 setValue(Key_View_DefaultDivHeight,
72 3 * QFontMetrics(QApplication::font()).height());
74 if (!contains(Key_View_DefaultLogicHeight))
75 setValue(Key_View_DefaultLogicHeight,
76 2 * QFontMetrics(QApplication::font()).height());
78 // Default to 500 lines of backlog
79 if (!contains(Key_Log_BufferSize))
80 setValue(Key_Log_BufferSize, 500);
82 // Notify user of existing stack trace by default
83 if (!contains(Key_Log_NotifyOfStacktrace))
84 setValue(Key_Log_NotifyOfStacktrace, true);
87 void GlobalSettings::add_change_handler(GlobalSettingsInterface *cb)
89 callbacks_.push_back(cb);
92 void GlobalSettings::remove_change_handler(GlobalSettingsInterface *cb)
94 for (auto cb_it = callbacks_.begin(); cb_it != callbacks_.end(); cb_it++)
96 callbacks_.erase(cb_it);
101 void GlobalSettings::setValue(const QString &key, const QVariant &value)
103 // Save previous value if we're tracking changes,
104 // not altering an already-existing saved setting
106 tracked_changes_.emplace(key, QSettings::value(key));
108 QSettings::setValue(key, value);
110 // TODO Emulate noquote()
111 qDebug() << "Setting" << key << "changed to" << value;
113 // Call all registered callbacks
114 for (GlobalSettingsInterface *cb : callbacks_)
115 cb->on_setting_changed(key, value);
118 void GlobalSettings::start_tracking()
121 tracked_changes_.clear();
124 void GlobalSettings::stop_tracking()
127 tracked_changes_.clear();
130 void GlobalSettings::undo_tracked_changes()
134 for (auto entry : tracked_changes_)
135 setValue(entry.first, entry.second);
137 tracked_changes_.clear();
140 void GlobalSettings::store_gvariant(QSettings &settings, GVariant *v)
142 const GVariantType *var_type = g_variant_get_type(v);
143 char *var_type_str = g_variant_type_dup_string(var_type);
145 QByteArray var_data = QByteArray((const char*)g_variant_get_data(v),
146 g_variant_get_size(v));
148 settings.setValue("value", var_data);
149 settings.setValue("type", var_type_str);
151 g_free(var_type_str);
154 GVariant* GlobalSettings::restore_gvariant(QSettings &settings)
156 QString raw_type = settings.value("type").toString();
157 GVariantType *var_type = g_variant_type_new(raw_type.toUtf8());
159 QByteArray data = settings.value("value").toByteArray();
161 gpointer var_data = g_memdup((gconstpointer)data.constData(),
164 GVariant *value = g_variant_new_from_data(var_type, var_data,
165 data.size(), false, g_free, var_data);
167 g_variant_type_free(var_type);