2 * This file is part of the PulseView project.
4 * Copyright (C) 2017 Soeren Apel <soeren@apelpie.net>
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.
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.
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/>.
20 #include "globalsettings.hpp"
22 #include <QApplication>
26 #include <QFontMetrics>
27 #include <QPixmapCache>
39 const vector< pair<QString, QString> > Themes {
41 {"QDarkStyleSheet", ":/themes/qdarkstyle/style.qss"},
42 {"DarkStyle", ":/themes/darkstyle/darkstyle.qss"}
45 const QString GlobalSettings::Key_General_Theme = "General_Theme";
46 const QString GlobalSettings::Key_General_Style = "General_Style";
47 const QString GlobalSettings::Key_General_SaveWithSetup = "General_SaveWithSetup";
48 const QString GlobalSettings::Key_View_ZoomToFitDuringAcq = "View_ZoomToFitDuringAcq";
49 const QString GlobalSettings::Key_View_ZoomToFitAfterAcq = "View_ZoomToFitAfterAcq";
50 const QString GlobalSettings::Key_View_TriggerIsZeroTime = "View_TriggerIsZeroTime";
51 const QString GlobalSettings::Key_View_ColoredBG = "View_ColoredBG";
52 const QString GlobalSettings::Key_View_StickyScrolling = "View_StickyScrolling";
53 const QString GlobalSettings::Key_View_ShowSamplingPoints = "View_ShowSamplingPoints";
54 const QString GlobalSettings::Key_View_FillSignalHighAreas = "View_FillSignalHighAreas";
55 const QString GlobalSettings::Key_View_FillSignalHighAreaColor = "View_FillSignalHighAreaColor";
56 const QString GlobalSettings::Key_View_ShowAnalogMinorGrid = "View_ShowAnalogMinorGrid";
57 const QString GlobalSettings::Key_View_ConversionThresholdDispMode = "View_ConversionThresholdDispMode";
58 const QString GlobalSettings::Key_View_DefaultDivHeight = "View_DefaultDivHeight";
59 const QString GlobalSettings::Key_View_DefaultLogicHeight = "View_DefaultLogicHeight";
60 const QString GlobalSettings::Key_View_ShowHoverMarker = "View_ShowHoverMarker";
61 const QString GlobalSettings::Key_View_SnapDistance = "View_SnapDistance";
62 const QString GlobalSettings::Key_View_CursorFillColor = "View_CursorFillColor";
63 const QString GlobalSettings::Key_Dec_InitialStateConfigurable = "Dec_InitialStateConfigurable";
64 const QString GlobalSettings::Key_Dec_ExportFormat = "Dec_ExportFormat";
65 const QString GlobalSettings::Key_Log_BufferSize = "Log_BufferSize";
66 const QString GlobalSettings::Key_Log_NotifyOfStacktrace = "Log_NotifyOfStacktrace";
68 vector<GlobalSettingsInterface*> GlobalSettings::callbacks_;
69 bool GlobalSettings::tracking_ = false;
70 map<QString, QVariant> GlobalSettings::tracked_changes_;
71 QString GlobalSettings::default_style_;
72 QPalette GlobalSettings::default_palette_;
74 GlobalSettings::GlobalSettings() :
78 beginGroup("Settings");
81 void GlobalSettings::save_internal_defaults()
83 default_style_ = qApp->style()->objectName();
84 if (default_style_.isEmpty())
85 default_style_ = "fusion";
87 default_palette_ = QApplication::palette();
90 void GlobalSettings::set_defaults_where_needed()
92 // Use no theme by default
93 if (!contains(Key_General_Theme))
94 setValue(Key_General_Theme, 0);
95 if (!contains(Key_General_Style))
96 setValue(Key_General_Style, "");
98 // Save setup with .sr files by default
99 if (!contains(Key_General_SaveWithSetup))
100 setValue(Key_General_SaveWithSetup, true);
102 // Enable zoom-to-fit after acquisition by default
103 if (!contains(Key_View_ZoomToFitAfterAcq))
104 setValue(Key_View_ZoomToFitAfterAcq, true);
106 // Enable colored trace backgrounds by default
107 if (!contains(Key_View_ColoredBG))
108 setValue(Key_View_ColoredBG, true);
110 // Enable showing sampling points by default
111 if (!contains(Key_View_ShowSamplingPoints))
112 setValue(Key_View_ShowSamplingPoints, true);
114 // Enable filling logic signal high areas by default
115 if (!contains(Key_View_FillSignalHighAreas))
116 setValue(Key_View_FillSignalHighAreas, true);
118 if (!contains(Key_View_DefaultDivHeight))
119 setValue(Key_View_DefaultDivHeight,
120 3 * QFontMetrics(QApplication::font()).height());
122 if (!contains(Key_View_DefaultLogicHeight))
123 setValue(Key_View_DefaultLogicHeight,
124 2 * QFontMetrics(QApplication::font()).height());
126 if (!contains(Key_View_ShowHoverMarker))
127 setValue(Key_View_ShowHoverMarker, true);
129 if (!contains(Key_View_SnapDistance))
130 setValue(Key_View_SnapDistance, 15);
132 if (!contains(Key_Dec_ExportFormat))
133 setValue(Key_Dec_ExportFormat, "%s %d: %c: %1");
135 // Default to 500 lines of backlog
136 if (!contains(Key_Log_BufferSize))
137 setValue(Key_Log_BufferSize, 500);
139 // Notify user of existing stack trace by default
140 if (!contains(Key_Log_NotifyOfStacktrace))
141 setValue(Key_Log_NotifyOfStacktrace, true);
143 // Default theme is bright, so use its color scheme if undefined
144 if (!contains(Key_View_CursorFillColor))
145 set_bright_theme_default_colors();
148 void GlobalSettings::set_bright_theme_default_colors()
150 setValue(Key_View_FillSignalHighAreaColor,
151 QColor(0, 0, 0, 5 * 256 / 100).rgba());
153 setValue(Key_View_CursorFillColor,
154 QColor(220, 231, 243).rgba());
157 void GlobalSettings::set_dark_theme_default_colors()
159 setValue(Key_View_FillSignalHighAreaColor,
160 QColor(188, 188, 188, 9 * 256 / 100).rgba());
162 setValue(Key_View_CursorFillColor,
163 QColor(60, 60, 60).rgba());
166 bool GlobalSettings::current_theme_is_dark()
168 return is_dark_theme_;
171 void GlobalSettings::apply_theme()
173 QString theme_name = Themes.at(value(Key_General_Theme).toInt()).first;
174 QString resource_name = Themes.at(value(Key_General_Theme).toInt()).second;
176 if (!resource_name.isEmpty()) {
177 QFile file(resource_name);
178 file.open(QFile::ReadOnly | QFile::Text);
179 qApp->setStyleSheet(file.readAll());
181 qApp->setStyleSheet("");
183 qApp->setPalette(default_palette_);
185 const QString style = value(Key_General_Style).toString();
187 qApp->setStyle(default_style_);
189 qApp->setStyle(style);
191 is_dark_theme_ = false;
193 if (theme_name.compare("QDarkStyleSheet") == 0) {
194 QPalette dark_palette;
195 dark_palette.setColor(QPalette::Window, QColor(53, 53, 53));
196 dark_palette.setColor(QPalette::WindowText, Qt::white);
197 dark_palette.setColor(QPalette::Base, QColor(42, 42, 42));
198 dark_palette.setColor(QPalette::Dark, QColor(35, 35, 35));
199 dark_palette.setColor(QPalette::Highlight, QColor(42, 130, 218));
200 qApp->setPalette(dark_palette);
201 is_dark_theme_ = true;
202 } else if (theme_name.compare("DarkStyle") == 0) {
203 QPalette dark_palette;
204 dark_palette.setColor(QPalette::Window, QColor(53, 53, 53));
205 dark_palette.setColor(QPalette::WindowText, Qt::white);
206 dark_palette.setColor(QPalette::Disabled, QPalette::WindowText, QColor(127, 127, 127));
207 dark_palette.setColor(QPalette::Base, QColor(42, 42, 42));
208 dark_palette.setColor(QPalette::AlternateBase, QColor(66, 66, 66));
209 dark_palette.setColor(QPalette::ToolTipBase, Qt::white);
210 dark_palette.setColor(QPalette::ToolTipText, QColor(53, 53, 53));
211 dark_palette.setColor(QPalette::Text, Qt::white);
212 dark_palette.setColor(QPalette::Disabled, QPalette::Text, QColor(127, 127, 127));
213 dark_palette.setColor(QPalette::Dark, QColor(35, 35, 35));
214 dark_palette.setColor(QPalette::Shadow, QColor(20, 20, 20));
215 dark_palette.setColor(QPalette::Button, QColor(53, 53, 53));
216 dark_palette.setColor(QPalette::ButtonText, Qt::white);
217 dark_palette.setColor(QPalette::Disabled, QPalette::ButtonText, QColor(127, 127, 127));
218 dark_palette.setColor(QPalette::BrightText, Qt::red);
219 dark_palette.setColor(QPalette::Link, QColor(42, 130, 218));
220 dark_palette.setColor(QPalette::Highlight, QColor(42, 130, 218));
221 dark_palette.setColor(QPalette::Disabled, QPalette::Highlight, QColor(80, 80, 80));
222 dark_palette.setColor(QPalette::HighlightedText, Qt::white);
223 dark_palette.setColor(QPalette::Disabled, QPalette::HighlightedText, QColor(127, 127, 127));
224 qApp->setPalette(dark_palette);
225 is_dark_theme_ = true;
228 QPixmapCache::clear();
231 void GlobalSettings::add_change_handler(GlobalSettingsInterface *cb)
233 callbacks_.push_back(cb);
236 void GlobalSettings::remove_change_handler(GlobalSettingsInterface *cb)
238 for (auto cb_it = callbacks_.begin(); cb_it != callbacks_.end(); cb_it++)
240 callbacks_.erase(cb_it);
245 void GlobalSettings::setValue(const QString &key, const QVariant &value)
247 // Save previous value if we're tracking changes,
248 // not altering an already-existing saved setting
250 tracked_changes_.emplace(key, QSettings::value(key));
252 QSettings::setValue(key, value);
254 // TODO Emulate noquote()
255 qDebug() << "Setting" << key << "changed to" << value;
257 // Call all registered callbacks
258 for (GlobalSettingsInterface *cb : callbacks_)
259 cb->on_setting_changed(key, value);
262 void GlobalSettings::start_tracking()
265 tracked_changes_.clear();
268 void GlobalSettings::stop_tracking()
271 tracked_changes_.clear();
274 void GlobalSettings::undo_tracked_changes()
278 for (auto& entry : tracked_changes_)
279 setValue(entry.first, entry.second);
281 tracked_changes_.clear();
284 void GlobalSettings::store_gvariant(QSettings &settings, GVariant *v)
286 const GVariantType *var_type = g_variant_get_type(v);
287 char *var_type_str = g_variant_type_dup_string(var_type);
289 QByteArray var_data = QByteArray((const char*)g_variant_get_data(v),
290 g_variant_get_size(v));
292 settings.setValue("value", var_data);
293 settings.setValue("type", var_type_str);
295 g_free(var_type_str);
298 GVariant* GlobalSettings::restore_gvariant(QSettings &settings)
300 QString raw_type = settings.value("type").toString();
301 GVariantType *var_type = g_variant_type_new(raw_type.toUtf8());
303 QByteArray data = settings.value("value").toByteArray();
305 gpointer var_data = g_memdup((gconstpointer)data.constData(),
308 GVariant *value = g_variant_new_from_data(var_type, var_data,
309 data.size(), false, g_free, var_data);
311 g_variant_type_free(var_type);
316 void GlobalSettings::store_variantbase(QSettings &settings, Glib::VariantBase v)
318 const QByteArray var_data = QByteArray((const char*)v.get_data(), v.get_size());
320 settings.setValue("value", var_data);
321 settings.setValue("type", QString::fromStdString(v.get_type_string()));
324 Glib::VariantBase GlobalSettings::restore_variantbase(QSettings &settings)
326 QString raw_type = settings.value("type").toString();
327 GVariantType *var_type = g_variant_type_new(raw_type.toUtf8());
329 QByteArray data = settings.value("value").toByteArray();
331 gpointer var_data = g_memdup((gconstpointer)data.constData(),
334 GVariant *value = g_variant_new_from_data(var_type, var_data,
335 data.size(), false, g_free, var_data);
337 Glib::VariantBase ret_val = Glib::VariantBase(value, true);
339 g_variant_type_free(var_type);
340 g_variant_unref(value);