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