]>
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"; |
1cc1c8de | 65 | const QString GlobalSettings::Key_Dec_InitialStateConfigurable = "Dec_InitialStateConfigurable"; |
1ed996b4 | 66 | const QString GlobalSettings::Key_Dec_ExportFormat = "Dec_ExportFormat"; |
ab185f78 | 67 | const QString GlobalSettings::Key_Dec_AlwaysShowAllRows = "Dec_AlwaysShowAllRows"; |
bcb4c327 | 68 | const QString GlobalSettings::Key_Log_BufferSize = "Log_BufferSize"; |
c5d6200c | 69 | const QString GlobalSettings::Key_Log_NotifyOfStacktrace = "Log_NotifyOfStacktrace"; |
bf9f1268 | 70 | |
d0c0573b | 71 | vector<GlobalSettingsInterface*> GlobalSettings::callbacks_; |
2cca9ebf | 72 | bool GlobalSettings::tracking_ = false; |
c6b4e925 | 73 | bool GlobalSettings::is_dark_theme_ = false; |
6f925ba9 | 74 | map<QString, QVariant> GlobalSettings::tracked_changes_; |
374c697f | 75 | QString GlobalSettings::default_style_; |
37b0bd35 | 76 | QPalette GlobalSettings::default_palette_; |
bf9f1268 SA |
77 | |
78 | GlobalSettings::GlobalSettings() : | |
c6b4e925 | 79 | QSettings() |
bf9f1268 SA |
80 | { |
81 | beginGroup("Settings"); | |
82 | } | |
83 | ||
a42d2514 SA |
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 | ||
c031de4b SA |
93 | void GlobalSettings::set_defaults_where_needed() |
94 | { | |
0466001b SA |
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 | ||
37b0bd35 SA |
103 | // Use no theme by default |
104 | if (!contains(Key_General_Theme)) | |
105 | setValue(Key_General_Theme, 0); | |
374c697f SA |
106 | if (!contains(Key_General_Style)) |
107 | setValue(Key_General_Style, ""); | |
37b0bd35 | 108 | |
8962d7b3 SA |
109 | // Save setup with .sr files by default |
110 | if (!contains(Key_General_SaveWithSetup)) | |
111 | setValue(Key_General_SaveWithSetup, true); | |
112 | ||
28ceff25 SA |
113 | // Enable zoom-to-fit after acquisition by default |
114 | if (!contains(Key_View_ZoomToFitAfterAcq)) | |
115 | setValue(Key_View_ZoomToFitAfterAcq, true); | |
116 | ||
641574bc SA |
117 | // Enable colored trace backgrounds by default |
118 | if (!contains(Key_View_ColoredBG)) | |
119 | setValue(Key_View_ColoredBG, true); | |
df842d4f SA |
120 | |
121 | // Enable showing sampling points by default | |
122 | if (!contains(Key_View_ShowSamplingPoints)) | |
123 | setValue(Key_View_ShowSamplingPoints, true); | |
daa54986 | 124 | |
4521022b SA |
125 | // Enable filling logic signal high areas by default |
126 | if (!contains(Key_View_FillSignalHighAreas)) | |
127 | setValue(Key_View_FillSignalHighAreas, true); | |
4521022b | 128 | |
daa54986 SA |
129 | if (!contains(Key_View_DefaultDivHeight)) |
130 | setValue(Key_View_DefaultDivHeight, | |
131 | 3 * QFontMetrics(QApplication::font()).height()); | |
48051ccb SA |
132 | |
133 | if (!contains(Key_View_DefaultLogicHeight)) | |
134 | setValue(Key_View_DefaultLogicHeight, | |
135 | 2 * QFontMetrics(QApplication::font()).height()); | |
bcb4c327 | 136 | |
6fff12ac SA |
137 | if (!contains(Key_View_ShowHoverMarker)) |
138 | setValue(Key_View_ShowHoverMarker, true); | |
139 | ||
fb641801 SA |
140 | if (!contains(Key_View_SnapDistance)) |
141 | setValue(Key_View_SnapDistance, 15); | |
142 | ||
6a26fc44 SA |
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"); | |
1ed996b4 | 147 | |
bcb4c327 SA |
148 | // Default to 500 lines of backlog |
149 | if (!contains(Key_Log_BufferSize)) | |
150 | setValue(Key_Log_BufferSize, 500); | |
c5d6200c SA |
151 | |
152 | // Notify user of existing stack trace by default | |
153 | if (!contains(Key_Log_NotifyOfStacktrace)) | |
154 | setValue(Key_Log_NotifyOfStacktrace, true); | |
a42d2514 | 155 | |
c04f5a29 SA |
156 | // Default theme is bright, so use its color scheme if undefined |
157 | if (!contains(Key_View_CursorFillColor)) | |
158 | set_bright_theme_default_colors(); | |
c031de4b SA |
159 | } |
160 | ||
a42d2514 | 161 | void GlobalSettings::set_bright_theme_default_colors() |
37b0bd35 | 162 | { |
a42d2514 SA |
163 | setValue(Key_View_FillSignalHighAreaColor, |
164 | QColor(0, 0, 0, 5 * 256 / 100).rgba()); | |
c04f5a29 SA |
165 | |
166 | setValue(Key_View_CursorFillColor, | |
167 | QColor(220, 231, 243).rgba()); | |
a42d2514 | 168 | } |
374c697f | 169 | |
a42d2514 SA |
170 | void GlobalSettings::set_dark_theme_default_colors() |
171 | { | |
172 | setValue(Key_View_FillSignalHighAreaColor, | |
173 | QColor(188, 188, 188, 9 * 256 / 100).rgba()); | |
c04f5a29 SA |
174 | |
175 | setValue(Key_View_CursorFillColor, | |
176 | QColor(60, 60, 60).rgba()); | |
a42d2514 SA |
177 | } |
178 | ||
179 | bool GlobalSettings::current_theme_is_dark() | |
180 | { | |
181 | return is_dark_theme_; | |
37b0bd35 SA |
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 | ||
374c697f SA |
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 | ||
a42d2514 SA |
204 | is_dark_theme_ = false; |
205 | ||
37b0bd35 SA |
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); | |
a42d2514 | 214 | is_dark_theme_ = true; |
37b0bd35 SA |
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); | |
a42d2514 | 238 | is_dark_theme_ = true; |
37b0bd35 SA |
239 | } |
240 | ||
241 | QPixmapCache::clear(); | |
242 | } | |
243 | ||
0466001b SA |
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 | ||
d0c0573b | 250 | void GlobalSettings::add_change_handler(GlobalSettingsInterface *cb) |
bf9f1268 | 251 | { |
d0c0573b SA |
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 | } | |
bf9f1268 SA |
262 | } |
263 | ||
264 | void GlobalSettings::setValue(const QString &key, const QVariant &value) | |
265 | { | |
2cca9ebf SA |
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 | ||
bf9f1268 SA |
271 | QSettings::setValue(key, value); |
272 | ||
fe060a48 SA |
273 | // TODO Emulate noquote() |
274 | qDebug() << "Setting" << key << "changed to" << value; | |
7ba25e4f | 275 | |
d0c0573b SA |
276 | // Call all registered callbacks |
277 | for (GlobalSettingsInterface *cb : callbacks_) | |
278 | cb->on_setting_changed(key, value); | |
bf9f1268 SA |
279 | } |
280 | ||
2cca9ebf SA |
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 | ||
f4ab4b5c | 297 | for (auto& entry : tracked_changes_) |
2cca9ebf SA |
298 | setValue(entry.first, entry.second); |
299 | ||
300 | tracked_changes_.clear(); | |
301 | } | |
bf9f1268 | 302 | |
d3feec23 SA |
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 | ||
1325ce42 SA |
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 | } | |
d3feec23 | 363 | |
bf9f1268 | 364 | } // namespace pv |