]>
Commit | Line | Data |
---|---|---|
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 "globalsettings.hpp" | |
21 | ||
22 | #include <QApplication> | |
23 | #include <QDebug> | |
24 | #include <QFontMetrics> | |
25 | #include <QString> | |
26 | ||
27 | using std::map; | |
28 | using std::vector; | |
29 | ||
30 | namespace pv { | |
31 | ||
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_ColouredBG = "View_ColouredBG"; | |
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"; | |
45 | ||
46 | vector<GlobalSettingsInterface*> GlobalSettings::callbacks_; | |
47 | bool GlobalSettings::tracking_ = false; | |
48 | map<QString, QVariant> GlobalSettings::tracked_changes_; | |
49 | ||
50 | GlobalSettings::GlobalSettings() : | |
51 | QSettings() | |
52 | { | |
53 | beginGroup("Settings"); | |
54 | } | |
55 | ||
56 | void GlobalSettings::set_defaults_where_needed() | |
57 | { | |
58 | // Enable zoom-to-fit after acquisition by default | |
59 | if (!contains(Key_View_ZoomToFitAfterAcq)) | |
60 | setValue(Key_View_ZoomToFitAfterAcq, true); | |
61 | ||
62 | // Enable coloured trace backgrounds by default | |
63 | if (!contains(Key_View_ColouredBG)) | |
64 | setValue(Key_View_ColouredBG, true); | |
65 | ||
66 | // Enable showing sampling points by default | |
67 | if (!contains(Key_View_ShowSamplingPoints)) | |
68 | setValue(Key_View_ShowSamplingPoints, true); | |
69 | ||
70 | if (!contains(Key_View_DefaultDivHeight)) | |
71 | setValue(Key_View_DefaultDivHeight, | |
72 | 3 * QFontMetrics(QApplication::font()).height()); | |
73 | ||
74 | if (!contains(Key_View_DefaultLogicHeight)) | |
75 | setValue(Key_View_DefaultLogicHeight, | |
76 | 2 * QFontMetrics(QApplication::font()).height()); | |
77 | ||
78 | // Default to 500 lines of backlog | |
79 | if (!contains(Key_Log_BufferSize)) | |
80 | setValue(Key_Log_BufferSize, 500); | |
81 | ||
82 | // Notify user of existing stack trace by default | |
83 | if (!contains(Key_Log_NotifyOfStacktrace)) | |
84 | setValue(Key_Log_NotifyOfStacktrace, true); | |
85 | } | |
86 | ||
87 | void GlobalSettings::add_change_handler(GlobalSettingsInterface *cb) | |
88 | { | |
89 | callbacks_.push_back(cb); | |
90 | } | |
91 | ||
92 | void GlobalSettings::remove_change_handler(GlobalSettingsInterface *cb) | |
93 | { | |
94 | for (auto cb_it = callbacks_.begin(); cb_it != callbacks_.end(); cb_it++) | |
95 | if (*cb_it == cb) { | |
96 | callbacks_.erase(cb_it); | |
97 | break; | |
98 | } | |
99 | } | |
100 | ||
101 | void GlobalSettings::setValue(const QString &key, const QVariant &value) | |
102 | { | |
103 | // Save previous value if we're tracking changes, | |
104 | // not altering an already-existing saved setting | |
105 | if (tracking_) | |
106 | tracked_changes_.emplace(key, QSettings::value(key)); | |
107 | ||
108 | QSettings::setValue(key, value); | |
109 | ||
110 | qDebug().noquote() << "Setting" << key << "changed to" << value; | |
111 | ||
112 | // Call all registered callbacks | |
113 | for (GlobalSettingsInterface *cb : callbacks_) | |
114 | cb->on_setting_changed(key, value); | |
115 | } | |
116 | ||
117 | void GlobalSettings::start_tracking() | |
118 | { | |
119 | tracking_ = true; | |
120 | tracked_changes_.clear(); | |
121 | } | |
122 | ||
123 | void GlobalSettings::stop_tracking() | |
124 | { | |
125 | tracking_ = false; | |
126 | tracked_changes_.clear(); | |
127 | } | |
128 | ||
129 | void GlobalSettings::undo_tracked_changes() | |
130 | { | |
131 | tracking_ = false; | |
132 | ||
133 | for (auto entry : tracked_changes_) | |
134 | setValue(entry.first, entry.second); | |
135 | ||
136 | tracked_changes_.clear(); | |
137 | } | |
138 | ||
139 | void GlobalSettings::store_gvariant(QSettings &settings, GVariant *v) | |
140 | { | |
141 | const GVariantType *var_type = g_variant_get_type(v); | |
142 | char *var_type_str = g_variant_type_dup_string(var_type); | |
143 | ||
144 | QByteArray var_data = QByteArray((const char*)g_variant_get_data(v), | |
145 | g_variant_get_size(v)); | |
146 | ||
147 | settings.setValue("value", var_data); | |
148 | settings.setValue("type", var_type_str); | |
149 | ||
150 | g_free(var_type_str); | |
151 | } | |
152 | ||
153 | GVariant* GlobalSettings::restore_gvariant(QSettings &settings) | |
154 | { | |
155 | QString raw_type = settings.value("type").toString(); | |
156 | GVariantType *var_type = g_variant_type_new(raw_type.toUtf8()); | |
157 | ||
158 | QByteArray data = settings.value("value").toByteArray(); | |
159 | ||
160 | gpointer var_data = g_memdup((gconstpointer)data.constData(), | |
161 | (guint)data.size()); | |
162 | ||
163 | GVariant *value = g_variant_new_from_data(var_type, var_data, | |
164 | data.size(), false, g_free, var_data); | |
165 | ||
166 | g_variant_type_free(var_type); | |
167 | ||
168 | return value; | |
169 | } | |
170 | ||
171 | ||
172 | } // namespace pv |