Provide a settings checkbox for showing zero at the trigger
[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_TriggerIsZeroTime,
164                 SLOT(on_view_triggerIsZero_changed(int)));
165         trace_view_layout->addRow(tr("Show time zero at the trigger"), cb);
166
167         cb = create_checkbox(GlobalSettings::Key_View_StickyScrolling,
168                 SLOT(on_view_stickyScrolling_changed(int)));
169         trace_view_layout->addRow(tr("Always keep &newest samples at the right edge during capture"), cb);
170
171         cb = create_checkbox(GlobalSettings::Key_View_ShowSamplingPoints,
172                 SLOT(on_view_showSamplingPoints_changed(int)));
173         trace_view_layout->addRow(tr("Show data &sampling points"), cb);
174
175         cb = create_checkbox(GlobalSettings::Key_View_ShowAnalogMinorGrid,
176                 SLOT(on_view_showAnalogMinorGrid_changed(int)));
177         trace_view_layout->addRow(tr("Show analog minor grid in addition to div grid"), cb);
178
179         QComboBox *thr_disp_mode_cb = new QComboBox();
180         thr_disp_mode_cb->addItem(tr("None"), GlobalSettings::ConvThrDispMode_None);
181         thr_disp_mode_cb->addItem(tr("Background"), GlobalSettings::ConvThrDispMode_Background);
182         thr_disp_mode_cb->addItem(tr("Dots"), GlobalSettings::ConvThrDispMode_Dots);
183         thr_disp_mode_cb->setCurrentIndex(
184                 settings.value(GlobalSettings::Key_View_ConversionThresholdDispMode).toInt());
185         connect(thr_disp_mode_cb, SIGNAL(currentIndexChanged(int)),
186                 this, SLOT(on_view_conversionThresholdDispMode_changed(int)));
187         trace_view_layout->addRow(tr("Conversion threshold display mode (analog traces only)"), thr_disp_mode_cb);
188
189         QSpinBox *default_div_height_sb = new QSpinBox();
190         default_div_height_sb->setRange(20, 1000);
191         default_div_height_sb->setSuffix(tr(" pixels"));
192         default_div_height_sb->setValue(
193                 settings.value(GlobalSettings::Key_View_DefaultDivHeight).toInt());
194         connect(default_div_height_sb, SIGNAL(valueChanged(int)), this,
195                 SLOT(on_view_defaultDivHeight_changed(int)));
196         trace_view_layout->addRow(tr("Default analog trace div height"), default_div_height_sb);
197
198         QSpinBox *default_logic_height_sb = new QSpinBox();
199         default_logic_height_sb->setRange(5, 1000);
200         default_logic_height_sb->setSuffix(tr(" pixels"));
201         default_logic_height_sb->setValue(
202                 settings.value(GlobalSettings::Key_View_DefaultLogicHeight).toInt());
203         connect(default_logic_height_sb, SIGNAL(valueChanged(int)), this,
204                 SLOT(on_view_defaultLogicHeight_changed(int)));
205         trace_view_layout->addRow(tr("Default logic trace height"), default_logic_height_sb);
206
207         return form;
208 }
209
210 QWidget *Settings::get_decoder_settings_form(QWidget *parent) const
211 {
212 #ifdef ENABLE_DECODE
213         QCheckBox *cb;
214
215         QWidget *form = new QWidget(parent);
216         QVBoxLayout *form_layout = new QVBoxLayout(form);
217
218         // Decoder settings
219         QGroupBox *decoder_group = new QGroupBox(tr("Decoders"));
220         form_layout->addWidget(decoder_group);
221
222         QFormLayout *decoder_layout = new QFormLayout();
223         decoder_group->setLayout(decoder_layout);
224
225         cb = create_checkbox(GlobalSettings::Key_Dec_InitialStateConfigurable,
226                 SLOT(on_dec_initialStateConfigurable_changed(int)));
227         decoder_layout->addRow(tr("Allow configuration of &initial signal state"), cb);
228
229         return form;
230 #else
231         (void)parent;
232         return nullptr;
233 #endif
234 }
235
236 #ifdef ENABLE_DECODE
237 static gint sort_pds(gconstpointer a, gconstpointer b)
238 {
239         const struct srd_decoder *sda, *sdb;
240
241         sda = (const struct srd_decoder *)a;
242         sdb = (const struct srd_decoder *)b;
243         return strcmp(sda->id, sdb->id);
244 }
245 #endif
246
247 QWidget *Settings::get_about_page(QWidget *parent) const
248 {
249 #ifdef ENABLE_DECODE
250         struct srd_decoder *dec;
251 #endif
252
253         QLabel *icon = new QLabel();
254         icon->setPixmap(QPixmap(QString::fromUtf8(":/icons/pulseview.svg")));
255
256         /* Setup the version field */
257         QLabel *version_info = new QLabel();
258         version_info->setText(tr("%1 %2<br />%3<br /><a href=\"http://%4\">%4</a>")
259                 .arg(QApplication::applicationName(),
260                 QApplication::applicationVersion(),
261                 tr("GNU GPL, version 3 or later"),
262                 QApplication::organizationDomain()));
263         version_info->setOpenExternalLinks(true);
264
265         shared_ptr<sigrok::Context> context = device_manager_.context();
266
267         QString s;
268
269         s.append("<style type=\"text/css\"> tr .id { white-space: pre; padding-right: 5px; } </style>");
270
271         s.append("<table>");
272
273         /* Library info */
274         s.append("<tr><td colspan=\"2\"><b>" +
275                 tr("Libraries and features:") + "</b></td></tr>");
276
277         s.append(QString("<tr><td><i>%1</i></td><td>%2</td></tr>")
278                 .arg(QString("Qt"), qVersion()));
279         s.append(QString("<tr><td><i>%1</i></td><td>%2</td></tr>")
280                 .arg(QString("glibmm"), PV_GLIBMM_VERSION));
281         s.append(QString("<tr><td><i>%1</i></td><td>%2</td></tr>")
282                 .arg(QString("Boost"), BOOST_LIB_VERSION));
283
284         s.append(QString("<tr><td><i>%1</i></td><td>%2/%3 (rt: %4/%5)</td></tr>")
285                 .arg(QString("libsigrok"), SR_PACKAGE_VERSION_STRING,
286                 SR_LIB_VERSION_STRING, sr_package_version_string_get(),
287                 sr_lib_version_string_get()));
288
289         GSList *l_orig = sr_buildinfo_libs_get();
290         for (GSList *l = l_orig; l; l = l->next) {
291                 GSList *m = (GSList *)l->data;
292                 const char *lib = (const char *)m->data;
293                 const char *version = (const char *)m->next->data;
294                 s.append(QString("<tr><td><i>- %1</i></td><td>%2</td></tr>")
295                         .arg(QString(lib), QString(version)));
296                 g_slist_free_full(m, g_free);
297         }
298         g_slist_free(l_orig);
299
300         char *host = sr_buildinfo_host_get();
301         s.append(QString("<tr><td><i>- Host</i></td><td>%1</td></tr>")
302                 .arg(QString(host)));
303         g_free(host);
304
305         char *scpi_backends = sr_buildinfo_scpi_backends_get();
306         s.append(QString("<tr><td><i>- SCPI backends</i></td><td>%1</td></tr>")
307                 .arg(QString(scpi_backends)));
308         g_free(scpi_backends);
309
310 #ifdef ENABLE_DECODE
311         s.append(QString("<tr><td><i>%1</i></td><td>%2/%3 (rt: %4/%5)</td></tr>")
312                 .arg(QString("libsigrokdecode"), SRD_PACKAGE_VERSION_STRING,
313                 SRD_LIB_VERSION_STRING, srd_package_version_string_get(),
314                 srd_lib_version_string_get()));
315
316         l_orig = srd_buildinfo_libs_get();
317         for (GSList *l = l_orig; l; l = l->next) {
318                 GSList *m = (GSList *)l->data;
319                 const char *lib = (const char *)m->data;
320                 const char *version = (const char *)m->next->data;
321                 s.append(QString("<tr><td><i>- %1</i></td><td>%2</td></tr>")
322                         .arg(QString(lib), QString(version)));
323                 g_slist_free_full(m, g_free);
324         }
325         g_slist_free(l_orig);
326
327         host = srd_buildinfo_host_get();
328         s.append(QString("<tr><td><i>- Host</i></td><td>%1</td></tr>")
329                 .arg(QString(host)));
330         g_free(host);
331 #endif
332
333         /* Set up the supported field */
334         s.append("<tr><td colspan=\"2\"></td></tr>");
335         s.append("<tr><td colspan=\"2\"><b>" +
336                 tr("Supported hardware drivers:") + "</b></td></tr>");
337         for (auto entry : context->drivers()) {
338                 s.append(QString("<tr><td class=\"id\"><i>%1</i></td><td>%2</td></tr>")
339                         .arg(QString::fromUtf8(entry.first.c_str()),
340                                 QString::fromUtf8(entry.second->long_name().c_str())));
341         }
342
343         s.append("<tr><td colspan=\"2\"></td></tr>");
344         s.append("<tr><td colspan=\"2\"><b>" +
345                 tr("Supported input formats:") + "</b></td></tr>");
346         for (auto entry : context->input_formats()) {
347                 s.append(QString("<tr><td class=\"id\"><i>%1</i></td><td>%2</td></tr>")
348                         .arg(QString::fromUtf8(entry.first.c_str()),
349                                 QString::fromUtf8(entry.second->description().c_str())));
350         }
351
352         s.append("<tr><td colspan=\"2\"></td></tr>");
353         s.append("<tr><td colspan=\"2\"><b>" +
354                 tr("Supported output formats:") + "</b></td></tr>");
355         for (auto entry : context->output_formats()) {
356                 s.append(QString("<tr><td class=\"id\"><i>%1</i></td><td>%2</td></tr>")
357                         .arg(QString::fromUtf8(entry.first.c_str()),
358                                 QString::fromUtf8(entry.second->description().c_str())));
359         }
360
361 #ifdef ENABLE_DECODE
362         s.append("<tr><td colspan=\"2\"></td></tr>");
363         s.append("<tr><td colspan=\"2\"><b>" +
364                 tr("Supported protocol decoders:") + "</b></td></tr>");
365         GSList *sl = g_slist_copy((GSList *)srd_decoder_list());
366         sl = g_slist_sort(sl, sort_pds);
367         for (const GSList *l = sl; l; l = l->next) {
368                 dec = (struct srd_decoder *)l->data;
369                 s.append(QString("<tr><td class=\"id\"><i>%1</i></td><td>%2</td></tr>")
370                         .arg(QString::fromUtf8(dec->id),
371                                 QString::fromUtf8(dec->longname)));
372         }
373         g_slist_free(sl);
374 #endif
375
376         s.append("</table>");
377
378         QTextDocument *supported_doc = new QTextDocument();
379         supported_doc->setHtml(s);
380
381         QTextBrowser *support_list = new QTextBrowser();
382         support_list->setDocument(supported_doc);
383
384         QGridLayout *layout = new QGridLayout();
385         layout->addWidget(icon, 0, 0, 1, 1);
386         layout->addWidget(version_info, 0, 1, 1, 1);
387         layout->addWidget(support_list, 1, 1, 1, 1);
388
389         QWidget *page = new QWidget(parent);
390         page->setLayout(layout);
391
392         return page;
393 }
394
395 void Settings::accept()
396 {
397         GlobalSettings settings;
398         settings.stop_tracking();
399
400         QDialog::accept();
401 }
402
403 void Settings::reject()
404 {
405         GlobalSettings settings;
406         settings.undo_tracked_changes();
407
408         QDialog::reject();
409 }
410
411 void Settings::on_page_changed(QListWidgetItem *current, QListWidgetItem *previous)
412 {
413         if (!current)
414                 current = previous;
415
416         pages->setCurrentIndex(page_list->row(current));
417 }
418
419 void Settings::on_view_zoomToFitDuringAcq_changed(int state)
420 {
421         GlobalSettings settings;
422         settings.setValue(GlobalSettings::Key_View_ZoomToFitDuringAcq, state ? true : false);
423 }
424
425 void Settings::on_view_zoomToFitAfterAcq_changed(int state)
426 {
427         GlobalSettings settings;
428         settings.setValue(GlobalSettings::Key_View_ZoomToFitAfterAcq, state ? true : false);
429 }
430
431 void Settings::on_view_triggerIsZero_changed(int state)
432 {
433         GlobalSettings settings;
434         settings.setValue(GlobalSettings::Key_View_TriggerIsZeroTime, state ? true : false);
435 }
436
437 void Settings::on_view_colouredBG_changed(int state)
438 {
439         GlobalSettings settings;
440         settings.setValue(GlobalSettings::Key_View_ColouredBG, state ? true : false);
441 }
442
443 void Settings::on_view_stickyScrolling_changed(int state)
444 {
445         GlobalSettings settings;
446         settings.setValue(GlobalSettings::Key_View_StickyScrolling, state ? true : false);
447 }
448
449 void Settings::on_view_showSamplingPoints_changed(int state)
450 {
451         GlobalSettings settings;
452         settings.setValue(GlobalSettings::Key_View_ShowSamplingPoints, state ? true : false);
453 }
454
455 void Settings::on_view_showAnalogMinorGrid_changed(int state)
456 {
457         GlobalSettings settings;
458         settings.setValue(GlobalSettings::Key_View_ShowAnalogMinorGrid, state ? true : false);
459 }
460
461 void Settings::on_view_conversionThresholdDispMode_changed(int state)
462 {
463         GlobalSettings settings;
464         settings.setValue(GlobalSettings::Key_View_ConversionThresholdDispMode, state);
465 }
466
467 void Settings::on_view_defaultDivHeight_changed(int value)
468 {
469         GlobalSettings settings;
470         settings.setValue(GlobalSettings::Key_View_DefaultDivHeight, value);
471 }
472
473 void Settings::on_view_defaultLogicHeight_changed(int value)
474 {
475         GlobalSettings settings;
476         settings.setValue(GlobalSettings::Key_View_DefaultLogicHeight, value);
477 }
478
479 void Settings::on_dec_initialStateConfigurable_changed(int state)
480 {
481         GlobalSettings settings;
482         settings.setValue(GlobalSettings::Key_Dec_InitialStateConfigurable, state ? true : false);
483 }
484
485 } // namespace dialogs
486 } // namespace pv