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