]>
Commit | Line | Data |
---|---|---|
bf9f1268 SA |
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 | ||
daa54986 | 22 | #include <QApplication> |
7ba25e4f | 23 | #include <QDebug> |
daa54986 | 24 | #include <QFontMetrics> |
d3feec23 SA |
25 | #include <QString> |
26 | ||
6f925ba9 | 27 | using std::map; |
d0c0573b | 28 | using std::vector; |
6f925ba9 | 29 | |
bf9f1268 SA |
30 | namespace pv { |
31 | ||
e91fb166 | 32 | const QString GlobalSettings::Key_View_ZoomToFitDuringAcq = "View_ZoomToFitDuringAcq"; |
28ceff25 | 33 | const QString GlobalSettings::Key_View_ZoomToFitAfterAcq = "View_ZoomToFitAfterAcq"; |
ffc00fdd | 34 | const QString GlobalSettings::Key_View_TriggerIsZeroTime = "View_TriggerIsZeroTime"; |
641574bc | 35 | const QString GlobalSettings::Key_View_ColoredBG = "View_ColoredBG"; |
87a97d8a | 36 | const QString GlobalSettings::Key_View_StickyScrolling = "View_StickyScrolling"; |
051ba3b3 | 37 | const QString GlobalSettings::Key_View_ShowSamplingPoints = "View_ShowSamplingPoints"; |
8ad61f40 | 38 | const QString GlobalSettings::Key_View_ShowAnalogMinorGrid = "View_ShowAnalogMinorGrid"; |
90ee1ed9 | 39 | const QString GlobalSettings::Key_View_ConversionThresholdDispMode = "View_ConversionThresholdDispMode"; |
daa54986 | 40 | const QString GlobalSettings::Key_View_DefaultDivHeight = "View_DefaultDivHeight"; |
48051ccb | 41 | const QString GlobalSettings::Key_View_DefaultLogicHeight = "View_DefaultLogicHeight"; |
1cc1c8de | 42 | const QString GlobalSettings::Key_Dec_InitialStateConfigurable = "Dec_InitialStateConfigurable"; |
bcb4c327 | 43 | const QString GlobalSettings::Key_Log_BufferSize = "Log_BufferSize"; |
c5d6200c | 44 | const QString GlobalSettings::Key_Log_NotifyOfStacktrace = "Log_NotifyOfStacktrace"; |
bf9f1268 | 45 | |
d0c0573b | 46 | vector<GlobalSettingsInterface*> GlobalSettings::callbacks_; |
2cca9ebf | 47 | bool GlobalSettings::tracking_ = false; |
6f925ba9 | 48 | map<QString, QVariant> GlobalSettings::tracked_changes_; |
bf9f1268 SA |
49 | |
50 | GlobalSettings::GlobalSettings() : | |
51 | QSettings() | |
52 | { | |
53 | beginGroup("Settings"); | |
54 | } | |
55 | ||
c031de4b SA |
56 | void GlobalSettings::set_defaults_where_needed() |
57 | { | |
28ceff25 SA |
58 | // Enable zoom-to-fit after acquisition by default |
59 | if (!contains(Key_View_ZoomToFitAfterAcq)) | |
60 | setValue(Key_View_ZoomToFitAfterAcq, true); | |
61 | ||
641574bc SA |
62 | // Enable colored trace backgrounds by default |
63 | if (!contains(Key_View_ColoredBG)) | |
64 | setValue(Key_View_ColoredBG, true); | |
df842d4f SA |
65 | |
66 | // Enable showing sampling points by default | |
67 | if (!contains(Key_View_ShowSamplingPoints)) | |
68 | setValue(Key_View_ShowSamplingPoints, true); | |
daa54986 SA |
69 | |
70 | if (!contains(Key_View_DefaultDivHeight)) | |
71 | setValue(Key_View_DefaultDivHeight, | |
72 | 3 * QFontMetrics(QApplication::font()).height()); | |
48051ccb SA |
73 | |
74 | if (!contains(Key_View_DefaultLogicHeight)) | |
75 | setValue(Key_View_DefaultLogicHeight, | |
76 | 2 * QFontMetrics(QApplication::font()).height()); | |
bcb4c327 SA |
77 | |
78 | // Default to 500 lines of backlog | |
79 | if (!contains(Key_Log_BufferSize)) | |
80 | setValue(Key_Log_BufferSize, 500); | |
c5d6200c SA |
81 | |
82 | // Notify user of existing stack trace by default | |
83 | if (!contains(Key_Log_NotifyOfStacktrace)) | |
84 | setValue(Key_Log_NotifyOfStacktrace, true); | |
c031de4b SA |
85 | } |
86 | ||
d0c0573b | 87 | void GlobalSettings::add_change_handler(GlobalSettingsInterface *cb) |
bf9f1268 | 88 | { |
d0c0573b SA |
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 | } | |
bf9f1268 SA |
99 | } |
100 | ||
101 | void GlobalSettings::setValue(const QString &key, const QVariant &value) | |
102 | { | |
2cca9ebf SA |
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 | ||
bf9f1268 SA |
108 | QSettings::setValue(key, value); |
109 | ||
403c3e87 | 110 | qDebug().noquote() << "Setting" << key << "changed to" << value; |
7ba25e4f | 111 | |
d0c0573b SA |
112 | // Call all registered callbacks |
113 | for (GlobalSettingsInterface *cb : callbacks_) | |
114 | cb->on_setting_changed(key, value); | |
bf9f1268 SA |
115 | } |
116 | ||
2cca9ebf SA |
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 | } | |
bf9f1268 | 138 | |
d3feec23 SA |
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 | ||
bf9f1268 | 172 | } // namespace pv |