2 * This file is part of the PulseView project.
4 * Copyright (C) 2017 Soeren Apel <soeren@apelpie.net>
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.
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.
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/>.
23 #include <boost/version.hpp>
25 #include <QApplication>
27 #include <QDialogButtonBox>
28 #include <QFileDialog>
29 #include <QFormLayout>
31 #include <QHBoxLayout>
33 #include <QMainWindow>
34 #include <QMessageBox>
35 #include <QPushButton>
38 #include <QTextBrowser>
39 #include <QTextDocument>
40 #include <QTextStream>
41 #include <QVBoxLayout>
43 #include "settings.hpp"
45 #include "pv/devicemanager.hpp"
46 #include "pv/globalsettings.hpp"
47 #include "pv/logging.hpp"
49 #include <libsigrokcxx/libsigrokcxx.hpp>
52 #include <libsigrokdecode/libsigrokdecode.h>
55 using std::shared_ptr;
60 Settings::Settings(DeviceManager &device_manager, QWidget *parent) :
61 QDialog(parent, nullptr),
62 device_manager_(device_manager)
64 const int icon_size = 64;
69 log_view_ = create_log_view();
72 page_list = new QListWidget;
73 page_list->setViewMode(QListView::IconMode);
74 page_list->setIconSize(QSize(icon_size, icon_size));
75 page_list->setMovement(QListView::Static);
76 page_list->setMaximumWidth(icon_size + (icon_size / 2));
77 page_list->setSpacing(12);
79 pages = new QStackedWidget;
81 page_list->setCurrentIndex(page_list->model()->index(0, 0));
83 QHBoxLayout *tab_layout = new QHBoxLayout;
84 tab_layout->addWidget(page_list);
85 tab_layout->addWidget(pages, Qt::AlignLeft);
87 QDialogButtonBox *button_box = new QDialogButtonBox(
88 QDialogButtonBox::Ok | QDialogButtonBox::Cancel);
90 QVBoxLayout* root_layout = new QVBoxLayout(this);
91 root_layout->addLayout(tab_layout);
92 root_layout->addWidget(button_box);
94 connect(button_box, SIGNAL(accepted()), this, SLOT(accept()));
95 connect(button_box, SIGNAL(rejected()), this, SLOT(reject()));
96 connect(page_list, SIGNAL(currentItemChanged(QListWidgetItem*, QListWidgetItem*)),
97 this, SLOT(on_page_changed(QListWidgetItem*, QListWidgetItem*)));
99 // Start to record changes
100 GlobalSettings settings;
101 settings.start_tracking();
104 void Settings::create_pages()
107 pages->addWidget(get_view_settings_form(pages));
109 QListWidgetItem *viewButton = new QListWidgetItem(page_list);
110 viewButton->setIcon(QIcon(":/icons/settings-views.svg"));
111 viewButton->setText(tr("Views"));
112 viewButton->setTextAlignment(Qt::AlignHCenter);
113 viewButton->setFlags(Qt::ItemIsSelectable | Qt::ItemIsEnabled);
117 pages->addWidget(get_decoder_settings_form(pages));
119 QListWidgetItem *decoderButton = new QListWidgetItem(page_list);
120 decoderButton->setIcon(QIcon(":/icons/add-decoder.svg"));
121 decoderButton->setText(tr("Decoders"));
122 decoderButton->setTextAlignment(Qt::AlignHCenter);
123 decoderButton->setFlags(Qt::ItemIsSelectable | Qt::ItemIsEnabled);
127 pages->addWidget(get_about_page(pages));
129 QListWidgetItem *aboutButton = new QListWidgetItem(page_list);
130 aboutButton->setIcon(QIcon(":/icons/information.svg"));
131 aboutButton->setText(tr("About"));
132 aboutButton->setTextAlignment(Qt::AlignHCenter);
133 aboutButton->setFlags(Qt::ItemIsSelectable | Qt::ItemIsEnabled);
136 pages->addWidget(get_logging_page(pages));
138 QListWidgetItem *loggingButton = new QListWidgetItem(page_list);
139 loggingButton->setIcon(QIcon(":/icons/information.svg"));
140 loggingButton->setText(tr("Logging"));
141 loggingButton->setTextAlignment(Qt::AlignHCenter);
142 loggingButton->setFlags(Qt::ItemIsSelectable | Qt::ItemIsEnabled);
145 QCheckBox *Settings::create_checkbox(const QString& key, const char* slot) const
147 GlobalSettings settings;
149 QCheckBox *cb = new QCheckBox();
150 cb->setChecked(settings.value(key).toBool());
151 connect(cb, SIGNAL(stateChanged(int)), this, slot);
155 QPlainTextEdit *Settings::create_log_view() const
157 GlobalSettings settings;
159 QPlainTextEdit *log_view = new QPlainTextEdit();
161 log_view->setReadOnly(true);
162 log_view->setWordWrapMode(QTextOption::NoWrap);
163 log_view->setCenterOnScroll(true);
165 log_view->appendHtml(logging.get_log());
166 connect(&logging, SIGNAL(logged_text(QString)),
167 log_view, SLOT(appendHtml(QString)));
172 QWidget *Settings::get_view_settings_form(QWidget *parent) const
174 GlobalSettings settings;
177 QWidget *form = new QWidget(parent);
178 QVBoxLayout *form_layout = new QVBoxLayout(form);
180 // Trace view settings
181 QGroupBox *trace_view_group = new QGroupBox(tr("Trace View"));
182 form_layout->addWidget(trace_view_group);
184 QFormLayout *trace_view_layout = new QFormLayout();
185 trace_view_group->setLayout(trace_view_layout);
187 cb = create_checkbox(GlobalSettings::Key_View_ColouredBG,
188 SLOT(on_view_colouredBG_changed(int)));
189 trace_view_layout->addRow(tr("Use coloured trace &background"), cb);
191 cb = create_checkbox(GlobalSettings::Key_View_ZoomToFitDuringAcq,
192 SLOT(on_view_zoomToFitDuringAcq_changed(int)));
193 trace_view_layout->addRow(tr("Constantly perform &zoom-to-fit during acquisition"), cb);
195 cb = create_checkbox(GlobalSettings::Key_View_ZoomToFitAfterAcq,
196 SLOT(on_view_zoomToFitAfterAcq_changed(int)));
197 trace_view_layout->addRow(tr("Perform a zoom-to-&fit when acquisition stops"), cb);
199 cb = create_checkbox(GlobalSettings::Key_View_TriggerIsZeroTime,
200 SLOT(on_view_triggerIsZero_changed(int)));
201 trace_view_layout->addRow(tr("Show time zero at the trigger"), cb);
203 cb = create_checkbox(GlobalSettings::Key_View_StickyScrolling,
204 SLOT(on_view_stickyScrolling_changed(int)));
205 trace_view_layout->addRow(tr("Always keep &newest samples at the right edge during capture"), cb);
207 cb = create_checkbox(GlobalSettings::Key_View_ShowSamplingPoints,
208 SLOT(on_view_showSamplingPoints_changed(int)));
209 trace_view_layout->addRow(tr("Show data &sampling points"), cb);
211 cb = create_checkbox(GlobalSettings::Key_View_ShowAnalogMinorGrid,
212 SLOT(on_view_showAnalogMinorGrid_changed(int)));
213 trace_view_layout->addRow(tr("Show analog minor grid in addition to div grid"), cb);
215 QComboBox *thr_disp_mode_cb = new QComboBox();
216 thr_disp_mode_cb->addItem(tr("None"), GlobalSettings::ConvThrDispMode_None);
217 thr_disp_mode_cb->addItem(tr("Background"), GlobalSettings::ConvThrDispMode_Background);
218 thr_disp_mode_cb->addItem(tr("Dots"), GlobalSettings::ConvThrDispMode_Dots);
219 thr_disp_mode_cb->setCurrentIndex(
220 settings.value(GlobalSettings::Key_View_ConversionThresholdDispMode).toInt());
221 connect(thr_disp_mode_cb, SIGNAL(currentIndexChanged(int)),
222 this, SLOT(on_view_conversionThresholdDispMode_changed(int)));
223 trace_view_layout->addRow(tr("Conversion threshold display mode (analog traces only)"), thr_disp_mode_cb);
225 QSpinBox *default_div_height_sb = new QSpinBox();
226 default_div_height_sb->setRange(20, 1000);
227 default_div_height_sb->setSuffix(tr(" pixels"));
228 default_div_height_sb->setValue(
229 settings.value(GlobalSettings::Key_View_DefaultDivHeight).toInt());
230 connect(default_div_height_sb, SIGNAL(valueChanged(int)), this,
231 SLOT(on_view_defaultDivHeight_changed(int)));
232 trace_view_layout->addRow(tr("Default analog trace div height"), default_div_height_sb);
234 QSpinBox *default_logic_height_sb = new QSpinBox();
235 default_logic_height_sb->setRange(5, 1000);
236 default_logic_height_sb->setSuffix(tr(" pixels"));
237 default_logic_height_sb->setValue(
238 settings.value(GlobalSettings::Key_View_DefaultLogicHeight).toInt());
239 connect(default_logic_height_sb, SIGNAL(valueChanged(int)), this,
240 SLOT(on_view_defaultLogicHeight_changed(int)));
241 trace_view_layout->addRow(tr("Default logic trace height"), default_logic_height_sb);
246 QWidget *Settings::get_decoder_settings_form(QWidget *parent) const
251 QWidget *form = new QWidget(parent);
252 QVBoxLayout *form_layout = new QVBoxLayout(form);
255 QGroupBox *decoder_group = new QGroupBox(tr("Decoders"));
256 form_layout->addWidget(decoder_group);
258 QFormLayout *decoder_layout = new QFormLayout();
259 decoder_group->setLayout(decoder_layout);
261 cb = create_checkbox(GlobalSettings::Key_Dec_InitialStateConfigurable,
262 SLOT(on_dec_initialStateConfigurable_changed(int)));
263 decoder_layout->addRow(tr("Allow configuration of &initial signal state"), cb);
273 static gint sort_pds(gconstpointer a, gconstpointer b)
275 const struct srd_decoder *sda, *sdb;
277 sda = (const struct srd_decoder *)a;
278 sdb = (const struct srd_decoder *)b;
279 return strcmp(sda->id, sdb->id);
283 QWidget *Settings::get_about_page(QWidget *parent) const
286 struct srd_decoder *dec;
289 QLabel *icon = new QLabel();
290 icon->setPixmap(QPixmap(QString::fromUtf8(":/icons/pulseview.svg")));
292 /* Setup the version field */
293 QLabel *version_info = new QLabel();
294 version_info->setText(tr("%1 %2<br />%3<br /><a href=\"http://%4\">%4</a>")
295 .arg(QApplication::applicationName(),
296 QApplication::applicationVersion(),
297 tr("GNU GPL, version 3 or later"),
298 QApplication::organizationDomain()));
299 version_info->setOpenExternalLinks(true);
301 shared_ptr<sigrok::Context> context = device_manager_.context();
305 s.append("<style type=\"text/css\"> tr .id { white-space: pre; padding-right: 5px; } </style>");
310 s.append("<tr><td colspan=\"2\"><b>" +
311 tr("Libraries and features:") + "</b></td></tr>");
313 s.append(QString("<tr><td><i>%1</i></td><td>%2</td></tr>")
314 .arg(QString("Qt"), qVersion()));
315 s.append(QString("<tr><td><i>%1</i></td><td>%2</td></tr>")
316 .arg(QString("glibmm"), PV_GLIBMM_VERSION));
317 s.append(QString("<tr><td><i>%1</i></td><td>%2</td></tr>")
318 .arg(QString("Boost"), BOOST_LIB_VERSION));
320 s.append(QString("<tr><td><i>%1</i></td><td>%2/%3 (rt: %4/%5)</td></tr>")
321 .arg(QString("libsigrok"), SR_PACKAGE_VERSION_STRING,
322 SR_LIB_VERSION_STRING, sr_package_version_string_get(),
323 sr_lib_version_string_get()));
325 GSList *l_orig = sr_buildinfo_libs_get();
326 for (GSList *l = l_orig; l; l = l->next) {
327 GSList *m = (GSList *)l->data;
328 const char *lib = (const char *)m->data;
329 const char *version = (const char *)m->next->data;
330 s.append(QString("<tr><td><i>- %1</i></td><td>%2</td></tr>")
331 .arg(QString(lib), QString(version)));
332 g_slist_free_full(m, g_free);
334 g_slist_free(l_orig);
336 char *host = sr_buildinfo_host_get();
337 s.append(QString("<tr><td><i>- Host</i></td><td>%1</td></tr>")
338 .arg(QString(host)));
341 char *scpi_backends = sr_buildinfo_scpi_backends_get();
342 s.append(QString("<tr><td><i>- SCPI backends</i></td><td>%1</td></tr>")
343 .arg(QString(scpi_backends)));
344 g_free(scpi_backends);
347 s.append(QString("<tr><td><i>%1</i></td><td>%2/%3 (rt: %4/%5)</td></tr>")
348 .arg(QString("libsigrokdecode"), SRD_PACKAGE_VERSION_STRING,
349 SRD_LIB_VERSION_STRING, srd_package_version_string_get(),
350 srd_lib_version_string_get()));
352 l_orig = srd_buildinfo_libs_get();
353 for (GSList *l = l_orig; l; l = l->next) {
354 GSList *m = (GSList *)l->data;
355 const char *lib = (const char *)m->data;
356 const char *version = (const char *)m->next->data;
357 s.append(QString("<tr><td><i>- %1</i></td><td>%2</td></tr>")
358 .arg(QString(lib), QString(version)));
359 g_slist_free_full(m, g_free);
361 g_slist_free(l_orig);
363 host = srd_buildinfo_host_get();
364 s.append(QString("<tr><td><i>- Host</i></td><td>%1</td></tr>")
365 .arg(QString(host)));
369 s.append("<tr><td colspan=\"2\"></td></tr>");
370 s.append("<tr><td colspan=\"2\"><b>" +
371 tr("Firmware search paths:") + "</b></td></tr>");
373 l_orig = sr_resourcepaths_get(SR_RESOURCE_FIRMWARE);
374 for (GSList *l = l_orig; l; l = l->next)
375 s.append(QString("<tr><td colspan=\"2\">%1</td></tr>").arg(
376 QString((char*)l->data)));
377 g_slist_free_full(l_orig, g_free);
380 s.append("<tr><td colspan=\"2\"></td></tr>");
381 s.append("<tr><td colspan=\"2\"><b>" +
382 tr("Protocol decoder search paths:") + "</b></td></tr>");
384 l_orig = srd_searchpaths_get();
385 for (GSList *l = l_orig; l; l = l->next)
386 s.append(QString("<tr><td colspan=\"2\">%1</td></tr>").arg(
387 QString((char*)l->data)));
388 g_slist_free_full(l_orig, g_free);
391 /* Set up the supported field */
392 s.append("<tr><td colspan=\"2\"></td></tr>");
393 s.append("<tr><td colspan=\"2\"><b>" +
394 tr("Supported hardware drivers:") + "</b></td></tr>");
395 for (auto entry : context->drivers()) {
396 s.append(QString("<tr><td class=\"id\"><i>%1</i></td><td>%2</td></tr>")
397 .arg(QString::fromUtf8(entry.first.c_str()),
398 QString::fromUtf8(entry.second->long_name().c_str())));
401 s.append("<tr><td colspan=\"2\"></td></tr>");
402 s.append("<tr><td colspan=\"2\"><b>" +
403 tr("Supported input formats:") + "</b></td></tr>");
404 for (auto entry : context->input_formats()) {
405 s.append(QString("<tr><td class=\"id\"><i>%1</i></td><td>%2</td></tr>")
406 .arg(QString::fromUtf8(entry.first.c_str()),
407 QString::fromUtf8(entry.second->description().c_str())));
410 s.append("<tr><td colspan=\"2\"></td></tr>");
411 s.append("<tr><td colspan=\"2\"><b>" +
412 tr("Supported output formats:") + "</b></td></tr>");
413 for (auto entry : context->output_formats()) {
414 s.append(QString("<tr><td class=\"id\"><i>%1</i></td><td>%2</td></tr>")
415 .arg(QString::fromUtf8(entry.first.c_str()),
416 QString::fromUtf8(entry.second->description().c_str())));
420 s.append("<tr><td colspan=\"2\"></td></tr>");
421 s.append("<tr><td colspan=\"2\"><b>" +
422 tr("Supported protocol decoders:") + "</b></td></tr>");
423 GSList *sl = g_slist_copy((GSList *)srd_decoder_list());
424 sl = g_slist_sort(sl, sort_pds);
425 for (const GSList *l = sl; l; l = l->next) {
426 dec = (struct srd_decoder *)l->data;
427 s.append(QString("<tr><td class=\"id\"><i>%1</i></td><td>%2</td></tr>")
428 .arg(QString::fromUtf8(dec->id),
429 QString::fromUtf8(dec->longname)));
434 s.append("</table>");
436 QTextDocument *supported_doc = new QTextDocument();
437 supported_doc->setHtml(s);
439 QTextBrowser *support_list = new QTextBrowser();
440 support_list->setDocument(supported_doc);
442 QGridLayout *layout = new QGridLayout();
443 layout->addWidget(icon, 0, 0, 1, 1);
444 layout->addWidget(version_info, 0, 1, 1, 1);
445 layout->addWidget(support_list, 1, 1, 1, 1);
447 QWidget *page = new QWidget(parent);
448 page->setLayout(layout);
453 QWidget *Settings::get_logging_page(QWidget *parent) const
455 GlobalSettings settings;
458 QSpinBox *loglevel_sb = new QSpinBox();
459 loglevel_sb->setMaximum(SR_LOG_SPEW);
460 loglevel_sb->setValue(logging.get_log_level());
461 connect(loglevel_sb, SIGNAL(valueChanged(int)), this,
462 SLOT(on_log_logLevel_changed(int)));
464 QHBoxLayout *loglevel_layout = new QHBoxLayout();
465 loglevel_layout->addWidget(new QLabel(tr("Log level:")));
466 loglevel_layout->addWidget(loglevel_sb);
468 // Background buffer size
469 QSpinBox *buffersize_sb = new QSpinBox();
470 buffersize_sb->setSuffix(tr(" lines"));
471 buffersize_sb->setMaximum(Logging::MAX_BUFFER_SIZE);
472 buffersize_sb->setValue(
473 settings.value(GlobalSettings::Key_Log_BufferSize).toInt());
474 connect(buffersize_sb, SIGNAL(valueChanged(int)), this,
475 SLOT(on_log_bufferSize_changed(int)));
477 QHBoxLayout *buffersize_layout = new QHBoxLayout();
478 buffersize_layout->addWidget(new QLabel(tr("Length of background buffer:")));
479 buffersize_layout->addWidget(buffersize_sb);
482 QPushButton *save_log_pb = new QPushButton(
483 QIcon::fromTheme("document-save-as", QIcon(":/icons/document-save-as.png")),
484 tr("&Save to File"));
485 connect(save_log_pb, SIGNAL(clicked(bool)),
486 this, SLOT(on_log_saveToFile_clicked(bool)));
489 QPushButton *pop_out_pb = new QPushButton(
490 QIcon::fromTheme("window-new", QIcon(":/icons/window-new.png")),
492 connect(pop_out_pb, SIGNAL(clicked(bool)),
493 this, SLOT(on_log_popOut_clicked(bool)));
495 QHBoxLayout *control_layout = new QHBoxLayout();
496 control_layout->addLayout(loglevel_layout);
497 control_layout->addLayout(buffersize_layout);
498 control_layout->addWidget(save_log_pb);
499 control_layout->addWidget(pop_out_pb);
501 QVBoxLayout *root_layout = new QVBoxLayout();
502 root_layout->addLayout(control_layout);
503 root_layout->addWidget(log_view_);
505 QWidget *page = new QWidget(parent);
506 page->setLayout(root_layout);
511 void Settings::accept()
513 GlobalSettings settings;
514 settings.stop_tracking();
519 void Settings::reject()
521 GlobalSettings settings;
522 settings.undo_tracked_changes();
527 void Settings::on_page_changed(QListWidgetItem *current, QListWidgetItem *previous)
532 pages->setCurrentIndex(page_list->row(current));
535 void Settings::on_view_zoomToFitDuringAcq_changed(int state)
537 GlobalSettings settings;
538 settings.setValue(GlobalSettings::Key_View_ZoomToFitDuringAcq, state ? true : false);
541 void Settings::on_view_zoomToFitAfterAcq_changed(int state)
543 GlobalSettings settings;
544 settings.setValue(GlobalSettings::Key_View_ZoomToFitAfterAcq, state ? true : false);
547 void Settings::on_view_triggerIsZero_changed(int state)
549 GlobalSettings settings;
550 settings.setValue(GlobalSettings::Key_View_TriggerIsZeroTime, state ? true : false);
553 void Settings::on_view_colouredBG_changed(int state)
555 GlobalSettings settings;
556 settings.setValue(GlobalSettings::Key_View_ColouredBG, state ? true : false);
559 void Settings::on_view_stickyScrolling_changed(int state)
561 GlobalSettings settings;
562 settings.setValue(GlobalSettings::Key_View_StickyScrolling, state ? true : false);
565 void Settings::on_view_showSamplingPoints_changed(int state)
567 GlobalSettings settings;
568 settings.setValue(GlobalSettings::Key_View_ShowSamplingPoints, state ? true : false);
571 void Settings::on_view_showAnalogMinorGrid_changed(int state)
573 GlobalSettings settings;
574 settings.setValue(GlobalSettings::Key_View_ShowAnalogMinorGrid, state ? true : false);
577 void Settings::on_view_conversionThresholdDispMode_changed(int state)
579 GlobalSettings settings;
580 settings.setValue(GlobalSettings::Key_View_ConversionThresholdDispMode, state);
583 void Settings::on_view_defaultDivHeight_changed(int value)
585 GlobalSettings settings;
586 settings.setValue(GlobalSettings::Key_View_DefaultDivHeight, value);
589 void Settings::on_view_defaultLogicHeight_changed(int value)
591 GlobalSettings settings;
592 settings.setValue(GlobalSettings::Key_View_DefaultLogicHeight, value);
595 void Settings::on_dec_initialStateConfigurable_changed(int state)
597 GlobalSettings settings;
598 settings.setValue(GlobalSettings::Key_Dec_InitialStateConfigurable, state ? true : false);
601 void Settings::on_log_logLevel_changed(int value)
603 logging.set_log_level(value);
606 void Settings::on_log_bufferSize_changed(int value)
608 GlobalSettings settings;
609 settings.setValue(GlobalSettings::Key_Log_BufferSize, value);
612 void Settings::on_log_saveToFile_clicked(bool checked)
616 const QString file_name = QFileDialog::getSaveFileName(
617 this, tr("Save Log"), "", tr("Log Files (*.txt *.log);;All Files (*)"));
619 if (file_name.isEmpty())
622 QFile file(file_name);
623 if (file.open(QIODevice::WriteOnly | QIODevice::Truncate | QIODevice::Text)) {
624 QTextStream out_stream(&file);
625 out_stream << log_view_->toPlainText();
627 if (out_stream.status() == QTextStream::Ok) {
628 QMessageBox msg(this);
629 msg.setText(tr("Success"));
630 msg.setInformativeText(tr("Log saved to %1.").arg(file_name));
631 msg.setStandardButtons(QMessageBox::Ok);
632 msg.setIcon(QMessageBox::Information);
639 QMessageBox msg(this);
640 msg.setText(tr("Error"));
641 msg.setInformativeText(tr("File %1 could not be written to.").arg(file_name));
642 msg.setStandardButtons(QMessageBox::Ok);
643 msg.setIcon(QMessageBox::Warning);
647 void Settings::on_log_popOut_clicked(bool checked)
651 // Create the window as a sub-window so it closes when the main window closes
652 QMainWindow *window = new QMainWindow(0, Qt::SubWindow);
654 window->setObjectName(QString::fromUtf8("Log Window"));
655 window->setWindowTitle(tr("%1 Log").arg(PV_TITLE));
657 // Use same width/height as the settings dialog
658 window->resize(width(), height());
660 window->setCentralWidget(create_log_view());
664 } // namespace dialogs