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