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