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