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