]> sigrok.org Git - pulseview.git/blob - pv/dialogs/settings.cpp
ccf6c519748ad702ca71fea7269ce62470dcf1b3
[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         // Generate language combobox
220         QComboBox *language_cb = new QComboBox();
221         Application* a = qobject_cast<Application*>(QApplication::instance());
222
223         QString current_language = settings.value(GlobalSettings::Key_General_Language).toString();
224         for (const QString& language : a->get_languages()) {
225                 const QLocale locale = QLocale(language);
226                 const QString desc = locale.languageToString(locale.language());
227                 language_cb->addItem(desc, language);
228
229                 if (language == current_language) {
230                         int index = language_cb->findText(desc, Qt::MatchFixedString);
231                         language_cb->setCurrentIndex(index);
232                 }
233         }
234         connect(language_cb, SIGNAL(currentIndexChanged(const QString&)),
235                 this, SLOT(on_general_language_changed(const QString&)));
236         general_layout->addRow(tr("User interface language"), language_cb);
237
238         // Theme combobox
239         QComboBox *theme_cb = new QComboBox();
240         for (const pair<QString, QString>& entry : Themes)
241                 theme_cb->addItem(entry.first, entry.second);
242
243         theme_cb->setCurrentIndex(
244                 settings.value(GlobalSettings::Key_General_Theme).toInt());
245         connect(theme_cb, SIGNAL(currentIndexChanged(int)),
246                 this, SLOT(on_general_theme_changed(int)));
247         general_layout->addRow(tr("User interface theme"), theme_cb);
248
249         QLabel *description_1 = new QLabel(tr("(You may need to restart PulseView for all UI elements to update)"));
250         description_1->setAlignment(Qt::AlignRight);
251         general_layout->addRow(description_1);
252
253         // Style combobox
254         QComboBox *style_cb = new QComboBox();
255         style_cb->addItem(tr("System Default"), "");
256         for (QString& s : QStyleFactory::keys())
257                 style_cb->addItem(s, s);
258
259         const QString current_style =
260                 settings.value(GlobalSettings::Key_General_Style).toString();
261         if (current_style.isEmpty())
262                 style_cb->setCurrentIndex(0);
263         else
264                 style_cb->setCurrentIndex(style_cb->findText(current_style, nullptr));
265
266         connect(style_cb, SIGNAL(currentIndexChanged(int)),
267                 this, SLOT(on_general_style_changed(int)));
268         general_layout->addRow(tr("Qt widget style"), style_cb);
269
270         QLabel *description_2 = new QLabel(tr("(Dark themes look best with the Fusion style)"));
271         description_2->setAlignment(Qt::AlignRight);
272         general_layout->addRow(description_2);
273
274         // Misc
275         cb = create_checkbox(GlobalSettings::Key_General_SaveWithSetup,
276                 SLOT(on_general_save_with_setup_changed(int)));
277         general_layout->addRow(tr("Save session &setup along with .sr file"), cb);
278
279         return form;
280 }
281
282 QWidget *Settings::get_view_settings_form(QWidget *parent) const
283 {
284         GlobalSettings settings;
285         QCheckBox *cb;
286
287         QWidget *form = new QWidget(parent);
288         QVBoxLayout *form_layout = new QVBoxLayout(form);
289
290         // Trace view settings
291         QGroupBox *trace_view_group = new QGroupBox(tr("Trace View"));
292         form_layout->addWidget(trace_view_group);
293
294         QFormLayout *trace_view_layout = new QFormLayout();
295         trace_view_group->setLayout(trace_view_layout);
296
297         cb = create_checkbox(GlobalSettings::Key_View_ColoredBG,
298                 SLOT(on_view_coloredBG_changed(int)));
299         trace_view_layout->addRow(tr("Use colored trace &background"), cb);
300
301         cb = create_checkbox(GlobalSettings::Key_View_ZoomToFitDuringAcq,
302                 SLOT(on_view_zoomToFitDuringAcq_changed(int)));
303         trace_view_layout->addRow(tr("Constantly perform &zoom-to-fit during acquisition"), cb);
304
305         cb = create_checkbox(GlobalSettings::Key_View_ZoomToFitAfterAcq,
306                 SLOT(on_view_zoomToFitAfterAcq_changed(int)));
307         trace_view_layout->addRow(tr("Perform a zoom-to-&fit when acquisition stops"), cb);
308
309         cb = create_checkbox(GlobalSettings::Key_View_TriggerIsZeroTime,
310                 SLOT(on_view_triggerIsZero_changed(int)));
311         trace_view_layout->addRow(tr("Show time zero at the trigger"), cb);
312
313         cb = create_checkbox(GlobalSettings::Key_View_StickyScrolling,
314                 SLOT(on_view_stickyScrolling_changed(int)));
315         trace_view_layout->addRow(tr("Always keep &newest samples at the right edge during capture"), cb);
316
317         cb = create_checkbox(GlobalSettings::Key_View_ShowSamplingPoints,
318                 SLOT(on_view_showSamplingPoints_changed(int)));
319         trace_view_layout->addRow(tr("Show data &sampling points"), cb);
320
321         cb = create_checkbox(GlobalSettings::Key_View_FillSignalHighAreas,
322                 SLOT(on_view_fillSignalHighAreas_changed(int)));
323         trace_view_layout->addRow(tr("Fill high areas of logic signals"), cb);
324
325         ColorButton* high_fill_cb = new ColorButton(parent);
326         high_fill_cb->set_color(QColor::fromRgba(
327                 settings.value(GlobalSettings::Key_View_FillSignalHighAreaColor).value<uint32_t>()));
328         connect(high_fill_cb, SIGNAL(selected(QColor)),
329                 this, SLOT(on_view_fillSignalHighAreaColor_changed(QColor)));
330         trace_view_layout->addRow(tr("Color to fill high areas of logic signals with"), high_fill_cb);
331
332         cb = create_checkbox(GlobalSettings::Key_View_ShowAnalogMinorGrid,
333                 SLOT(on_view_showAnalogMinorGrid_changed(int)));
334         trace_view_layout->addRow(tr("Show analog minor grid in addition to div grid"), cb);
335
336         cb = create_checkbox(GlobalSettings::Key_View_ShowHoverMarker,
337                 SLOT(on_view_showHoverMarker_changed(int)));
338         trace_view_layout->addRow(tr("Highlight mouse cursor using a vertical marker line"), cb);
339
340         QSpinBox *snap_distance_sb = new QSpinBox();
341         snap_distance_sb->setRange(0, 1000);
342         snap_distance_sb->setSuffix(tr(" pixels"));
343         snap_distance_sb->setValue(
344                 settings.value(GlobalSettings::Key_View_SnapDistance).toInt());
345         connect(snap_distance_sb, SIGNAL(valueChanged(int)), this,
346                 SLOT(on_view_snapDistance_changed(int)));
347         trace_view_layout->addRow(tr("Maximum distance from edges before markers snap to them"), snap_distance_sb);
348
349         ColorButton* cursor_fill_cb = new ColorButton(parent);
350         cursor_fill_cb->set_color(QColor::fromRgba(
351                 settings.value(GlobalSettings::Key_View_CursorFillColor).value<uint32_t>()));
352         connect(cursor_fill_cb, SIGNAL(selected(QColor)),
353                 this, SLOT(on_view_cursorFillColor_changed(QColor)));
354         trace_view_layout->addRow(tr("Color to fill cursor area with"), cursor_fill_cb);
355
356         QComboBox *thr_disp_mode_cb = new QComboBox();
357         thr_disp_mode_cb->addItem(tr("None"), GlobalSettings::ConvThrDispMode_None);
358         thr_disp_mode_cb->addItem(tr("Background"), GlobalSettings::ConvThrDispMode_Background);
359         thr_disp_mode_cb->addItem(tr("Dots"), GlobalSettings::ConvThrDispMode_Dots);
360         thr_disp_mode_cb->setCurrentIndex(
361                 settings.value(GlobalSettings::Key_View_ConversionThresholdDispMode).toInt());
362         connect(thr_disp_mode_cb, SIGNAL(currentIndexChanged(int)),
363                 this, SLOT(on_view_conversionThresholdDispMode_changed(int)));
364         trace_view_layout->addRow(tr("Conversion threshold display mode (analog traces only)"), thr_disp_mode_cb);
365
366         QSpinBox *default_div_height_sb = new QSpinBox();
367         default_div_height_sb->setRange(20, 1000);
368         default_div_height_sb->setSuffix(tr(" pixels"));
369         default_div_height_sb->setValue(
370                 settings.value(GlobalSettings::Key_View_DefaultDivHeight).toInt());
371         connect(default_div_height_sb, SIGNAL(valueChanged(int)), this,
372                 SLOT(on_view_defaultDivHeight_changed(int)));
373         trace_view_layout->addRow(tr("Default analog trace div height"), default_div_height_sb);
374
375         QSpinBox *default_logic_height_sb = new QSpinBox();
376         default_logic_height_sb->setRange(5, 1000);
377         default_logic_height_sb->setSuffix(tr(" pixels"));
378         default_logic_height_sb->setValue(
379                 settings.value(GlobalSettings::Key_View_DefaultLogicHeight).toInt());
380         connect(default_logic_height_sb, SIGNAL(valueChanged(int)), this,
381                 SLOT(on_view_defaultLogicHeight_changed(int)));
382         trace_view_layout->addRow(tr("Default logic trace height"), default_logic_height_sb);
383
384         return form;
385 }
386
387 QWidget *Settings::get_decoder_settings_form(QWidget *parent)
388 {
389 #ifdef ENABLE_DECODE
390         GlobalSettings settings;
391         QCheckBox *cb;
392
393         QWidget *form = new QWidget(parent);
394         QVBoxLayout *form_layout = new QVBoxLayout(form);
395
396         // Decoder settings
397         QGroupBox *decoder_group = new QGroupBox(tr("Decoders"));
398         form_layout->addWidget(decoder_group);
399
400         QFormLayout *decoder_layout = new QFormLayout();
401         decoder_group->setLayout(decoder_layout);
402
403         cb = create_checkbox(GlobalSettings::Key_Dec_InitialStateConfigurable,
404                 SLOT(on_dec_initialStateConfigurable_changed(int)));
405         decoder_layout->addRow(tr("Allow configuration of &initial signal state"), cb);
406
407         cb = create_checkbox(GlobalSettings::Key_Dec_AlwaysShowAllRows,
408                 SLOT(on_dec_alwaysshowallrows_changed(int)));
409         decoder_layout->addRow(tr("Always show all &rows, even if no annotation is visible"), cb);
410
411         // Annotation export settings
412         ann_export_format_ = new QLineEdit();
413         ann_export_format_->setText(
414                 settings.value(GlobalSettings::Key_Dec_ExportFormat).toString());
415         connect(ann_export_format_, SIGNAL(textChanged(const QString&)),
416                 this, SLOT(on_dec_exportFormat_changed(const QString&)));
417         decoder_layout->addRow(tr("Annotation export format"), ann_export_format_);
418         QLabel *description_1 = new QLabel(tr("%s = sample range; %d: decoder name; %r: row name; %c: class name"));
419         description_1->setAlignment(Qt::AlignRight);
420         decoder_layout->addRow(description_1);
421         QLabel *description_2 = new QLabel(tr("%1: longest annotation text; %a: all annotation texts; %q: use quotation marks"));
422         description_2->setAlignment(Qt::AlignRight);
423         decoder_layout->addRow(description_2);
424
425         return form;
426 #else
427         (void)parent;
428         return nullptr;
429 #endif
430 }
431
432 QWidget *Settings::get_about_page(QWidget *parent) const
433 {
434         Application* a = qobject_cast<Application*>(QApplication::instance());
435
436         QLabel *icon = new QLabel();
437         icon->setPixmap(QPixmap(QString::fromUtf8(":/icons/pulseview.svg")));
438
439         // Setup the license field with the project homepage link
440         QLabel *gpl_home_info = new QLabel();
441         gpl_home_info->setText(tr("%1<br /><a href=\"http://%2\">%2</a>").arg(
442                 tr("GNU GPL, version 3 or later"),
443                 QApplication::organizationDomain()));
444         gpl_home_info->setOpenExternalLinks(true);
445
446         QString s;
447
448         s.append("<style type=\"text/css\"> tr .id { white-space: pre; padding-right: 5px; } </style>");
449
450         s.append("<table>");
451
452         s.append("<tr><td colspan=\"2\"><b>" +
453                 tr("Versions, libraries and features:") + "</b></td></tr>");
454         for (pair<QString, QString> &entry : a->get_version_info())
455                 s.append(QString("<tr><td><i>%1</i></td><td>%2</td></tr>")
456                         .arg(entry.first, entry.second));
457
458         s.append("<tr><td colspan=\"2\"></td></tr>");
459         s.append("<tr><td colspan=\"2\"><b>" +
460                 tr("Firmware search paths:") + "</b></td></tr>");
461         for (QString &entry : a->get_fw_path_list())
462                 s.append(QString("<tr><td colspan=\"2\">%1</td></tr>").arg(entry));
463
464 #ifdef ENABLE_DECODE
465         s.append("<tr><td colspan=\"2\"></td></tr>");
466         s.append("<tr><td colspan=\"2\"><b>" +
467                 tr("Protocol decoder search paths:") + "</b></td></tr>");
468         for (QString &entry : a->get_pd_path_list())
469                 s.append(QString("<tr><td colspan=\"2\">%1</td></tr>").arg(entry));
470 #endif
471
472         s.append("<tr><td colspan=\"2\"></td></tr>");
473         s.append("<tr><td colspan=\"2\"><b>" +
474                 tr("Supported hardware drivers:") + "</b></td></tr>");
475         for (pair<QString, QString> &entry : a->get_driver_list())
476                 s.append(QString("<tr><td class=\"id\"><i>%1</i></td><td>%2</td></tr>")
477                         .arg(entry.first, entry.second));
478
479         s.append("<tr><td colspan=\"2\"></td></tr>");
480         s.append("<tr><td colspan=\"2\"><b>" +
481                 tr("Supported input formats:") + "</b></td></tr>");
482         for (pair<QString, QString> &entry : a->get_input_format_list())
483                 s.append(QString("<tr><td class=\"id\"><i>%1</i></td><td>%2</td></tr>")
484                         .arg(entry.first, entry.second));
485
486         s.append("<tr><td colspan=\"2\"></td></tr>");
487         s.append("<tr><td colspan=\"2\"><b>" +
488                 tr("Supported output formats:") + "</b></td></tr>");
489         for (pair<QString, QString> &entry : a->get_output_format_list())
490                 s.append(QString("<tr><td class=\"id\"><i>%1</i></td><td>%2</td></tr>")
491                         .arg(entry.first, entry.second));
492
493 #ifdef ENABLE_DECODE
494         s.append("<tr><td colspan=\"2\"></td></tr>");
495         s.append("<tr><td colspan=\"2\"><b>" +
496                 tr("Supported protocol decoders:") + "</b></td></tr>");
497         for (pair<QString, QString> &entry : a->get_pd_list())
498                 s.append(QString("<tr><td class=\"id\"><i>%1</i></td><td>%2</td></tr>")
499                         .arg(entry.first, entry.second));
500 #endif
501
502         s.append("<tr><td colspan=\"2\"></td></tr>");
503         s.append("<tr><td colspan=\"2\"><b>" +
504                 tr("Available Translations:") + "</b></td></tr>");
505         for (const QString& language : a->get_languages()) {
506                 if (language == "en")
507                         continue;
508
509                 const QLocale locale = QLocale(language);
510                 const QString desc = locale.languageToString(locale.language());
511                 const QString editors = a->get_language_editors(language);
512
513                 s.append(QString("<tr><td class=\"id\"><i>%1</i></td><td>(%2)</td></tr>")
514                         .arg(desc, editors));
515         }
516
517         s.append("</table>");
518
519         QTextDocument *supported_doc = new QTextDocument();
520         supported_doc->setHtml(s);
521
522         QTextBrowser *support_list = new QTextBrowser();
523         support_list->setDocument(supported_doc);
524
525         QHBoxLayout *h_layout = new QHBoxLayout();
526         h_layout->setAlignment(Qt::AlignLeft);
527         h_layout->addWidget(icon);
528         h_layout->addWidget(gpl_home_info);
529
530         QVBoxLayout *layout = new QVBoxLayout();
531         layout->addLayout(h_layout);
532         layout->addWidget(support_list);
533
534         QWidget *page = new QWidget(parent);
535         page->setLayout(layout);
536
537         return page;
538 }
539
540 QWidget *Settings::get_logging_page(QWidget *parent) const
541 {
542         GlobalSettings settings;
543
544         // Log level
545         QSpinBox *loglevel_sb = new QSpinBox();
546         loglevel_sb->setMaximum(SR_LOG_SPEW);
547         loglevel_sb->setValue(logging.get_log_level());
548         connect(loglevel_sb, SIGNAL(valueChanged(int)), this,
549                 SLOT(on_log_logLevel_changed(int)));
550
551         QHBoxLayout *loglevel_layout = new QHBoxLayout();
552         loglevel_layout->addWidget(new QLabel(tr("Log level:")));
553         loglevel_layout->addWidget(loglevel_sb);
554
555         // Background buffer size
556         QSpinBox *buffersize_sb = new QSpinBox();
557         buffersize_sb->setSuffix(tr(" lines"));
558         buffersize_sb->setMinimum(Logging::MIN_BUFFER_SIZE);
559         buffersize_sb->setMaximum(Logging::MAX_BUFFER_SIZE);
560         buffersize_sb->setValue(
561                 settings.value(GlobalSettings::Key_Log_BufferSize).toInt());
562         connect(buffersize_sb, SIGNAL(valueChanged(int)), this,
563                 SLOT(on_log_bufferSize_changed(int)));
564
565         QHBoxLayout *buffersize_layout = new QHBoxLayout();
566         buffersize_layout->addWidget(new QLabel(tr("Length of background buffer:")));
567         buffersize_layout->addWidget(buffersize_sb);
568
569         // Save to file
570         QPushButton *save_log_pb = new QPushButton(
571                 QIcon::fromTheme("document-save-as", QIcon(":/icons/document-save-as.png")),
572                 tr("&Save to File"));
573         connect(save_log_pb, SIGNAL(clicked(bool)),
574                 this, SLOT(on_log_saveToFile_clicked(bool)));
575
576         // Pop out
577         QPushButton *pop_out_pb = new QPushButton(
578                 QIcon::fromTheme("window-new", QIcon(":/icons/window-new.png")),
579                 tr("&Pop out"));
580         connect(pop_out_pb, SIGNAL(clicked(bool)),
581                 this, SLOT(on_log_popOut_clicked(bool)));
582
583         QHBoxLayout *control_layout = new QHBoxLayout();
584         control_layout->addLayout(loglevel_layout);
585         control_layout->addLayout(buffersize_layout);
586         control_layout->addWidget(save_log_pb);
587         control_layout->addWidget(pop_out_pb);
588
589         QVBoxLayout *root_layout = new QVBoxLayout();
590         root_layout->addLayout(control_layout);
591         root_layout->addWidget(log_view_);
592
593         QWidget *page = new QWidget(parent);
594         page->setLayout(root_layout);
595
596         return page;
597 }
598
599 void Settings::accept()
600 {
601         GlobalSettings settings;
602         settings.stop_tracking();
603
604         QDialog::accept();
605 }
606
607 void Settings::reject()
608 {
609         GlobalSettings settings;
610         settings.undo_tracked_changes();
611
612         QDialog::reject();
613 }
614
615 void Settings::on_page_changed(QListWidgetItem *current, QListWidgetItem *previous)
616 {
617         if (!current)
618                 current = previous;
619
620         pages->setCurrentIndex(page_list->row(current));
621 }
622
623 void Settings::on_general_language_changed(const QString &text)
624 {
625         GlobalSettings settings;
626         Application* a = qobject_cast<Application*>(QApplication::instance());
627
628         for (const QString& language : a->get_languages()) {
629                 QLocale locale = QLocale(language);
630                 QString desc = locale.languageToString(locale.language());
631
632                 if (text == desc)
633                         settings.setValue(GlobalSettings::Key_General_Language, language);
634         }
635 }
636
637 void Settings::on_general_theme_changed(int value)
638 {
639         GlobalSettings settings;
640         settings.setValue(GlobalSettings::Key_General_Theme, value);
641         settings.apply_theme();
642
643         QMessageBox msg(this);
644         msg.setStandardButtons(QMessageBox::Yes | QMessageBox::No);
645         msg.setIcon(QMessageBox::Question);
646
647         if (settings.current_theme_is_dark()) {
648                 msg.setText(tr("You selected a dark theme.\n" \
649                         "Should I set the user-adjustable colors to better suit your choice?\n\n" \
650                         "Please keep in mind that PulseView may need a restart to display correctly."));
651                 if (msg.exec() == QMessageBox::Yes)
652                         settings.set_dark_theme_default_colors();
653         } else {
654                 msg.setText(tr("You selected a bright theme.\n" \
655                         "Should I set the user-adjustable colors to better suit your choice?\n\n" \
656                         "Please keep in mind that PulseView may need a restart to display correctly."));
657                 if (msg.exec() == QMessageBox::Yes)
658                         settings.set_bright_theme_default_colors();
659         }
660 }
661
662 void Settings::on_general_style_changed(int value)
663 {
664         GlobalSettings settings;
665
666         if (value == 0)
667                 settings.setValue(GlobalSettings::Key_General_Style, "");
668         else
669                 settings.setValue(GlobalSettings::Key_General_Style,
670                         QStyleFactory::keys().at(value - 1));
671
672         settings.apply_theme();
673 }
674
675 void Settings::on_general_save_with_setup_changed(int state)
676 {
677         GlobalSettings settings;
678         settings.setValue(GlobalSettings::Key_General_SaveWithSetup, state ? true : false);
679 }
680
681 void Settings::on_view_zoomToFitDuringAcq_changed(int state)
682 {
683         GlobalSettings settings;
684         settings.setValue(GlobalSettings::Key_View_ZoomToFitDuringAcq, state ? true : false);
685 }
686
687 void Settings::on_view_zoomToFitAfterAcq_changed(int state)
688 {
689         GlobalSettings settings;
690         settings.setValue(GlobalSettings::Key_View_ZoomToFitAfterAcq, state ? true : false);
691 }
692
693 void Settings::on_view_triggerIsZero_changed(int state)
694 {
695         GlobalSettings settings;
696         settings.setValue(GlobalSettings::Key_View_TriggerIsZeroTime, state ? true : false);
697 }
698
699 void Settings::on_view_coloredBG_changed(int state)
700 {
701         GlobalSettings settings;
702         settings.setValue(GlobalSettings::Key_View_ColoredBG, state ? true : false);
703 }
704
705 void Settings::on_view_stickyScrolling_changed(int state)
706 {
707         GlobalSettings settings;
708         settings.setValue(GlobalSettings::Key_View_StickyScrolling, state ? true : false);
709 }
710
711 void Settings::on_view_showSamplingPoints_changed(int state)
712 {
713         GlobalSettings settings;
714         settings.setValue(GlobalSettings::Key_View_ShowSamplingPoints, state ? true : false);
715 }
716
717 void Settings::on_view_fillSignalHighAreas_changed(int state)
718 {
719         GlobalSettings settings;
720         settings.setValue(GlobalSettings::Key_View_FillSignalHighAreas, state ? true : false);
721 }
722
723 void Settings::on_view_fillSignalHighAreaColor_changed(QColor color)
724 {
725         GlobalSettings settings;
726         settings.setValue(GlobalSettings::Key_View_FillSignalHighAreaColor, color.rgba());
727 }
728
729 void Settings::on_view_showAnalogMinorGrid_changed(int state)
730 {
731         GlobalSettings settings;
732         settings.setValue(GlobalSettings::Key_View_ShowAnalogMinorGrid, state ? true : false);
733 }
734
735 void Settings::on_view_showHoverMarker_changed(int state)
736 {
737         GlobalSettings settings;
738         settings.setValue(GlobalSettings::Key_View_ShowHoverMarker, state ? true : false);
739 }
740
741 void Settings::on_view_snapDistance_changed(int value)
742 {
743         GlobalSettings settings;
744         settings.setValue(GlobalSettings::Key_View_SnapDistance, value);
745 }
746
747 void Settings::on_view_cursorFillColor_changed(QColor color)
748 {
749         GlobalSettings settings;
750         settings.setValue(GlobalSettings::Key_View_CursorFillColor, color.rgba());
751 }
752
753 void Settings::on_view_conversionThresholdDispMode_changed(int state)
754 {
755         GlobalSettings settings;
756         settings.setValue(GlobalSettings::Key_View_ConversionThresholdDispMode, state);
757 }
758
759 void Settings::on_view_defaultDivHeight_changed(int value)
760 {
761         GlobalSettings settings;
762         settings.setValue(GlobalSettings::Key_View_DefaultDivHeight, value);
763 }
764
765 void Settings::on_view_defaultLogicHeight_changed(int value)
766 {
767         GlobalSettings settings;
768         settings.setValue(GlobalSettings::Key_View_DefaultLogicHeight, value);
769 }
770
771 #ifdef ENABLE_DECODE
772 void Settings::on_dec_initialStateConfigurable_changed(int state)
773 {
774         GlobalSettings settings;
775         settings.setValue(GlobalSettings::Key_Dec_InitialStateConfigurable, state ? true : false);
776 }
777
778 void Settings::on_dec_exportFormat_changed(const QString &text)
779 {
780         GlobalSettings settings;
781         settings.setValue(GlobalSettings::Key_Dec_ExportFormat, text);
782 }
783
784 void Settings::on_dec_alwaysshowallrows_changed(int state)
785 {
786         GlobalSettings settings;
787         settings.setValue(GlobalSettings::Key_Dec_AlwaysShowAllRows, state ? true : false);
788 }
789 #endif
790
791 void Settings::on_log_logLevel_changed(int value)
792 {
793         logging.set_log_level(value);
794 }
795
796 void Settings::on_log_bufferSize_changed(int value)
797 {
798         GlobalSettings settings;
799         settings.setValue(GlobalSettings::Key_Log_BufferSize, value);
800 }
801
802 void Settings::on_log_saveToFile_clicked(bool checked)
803 {
804         (void)checked;
805
806         const QString file_name = QFileDialog::getSaveFileName(
807                 this, tr("Save Log"), "", tr("Log Files (*.txt *.log);;All Files (*)"));
808
809         if (file_name.isEmpty())
810                 return;
811
812         QFile file(file_name);
813         if (file.open(QIODevice::WriteOnly | QIODevice::Truncate | QIODevice::Text)) {
814                 QTextStream out_stream(&file);
815                 out_stream << log_view_->toPlainText();
816
817                 if (out_stream.status() == QTextStream::Ok) {
818                         QMessageBox msg(this);
819                         msg.setText(tr("Success") + "\n\n" + tr("Log saved to %1.").arg(file_name));
820                         msg.setStandardButtons(QMessageBox::Ok);
821                         msg.setIcon(QMessageBox::Information);
822                         msg.exec();
823
824                         return;
825                 }
826         }
827
828         QMessageBox msg(this);
829         msg.setText(tr("Error") + "\n\n" + tr("File %1 could not be written to.").arg(file_name));
830         msg.setStandardButtons(QMessageBox::Ok);
831         msg.setIcon(QMessageBox::Warning);
832         msg.exec();
833 }
834
835 void Settings::on_log_popOut_clicked(bool checked)
836 {
837         (void)checked;
838
839         // Create the window as a sub-window so it closes when the main window closes
840         QMainWindow *window = new QMainWindow(nullptr, Qt::SubWindow);
841
842         window->setObjectName(QString::fromUtf8("Log Window"));
843         window->setWindowTitle(tr("%1 Log").arg(PV_TITLE));
844
845         // Use same width/height as the settings dialog
846         window->resize(width(), height());
847
848         window->setCentralWidget(create_log_view());
849         window->show();
850 }
851
852 } // namespace dialogs
853 } // namespace pv