]>
Commit | Line | Data |
---|---|---|
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 "config.h" | |
21 | ||
22 | #include <glib.h> | |
23 | #include <boost/version.hpp> | |
24 | ||
25 | #include <QApplication> | |
26 | #include <QDialogButtonBox> | |
27 | #include <QFormLayout> | |
28 | #include <QGroupBox> | |
29 | #include <QHBoxLayout> | |
30 | #include <QLabel> | |
31 | #include <QString> | |
32 | #include <QTextBrowser> | |
33 | #include <QTextDocument> | |
34 | #include <QVBoxLayout> | |
35 | ||
36 | #include "settings.hpp" | |
37 | ||
38 | #include "pv/devicemanager.hpp" | |
39 | #include "pv/globalsettings.hpp" | |
40 | ||
41 | #include <libsigrokcxx/libsigrokcxx.hpp> | |
42 | ||
43 | #ifdef ENABLE_DECODE | |
44 | #include <libsigrokdecode/libsigrokdecode.h> | |
45 | #endif | |
46 | ||
47 | using std::shared_ptr; | |
48 | ||
49 | namespace pv { | |
50 | namespace dialogs { | |
51 | ||
52 | Settings::Settings(DeviceManager &device_manager, QWidget *parent) : | |
53 | QDialog(parent, nullptr), | |
54 | device_manager_(device_manager) | |
55 | { | |
56 | const int icon_size = 64; | |
57 | ||
58 | resize(600, 400); | |
59 | ||
60 | page_list = new QListWidget; | |
61 | page_list->setViewMode(QListView::IconMode); | |
62 | page_list->setIconSize(QSize(icon_size, icon_size)); | |
63 | page_list->setMovement(QListView::Static); | |
64 | page_list->setMaximumWidth(icon_size + (icon_size / 2)); | |
65 | page_list->setSpacing(12); | |
66 | ||
67 | pages = new QStackedWidget; | |
68 | create_pages(); | |
69 | page_list->setCurrentIndex(page_list->model()->index(0, 0)); | |
70 | ||
71 | QHBoxLayout *tab_layout = new QHBoxLayout; | |
72 | tab_layout->addWidget(page_list); | |
73 | tab_layout->addWidget(pages, Qt::AlignLeft); | |
74 | ||
75 | QDialogButtonBox *button_box = new QDialogButtonBox( | |
76 | QDialogButtonBox::Ok | QDialogButtonBox::Cancel); | |
77 | ||
78 | QVBoxLayout* root_layout = new QVBoxLayout(this); | |
79 | root_layout->addLayout(tab_layout); | |
80 | root_layout->addWidget(button_box); | |
81 | ||
82 | connect(button_box, SIGNAL(accepted()), this, SLOT(accept())); | |
83 | connect(button_box, SIGNAL(rejected()), this, SLOT(reject())); | |
84 | connect(page_list, SIGNAL(currentItemChanged(QListWidgetItem*, QListWidgetItem*)), | |
85 | this, SLOT(on_page_changed(QListWidgetItem*, QListWidgetItem*))); | |
86 | ||
87 | // Start to record changes | |
88 | GlobalSettings settings; | |
89 | settings.start_tracking(); | |
90 | } | |
91 | ||
92 | void Settings::create_pages() | |
93 | { | |
94 | // View page | |
95 | pages->addWidget(get_view_settings_form(pages)); | |
96 | ||
97 | QListWidgetItem *viewButton = new QListWidgetItem(page_list); | |
98 | viewButton->setIcon(QIcon(":/icons/settings-views.svg")); | |
99 | viewButton->setText(tr("Views")); | |
100 | viewButton->setTextAlignment(Qt::AlignHCenter); | |
101 | viewButton->setFlags(Qt::ItemIsSelectable | Qt::ItemIsEnabled); | |
102 | ||
103 | #ifdef ENABLE_DECODE | |
104 | // Decoder page | |
105 | pages->addWidget(get_decoder_settings_form(pages)); | |
106 | ||
107 | QListWidgetItem *decoderButton = new QListWidgetItem(page_list); | |
108 | decoderButton->setIcon(QIcon(":/icons/add-decoder.svg")); | |
109 | decoderButton->setText(tr("Decoders")); | |
110 | decoderButton->setTextAlignment(Qt::AlignHCenter); | |
111 | decoderButton->setFlags(Qt::ItemIsSelectable | Qt::ItemIsEnabled); | |
112 | #endif | |
113 | ||
114 | // About page | |
115 | pages->addWidget(get_about_page(pages)); | |
116 | ||
117 | QListWidgetItem *aboutButton = new QListWidgetItem(page_list); | |
118 | aboutButton->setIcon(QIcon(":/icons/information.svg")); | |
119 | aboutButton->setText(tr("About")); | |
120 | aboutButton->setTextAlignment(Qt::AlignHCenter); | |
121 | aboutButton->setFlags(Qt::ItemIsSelectable | Qt::ItemIsEnabled); | |
122 | } | |
123 | ||
124 | QCheckBox *Settings::create_checkbox(const QString& key, const char* slot) const | |
125 | { | |
126 | GlobalSettings settings; | |
127 | ||
128 | QCheckBox *cb = new QCheckBox(); | |
129 | cb->setChecked(settings.value(key).toBool()); | |
130 | connect(cb, SIGNAL(stateChanged(int)), this, slot); | |
131 | return cb; | |
132 | } | |
133 | ||
134 | QWidget *Settings::get_view_settings_form(QWidget *parent) const | |
135 | { | |
136 | QCheckBox *cb; | |
137 | ||
138 | QWidget *form = new QWidget(parent); | |
139 | QVBoxLayout *form_layout = new QVBoxLayout(form); | |
140 | ||
141 | // Trace view settings | |
142 | QGroupBox *trace_view_group = new QGroupBox(tr("Trace View")); | |
143 | form_layout->addWidget(trace_view_group); | |
144 | ||
145 | QFormLayout *trace_view_layout = new QFormLayout(); | |
146 | trace_view_group->setLayout(trace_view_layout); | |
147 | ||
148 | cb = create_checkbox(GlobalSettings::Key_View_ColouredBG, | |
149 | SLOT(on_view_colouredBG_changed(int))); | |
150 | trace_view_layout->addRow(tr("Use coloured trace &background"), cb); | |
151 | ||
152 | cb = create_checkbox(GlobalSettings::Key_View_AlwaysZoomToFit, | |
153 | SLOT(on_view_alwaysZoomToFit_changed(int))); | |
154 | trace_view_layout->addRow(tr("Constantly perform &zoom-to-fit during capture"), cb); | |
155 | ||
156 | cb = create_checkbox(GlobalSettings::Key_View_StickyScrolling, | |
157 | SLOT(on_view_stickyScrolling_changed(int))); | |
158 | trace_view_layout->addRow(tr("Always keep &newest samples at the right edge during capture"), cb); | |
159 | ||
160 | cb = create_checkbox(GlobalSettings::Key_View_ShowSamplingPoints, | |
161 | SLOT(on_view_showSamplingPoints_changed(int))); | |
162 | trace_view_layout->addRow(tr("Show data &sampling points"), cb); | |
163 | ||
164 | cb = create_checkbox(GlobalSettings::Key_View_ShowAnalogMinorGrid, | |
165 | SLOT(on_view_showAnalogMinorGrid_changed(int))); | |
166 | trace_view_layout->addRow(tr("Show analog minor grid in addition to vdiv grid"), cb); | |
167 | ||
168 | return form; | |
169 | } | |
170 | ||
171 | QWidget *Settings::get_decoder_settings_form(QWidget *parent) const | |
172 | { | |
173 | #ifdef ENABLE_DECODE | |
174 | QCheckBox *cb; | |
175 | ||
176 | QWidget *form = new QWidget(parent); | |
177 | QVBoxLayout *form_layout = new QVBoxLayout(form); | |
178 | ||
179 | // Decoder settings | |
180 | QGroupBox *decoder_group = new QGroupBox(tr("Decoders")); | |
181 | form_layout->addWidget(decoder_group); | |
182 | ||
183 | QFormLayout *decoder_layout = new QFormLayout(); | |
184 | decoder_group->setLayout(decoder_layout); | |
185 | ||
186 | cb = create_checkbox(GlobalSettings::Key_Dec_InitialStateConfigurable, | |
187 | SLOT(on_dec_initialStateConfigurable_changed(int))); | |
188 | decoder_layout->addRow(tr("Allow configuration of &initial signal state"), cb); | |
189 | ||
190 | return form; | |
191 | #else | |
192 | (void)parent; | |
193 | #endif | |
194 | } | |
195 | ||
196 | #ifdef ENABLE_DECODE | |
197 | static gint sort_pds(gconstpointer a, gconstpointer b) | |
198 | { | |
199 | const struct srd_decoder *sda, *sdb; | |
200 | ||
201 | sda = (const struct srd_decoder *)a; | |
202 | sdb = (const struct srd_decoder *)b; | |
203 | return strcmp(sda->id, sdb->id); | |
204 | } | |
205 | #endif | |
206 | ||
207 | QWidget *Settings::get_about_page(QWidget *parent) const | |
208 | { | |
209 | #ifdef ENABLE_DECODE | |
210 | struct srd_decoder *dec; | |
211 | #endif | |
212 | ||
213 | QLabel *icon = new QLabel(); | |
214 | icon->setPixmap(QPixmap(QString::fromUtf8(":/icons/sigrok-logo-notext.svg"))); | |
215 | ||
216 | /* Setup the version field */ | |
217 | QLabel *version_info = new QLabel(); | |
218 | version_info->setText(tr("%1 %2<br />%3<br /><a href=\"http://%4\">%4</a>") | |
219 | .arg(QApplication::applicationName(), | |
220 | QApplication::applicationVersion(), | |
221 | tr("GNU GPL, version 3 or later"), | |
222 | QApplication::organizationDomain())); | |
223 | version_info->setOpenExternalLinks(true); | |
224 | ||
225 | shared_ptr<sigrok::Context> context = device_manager_.context(); | |
226 | ||
227 | QString s; | |
228 | ||
229 | s.append("<style type=\"text/css\"> tr .id { white-space: pre; padding-right: 5px; } </style>"); | |
230 | ||
231 | s.append("<table>"); | |
232 | ||
233 | /* Library info */ | |
234 | s.append("<tr><td colspan=\"2\"><b>" + | |
235 | tr("Libraries and features:") + "</b></td></tr>"); | |
236 | ||
237 | s.append(QString("<tr><td><i>%1</i></td><td>%2</td></tr>") | |
238 | .arg(QString("Qt"), qVersion())); | |
239 | s.append(QString("<tr><td><i>%1</i></td><td>%2</td></tr>") | |
240 | .arg(QString("glibmm"), PV_GLIBMM_VERSION)); | |
241 | s.append(QString("<tr><td><i>%1</i></td><td>%2</td></tr>") | |
242 | .arg(QString("Boost"), BOOST_LIB_VERSION)); | |
243 | ||
244 | s.append(QString("<tr><td><i>%1</i></td><td>%2/%3 (rt: %4/%5)</td></tr>") | |
245 | .arg(QString("libsigrok"), SR_PACKAGE_VERSION_STRING, | |
246 | SR_LIB_VERSION_STRING, sr_package_version_string_get(), | |
247 | sr_lib_version_string_get())); | |
248 | ||
249 | GSList *l_orig = sr_buildinfo_libs_get(); | |
250 | for (GSList *l = l_orig; l; l = l->next) { | |
251 | GSList *m = (GSList *)l->data; | |
252 | const char *lib = (const char *)m->data; | |
253 | const char *version = (const char *)m->next->data; | |
254 | s.append(QString("<tr><td><i>- %1</i></td><td>%2</td></tr>") | |
255 | .arg(QString(lib), QString(version))); | |
256 | g_slist_free_full(m, g_free); | |
257 | } | |
258 | g_slist_free(l_orig); | |
259 | ||
260 | char *host = sr_buildinfo_host_get(); | |
261 | s.append(QString("<tr><td><i>- Host</i></td><td>%1</td></tr>") | |
262 | .arg(QString(host))); | |
263 | g_free(host); | |
264 | ||
265 | char *scpi_backends = sr_buildinfo_scpi_backends_get(); | |
266 | s.append(QString("<tr><td><i>- SCPI backends</i></td><td>%1</td></tr>") | |
267 | .arg(QString(scpi_backends))); | |
268 | g_free(scpi_backends); | |
269 | ||
270 | #ifdef ENABLE_DECODE | |
271 | s.append(QString("<tr><td><i>%1</i></td><td>%2/%3 (rt: %4/%5)</td></tr>") | |
272 | .arg(QString("libsigrokdecode"), SRD_PACKAGE_VERSION_STRING, | |
273 | SRD_LIB_VERSION_STRING, srd_package_version_string_get(), | |
274 | srd_lib_version_string_get())); | |
275 | ||
276 | l_orig = srd_buildinfo_libs_get(); | |
277 | for (GSList *l = l_orig; l; l = l->next) { | |
278 | GSList *m = (GSList *)l->data; | |
279 | const char *lib = (const char *)m->data; | |
280 | const char *version = (const char *)m->next->data; | |
281 | s.append(QString("<tr><td><i>- %1</i></td><td>%2</td></tr>") | |
282 | .arg(QString(lib), QString(version))); | |
283 | g_slist_free_full(m, g_free); | |
284 | } | |
285 | g_slist_free(l_orig); | |
286 | ||
287 | host = srd_buildinfo_host_get(); | |
288 | s.append(QString("<tr><td><i>- Host</i></td><td>%1</td></tr>") | |
289 | .arg(QString(host))); | |
290 | g_free(host); | |
291 | #endif | |
292 | ||
293 | /* Set up the supported field */ | |
294 | s.append("<tr><td colspan=\"2\"></td></tr>"); | |
295 | s.append("<tr><td colspan=\"2\"><b>" + | |
296 | tr("Supported hardware drivers:") + "</b></td></tr>"); | |
297 | for (auto entry : context->drivers()) { | |
298 | s.append(QString("<tr><td class=\"id\"><i>%1</i></td><td>%2</td></tr>") | |
299 | .arg(QString::fromUtf8(entry.first.c_str()), | |
300 | QString::fromUtf8(entry.second->long_name().c_str()))); | |
301 | } | |
302 | ||
303 | s.append("<tr><td colspan=\"2\"></td></tr>"); | |
304 | s.append("<tr><td colspan=\"2\"><b>" + | |
305 | tr("Supported input formats:") + "</b></td></tr>"); | |
306 | for (auto entry : context->input_formats()) { | |
307 | s.append(QString("<tr><td class=\"id\"><i>%1</i></td><td>%2</td></tr>") | |
308 | .arg(QString::fromUtf8(entry.first.c_str()), | |
309 | QString::fromUtf8(entry.second->description().c_str()))); | |
310 | } | |
311 | ||
312 | s.append("<tr><td colspan=\"2\"></td></tr>"); | |
313 | s.append("<tr><td colspan=\"2\"><b>" + | |
314 | tr("Supported output formats:") + "</b></td></tr>"); | |
315 | for (auto entry : context->output_formats()) { | |
316 | s.append(QString("<tr><td class=\"id\"><i>%1</i></td><td>%2</td></tr>") | |
317 | .arg(QString::fromUtf8(entry.first.c_str()), | |
318 | QString::fromUtf8(entry.second->description().c_str()))); | |
319 | } | |
320 | ||
321 | #ifdef ENABLE_DECODE | |
322 | s.append("<tr><td colspan=\"2\"></td></tr>"); | |
323 | s.append("<tr><td colspan=\"2\"><b>" + | |
324 | tr("Supported protocol decoders:") + "</b></td></tr>"); | |
325 | GSList *sl = g_slist_copy((GSList *)srd_decoder_list()); | |
326 | sl = g_slist_sort(sl, sort_pds); | |
327 | for (const GSList *l = sl; l; l = l->next) { | |
328 | dec = (struct srd_decoder *)l->data; | |
329 | s.append(QString("<tr><td class=\"id\"><i>%1</i></td><td>%2</td></tr>") | |
330 | .arg(QString::fromUtf8(dec->id), | |
331 | QString::fromUtf8(dec->longname))); | |
332 | } | |
333 | g_slist_free(sl); | |
334 | #endif | |
335 | ||
336 | s.append("</table>"); | |
337 | ||
338 | QTextDocument *supported_doc = new QTextDocument(); | |
339 | supported_doc->setHtml(s); | |
340 | ||
341 | QTextBrowser *support_list = new QTextBrowser(); | |
342 | support_list->setDocument(supported_doc); | |
343 | ||
344 | QGridLayout *layout = new QGridLayout(); | |
345 | layout->addWidget(icon, 0, 0, 1, 1); | |
346 | layout->addWidget(version_info, 0, 1, 1, 1); | |
347 | layout->addWidget(support_list, 1, 1, 1, 1); | |
348 | ||
349 | QWidget *page = new QWidget(parent); | |
350 | page->setLayout(layout); | |
351 | ||
352 | return page; | |
353 | } | |
354 | ||
355 | void Settings::accept() | |
356 | { | |
357 | GlobalSettings settings; | |
358 | settings.stop_tracking(); | |
359 | ||
360 | QDialog::accept(); | |
361 | } | |
362 | ||
363 | void Settings::reject() | |
364 | { | |
365 | GlobalSettings settings; | |
366 | settings.undo_tracked_changes(); | |
367 | ||
368 | QDialog::reject(); | |
369 | } | |
370 | ||
371 | void Settings::on_page_changed(QListWidgetItem *current, QListWidgetItem *previous) | |
372 | { | |
373 | if (!current) | |
374 | current = previous; | |
375 | ||
376 | pages->setCurrentIndex(page_list->row(current)); | |
377 | } | |
378 | ||
379 | void Settings::on_view_alwaysZoomToFit_changed(int state) | |
380 | { | |
381 | GlobalSettings settings; | |
382 | settings.setValue(GlobalSettings::Key_View_AlwaysZoomToFit, state ? true : false); | |
383 | } | |
384 | ||
385 | void Settings::on_view_colouredBG_changed(int state) | |
386 | { | |
387 | GlobalSettings settings; | |
388 | settings.setValue(GlobalSettings::Key_View_ColouredBG, state ? true : false); | |
389 | } | |
390 | ||
391 | void Settings::on_view_stickyScrolling_changed(int state) | |
392 | { | |
393 | GlobalSettings settings; | |
394 | settings.setValue(GlobalSettings::Key_View_StickyScrolling, state ? true : false); | |
395 | } | |
396 | ||
397 | void Settings::on_view_showSamplingPoints_changed(int state) | |
398 | { | |
399 | GlobalSettings settings; | |
400 | settings.setValue(GlobalSettings::Key_View_ShowSamplingPoints, state ? true : false); | |
401 | } | |
402 | ||
403 | void Settings::on_view_showAnalogMinorGrid_changed(int state) | |
404 | { | |
405 | GlobalSettings settings; | |
406 | settings.setValue(GlobalSettings::Key_View_ShowAnalogMinorGrid, state ? true : false); | |
407 | } | |
408 | ||
409 | void Settings::on_dec_initialStateConfigurable_changed(int state) | |
410 | { | |
411 | GlobalSettings settings; | |
412 | settings.setValue(GlobalSettings::Key_Dec_InitialStateConfigurable, state ? true : false); | |
413 | } | |
414 | ||
415 | } // namespace dialogs | |
416 | } // namespace pv |