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/>.
20 #include <QApplication>
22 #include <QDialogButtonBox>
23 #include <QFormLayout>
25 #include <QHBoxLayout>
28 #include <QTextBrowser>
29 #include <QTextDocument>
30 #include <QVBoxLayout>
32 #include "settings.hpp"
34 #include "pv/devicemanager.hpp"
35 #include "pv/globalsettings.hpp"
37 #include <libsigrokcxx/libsigrokcxx.hpp>
40 #include <libsigrokdecode/libsigrokdecode.h>
43 using std::shared_ptr;
48 Settings::Settings(DeviceManager &device_manager, QWidget *parent) :
49 QDialog(parent, nullptr),
50 device_manager_(device_manager)
52 const int icon_size = 64;
56 page_list = new QListWidget;
57 page_list->setViewMode(QListView::IconMode);
58 page_list->setIconSize(QSize(icon_size, icon_size));
59 page_list->setMovement(QListView::Static);
60 page_list->setMaximumWidth(icon_size + (icon_size / 2));
61 page_list->setSpacing(12);
63 pages = new QStackedWidget;
65 page_list->setCurrentIndex(page_list->model()->index(0, 0));
67 QHBoxLayout *tab_layout = new QHBoxLayout;
68 tab_layout->addWidget(page_list);
69 tab_layout->addWidget(pages, Qt::AlignLeft);
71 QDialogButtonBox *button_box = new QDialogButtonBox(
72 QDialogButtonBox::Ok | QDialogButtonBox::Cancel);
74 QVBoxLayout* root_layout = new QVBoxLayout(this);
75 root_layout->addLayout(tab_layout);
76 root_layout->addWidget(button_box);
78 connect(button_box, SIGNAL(accepted()), this, SLOT(accept()));
79 connect(button_box, SIGNAL(rejected()), this, SLOT(reject()));
80 connect(page_list, SIGNAL(currentItemChanged(QListWidgetItem*, QListWidgetItem*)),
81 this, SLOT(on_page_changed(QListWidgetItem*, QListWidgetItem*)));
83 // Start to record changes
84 GlobalSettings settings;
85 settings.start_tracking();
88 void Settings::create_pages()
91 pages->addWidget(get_view_settings_form(pages));
93 QListWidgetItem *viewButton = new QListWidgetItem(page_list);
94 viewButton->setIcon(QIcon(":/icons/settings-views.svg"));
95 viewButton->setText(tr("Views"));
96 viewButton->setTextAlignment(Qt::AlignHCenter);
97 viewButton->setFlags(Qt::ItemIsSelectable | Qt::ItemIsEnabled);
100 pages->addWidget(get_about_page(pages));
102 QListWidgetItem *aboutButton = new QListWidgetItem(page_list);
103 aboutButton->setIcon(QIcon(":/icons/information.svg"));
104 aboutButton->setText(tr("About"));
105 aboutButton->setTextAlignment(Qt::AlignHCenter);
106 aboutButton->setFlags(Qt::ItemIsSelectable | Qt::ItemIsEnabled);
109 QWidget *Settings::get_view_settings_form(QWidget *parent) const
111 GlobalSettings settings;
113 QWidget *form = new QWidget(parent);
114 QVBoxLayout *form_layout = new QVBoxLayout(form);
116 // Trace view settings
117 QGroupBox *trace_view_group = new QGroupBox(tr("Trace View"));
118 form_layout->addWidget(trace_view_group);
120 QFormLayout *trace_view_layout = new QFormLayout();
121 trace_view_group->setLayout(trace_view_layout);
123 QCheckBox *coloured_bg_cb = new QCheckBox();
124 coloured_bg_cb->setChecked(settings.value(GlobalSettings::Key_View_ColouredBG).toBool());
125 connect(coloured_bg_cb, SIGNAL(stateChanged(int)), this, SLOT(on_view_colouredBG_changed(int)));
126 trace_view_layout->addRow(tr("Use coloured trace &background"), coloured_bg_cb);
128 QCheckBox *always_zoom_to_fit_cb = new QCheckBox();
129 always_zoom_to_fit_cb->setChecked(settings.value(GlobalSettings::Key_View_AlwaysZoomToFit).toBool());
130 connect(always_zoom_to_fit_cb, SIGNAL(stateChanged(int)), this, SLOT(on_view_alwaysZoomToFit_changed(int)));
131 trace_view_layout->addRow(tr("Constantly perform &zoom-to-fit during capture"), always_zoom_to_fit_cb);
133 QCheckBox *sticky_scrolling_cb = new QCheckBox();
134 sticky_scrolling_cb->setChecked(settings.value(GlobalSettings::Key_View_StickyScrolling).toBool());
135 connect(sticky_scrolling_cb, SIGNAL(stateChanged(int)), this, SLOT(on_view_stickyScrolling_changed(int)));
136 trace_view_layout->addRow(tr("Always keep &newest samples at the right edge during capture"), sticky_scrolling_cb);
138 QCheckBox *show_sampling_points_cb = new QCheckBox();
139 show_sampling_points_cb->setChecked(settings.value(GlobalSettings::Key_View_ShowSamplingPoints).toBool());
140 connect(show_sampling_points_cb, SIGNAL(stateChanged(int)), this, SLOT(on_view_showSamplingPoints_changed(int)));
141 trace_view_layout->addRow(tr("Show data &sampling points"), show_sampling_points_cb);
143 QCheckBox *show_analog_minor_grid_cb = new QCheckBox();
144 show_analog_minor_grid_cb->setChecked(settings.value(GlobalSettings::Key_View_ShowAnalogMinorGrid).toBool());
145 connect(show_analog_minor_grid_cb, SIGNAL(stateChanged(int)), this, SLOT(on_view_showAnalogMinorGrid_changed(int)));
146 trace_view_layout->addRow(tr("Show analog minor grid in addition to vdiv grid"), show_analog_minor_grid_cb);
152 static gint sort_pds(gconstpointer a, gconstpointer b)
154 const struct srd_decoder *sda, *sdb;
156 sda = (const struct srd_decoder *)a;
157 sdb = (const struct srd_decoder *)b;
158 return strcmp(sda->id, sdb->id);
162 QWidget *Settings::get_about_page(QWidget *parent) const
165 struct srd_decoder *dec;
168 QLabel *icon = new QLabel();
169 icon->setPixmap(QPixmap(QString::fromUtf8(":/icons/sigrok-logo-notext.svg")));
171 /* Setup the version field */
172 QLabel *version_info = new QLabel();
173 version_info->setText(tr("%1 %2<br />%3<br /><a href=\"http://%4\">%4</a>")
174 .arg(QApplication::applicationName(),
175 QApplication::applicationVersion(),
176 tr("GNU GPL, version 3 or later"),
177 QApplication::organizationDomain()));
178 version_info->setOpenExternalLinks(true);
180 shared_ptr<sigrok::Context> context = device_manager_.context();
186 s.append("<tr><td colspan=\"2\"><b>" +
187 tr("Used libraries:") + "</b></td></tr>");
188 s.append(QString("<tr><td><i>%1</i></td><td>%2</td></tr>")
189 .arg(QString("Qt"), qVersion()));
190 s.append(QString("<tr><td><i>%1</i></td><td>%2 (lib version %3)</td></tr>")
191 .arg(QString("libsigrok"), sr_package_version_string_get(), sr_lib_version_string_get()));
193 s.append(QString("<tr><td><i>%1</i></td><td>%2 (lib version %3)</td></tr>")
194 .arg(QString("libsigrokdecode"), srd_package_version_string_get(), srd_lib_version_string_get()));
197 /* Set up the supported field */
198 s.append("<tr><td colspan=\"2\"><b>" +
199 tr("Supported hardware drivers:") + "</b></td></tr>");
200 for (auto entry : context->drivers()) {
201 s.append(QString("<tr><td><i>%1</i></td><td>%2</td></tr>")
202 .arg(QString::fromUtf8(entry.first.c_str()),
203 QString::fromUtf8(entry.second->long_name().c_str())));
206 s.append("<tr><td colspan=\"2\"><b>" +
207 tr("Supported input formats:") + "</b></td></tr>");
208 for (auto entry : context->input_formats()) {
209 s.append(QString("<tr><td><i>%1</i></td><td>%2</td></tr>")
210 .arg(QString::fromUtf8(entry.first.c_str()),
211 QString::fromUtf8(entry.second->description().c_str())));
214 s.append("<tr><td colspan=\"2\"><b>" +
215 tr("Supported output formats:") + "</b></td></tr>");
216 for (auto entry : context->output_formats()) {
217 s.append(QString("<tr><td><i>%1</i></td><td>%2</td></tr>")
218 .arg(QString::fromUtf8(entry.first.c_str()),
219 QString::fromUtf8(entry.second->description().c_str())));
223 s.append("<tr><td colspan=\"2\"><b>" +
224 tr("Supported protocol decoders:") + "</b></td></tr>");
225 GSList *sl = g_slist_copy((GSList *)srd_decoder_list());
226 sl = g_slist_sort(sl, sort_pds);
227 for (const GSList *l = sl; l; l = l->next) {
228 dec = (struct srd_decoder *)l->data;
229 s.append(QString("<tr><td><i>%1</i></td><td>%2</td></tr>")
230 .arg(QString::fromUtf8(dec->id),
231 QString::fromUtf8(dec->longname)));
236 s.append("</table>");
238 QTextDocument *supported_doc = new QTextDocument();
239 supported_doc->setHtml(s);
241 QTextBrowser *support_list = new QTextBrowser();
242 support_list->setDocument(supported_doc);
244 QGridLayout *layout = new QGridLayout();
245 layout->addWidget(icon, 0, 0, 1, 1);
246 layout->addWidget(version_info, 0, 1, 1, 1);
247 layout->addWidget(support_list, 1, 1, 1, 1);
249 QWidget *page = new QWidget(parent);
250 page->setLayout(layout);
255 void Settings::accept()
257 GlobalSettings settings;
258 settings.stop_tracking();
263 void Settings::reject()
265 GlobalSettings settings;
266 settings.undo_tracked_changes();
271 void Settings::on_page_changed(QListWidgetItem *current, QListWidgetItem *previous)
276 pages->setCurrentIndex(page_list->row(current));
279 void Settings::on_view_alwaysZoomToFit_changed(int state)
281 GlobalSettings settings;
282 settings.setValue(GlobalSettings::Key_View_AlwaysZoomToFit, state ? true : false);
285 void Settings::on_view_colouredBG_changed(int state)
287 GlobalSettings settings;
288 settings.setValue(GlobalSettings::Key_View_ColouredBG, state ? true : false);
291 void Settings::on_view_stickyScrolling_changed(int state)
293 GlobalSettings settings;
294 settings.setValue(GlobalSettings::Key_View_StickyScrolling, state ? true : false);
297 void Settings::on_view_showSamplingPoints_changed(int state)
299 GlobalSettings settings;
300 settings.setValue(GlobalSettings::Key_View_ShowSamplingPoints, state ? true : false);
303 void Settings::on_view_showAnalogMinorGrid_changed(int state)
305 GlobalSettings settings;
306 settings.setValue(GlobalSettings::Key_View_ShowAnalogMinorGrid, state ? true : false);
309 } // namespace dialogs