device.cpp: Add SR_CONF_AVG_SAMPLES.
[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 <QFileDialog>
29 #include <QFormLayout>
30 #include <QGroupBox>
31 #include <QHBoxLayout>
32 #include <QLabel>
33 #include <QMainWindow>
34 #include <QMessageBox>
35 #include <QPushButton>
36 #include <QScrollBar>
37 #include <QSpinBox>
38 #include <QString>
39 #include <QTextBrowser>
40 #include <QTextDocument>
41 #include <QTextStream>
42 #include <QVBoxLayout>
43
44 #include "settings.hpp"
45
46 #include "pv/devicemanager.hpp"
47 #include "pv/globalsettings.hpp"
48 #include "pv/logging.hpp"
49
50 #include <libsigrokcxx/libsigrokcxx.hpp>
51
52 #ifdef ENABLE_DECODE
53 #include <libsigrokdecode/libsigrokdecode.h>
54 #endif
55
56 using std::shared_ptr;
57
58 namespace pv {
59 namespace dialogs {
60
61 /**
62  * Special version of a QListView that has the width of the first column as minimum size.
63  *
64  * @note Inspired by https://github.com/qt-creator/qt-creator/blob/master/src/plugins/coreplugin/dialogs/settingsdialog.cpp
65  */
66 class PageListWidget: public QListWidget
67 {
68 public:
69         PageListWidget() :
70                 QListWidget()
71         {
72                 setSizePolicy(QSizePolicy::MinimumExpanding, QSizePolicy::Expanding);
73         }
74
75         QSize sizeHint() const final
76         {
77                 int width = sizeHintForColumn(0) + frameWidth() * 2 + 5;
78                 if (verticalScrollBar()->isVisible())
79                         width += verticalScrollBar()->width();
80                 return QSize(width, 100);
81         }
82 };
83
84 Settings::Settings(DeviceManager &device_manager, QWidget *parent) :
85         QDialog(parent, nullptr),
86         device_manager_(device_manager)
87 {
88         resize(600, 400);
89
90         // Create log view
91         log_view_ = create_log_view();
92
93         // Create pages
94         page_list = new PageListWidget();
95         page_list->setViewMode(QListView::ListMode);
96         page_list->setMovement(QListView::Static);
97         page_list->setHorizontalScrollBarPolicy(Qt::ScrollBarAlwaysOff);
98
99         pages = new QStackedWidget;
100         create_pages();
101         page_list->setCurrentIndex(page_list->model()->index(0, 0));
102
103         // Create the rest of the dialog
104         QHBoxLayout *tab_layout = new QHBoxLayout;
105         tab_layout->addWidget(page_list);
106         tab_layout->addWidget(pages, Qt::AlignLeft);
107
108         QDialogButtonBox *button_box = new QDialogButtonBox(
109                 QDialogButtonBox::Ok | QDialogButtonBox::Cancel);
110
111         QVBoxLayout* root_layout = new QVBoxLayout(this);
112         root_layout->addLayout(tab_layout);
113         root_layout->addWidget(button_box);
114
115         connect(button_box, SIGNAL(accepted()), this, SLOT(accept()));
116         connect(button_box, SIGNAL(rejected()), this, SLOT(reject()));
117         connect(page_list, SIGNAL(currentItemChanged(QListWidgetItem*, QListWidgetItem*)),
118                 this, SLOT(on_page_changed(QListWidgetItem*, QListWidgetItem*)));
119
120         // Start to record changes
121         GlobalSettings settings;
122         settings.start_tracking();
123 }
124
125 void Settings::create_pages()
126 {
127         // View page
128         pages->addWidget(get_view_settings_form(pages));
129
130         QListWidgetItem *viewButton = new QListWidgetItem(page_list);
131         viewButton->setIcon(QIcon(":/icons/settings-views.svg"));
132         viewButton->setText(tr("Views"));
133         viewButton->setTextAlignment(Qt::AlignVCenter);
134         viewButton->setFlags(Qt::ItemIsSelectable | Qt::ItemIsEnabled);
135
136 #ifdef ENABLE_DECODE
137         // Decoder page
138         pages->addWidget(get_decoder_settings_form(pages));
139
140         QListWidgetItem *decoderButton = new QListWidgetItem(page_list);
141         decoderButton->setIcon(QIcon(":/icons/add-decoder.svg"));
142         decoderButton->setText(tr("Decoders"));
143         decoderButton->setTextAlignment(Qt::AlignVCenter);
144         decoderButton->setFlags(Qt::ItemIsSelectable | Qt::ItemIsEnabled);
145 #endif
146
147         // About page
148         pages->addWidget(get_about_page(pages));
149
150         QListWidgetItem *aboutButton = new QListWidgetItem(page_list);
151         aboutButton->setIcon(QIcon(":/icons/information.svg"));
152         aboutButton->setText(tr("About"));
153         aboutButton->setTextAlignment(Qt::AlignVCenter);
154         aboutButton->setFlags(Qt::ItemIsSelectable | Qt::ItemIsEnabled);
155
156         // Logging page
157         pages->addWidget(get_logging_page(pages));
158
159         QListWidgetItem *loggingButton = new QListWidgetItem(page_list);
160         loggingButton->setIcon(QIcon(":/icons/information.svg"));
161         loggingButton->setText(tr("Logging"));
162         loggingButton->setTextAlignment(Qt::AlignVCenter);
163         loggingButton->setFlags(Qt::ItemIsSelectable | Qt::ItemIsEnabled);
164 }
165
166 QCheckBox *Settings::create_checkbox(const QString& key, const char* slot) const
167 {
168         GlobalSettings settings;
169
170         QCheckBox *cb = new QCheckBox();
171         cb->setChecked(settings.value(key).toBool());
172         connect(cb, SIGNAL(stateChanged(int)), this, slot);
173         return cb;
174 }
175
176 QPlainTextEdit *Settings::create_log_view() const
177 {
178         GlobalSettings settings;
179
180         QPlainTextEdit *log_view = new QPlainTextEdit();
181
182         log_view->setReadOnly(true);
183         log_view->setWordWrapMode(QTextOption::NoWrap);
184         log_view->setCenterOnScroll(true);
185
186         log_view->appendHtml(logging.get_log());
187         connect(&logging, SIGNAL(logged_text(QString)),
188                 log_view, SLOT(appendHtml(QString)));
189
190         return log_view;
191 }
192
193 QWidget *Settings::get_view_settings_form(QWidget *parent) const
194 {
195         GlobalSettings settings;
196         QCheckBox *cb;
197
198         QWidget *form = new QWidget(parent);
199         QVBoxLayout *form_layout = new QVBoxLayout(form);
200
201         // Trace view settings
202         QGroupBox *trace_view_group = new QGroupBox(tr("Trace View"));
203         form_layout->addWidget(trace_view_group);
204
205         QFormLayout *trace_view_layout = new QFormLayout();
206         trace_view_group->setLayout(trace_view_layout);
207
208         cb = create_checkbox(GlobalSettings::Key_View_ColoredBG,
209                 SLOT(on_view_coloredBG_changed(int)));
210         trace_view_layout->addRow(tr("Use colored trace &background"), cb);
211
212         cb = create_checkbox(GlobalSettings::Key_View_ZoomToFitDuringAcq,
213                 SLOT(on_view_zoomToFitDuringAcq_changed(int)));
214         trace_view_layout->addRow(tr("Constantly perform &zoom-to-fit during acquisition"), cb);
215
216         cb = create_checkbox(GlobalSettings::Key_View_ZoomToFitAfterAcq,
217                 SLOT(on_view_zoomToFitAfterAcq_changed(int)));
218         trace_view_layout->addRow(tr("Perform a zoom-to-&fit when acquisition stops"), cb);
219
220         cb = create_checkbox(GlobalSettings::Key_View_TriggerIsZeroTime,
221                 SLOT(on_view_triggerIsZero_changed(int)));
222         trace_view_layout->addRow(tr("Show time zero at the trigger"), cb);
223
224         cb = create_checkbox(GlobalSettings::Key_View_StickyScrolling,
225                 SLOT(on_view_stickyScrolling_changed(int)));
226         trace_view_layout->addRow(tr("Always keep &newest samples at the right edge during capture"), cb);
227
228         cb = create_checkbox(GlobalSettings::Key_View_ShowSamplingPoints,
229                 SLOT(on_view_showSamplingPoints_changed(int)));
230         trace_view_layout->addRow(tr("Show data &sampling points"), cb);
231
232         cb = create_checkbox(GlobalSettings::Key_View_ShowAnalogMinorGrid,
233                 SLOT(on_view_showAnalogMinorGrid_changed(int)));
234         trace_view_layout->addRow(tr("Show analog minor grid in addition to div grid"), cb);
235
236         cb = create_checkbox(GlobalSettings::Key_View_ShowHoverMarker,
237                 SLOT(on_view_showHoverMarker_changed(int)));
238         trace_view_layout->addRow(tr("Highlight mouse cursor using a vertical marker line"), cb);
239
240         QComboBox *thr_disp_mode_cb = new QComboBox();
241         thr_disp_mode_cb->addItem(tr("None"), GlobalSettings::ConvThrDispMode_None);
242         thr_disp_mode_cb->addItem(tr("Background"), GlobalSettings::ConvThrDispMode_Background);
243         thr_disp_mode_cb->addItem(tr("Dots"), GlobalSettings::ConvThrDispMode_Dots);
244         thr_disp_mode_cb->setCurrentIndex(
245                 settings.value(GlobalSettings::Key_View_ConversionThresholdDispMode).toInt());
246         connect(thr_disp_mode_cb, SIGNAL(currentIndexChanged(int)),
247                 this, SLOT(on_view_conversionThresholdDispMode_changed(int)));
248         trace_view_layout->addRow(tr("Conversion threshold display mode (analog traces only)"), thr_disp_mode_cb);
249
250         QSpinBox *default_div_height_sb = new QSpinBox();
251         default_div_height_sb->setRange(20, 1000);
252         default_div_height_sb->setSuffix(tr(" pixels"));
253         default_div_height_sb->setValue(
254                 settings.value(GlobalSettings::Key_View_DefaultDivHeight).toInt());
255         connect(default_div_height_sb, SIGNAL(valueChanged(int)), this,
256                 SLOT(on_view_defaultDivHeight_changed(int)));
257         trace_view_layout->addRow(tr("Default analog trace div height"), default_div_height_sb);
258
259         QSpinBox *default_logic_height_sb = new QSpinBox();
260         default_logic_height_sb->setRange(5, 1000);
261         default_logic_height_sb->setSuffix(tr(" pixels"));
262         default_logic_height_sb->setValue(
263                 settings.value(GlobalSettings::Key_View_DefaultLogicHeight).toInt());
264         connect(default_logic_height_sb, SIGNAL(valueChanged(int)), this,
265                 SLOT(on_view_defaultLogicHeight_changed(int)));
266         trace_view_layout->addRow(tr("Default logic trace height"), default_logic_height_sb);
267
268         return form;
269 }
270
271 QWidget *Settings::get_decoder_settings_form(QWidget *parent)
272 {
273 #ifdef ENABLE_DECODE
274         GlobalSettings settings;
275         QCheckBox *cb;
276
277         QWidget *form = new QWidget(parent);
278         QVBoxLayout *form_layout = new QVBoxLayout(form);
279
280         // Decoder settings
281         QGroupBox *decoder_group = new QGroupBox(tr("Decoders"));
282         form_layout->addWidget(decoder_group);
283
284         QFormLayout *decoder_layout = new QFormLayout();
285         decoder_group->setLayout(decoder_layout);
286
287         cb = create_checkbox(GlobalSettings::Key_Dec_InitialStateConfigurable,
288                 SLOT(on_dec_initialStateConfigurable_changed(int)));
289         decoder_layout->addRow(tr("Allow configuration of &initial signal state"), cb);
290
291         // Annotation export settings
292         ann_export_format_ = new QLineEdit();
293         ann_export_format_->setText(
294                 settings.value(GlobalSettings::Key_Dec_ExportFormat).toString());
295         connect(ann_export_format_, SIGNAL(textChanged(const QString&)),
296                 this, SLOT(on_dec_exportFormat_changed(const QString&)));
297         decoder_layout->addRow(tr("Annotation export format"), ann_export_format_);
298         QLabel *description_1 = new QLabel(tr("%s = sample range; %d: decoder name; %c: row name; %q: use quotations marks"));
299         description_1->setAlignment(Qt::AlignRight);
300         decoder_layout->addRow(description_1);
301         QLabel *description_2 = new QLabel(tr("%1: longest annotation text; %a: all annotation texts"));
302         description_2->setAlignment(Qt::AlignRight);
303         decoder_layout->addRow(description_2);
304
305         return form;
306 #else
307         (void)parent;
308         return nullptr;
309 #endif
310 }
311
312 #ifdef ENABLE_DECODE
313 static gint sort_pds(gconstpointer a, gconstpointer b)
314 {
315         const struct srd_decoder *sda, *sdb;
316
317         sda = (const struct srd_decoder *)a;
318         sdb = (const struct srd_decoder *)b;
319         return strcmp(sda->id, sdb->id);
320 }
321 #endif
322
323 QWidget *Settings::get_about_page(QWidget *parent) const
324 {
325 #ifdef ENABLE_DECODE
326         struct srd_decoder *dec;
327 #endif
328
329         QLabel *icon = new QLabel();
330         icon->setPixmap(QPixmap(QString::fromUtf8(":/icons/pulseview.svg")));
331
332         /* Setup the license field, with the project homepage link. */
333         QLabel *gpl_home_info = new QLabel();
334         gpl_home_info->setText(tr("%1<br /><a href=\"http://%2\">%2</a>").arg(
335                 tr("GNU GPL, version 3 or later"),
336                 QApplication::organizationDomain()));
337         gpl_home_info->setOpenExternalLinks(true);
338
339         shared_ptr<sigrok::Context> context = device_manager_.context();
340
341         QString s;
342
343         s.append("<style type=\"text/css\"> tr .id { white-space: pre; padding-right: 5px; } </style>");
344
345         s.append("<table>");
346
347         /* Version, library, and feature info */
348         s.append("<tr><td colspan=\"2\"><b>" +
349                 tr("Versions, libraries and features:") + "</b></td></tr>");
350
351         s.append(QString("<tr><td><i>%1</i></td><td>%2</td></tr>")
352                 .arg(QApplication::applicationName(),
353                 QApplication::applicationVersion()));
354         s.append(QString("<tr><td><i>%1</i></td><td>%2</td></tr>")
355                 .arg(QString("Qt"), qVersion()));
356         s.append(QString("<tr><td><i>%1</i></td><td>%2</td></tr>")
357                 .arg(QString("glibmm"), PV_GLIBMM_VERSION));
358         s.append(QString("<tr><td><i>%1</i></td><td>%2</td></tr>")
359                 .arg(QString("Boost"), BOOST_LIB_VERSION));
360
361         s.append(QString("<tr><td><i>%1</i></td><td>%2/%3 (rt: %4/%5)</td></tr>")
362                 .arg(QString("libsigrok"), SR_PACKAGE_VERSION_STRING,
363                 SR_LIB_VERSION_STRING, sr_package_version_string_get(),
364                 sr_lib_version_string_get()));
365
366         GSList *l_orig = sr_buildinfo_libs_get();
367         for (GSList *l = l_orig; l; l = l->next) {
368                 GSList *m = (GSList *)l->data;
369                 const char *lib = (const char *)m->data;
370                 const char *version = (const char *)m->next->data;
371                 s.append(QString("<tr><td><i>- %1</i></td><td>%2</td></tr>")
372                         .arg(QString(lib), QString(version)));
373                 g_slist_free_full(m, g_free);
374         }
375         g_slist_free(l_orig);
376
377         char *host = sr_buildinfo_host_get();
378         s.append(QString("<tr><td><i>- Host</i></td><td>%1</td></tr>")
379                 .arg(QString(host)));
380         g_free(host);
381
382         char *scpi_backends = sr_buildinfo_scpi_backends_get();
383         s.append(QString("<tr><td><i>- SCPI backends</i></td><td>%1</td></tr>")
384                 .arg(QString(scpi_backends)));
385         g_free(scpi_backends);
386
387 #ifdef ENABLE_DECODE
388         s.append(QString("<tr><td><i>%1</i></td><td>%2/%3 (rt: %4/%5)</td></tr>")
389                 .arg(QString("libsigrokdecode"), SRD_PACKAGE_VERSION_STRING,
390                 SRD_LIB_VERSION_STRING, srd_package_version_string_get(),
391                 srd_lib_version_string_get()));
392
393         l_orig = srd_buildinfo_libs_get();
394         for (GSList *l = l_orig; l; l = l->next) {
395                 GSList *m = (GSList *)l->data;
396                 const char *lib = (const char *)m->data;
397                 const char *version = (const char *)m->next->data;
398                 s.append(QString("<tr><td><i>- %1</i></td><td>%2</td></tr>")
399                         .arg(QString(lib), QString(version)));
400                 g_slist_free_full(m, g_free);
401         }
402         g_slist_free(l_orig);
403
404         host = srd_buildinfo_host_get();
405         s.append(QString("<tr><td><i>- Host</i></td><td>%1</td></tr>")
406                 .arg(QString(host)));
407         g_free(host);
408 #endif
409
410         s.append("<tr><td colspan=\"2\"></td></tr>");
411         s.append("<tr><td colspan=\"2\"><b>" +
412                 tr("Firmware search paths:") + "</b></td></tr>");
413
414         l_orig = sr_resourcepaths_get(SR_RESOURCE_FIRMWARE);
415         for (GSList *l = l_orig; l; l = l->next)
416                 s.append(QString("<tr><td colspan=\"2\">%1</td></tr>").arg(
417                         QString((char*)l->data)));
418         g_slist_free_full(l_orig, g_free);
419
420 #ifdef ENABLE_DECODE
421         s.append("<tr><td colspan=\"2\"></td></tr>");
422         s.append("<tr><td colspan=\"2\"><b>" +
423                 tr("Protocol decoder search paths:") + "</b></td></tr>");
424
425         l_orig = srd_searchpaths_get();
426         for (GSList *l = l_orig; l; l = l->next)
427                 s.append(QString("<tr><td colspan=\"2\">%1</td></tr>").arg(
428                         QString((char*)l->data)));
429         g_slist_free_full(l_orig, g_free);
430 #endif
431
432         /* Set up the supported field */
433         s.append("<tr><td colspan=\"2\"></td></tr>");
434         s.append("<tr><td colspan=\"2\"><b>" +
435                 tr("Supported hardware drivers:") + "</b></td></tr>");
436         for (auto entry : context->drivers()) {
437                 s.append(QString("<tr><td class=\"id\"><i>%1</i></td><td>%2</td></tr>")
438                         .arg(QString::fromUtf8(entry.first.c_str()),
439                                 QString::fromUtf8(entry.second->long_name().c_str())));
440         }
441
442         s.append("<tr><td colspan=\"2\"></td></tr>");
443         s.append("<tr><td colspan=\"2\"><b>" +
444                 tr("Supported input formats:") + "</b></td></tr>");
445         for (auto entry : context->input_formats()) {
446                 s.append(QString("<tr><td class=\"id\"><i>%1</i></td><td>%2</td></tr>")
447                         .arg(QString::fromUtf8(entry.first.c_str()),
448                                 QString::fromUtf8(entry.second->description().c_str())));
449         }
450
451         s.append("<tr><td colspan=\"2\"></td></tr>");
452         s.append("<tr><td colspan=\"2\"><b>" +
453                 tr("Supported output formats:") + "</b></td></tr>");
454         for (auto entry : context->output_formats()) {
455                 s.append(QString("<tr><td class=\"id\"><i>%1</i></td><td>%2</td></tr>")
456                         .arg(QString::fromUtf8(entry.first.c_str()),
457                                 QString::fromUtf8(entry.second->description().c_str())));
458         }
459
460 #ifdef ENABLE_DECODE
461         s.append("<tr><td colspan=\"2\"></td></tr>");
462         s.append("<tr><td colspan=\"2\"><b>" +
463                 tr("Supported protocol decoders:") + "</b></td></tr>");
464         GSList *sl = g_slist_copy((GSList *)srd_decoder_list());
465         sl = g_slist_sort(sl, sort_pds);
466         for (const GSList *l = sl; l; l = l->next) {
467                 dec = (struct srd_decoder *)l->data;
468                 s.append(QString("<tr><td class=\"id\"><i>%1</i></td><td>%2</td></tr>")
469                         .arg(QString::fromUtf8(dec->id),
470                                 QString::fromUtf8(dec->longname)));
471         }
472         g_slist_free(sl);
473 #endif
474
475         s.append("</table>");
476
477         QTextDocument *supported_doc = new QTextDocument();
478         supported_doc->setHtml(s);
479
480         QTextBrowser *support_list = new QTextBrowser();
481         support_list->setDocument(supported_doc);
482
483         QHBoxLayout *h_layout = new QHBoxLayout();
484         h_layout->setAlignment(Qt::AlignLeft);
485         h_layout->addWidget(icon);
486         h_layout->addWidget(gpl_home_info);
487
488         QVBoxLayout *layout = new QVBoxLayout();
489         layout->addLayout(h_layout);
490         layout->addWidget(support_list);
491
492         QWidget *page = new QWidget(parent);
493         page->setLayout(layout);
494
495         return page;
496 }
497
498 QWidget *Settings::get_logging_page(QWidget *parent) const
499 {
500         GlobalSettings settings;
501
502         // Log level
503         QSpinBox *loglevel_sb = new QSpinBox();
504         loglevel_sb->setMaximum(SR_LOG_SPEW);
505         loglevel_sb->setValue(logging.get_log_level());
506         connect(loglevel_sb, SIGNAL(valueChanged(int)), this,
507                 SLOT(on_log_logLevel_changed(int)));
508
509         QHBoxLayout *loglevel_layout = new QHBoxLayout();
510         loglevel_layout->addWidget(new QLabel(tr("Log level:")));
511         loglevel_layout->addWidget(loglevel_sb);
512
513         // Background buffer size
514         QSpinBox *buffersize_sb = new QSpinBox();
515         buffersize_sb->setSuffix(tr(" lines"));
516         buffersize_sb->setMinimum(Logging::MIN_BUFFER_SIZE);
517         buffersize_sb->setMaximum(Logging::MAX_BUFFER_SIZE);
518         buffersize_sb->setValue(
519                 settings.value(GlobalSettings::Key_Log_BufferSize).toInt());
520         connect(buffersize_sb, SIGNAL(valueChanged(int)), this,
521                 SLOT(on_log_bufferSize_changed(int)));
522
523         QHBoxLayout *buffersize_layout = new QHBoxLayout();
524         buffersize_layout->addWidget(new QLabel(tr("Length of background buffer:")));
525         buffersize_layout->addWidget(buffersize_sb);
526
527         // Save to file
528         QPushButton *save_log_pb = new QPushButton(
529                 QIcon::fromTheme("document-save-as", QIcon(":/icons/document-save-as.png")),
530                 tr("&Save to File"));
531         connect(save_log_pb, SIGNAL(clicked(bool)),
532                 this, SLOT(on_log_saveToFile_clicked(bool)));
533
534         // Pop out
535         QPushButton *pop_out_pb = new QPushButton(
536                 QIcon::fromTheme("window-new", QIcon(":/icons/window-new.png")),
537                 tr("&Pop out"));
538         connect(pop_out_pb, SIGNAL(clicked(bool)),
539                 this, SLOT(on_log_popOut_clicked(bool)));
540
541         QHBoxLayout *control_layout = new QHBoxLayout();
542         control_layout->addLayout(loglevel_layout);
543         control_layout->addLayout(buffersize_layout);
544         control_layout->addWidget(save_log_pb);
545         control_layout->addWidget(pop_out_pb);
546
547         QVBoxLayout *root_layout = new QVBoxLayout();
548         root_layout->addLayout(control_layout);
549         root_layout->addWidget(log_view_);
550
551         QWidget *page = new QWidget(parent);
552         page->setLayout(root_layout);
553
554         return page;
555 }
556
557 void Settings::accept()
558 {
559         GlobalSettings settings;
560         settings.stop_tracking();
561
562         QDialog::accept();
563 }
564
565 void Settings::reject()
566 {
567         GlobalSettings settings;
568         settings.undo_tracked_changes();
569
570         QDialog::reject();
571 }
572
573 void Settings::on_page_changed(QListWidgetItem *current, QListWidgetItem *previous)
574 {
575         if (!current)
576                 current = previous;
577
578         pages->setCurrentIndex(page_list->row(current));
579 }
580
581 void Settings::on_view_zoomToFitDuringAcq_changed(int state)
582 {
583         GlobalSettings settings;
584         settings.setValue(GlobalSettings::Key_View_ZoomToFitDuringAcq, state ? true : false);
585 }
586
587 void Settings::on_view_zoomToFitAfterAcq_changed(int state)
588 {
589         GlobalSettings settings;
590         settings.setValue(GlobalSettings::Key_View_ZoomToFitAfterAcq, state ? true : false);
591 }
592
593 void Settings::on_view_triggerIsZero_changed(int state)
594 {
595         GlobalSettings settings;
596         settings.setValue(GlobalSettings::Key_View_TriggerIsZeroTime, state ? true : false);
597 }
598
599 void Settings::on_view_coloredBG_changed(int state)
600 {
601         GlobalSettings settings;
602         settings.setValue(GlobalSettings::Key_View_ColoredBG, state ? true : false);
603 }
604
605 void Settings::on_view_stickyScrolling_changed(int state)
606 {
607         GlobalSettings settings;
608         settings.setValue(GlobalSettings::Key_View_StickyScrolling, state ? true : false);
609 }
610
611 void Settings::on_view_showSamplingPoints_changed(int state)
612 {
613         GlobalSettings settings;
614         settings.setValue(GlobalSettings::Key_View_ShowSamplingPoints, state ? true : false);
615 }
616
617 void Settings::on_view_showAnalogMinorGrid_changed(int state)
618 {
619         GlobalSettings settings;
620         settings.setValue(GlobalSettings::Key_View_ShowAnalogMinorGrid, state ? true : false);
621 }
622
623 void Settings::on_view_showHoverMarker_changed(int state)
624 {
625         GlobalSettings settings;
626         settings.setValue(GlobalSettings::Key_View_ShowHoverMarker, state ? true : false);
627 }
628
629 void Settings::on_view_conversionThresholdDispMode_changed(int state)
630 {
631         GlobalSettings settings;
632         settings.setValue(GlobalSettings::Key_View_ConversionThresholdDispMode, state);
633 }
634
635 void Settings::on_view_defaultDivHeight_changed(int value)
636 {
637         GlobalSettings settings;
638         settings.setValue(GlobalSettings::Key_View_DefaultDivHeight, value);
639 }
640
641 void Settings::on_view_defaultLogicHeight_changed(int value)
642 {
643         GlobalSettings settings;
644         settings.setValue(GlobalSettings::Key_View_DefaultLogicHeight, value);
645 }
646
647 #ifdef ENABLE_DECODE
648 void Settings::on_dec_initialStateConfigurable_changed(int state)
649 {
650         GlobalSettings settings;
651         settings.setValue(GlobalSettings::Key_Dec_InitialStateConfigurable, state ? true : false);
652 }
653
654 void Settings::on_dec_exportFormat_changed(const QString &text)
655 {
656         GlobalSettings settings;
657         settings.setValue(GlobalSettings::Key_Dec_ExportFormat, text);
658 }
659 #endif
660
661 void Settings::on_log_logLevel_changed(int value)
662 {
663         logging.set_log_level(value);
664 }
665
666 void Settings::on_log_bufferSize_changed(int value)
667 {
668         GlobalSettings settings;
669         settings.setValue(GlobalSettings::Key_Log_BufferSize, value);
670 }
671
672 void Settings::on_log_saveToFile_clicked(bool checked)
673 {
674         (void)checked;
675
676         const QString file_name = QFileDialog::getSaveFileName(
677                 this, tr("Save Log"), "", tr("Log Files (*.txt *.log);;All Files (*)"));
678
679         if (file_name.isEmpty())
680                 return;
681
682         QFile file(file_name);
683         if (file.open(QIODevice::WriteOnly | QIODevice::Truncate | QIODevice::Text)) {
684                 QTextStream out_stream(&file);
685                 out_stream << log_view_->toPlainText();
686
687                 if (out_stream.status() == QTextStream::Ok) {
688                         QMessageBox msg(this);
689                         msg.setText(tr("Success"));
690                         msg.setInformativeText(tr("Log saved to %1.").arg(file_name));
691                         msg.setStandardButtons(QMessageBox::Ok);
692                         msg.setIcon(QMessageBox::Information);
693                         msg.exec();
694
695                         return;
696                 }
697         }
698
699         QMessageBox msg(this);
700         msg.setText(tr("Error"));
701         msg.setInformativeText(tr("File %1 could not be written to.").arg(file_name));
702         msg.setStandardButtons(QMessageBox::Ok);
703         msg.setIcon(QMessageBox::Warning);
704         msg.exec();
705 }
706
707 void Settings::on_log_popOut_clicked(bool checked)
708 {
709         (void)checked;
710
711         // Create the window as a sub-window so it closes when the main window closes
712         QMainWindow *window = new QMainWindow(nullptr, Qt::SubWindow);
713
714         window->setObjectName(QString::fromUtf8("Log Window"));
715         window->setWindowTitle(tr("%1 Log").arg(PV_TITLE));
716
717         // Use same width/height as the settings dialog
718         window->resize(width(), height());
719
720         window->setCentralWidget(create_log_view());
721         window->show();
722 }
723
724 } // namespace dialogs
725 } // namespace pv