]>
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 <QColor> | |
24 | #include <QDebug> | |
25 | #include <QFile> | |
26 | #include <QFontMetrics> | |
27 | #include <QPixmapCache> | |
28 | #include <QString> | |
29 | ||
30 | using std::map; | |
31 | using std::pair; | |
32 | using std::string; | |
33 | using std::vector; | |
34 | ||
35 | namespace pv { | |
36 | ||
37 | const vector< pair<QString, QString> > Themes { | |
38 | {"None" , ""}, | |
39 | {"QDarkStyleSheet", ":/themes/qdarkstyle/style.qss"}, | |
40 | {"DarkStyle", ":/themes/darkstyle/darkstyle.qss"} | |
41 | }; | |
42 | ||
43 | const QString GlobalSettings::Key_General_Theme = "General_Theme"; | |
44 | const QString GlobalSettings::Key_View_ZoomToFitDuringAcq = "View_ZoomToFitDuringAcq"; | |
45 | const QString GlobalSettings::Key_View_ZoomToFitAfterAcq = "View_ZoomToFitAfterAcq"; | |
46 | const QString GlobalSettings::Key_View_TriggerIsZeroTime = "View_TriggerIsZeroTime"; | |
47 | const QString GlobalSettings::Key_View_ColoredBG = "View_ColoredBG"; | |
48 | const QString GlobalSettings::Key_View_StickyScrolling = "View_StickyScrolling"; | |
49 | const QString GlobalSettings::Key_View_ShowSamplingPoints = "View_ShowSamplingPoints"; | |
50 | const QString GlobalSettings::Key_View_FillSignalHighAreas = "View_FillSignalHighAreas"; | |
51 | const QString GlobalSettings::Key_View_FillSignalHighAreaColor = "View_FillSignalHighAreaColor"; | |
52 | const QString GlobalSettings::Key_View_ShowAnalogMinorGrid = "View_ShowAnalogMinorGrid"; | |
53 | const QString GlobalSettings::Key_View_ConversionThresholdDispMode = "View_ConversionThresholdDispMode"; | |
54 | const QString GlobalSettings::Key_View_DefaultDivHeight = "View_DefaultDivHeight"; | |
55 | const QString GlobalSettings::Key_View_DefaultLogicHeight = "View_DefaultLogicHeight"; | |
56 | const QString GlobalSettings::Key_View_ShowHoverMarker = "View_ShowHoverMarker"; | |
57 | const QString GlobalSettings::Key_View_SnapDistance = "View_SnapDistance"; | |
58 | const QString GlobalSettings::Key_Dec_InitialStateConfigurable = "Dec_InitialStateConfigurable"; | |
59 | const QString GlobalSettings::Key_Dec_ExportFormat = "Dec_ExportFormat"; | |
60 | const QString GlobalSettings::Key_Log_BufferSize = "Log_BufferSize"; | |
61 | const QString GlobalSettings::Key_Log_NotifyOfStacktrace = "Log_NotifyOfStacktrace"; | |
62 | ||
63 | vector<GlobalSettingsInterface*> GlobalSettings::callbacks_; | |
64 | bool GlobalSettings::tracking_ = false; | |
65 | map<QString, QVariant> GlobalSettings::tracked_changes_; | |
66 | QPalette GlobalSettings::default_palette_; | |
67 | ||
68 | GlobalSettings::GlobalSettings() : | |
69 | QSettings() | |
70 | { | |
71 | beginGroup("Settings"); | |
72 | } | |
73 | ||
74 | void GlobalSettings::set_defaults_where_needed() | |
75 | { | |
76 | // Use no theme by default | |
77 | if (!contains(Key_General_Theme)) | |
78 | setValue(Key_General_Theme, 0); | |
79 | ||
80 | // Enable zoom-to-fit after acquisition by default | |
81 | if (!contains(Key_View_ZoomToFitAfterAcq)) | |
82 | setValue(Key_View_ZoomToFitAfterAcq, true); | |
83 | ||
84 | // Enable colored trace backgrounds by default | |
85 | if (!contains(Key_View_ColoredBG)) | |
86 | setValue(Key_View_ColoredBG, true); | |
87 | ||
88 | // Enable showing sampling points by default | |
89 | if (!contains(Key_View_ShowSamplingPoints)) | |
90 | setValue(Key_View_ShowSamplingPoints, true); | |
91 | ||
92 | // Enable filling logic signal high areas by default | |
93 | if (!contains(Key_View_FillSignalHighAreas)) | |
94 | setValue(Key_View_FillSignalHighAreas, true); | |
95 | if (!contains(Key_View_FillSignalHighAreaColor)) | |
96 | setValue(Key_View_FillSignalHighAreaColor, | |
97 | QColor(0, 0, 0, 5 * 256 / 100).rgba()); | |
98 | ||
99 | if (!contains(Key_View_DefaultDivHeight)) | |
100 | setValue(Key_View_DefaultDivHeight, | |
101 | 3 * QFontMetrics(QApplication::font()).height()); | |
102 | ||
103 | if (!contains(Key_View_DefaultLogicHeight)) | |
104 | setValue(Key_View_DefaultLogicHeight, | |
105 | 2 * QFontMetrics(QApplication::font()).height()); | |
106 | ||
107 | if (!contains(Key_View_ShowHoverMarker)) | |
108 | setValue(Key_View_ShowHoverMarker, true); | |
109 | ||
110 | if (!contains(Key_View_SnapDistance)) | |
111 | setValue(Key_View_SnapDistance, 15); | |
112 | ||
113 | if (!contains(Key_Dec_ExportFormat)) | |
114 | setValue(Key_Dec_ExportFormat, "%s %d: %c: %1"); | |
115 | ||
116 | // Default to 500 lines of backlog | |
117 | if (!contains(Key_Log_BufferSize)) | |
118 | setValue(Key_Log_BufferSize, 500); | |
119 | ||
120 | // Notify user of existing stack trace by default | |
121 | if (!contains(Key_Log_NotifyOfStacktrace)) | |
122 | setValue(Key_Log_NotifyOfStacktrace, true); | |
123 | } | |
124 | ||
125 | void GlobalSettings::save_default_palette() | |
126 | { | |
127 | default_palette_ = QApplication::palette(); | |
128 | } | |
129 | ||
130 | void GlobalSettings::apply_theme() | |
131 | { | |
132 | QString theme_name = Themes.at(value(Key_General_Theme).toInt()).first; | |
133 | QString resource_name = Themes.at(value(Key_General_Theme).toInt()).second; | |
134 | ||
135 | if (!resource_name.isEmpty()) { | |
136 | QFile file(resource_name); | |
137 | file.open(QFile::ReadOnly | QFile::Text); | |
138 | qApp->setStyleSheet(file.readAll()); | |
139 | } else | |
140 | qApp->setStyleSheet(""); | |
141 | ||
142 | qApp->setPalette(default_palette_); | |
143 | ||
144 | if (theme_name.compare("QDarkStyleSheet") == 0) { | |
145 | QPalette dark_palette; | |
146 | dark_palette.setColor(QPalette::Window, QColor(53, 53, 53)); | |
147 | dark_palette.setColor(QPalette::WindowText, Qt::white); | |
148 | dark_palette.setColor(QPalette::Base, QColor(42, 42, 42)); | |
149 | dark_palette.setColor(QPalette::Dark, QColor(35, 35, 35)); | |
150 | dark_palette.setColor(QPalette::Highlight, QColor(42, 130, 218)); | |
151 | qApp->setPalette(dark_palette); | |
152 | } else if (theme_name.compare("DarkStyle") == 0) { | |
153 | QPalette dark_palette; | |
154 | dark_palette.setColor(QPalette::Window, QColor(53, 53, 53)); | |
155 | dark_palette.setColor(QPalette::WindowText, Qt::white); | |
156 | dark_palette.setColor(QPalette::Disabled, QPalette::WindowText, QColor(127, 127, 127)); | |
157 | dark_palette.setColor(QPalette::Base, QColor(42, 42, 42)); | |
158 | dark_palette.setColor(QPalette::AlternateBase, QColor(66, 66, 66)); | |
159 | dark_palette.setColor(QPalette::ToolTipBase, Qt::white); | |
160 | dark_palette.setColor(QPalette::ToolTipText, QColor(53, 53, 53)); | |
161 | dark_palette.setColor(QPalette::Text, Qt::white); | |
162 | dark_palette.setColor(QPalette::Disabled, QPalette::Text, QColor(127, 127, 127)); | |
163 | dark_palette.setColor(QPalette::Dark, QColor(35, 35, 35)); | |
164 | dark_palette.setColor(QPalette::Shadow, QColor(20, 20, 20)); | |
165 | dark_palette.setColor(QPalette::Button, QColor(53, 53, 53)); | |
166 | dark_palette.setColor(QPalette::ButtonText, Qt::white); | |
167 | dark_palette.setColor(QPalette::Disabled, QPalette::ButtonText, QColor(127, 127, 127)); | |
168 | dark_palette.setColor(QPalette::BrightText, Qt::red); | |
169 | dark_palette.setColor(QPalette::Link, QColor(42, 130, 218)); | |
170 | dark_palette.setColor(QPalette::Highlight, QColor(42, 130, 218)); | |
171 | dark_palette.setColor(QPalette::Disabled, QPalette::Highlight, QColor(80, 80, 80)); | |
172 | dark_palette.setColor(QPalette::HighlightedText, Qt::white); | |
173 | dark_palette.setColor(QPalette::Disabled, QPalette::HighlightedText, QColor(127, 127, 127)); | |
174 | qApp->setPalette(dark_palette); | |
175 | } | |
176 | ||
177 | QPixmapCache::clear(); | |
178 | } | |
179 | ||
180 | void GlobalSettings::add_change_handler(GlobalSettingsInterface *cb) | |
181 | { | |
182 | callbacks_.push_back(cb); | |
183 | } | |
184 | ||
185 | void GlobalSettings::remove_change_handler(GlobalSettingsInterface *cb) | |
186 | { | |
187 | for (auto cb_it = callbacks_.begin(); cb_it != callbacks_.end(); cb_it++) | |
188 | if (*cb_it == cb) { | |
189 | callbacks_.erase(cb_it); | |
190 | break; | |
191 | } | |
192 | } | |
193 | ||
194 | void GlobalSettings::setValue(const QString &key, const QVariant &value) | |
195 | { | |
196 | // Save previous value if we're tracking changes, | |
197 | // not altering an already-existing saved setting | |
198 | if (tracking_) | |
199 | tracked_changes_.emplace(key, QSettings::value(key)); | |
200 | ||
201 | QSettings::setValue(key, value); | |
202 | ||
203 | // TODO Emulate noquote() | |
204 | qDebug() << "Setting" << key << "changed to" << value; | |
205 | ||
206 | // Call all registered callbacks | |
207 | for (GlobalSettingsInterface *cb : callbacks_) | |
208 | cb->on_setting_changed(key, value); | |
209 | } | |
210 | ||
211 | void GlobalSettings::start_tracking() | |
212 | { | |
213 | tracking_ = true; | |
214 | tracked_changes_.clear(); | |
215 | } | |
216 | ||
217 | void GlobalSettings::stop_tracking() | |
218 | { | |
219 | tracking_ = false; | |
220 | tracked_changes_.clear(); | |
221 | } | |
222 | ||
223 | void GlobalSettings::undo_tracked_changes() | |
224 | { | |
225 | tracking_ = false; | |
226 | ||
227 | for (auto entry : tracked_changes_) | |
228 | setValue(entry.first, entry.second); | |
229 | ||
230 | tracked_changes_.clear(); | |
231 | } | |
232 | ||
233 | void GlobalSettings::store_gvariant(QSettings &settings, GVariant *v) | |
234 | { | |
235 | const GVariantType *var_type = g_variant_get_type(v); | |
236 | char *var_type_str = g_variant_type_dup_string(var_type); | |
237 | ||
238 | QByteArray var_data = QByteArray((const char*)g_variant_get_data(v), | |
239 | g_variant_get_size(v)); | |
240 | ||
241 | settings.setValue("value", var_data); | |
242 | settings.setValue("type", var_type_str); | |
243 | ||
244 | g_free(var_type_str); | |
245 | } | |
246 | ||
247 | GVariant* GlobalSettings::restore_gvariant(QSettings &settings) | |
248 | { | |
249 | QString raw_type = settings.value("type").toString(); | |
250 | GVariantType *var_type = g_variant_type_new(raw_type.toUtf8()); | |
251 | ||
252 | QByteArray data = settings.value("value").toByteArray(); | |
253 | ||
254 | gpointer var_data = g_memdup((gconstpointer)data.constData(), | |
255 | (guint)data.size()); | |
256 | ||
257 | GVariant *value = g_variant_new_from_data(var_type, var_data, | |
258 | data.size(), false, g_free, var_data); | |
259 | ||
260 | g_variant_type_free(var_type); | |
261 | ||
262 | return value; | |
263 | } | |
264 | ||
265 | void GlobalSettings::store_variantbase(QSettings &settings, Glib::VariantBase v) | |
266 | { | |
267 | const QByteArray var_data = QByteArray((const char*)v.get_data(), v.get_size()); | |
268 | ||
269 | settings.setValue("value", var_data); | |
270 | settings.setValue("type", QString::fromStdString(v.get_type_string())); | |
271 | } | |
272 | ||
273 | Glib::VariantBase GlobalSettings::restore_variantbase(QSettings &settings) | |
274 | { | |
275 | QString raw_type = settings.value("type").toString(); | |
276 | GVariantType *var_type = g_variant_type_new(raw_type.toUtf8()); | |
277 | ||
278 | QByteArray data = settings.value("value").toByteArray(); | |
279 | ||
280 | gpointer var_data = g_memdup((gconstpointer)data.constData(), | |
281 | (guint)data.size()); | |
282 | ||
283 | GVariant *value = g_variant_new_from_data(var_type, var_data, | |
284 | data.size(), false, g_free, var_data); | |
285 | ||
286 | Glib::VariantBase ret_val = Glib::VariantBase(value, true); | |
287 | ||
288 | g_variant_type_free(var_type); | |
289 | g_variant_unref(value); | |
290 | ||
291 | return ret_val; | |
292 | } | |
293 | ||
294 | } // namespace pv |