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