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