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