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