View: Limit header pane width
[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 #include <boost/version.hpp>
24
25 #include <QApplication>
26 #include <QCheckBox>
27 #include <QDialogButtonBox>
28 #include <QFormLayout>
29 #include <QGroupBox>
30 #include <QHBoxLayout>
31 #include <QLabel>
32 #include <QString>
33 #include <QTextBrowser>
34 #include <QTextDocument>
35 #include <QVBoxLayout>
36
37 #include "settings.hpp"
38
39 #include "pv/devicemanager.hpp"
40 #include "pv/globalsettings.hpp"
41
42 #include <libsigrokcxx/libsigrokcxx.hpp>
43
44 #ifdef ENABLE_DECODE
45 #include <libsigrokdecode/libsigrokdecode.h>
46 #endif
47
48 using std::shared_ptr;
49
50 namespace pv {
51 namespace dialogs {
52
53 Settings::Settings(DeviceManager &device_manager, QWidget *parent) :
54         QDialog(parent, nullptr),
55         device_manager_(device_manager)
56 {
57         const int icon_size = 64;
58
59         resize(600, 400);
60
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);
67
68         pages = new QStackedWidget;
69         create_pages();
70         page_list->setCurrentIndex(page_list->model()->index(0, 0));
71
72         QHBoxLayout *tab_layout = new QHBoxLayout;
73         tab_layout->addWidget(page_list);
74         tab_layout->addWidget(pages, Qt::AlignLeft);
75
76         QDialogButtonBox *button_box = new QDialogButtonBox(
77                 QDialogButtonBox::Ok | QDialogButtonBox::Cancel);
78
79         QVBoxLayout* root_layout = new QVBoxLayout(this);
80         root_layout->addLayout(tab_layout);
81         root_layout->addWidget(button_box);
82
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*)));
87
88         // Start to record changes
89         GlobalSettings settings;
90         settings.start_tracking();
91 }
92
93 void Settings::create_pages()
94 {
95         // View page
96         pages->addWidget(get_view_settings_form(pages));
97
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);
103
104         // About page
105         pages->addWidget(get_about_page(pages));
106
107         QListWidgetItem *aboutButton = new QListWidgetItem(page_list);
108         aboutButton->setIcon(QIcon(":/icons/information.svg"));
109         aboutButton->setText(tr("About"));
110         aboutButton->setTextAlignment(Qt::AlignHCenter);
111         aboutButton->setFlags(Qt::ItemIsSelectable | Qt::ItemIsEnabled);
112 }
113
114 QWidget *Settings::get_view_settings_form(QWidget *parent) const
115 {
116         GlobalSettings settings;
117
118         QWidget *form = new QWidget(parent);
119         QVBoxLayout *form_layout = new QVBoxLayout(form);
120
121         // Trace view settings
122         QGroupBox *trace_view_group = new QGroupBox(tr("Trace View"));
123         form_layout->addWidget(trace_view_group);
124
125         QFormLayout *trace_view_layout = new QFormLayout();
126         trace_view_group->setLayout(trace_view_layout);
127
128         QCheckBox *coloured_bg_cb = new QCheckBox();
129         coloured_bg_cb->setChecked(settings.value(GlobalSettings::Key_View_ColouredBG).toBool());
130         connect(coloured_bg_cb, SIGNAL(stateChanged(int)), this, SLOT(on_view_colouredBG_changed(int)));
131         trace_view_layout->addRow(tr("Use coloured trace &background"), coloured_bg_cb);
132
133         QCheckBox *always_zoom_to_fit_cb = new QCheckBox();
134         always_zoom_to_fit_cb->setChecked(settings.value(GlobalSettings::Key_View_AlwaysZoomToFit).toBool());
135         connect(always_zoom_to_fit_cb, SIGNAL(stateChanged(int)), this, SLOT(on_view_alwaysZoomToFit_changed(int)));
136         trace_view_layout->addRow(tr("Constantly perform &zoom-to-fit during capture"), always_zoom_to_fit_cb);
137
138         QCheckBox *sticky_scrolling_cb = new QCheckBox();
139         sticky_scrolling_cb->setChecked(settings.value(GlobalSettings::Key_View_StickyScrolling).toBool());
140         connect(sticky_scrolling_cb, SIGNAL(stateChanged(int)), this, SLOT(on_view_stickyScrolling_changed(int)));
141         trace_view_layout->addRow(tr("Always keep &newest samples at the right edge during capture"), sticky_scrolling_cb);
142
143         QCheckBox *show_sampling_points_cb = new QCheckBox();
144         show_sampling_points_cb->setChecked(settings.value(GlobalSettings::Key_View_ShowSamplingPoints).toBool());
145         connect(show_sampling_points_cb, SIGNAL(stateChanged(int)), this, SLOT(on_view_showSamplingPoints_changed(int)));
146         trace_view_layout->addRow(tr("Show data &sampling points"), show_sampling_points_cb);
147
148         QCheckBox *show_analog_minor_grid_cb = new QCheckBox();
149         show_analog_minor_grid_cb->setChecked(settings.value(GlobalSettings::Key_View_ShowAnalogMinorGrid).toBool());
150         connect(show_analog_minor_grid_cb, SIGNAL(stateChanged(int)), this, SLOT(on_view_showAnalogMinorGrid_changed(int)));
151         trace_view_layout->addRow(tr("Show analog minor grid in addition to vdiv grid"), show_analog_minor_grid_cb);
152
153         return form;
154 }
155
156 #ifdef ENABLE_DECODE
157 static gint sort_pds(gconstpointer a, gconstpointer b)
158 {
159         const struct srd_decoder *sda, *sdb;
160
161         sda = (const struct srd_decoder *)a;
162         sdb = (const struct srd_decoder *)b;
163         return strcmp(sda->id, sdb->id);
164 }
165 #endif
166
167 QWidget *Settings::get_about_page(QWidget *parent) const
168 {
169 #ifdef ENABLE_DECODE
170         struct srd_decoder *dec;
171 #endif
172
173         QLabel *icon = new QLabel();
174         icon->setPixmap(QPixmap(QString::fromUtf8(":/icons/sigrok-logo-notext.svg")));
175
176         /* Setup the version field */
177         QLabel *version_info = new QLabel();
178         version_info->setText(tr("%1 %2<br />%3<br /><a href=\"http://%4\">%4</a>")
179                 .arg(QApplication::applicationName(),
180                 QApplication::applicationVersion(),
181                 tr("GNU GPL, version 3 or later"),
182                 QApplication::organizationDomain()));
183         version_info->setOpenExternalLinks(true);
184
185         shared_ptr<sigrok::Context> context = device_manager_.context();
186
187         QString s;
188
189         s.append("<style type=\"text/css\"> tr .id { white-space: pre; padding-right: 5px; } </style>");
190
191         s.append("<table>");
192
193         /* Library info */
194         s.append("<tr><td colspan=\"2\"><b>" +
195                 tr("Libraries and features:") + "</b></td></tr>");
196
197         s.append(QString("<tr><td><i>%1</i></td><td>%2</td></tr>")
198                 .arg(QString("Qt"), qVersion()));
199         s.append(QString("<tr><td><i>%1</i></td><td>%2</td></tr>")
200                 .arg(QString("glibmm"), PV_GLIBMM_VERSION));
201         s.append(QString("<tr><td><i>%1</i></td><td>%2</td></tr>")
202                 .arg(QString("Boost"), BOOST_LIB_VERSION));
203
204         s.append(QString("<tr><td><i>%1</i></td><td>%2/%3 (rt: %4/%5)</td></tr>")
205                 .arg(QString("libsigrok"), SR_PACKAGE_VERSION_STRING,
206                 SR_LIB_VERSION_STRING, sr_package_version_string_get(),
207                 sr_lib_version_string_get()));
208
209         GSList *l_orig = sr_buildinfo_libs_get();
210         for (GSList *l = l_orig; l; l = l->next) {
211                 GSList *m = (GSList *)l->data;
212                 const char *lib = (const char *)m->data;
213                 const char *version = (const char *)m->next->data;
214                 s.append(QString("<tr><td><i>- %1</i></td><td>%2</td></tr>")
215                         .arg(QString(lib), QString(version)));
216                 g_slist_free_full(m, g_free);
217         }
218         g_slist_free(l_orig);
219
220         char *host = sr_buildinfo_host_get();
221         s.append(QString("<tr><td><i>- Host</i></td><td>%1</td></tr>")
222                 .arg(QString(host)));
223         g_free(host);
224
225         char *scpi_backends = sr_buildinfo_scpi_backends_get();
226         s.append(QString("<tr><td><i>- SCPI backends</i></td><td>%1</td></tr>")
227                 .arg(QString(scpi_backends)));
228         g_free(scpi_backends);
229
230 #ifdef ENABLE_DECODE
231         s.append(QString("<tr><td><i>%1</i></td><td>%2/%3 (rt: %4/%5)</td></tr>")
232                 .arg(QString("libsigrokdecode"), SRD_PACKAGE_VERSION_STRING,
233                 SRD_LIB_VERSION_STRING, srd_package_version_string_get(),
234                 srd_lib_version_string_get()));
235
236         l_orig = srd_buildinfo_libs_get();
237         for (GSList *l = l_orig; l; l = l->next) {
238                 GSList *m = (GSList *)l->data;
239                 const char *lib = (const char *)m->data;
240                 const char *version = (const char *)m->next->data;
241                 s.append(QString("<tr><td><i>- %1</i></td><td>%2</td></tr>")
242                         .arg(QString(lib), QString(version)));
243                 g_slist_free_full(m, g_free);
244         }
245         g_slist_free(l_orig);
246
247         host = srd_buildinfo_host_get();
248         s.append(QString("<tr><td><i>- Host</i></td><td>%1</td></tr>")
249                 .arg(QString(host)));
250         g_free(host);
251 #endif
252
253         /* Set up the supported field */
254         s.append("<tr><td colspan=\"2\"></td></tr>");
255         s.append("<tr><td colspan=\"2\"><b>" +
256                 tr("Supported hardware drivers:") + "</b></td></tr>");
257         for (auto entry : context->drivers()) {
258                 s.append(QString("<tr><td class=\"id\"><i>%1</i></td><td>%2</td></tr>")
259                         .arg(QString::fromUtf8(entry.first.c_str()),
260                                 QString::fromUtf8(entry.second->long_name().c_str())));
261         }
262
263         s.append("<tr><td colspan=\"2\"></td></tr>");
264         s.append("<tr><td colspan=\"2\"><b>" +
265                 tr("Supported input formats:") + "</b></td></tr>");
266         for (auto entry : context->input_formats()) {
267                 s.append(QString("<tr><td class=\"id\"><i>%1</i></td><td>%2</td></tr>")
268                         .arg(QString::fromUtf8(entry.first.c_str()),
269                                 QString::fromUtf8(entry.second->description().c_str())));
270         }
271
272         s.append("<tr><td colspan=\"2\"></td></tr>");
273         s.append("<tr><td colspan=\"2\"><b>" +
274                 tr("Supported output formats:") + "</b></td></tr>");
275         for (auto entry : context->output_formats()) {
276                 s.append(QString("<tr><td class=\"id\"><i>%1</i></td><td>%2</td></tr>")
277                         .arg(QString::fromUtf8(entry.first.c_str()),
278                                 QString::fromUtf8(entry.second->description().c_str())));
279         }
280
281 #ifdef ENABLE_DECODE
282         s.append("<tr><td colspan=\"2\"></td></tr>");
283         s.append("<tr><td colspan=\"2\"><b>" +
284                 tr("Supported protocol decoders:") + "</b></td></tr>");
285         GSList *sl = g_slist_copy((GSList *)srd_decoder_list());
286         sl = g_slist_sort(sl, sort_pds);
287         for (const GSList *l = sl; l; l = l->next) {
288                 dec = (struct srd_decoder *)l->data;
289                 s.append(QString("<tr><td class=\"id\"><i>%1</i></td><td>%2</td></tr>")
290                         .arg(QString::fromUtf8(dec->id),
291                                 QString::fromUtf8(dec->longname)));
292         }
293         g_slist_free(sl);
294 #endif
295
296         s.append("</table>");
297
298         QTextDocument *supported_doc = new QTextDocument();
299         supported_doc->setHtml(s);
300
301         QTextBrowser *support_list = new QTextBrowser();
302         support_list->setDocument(supported_doc);
303
304         QGridLayout *layout = new QGridLayout();
305         layout->addWidget(icon, 0, 0, 1, 1);
306         layout->addWidget(version_info, 0, 1, 1, 1);
307         layout->addWidget(support_list, 1, 1, 1, 1);
308
309         QWidget *page = new QWidget(parent);
310         page->setLayout(layout);
311
312         return page;
313 }
314
315 void Settings::accept()
316 {
317         GlobalSettings settings;
318         settings.stop_tracking();
319
320         QDialog::accept();
321 }
322
323 void Settings::reject()
324 {
325         GlobalSettings settings;
326         settings.undo_tracked_changes();
327
328         QDialog::reject();
329 }
330
331 void Settings::on_page_changed(QListWidgetItem *current, QListWidgetItem *previous)
332 {
333         if (!current)
334                 current = previous;
335
336         pages->setCurrentIndex(page_list->row(current));
337 }
338
339 void Settings::on_view_alwaysZoomToFit_changed(int state)
340 {
341         GlobalSettings settings;
342         settings.setValue(GlobalSettings::Key_View_AlwaysZoomToFit, state ? true : false);
343 }
344
345 void Settings::on_view_colouredBG_changed(int state)
346 {
347         GlobalSettings settings;
348         settings.setValue(GlobalSettings::Key_View_ColouredBG, state ? true : false);
349 }
350
351 void Settings::on_view_stickyScrolling_changed(int state)
352 {
353         GlobalSettings settings;
354         settings.setValue(GlobalSettings::Key_View_StickyScrolling, state ? true : false);
355 }
356
357 void Settings::on_view_showSamplingPoints_changed(int state)
358 {
359         GlobalSettings settings;
360         settings.setValue(GlobalSettings::Key_View_ShowSamplingPoints, state ? true : false);
361 }
362
363 void Settings::on_view_showAnalogMinorGrid_changed(int state)
364 {
365         GlobalSettings settings;
366         settings.setValue(GlobalSettings::Key_View_ShowAnalogMinorGrid, state ? true : false);
367 }
368
369 } // namespace dialogs
370 } // namespace pv