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>
26 #include <QDialogButtonBox>
27 #include <QFormLayout>
29 #include <QHBoxLayout>
33 #include <QTextBrowser>
34 #include <QTextDocument>
35 #include <QVBoxLayout>
37 #include "settings.hpp"
39 #include "pv/devicemanager.hpp"
40 #include "pv/globalsettings.hpp"
42 #include <libsigrokcxx/libsigrokcxx.hpp>
45 #include <libsigrokdecode/libsigrokdecode.h>
48 using std::shared_ptr;
53 Settings::Settings(DeviceManager &device_manager, QWidget *parent) :
54 QDialog(parent, nullptr),
55 device_manager_(device_manager)
57 const int icon_size = 64;
61 page_list = new QListWidget;
62 page_list->setViewMode(QListView::IconMode);
63 page_list->setIconSize(QSize(icon_size, icon_size));
64 page_list->setMovement(QListView::Static);
65 page_list->setMaximumWidth(icon_size + (icon_size / 2));
66 page_list->setSpacing(12);
68 pages = new QStackedWidget;
70 page_list->setCurrentIndex(page_list->model()->index(0, 0));
72 QHBoxLayout *tab_layout = new QHBoxLayout;
73 tab_layout->addWidget(page_list);
74 tab_layout->addWidget(pages, Qt::AlignLeft);
76 QDialogButtonBox *button_box = new QDialogButtonBox(
77 QDialogButtonBox::Ok | QDialogButtonBox::Cancel);
79 QVBoxLayout* root_layout = new QVBoxLayout(this);
80 root_layout->addLayout(tab_layout);
81 root_layout->addWidget(button_box);
83 connect(button_box, SIGNAL(accepted()), this, SLOT(accept()));
84 connect(button_box, SIGNAL(rejected()), this, SLOT(reject()));
85 connect(page_list, SIGNAL(currentItemChanged(QListWidgetItem*, QListWidgetItem*)),
86 this, SLOT(on_page_changed(QListWidgetItem*, QListWidgetItem*)));
88 // Start to record changes
89 GlobalSettings settings;
90 settings.start_tracking();
93 void Settings::create_pages()
96 pages->addWidget(get_view_settings_form(pages));
98 QListWidgetItem *viewButton = new QListWidgetItem(page_list);
99 viewButton->setIcon(QIcon(":/icons/settings-views.svg"));
100 viewButton->setText(tr("Views"));
101 viewButton->setTextAlignment(Qt::AlignHCenter);
102 viewButton->setFlags(Qt::ItemIsSelectable | Qt::ItemIsEnabled);
106 pages->addWidget(get_decoder_settings_form(pages));
108 QListWidgetItem *decoderButton = new QListWidgetItem(page_list);
109 decoderButton->setIcon(QIcon(":/icons/add-decoder.svg"));
110 decoderButton->setText(tr("Decoders"));
111 decoderButton->setTextAlignment(Qt::AlignHCenter);
112 decoderButton->setFlags(Qt::ItemIsSelectable | Qt::ItemIsEnabled);
116 pages->addWidget(get_about_page(pages));
118 QListWidgetItem *aboutButton = new QListWidgetItem(page_list);
119 aboutButton->setIcon(QIcon(":/icons/information.svg"));
120 aboutButton->setText(tr("About"));
121 aboutButton->setTextAlignment(Qt::AlignHCenter);
122 aboutButton->setFlags(Qt::ItemIsSelectable | Qt::ItemIsEnabled);
125 QCheckBox *Settings::create_checkbox(const QString& key, const char* slot) const
127 GlobalSettings settings;
129 QCheckBox *cb = new QCheckBox();
130 cb->setChecked(settings.value(key).toBool());
131 connect(cb, SIGNAL(stateChanged(int)), this, slot);
135 QWidget *Settings::get_view_settings_form(QWidget *parent) const
137 GlobalSettings settings;
140 QWidget *form = new QWidget(parent);
141 QVBoxLayout *form_layout = new QVBoxLayout(form);
143 // Trace view settings
144 QGroupBox *trace_view_group = new QGroupBox(tr("Trace View"));
145 form_layout->addWidget(trace_view_group);
147 QFormLayout *trace_view_layout = new QFormLayout();
148 trace_view_group->setLayout(trace_view_layout);
150 cb = create_checkbox(GlobalSettings::Key_View_ColouredBG,
151 SLOT(on_view_colouredBG_changed(int)));
152 trace_view_layout->addRow(tr("Use coloured trace &background"), cb);
154 cb = create_checkbox(GlobalSettings::Key_View_ZoomToFitDuringAcq,
155 SLOT(on_view_zoomToFitDuringAcq_changed(int)));
156 trace_view_layout->addRow(tr("Constantly perform &zoom-to-fit during acquisition"), cb);
158 cb = create_checkbox(GlobalSettings::Key_View_ZoomToFitAfterAcq,
159 SLOT(on_view_zoomToFitAfterAcq_changed(int)));
160 trace_view_layout->addRow(tr("Perform a zoom-to-&fit when acquisition stops"), cb);
162 cb = create_checkbox(GlobalSettings::Key_View_StickyScrolling,
163 SLOT(on_view_stickyScrolling_changed(int)));
164 trace_view_layout->addRow(tr("Always keep &newest samples at the right edge during capture"), cb);
166 cb = create_checkbox(GlobalSettings::Key_View_ShowSamplingPoints,
167 SLOT(on_view_showSamplingPoints_changed(int)));
168 trace_view_layout->addRow(tr("Show data &sampling points"), cb);
170 cb = create_checkbox(GlobalSettings::Key_View_ShowAnalogMinorGrid,
171 SLOT(on_view_showAnalogMinorGrid_changed(int)));
172 trace_view_layout->addRow(tr("Show analog minor grid in addition to vdiv grid"), cb);
174 cb = create_checkbox(GlobalSettings::Key_View_ShowConversionThresholds,
175 SLOT(on_view_showConversionThresholds_changed(int)));
176 trace_view_layout->addRow(tr("Show conversion thresholds in analog traces"), cb);
178 QSpinBox *default_div_height_sb = new QSpinBox();
179 default_div_height_sb->setRange(20, 1000);
180 default_div_height_sb->setSuffix(tr(" pixels"));
181 default_div_height_sb->setValue(
182 settings.value(GlobalSettings::Key_View_DefaultDivHeight).toInt());
183 connect(default_div_height_sb, SIGNAL(valueChanged(int)), this,
184 SLOT(on_view_defaultDivHeight_changed(int)));
185 trace_view_layout->addRow(tr("Default analog trace div height"), default_div_height_sb);
187 QSpinBox *default_logic_height_sb = new QSpinBox();
188 default_logic_height_sb->setRange(5, 1000);
189 default_logic_height_sb->setSuffix(tr(" pixels"));
190 default_logic_height_sb->setValue(
191 settings.value(GlobalSettings::Key_View_DefaultLogicHeight).toInt());
192 connect(default_logic_height_sb, SIGNAL(valueChanged(int)), this,
193 SLOT(on_view_defaultLogicHeight_changed(int)));
194 trace_view_layout->addRow(tr("Default logic trace height"), default_logic_height_sb);
199 QWidget *Settings::get_decoder_settings_form(QWidget *parent) const
204 QWidget *form = new QWidget(parent);
205 QVBoxLayout *form_layout = new QVBoxLayout(form);
208 QGroupBox *decoder_group = new QGroupBox(tr("Decoders"));
209 form_layout->addWidget(decoder_group);
211 QFormLayout *decoder_layout = new QFormLayout();
212 decoder_group->setLayout(decoder_layout);
214 cb = create_checkbox(GlobalSettings::Key_Dec_InitialStateConfigurable,
215 SLOT(on_dec_initialStateConfigurable_changed(int)));
216 decoder_layout->addRow(tr("Allow configuration of &initial signal state"), cb);
226 static gint sort_pds(gconstpointer a, gconstpointer b)
228 const struct srd_decoder *sda, *sdb;
230 sda = (const struct srd_decoder *)a;
231 sdb = (const struct srd_decoder *)b;
232 return strcmp(sda->id, sdb->id);
236 QWidget *Settings::get_about_page(QWidget *parent) const
239 struct srd_decoder *dec;
242 QLabel *icon = new QLabel();
243 icon->setPixmap(QPixmap(QString::fromUtf8(":/icons/pulseview.svg")));
245 /* Setup the version field */
246 QLabel *version_info = new QLabel();
247 version_info->setText(tr("%1 %2<br />%3<br /><a href=\"http://%4\">%4</a>")
248 .arg(QApplication::applicationName(),
249 QApplication::applicationVersion(),
250 tr("GNU GPL, version 3 or later"),
251 QApplication::organizationDomain()));
252 version_info->setOpenExternalLinks(true);
254 shared_ptr<sigrok::Context> context = device_manager_.context();
258 s.append("<style type=\"text/css\"> tr .id { white-space: pre; padding-right: 5px; } </style>");
263 s.append("<tr><td colspan=\"2\"><b>" +
264 tr("Libraries and features:") + "</b></td></tr>");
266 s.append(QString("<tr><td><i>%1</i></td><td>%2</td></tr>")
267 .arg(QString("Qt"), qVersion()));
268 s.append(QString("<tr><td><i>%1</i></td><td>%2</td></tr>")
269 .arg(QString("glibmm"), PV_GLIBMM_VERSION));
270 s.append(QString("<tr><td><i>%1</i></td><td>%2</td></tr>")
271 .arg(QString("Boost"), BOOST_LIB_VERSION));
273 s.append(QString("<tr><td><i>%1</i></td><td>%2/%3 (rt: %4/%5)</td></tr>")
274 .arg(QString("libsigrok"), SR_PACKAGE_VERSION_STRING,
275 SR_LIB_VERSION_STRING, sr_package_version_string_get(),
276 sr_lib_version_string_get()));
278 GSList *l_orig = sr_buildinfo_libs_get();
279 for (GSList *l = l_orig; l; l = l->next) {
280 GSList *m = (GSList *)l->data;
281 const char *lib = (const char *)m->data;
282 const char *version = (const char *)m->next->data;
283 s.append(QString("<tr><td><i>- %1</i></td><td>%2</td></tr>")
284 .arg(QString(lib), QString(version)));
285 g_slist_free_full(m, g_free);
287 g_slist_free(l_orig);
289 char *host = sr_buildinfo_host_get();
290 s.append(QString("<tr><td><i>- Host</i></td><td>%1</td></tr>")
291 .arg(QString(host)));
294 char *scpi_backends = sr_buildinfo_scpi_backends_get();
295 s.append(QString("<tr><td><i>- SCPI backends</i></td><td>%1</td></tr>")
296 .arg(QString(scpi_backends)));
297 g_free(scpi_backends);
300 s.append(QString("<tr><td><i>%1</i></td><td>%2/%3 (rt: %4/%5)</td></tr>")
301 .arg(QString("libsigrokdecode"), SRD_PACKAGE_VERSION_STRING,
302 SRD_LIB_VERSION_STRING, srd_package_version_string_get(),
303 srd_lib_version_string_get()));
305 l_orig = srd_buildinfo_libs_get();
306 for (GSList *l = l_orig; l; l = l->next) {
307 GSList *m = (GSList *)l->data;
308 const char *lib = (const char *)m->data;
309 const char *version = (const char *)m->next->data;
310 s.append(QString("<tr><td><i>- %1</i></td><td>%2</td></tr>")
311 .arg(QString(lib), QString(version)));
312 g_slist_free_full(m, g_free);
314 g_slist_free(l_orig);
316 host = srd_buildinfo_host_get();
317 s.append(QString("<tr><td><i>- Host</i></td><td>%1</td></tr>")
318 .arg(QString(host)));
322 /* Set up the supported field */
323 s.append("<tr><td colspan=\"2\"></td></tr>");
324 s.append("<tr><td colspan=\"2\"><b>" +
325 tr("Supported hardware drivers:") + "</b></td></tr>");
326 for (auto entry : context->drivers()) {
327 s.append(QString("<tr><td class=\"id\"><i>%1</i></td><td>%2</td></tr>")
328 .arg(QString::fromUtf8(entry.first.c_str()),
329 QString::fromUtf8(entry.second->long_name().c_str())));
332 s.append("<tr><td colspan=\"2\"></td></tr>");
333 s.append("<tr><td colspan=\"2\"><b>" +
334 tr("Supported input formats:") + "</b></td></tr>");
335 for (auto entry : context->input_formats()) {
336 s.append(QString("<tr><td class=\"id\"><i>%1</i></td><td>%2</td></tr>")
337 .arg(QString::fromUtf8(entry.first.c_str()),
338 QString::fromUtf8(entry.second->description().c_str())));
341 s.append("<tr><td colspan=\"2\"></td></tr>");
342 s.append("<tr><td colspan=\"2\"><b>" +
343 tr("Supported output formats:") + "</b></td></tr>");
344 for (auto entry : context->output_formats()) {
345 s.append(QString("<tr><td class=\"id\"><i>%1</i></td><td>%2</td></tr>")
346 .arg(QString::fromUtf8(entry.first.c_str()),
347 QString::fromUtf8(entry.second->description().c_str())));
351 s.append("<tr><td colspan=\"2\"></td></tr>");
352 s.append("<tr><td colspan=\"2\"><b>" +
353 tr("Supported protocol decoders:") + "</b></td></tr>");
354 GSList *sl = g_slist_copy((GSList *)srd_decoder_list());
355 sl = g_slist_sort(sl, sort_pds);
356 for (const GSList *l = sl; l; l = l->next) {
357 dec = (struct srd_decoder *)l->data;
358 s.append(QString("<tr><td class=\"id\"><i>%1</i></td><td>%2</td></tr>")
359 .arg(QString::fromUtf8(dec->id),
360 QString::fromUtf8(dec->longname)));
365 s.append("</table>");
367 QTextDocument *supported_doc = new QTextDocument();
368 supported_doc->setHtml(s);
370 QTextBrowser *support_list = new QTextBrowser();
371 support_list->setDocument(supported_doc);
373 QGridLayout *layout = new QGridLayout();
374 layout->addWidget(icon, 0, 0, 1, 1);
375 layout->addWidget(version_info, 0, 1, 1, 1);
376 layout->addWidget(support_list, 1, 1, 1, 1);
378 QWidget *page = new QWidget(parent);
379 page->setLayout(layout);
384 void Settings::accept()
386 GlobalSettings settings;
387 settings.stop_tracking();
392 void Settings::reject()
394 GlobalSettings settings;
395 settings.undo_tracked_changes();
400 void Settings::on_page_changed(QListWidgetItem *current, QListWidgetItem *previous)
405 pages->setCurrentIndex(page_list->row(current));
408 void Settings::on_view_zoomToFitDuringAcq_changed(int state)
410 GlobalSettings settings;
411 settings.setValue(GlobalSettings::Key_View_ZoomToFitDuringAcq, state ? true : false);
414 void Settings::on_view_zoomToFitAfterAcq_changed(int state)
416 GlobalSettings settings;
417 settings.setValue(GlobalSettings::Key_View_ZoomToFitAfterAcq, state ? true : false);
420 void Settings::on_view_colouredBG_changed(int state)
422 GlobalSettings settings;
423 settings.setValue(GlobalSettings::Key_View_ColouredBG, state ? true : false);
426 void Settings::on_view_stickyScrolling_changed(int state)
428 GlobalSettings settings;
429 settings.setValue(GlobalSettings::Key_View_StickyScrolling, state ? true : false);
432 void Settings::on_view_showSamplingPoints_changed(int state)
434 GlobalSettings settings;
435 settings.setValue(GlobalSettings::Key_View_ShowSamplingPoints, state ? true : false);
438 void Settings::on_view_showAnalogMinorGrid_changed(int state)
440 GlobalSettings settings;
441 settings.setValue(GlobalSettings::Key_View_ShowAnalogMinorGrid, state ? true : false);
444 void Settings::on_view_showConversionThresholds_changed(int state)
446 GlobalSettings settings;
447 settings.setValue(GlobalSettings::Key_View_ShowConversionThresholds, state ? true : false);
450 void Settings::on_view_defaultDivHeight_changed(int value)
452 GlobalSettings settings;
453 settings.setValue(GlobalSettings::Key_View_DefaultDivHeight, value);
456 void Settings::on_view_defaultLogicHeight_changed(int value)
458 GlobalSettings settings;
459 settings.setValue(GlobalSettings::Key_View_DefaultLogicHeight, value);
462 void Settings::on_dec_initialStateConfigurable_changed(int state)
464 GlobalSettings settings;
465 settings.setValue(GlobalSettings::Key_Dec_InitialStateConfigurable, state ? true : false);
468 } // namespace dialogs