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