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