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