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