]> sigrok.org Git - pulseview.git/blob - pv/globalsettings.cpp
Fix #1290 by allowing the cursor to show samples
[pulseview.git] / pv / globalsettings.cpp
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_View_CursorShowFrequency = "View_CursorShowFrequency";
66 const QString GlobalSettings::Key_View_CursorShowInterval = "View_CursorShowInterval";
67 const QString GlobalSettings::Key_View_CursorShowSamples = "View_CursorShowSamples";
68 const QString GlobalSettings::Key_Dec_InitialStateConfigurable = "Dec_InitialStateConfigurable";
69 const QString GlobalSettings::Key_Dec_ExportFormat = "Dec_ExportFormat";
70 const QString GlobalSettings::Key_Dec_AlwaysShowAllRows = "Dec_AlwaysShowAllRows";
71 const QString GlobalSettings::Key_Log_BufferSize = "Log_BufferSize";
72 const QString GlobalSettings::Key_Log_NotifyOfStacktrace = "Log_NotifyOfStacktrace";
73
74 vector<GlobalSettingsInterface*> GlobalSettings::callbacks_;
75 bool GlobalSettings::tracking_ = false;
76 bool GlobalSettings::is_dark_theme_ = false;
77 map<QString, QVariant> GlobalSettings::tracked_changes_;
78 QString GlobalSettings::default_style_;
79 QPalette GlobalSettings::default_palette_;
80
81 GlobalSettings::GlobalSettings() :
82         QSettings()
83 {
84         beginGroup("Settings");
85 }
86
87 void 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
96 void GlobalSettings::set_defaults_where_needed()
97 {
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
106         // Use no theme by default
107         if (!contains(Key_General_Theme))
108                 setValue(Key_General_Theme, 0);
109         if (!contains(Key_General_Style))
110                 setValue(Key_General_Style, "");
111
112         // Save setup with .sr files by default
113         if (!contains(Key_General_SaveWithSetup))
114                 setValue(Key_General_SaveWithSetup, true);
115
116         // Enable zoom-to-fit after acquisition by default
117         if (!contains(Key_View_ZoomToFitAfterAcq))
118                 setValue(Key_View_ZoomToFitAfterAcq, true);
119
120         // Enable colored trace backgrounds by default
121         if (!contains(Key_View_ColoredBG))
122                 setValue(Key_View_ColoredBG, true);
123
124         // Enable showing sampling points by default
125         if (!contains(Key_View_ShowSamplingPoints))
126                 setValue(Key_View_ShowSamplingPoints, true);
127
128         // Enable filling logic signal high areas by default
129         if (!contains(Key_View_FillSignalHighAreas))
130                 setValue(Key_View_FillSignalHighAreas, true);
131
132         if (!contains(Key_View_DefaultDivHeight))
133                 setValue(Key_View_DefaultDivHeight,
134                 3 * QFontMetrics(QApplication::font()).height());
135
136         if (!contains(Key_View_DefaultLogicHeight))
137                 setValue(Key_View_DefaultLogicHeight,
138                 2 * QFontMetrics(QApplication::font()).height());
139
140         if (!contains(Key_View_ShowHoverMarker))
141                 setValue(Key_View_ShowHoverMarker, true);
142
143         if (!contains(Key_View_SnapDistance))
144                 setValue(Key_View_SnapDistance, 15);
145
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
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");
156
157         // Default to 500 lines of backlog
158         if (!contains(Key_Log_BufferSize))
159                 setValue(Key_Log_BufferSize, 500);
160
161         // Notify user of existing stack trace by default
162         if (!contains(Key_Log_NotifyOfStacktrace))
163                 setValue(Key_Log_NotifyOfStacktrace, true);
164
165         // Default theme is bright, so use its color scheme if undefined
166         if (!contains(Key_View_CursorFillColor))
167                 set_bright_theme_default_colors();
168 }
169
170 void GlobalSettings::set_bright_theme_default_colors()
171 {
172         setValue(Key_View_FillSignalHighAreaColor,
173                 QColor(0, 0, 0, 5 * 256 / 100).rgba());
174
175         setValue(Key_View_CursorFillColor,
176                 QColor(220, 231, 243).rgba());
177 }
178
179 void GlobalSettings::set_dark_theme_default_colors()
180 {
181         setValue(Key_View_FillSignalHighAreaColor,
182                 QColor(188, 188, 188, 9 * 256 / 100).rgba());
183
184         setValue(Key_View_CursorFillColor,
185                 QColor(60, 60, 60).rgba());
186 }
187
188 bool GlobalSettings::current_theme_is_dark()
189 {
190         return is_dark_theme_;
191 }
192
193 void 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
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
213         is_dark_theme_ = false;
214
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);
223                 is_dark_theme_ = true;
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);
247                 is_dark_theme_ = true;
248         }
249
250         QPixmapCache::clear();
251 }
252
253 void GlobalSettings::apply_language()
254 {
255         Application* a = qobject_cast<Application*>(QApplication::instance());
256         a->switch_language(value(Key_General_Language).toString());
257 }
258
259 void GlobalSettings::add_change_handler(GlobalSettingsInterface *cb)
260 {
261         callbacks_.push_back(cb);
262 }
263
264 void 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                 }
271 }
272
273 void GlobalSettings::setValue(const QString &key, const QVariant &value)
274 {
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
280         QSettings::setValue(key, value);
281
282         // TODO Emulate noquote()
283         qDebug() << "Setting" << key << "changed to" << value;
284
285         // Call all registered callbacks
286         for (GlobalSettingsInterface *cb : callbacks_)
287                 cb->on_setting_changed(key, value);
288 }
289
290 void GlobalSettings::start_tracking()
291 {
292         tracking_ = true;
293         tracked_changes_.clear();
294 }
295
296 void GlobalSettings::stop_tracking()
297 {
298         tracking_ = false;
299         tracked_changes_.clear();
300 }
301
302 void GlobalSettings::undo_tracked_changes()
303 {
304         tracking_ = false;
305
306         for (auto& entry : tracked_changes_)
307                 setValue(entry.first, entry.second);
308
309         tracked_changes_.clear();
310 }
311
312 void 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
326 GVariant* 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
344 void 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
352 Glib::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 }
372
373 } // namespace pv