068d99b3108ead8c35a5c2dce0e1884a096f6d9d
[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 <QApplication>
21 #include <QCheckBox>
22 #include <QDialogButtonBox>
23 #include <QFormLayout>
24 #include <QGroupBox>
25 #include <QHBoxLayout>
26 #include <QLabel>
27 #include <QString>
28 #include <QTextBrowser>
29 #include <QTextDocument>
30 #include <QVBoxLayout>
31
32 #include "settings.hpp"
33
34 #include "pv/devicemanager.hpp"
35 #include "pv/globalsettings.hpp"
36
37 #include <libsigrokcxx/libsigrokcxx.hpp>
38
39 #ifdef ENABLE_DECODE
40 #include <libsigrokdecode/libsigrokdecode.h>
41 #endif
42
43 namespace pv {
44 namespace dialogs {
45
46 Settings::Settings(DeviceManager &device_manager, QWidget *parent) :
47         QDialog(parent, nullptr),
48         device_manager_(device_manager)
49 {
50         const int icon_size = 64;
51
52         resize(600, 400);
53
54         page_list = new QListWidget;
55         page_list->setViewMode(QListView::IconMode);
56         page_list->setIconSize(QSize(icon_size, icon_size));
57         page_list->setMovement(QListView::Static);
58         page_list->setMaximumWidth(icon_size + icon_size/2);
59         page_list->setSpacing(12);
60
61         pages = new QStackedWidget;
62         create_pages();
63         page_list->setCurrentIndex(page_list->model()->index(0, 0));
64
65         QHBoxLayout *tab_layout = new QHBoxLayout;
66         tab_layout->addWidget(page_list);
67         tab_layout->addWidget(pages, Qt::AlignLeft);
68
69         QDialogButtonBox *button_box = new QDialogButtonBox(
70                 QDialogButtonBox::Ok | QDialogButtonBox::Cancel);
71
72         QVBoxLayout* root_layout = new QVBoxLayout(this);
73         root_layout->addLayout(tab_layout);
74         root_layout->addWidget(button_box);
75
76         connect(button_box, SIGNAL(accepted()), this, SLOT(accept()));
77         connect(button_box, SIGNAL(rejected()), this, SLOT(reject()));
78         connect(page_list, SIGNAL(currentItemChanged(QListWidgetItem*, QListWidgetItem*)),
79                 this, SLOT(on_page_changed(QListWidgetItem*, QListWidgetItem*)));
80
81         // Start to record changes
82         GlobalSettings settings;
83         settings.start_tracking();
84 }
85
86 void Settings::create_pages()
87 {
88         // View page
89         pages->addWidget(get_view_settings_form(pages));
90
91         QListWidgetItem *viewButton = new QListWidgetItem(page_list);
92         viewButton->setIcon(QIcon(":/icons/settings-views.svg"));
93         viewButton->setText(tr("Views"));
94         viewButton->setTextAlignment(Qt::AlignHCenter);
95         viewButton->setFlags(Qt::ItemIsSelectable | Qt::ItemIsEnabled);
96
97         // About page
98         pages->addWidget(get_about_page(pages));
99
100         QListWidgetItem *aboutButton = new QListWidgetItem(page_list);
101         aboutButton->setIcon(QIcon(":/icons/information.svg"));
102         aboutButton->setText(tr("About"));
103         aboutButton->setTextAlignment(Qt::AlignHCenter);
104         aboutButton->setFlags(Qt::ItemIsSelectable | Qt::ItemIsEnabled);
105 }
106
107 QWidget *Settings::get_view_settings_form(QWidget *parent) const
108 {
109         GlobalSettings settings;
110
111         QWidget *form = new QWidget(parent);
112         QVBoxLayout *form_layout = new QVBoxLayout(form);
113
114         // Trace view settings
115         QGroupBox *trace_view_group = new QGroupBox(tr("Trace View"));
116         form_layout->addWidget(trace_view_group);
117
118         QFormLayout *trace_view_layout = new QFormLayout();
119         trace_view_group->setLayout(trace_view_layout);
120
121         QCheckBox *coloured_bg_cb = new QCheckBox();
122         coloured_bg_cb->setChecked(settings.value(GlobalSettings::Key_View_ColouredBG).toBool());
123         connect(coloured_bg_cb, SIGNAL(stateChanged(int)), this, SLOT(on_view_colouredBG_changed(int)));
124         trace_view_layout->addRow(tr("Use coloured trace &background"), coloured_bg_cb);
125
126         QCheckBox *always_zoom_to_fit_cb = new QCheckBox();
127         always_zoom_to_fit_cb->setChecked(settings.value(GlobalSettings::Key_View_AlwaysZoomToFit).toBool());
128         connect(always_zoom_to_fit_cb, SIGNAL(stateChanged(int)), this, SLOT(on_view_alwaysZoomToFit_changed(int)));
129         trace_view_layout->addRow(tr("Constantly perform &zoom-to-fit during capture"), always_zoom_to_fit_cb);
130
131         QCheckBox *sticky_scrolling_cb = new QCheckBox();
132         sticky_scrolling_cb->setChecked(settings.value(GlobalSettings::Key_View_StickyScrolling).toBool());
133         connect(sticky_scrolling_cb, SIGNAL(stateChanged(int)), this, SLOT(on_view_stickyScrolling_changed(int)));
134         trace_view_layout->addRow(tr("Always keep &newest samples at the right edge during capture"), sticky_scrolling_cb);
135
136         QCheckBox *show_sampling_points_cb = new QCheckBox();
137         show_sampling_points_cb->setChecked(settings.value(GlobalSettings::Key_View_ShowSamplingPoints).toBool());
138         connect(show_sampling_points_cb, SIGNAL(stateChanged(int)), this, SLOT(on_view_showSamplingPoints_changed(int)));
139         trace_view_layout->addRow(tr("Show data &sampling points"), show_sampling_points_cb);
140
141         return form;
142 }
143
144 QWidget *Settings::get_about_page(QWidget *parent) const
145 {
146 #ifdef ENABLE_DECODE
147         struct srd_decoder *dec;
148 #endif
149
150         QLabel *icon = new QLabel();
151         icon->setPixmap(QPixmap(QString::fromUtf8(":/icons/sigrok-logo-notext.svg")));
152
153         /* Setup the version field */
154         QLabel *version_info = new QLabel();
155         version_info->setText(tr("%1 %2<br />%3<br /><a href=\"http://%4\">%4</a>")
156                 .arg(QApplication::applicationName(),
157                 QApplication::applicationVersion(),
158                 tr("GNU GPL, version 3 or later"),
159                 QApplication::organizationDomain()));
160         version_info->setOpenExternalLinks(true);
161
162         std::shared_ptr<sigrok::Context> context = device_manager_.context();
163
164         QString s;
165         s.append("<table>");
166
167         /* Set up the supported field */
168         s.append("<tr><td colspan=\"2\"><b>" +
169                 tr("Supported hardware drivers:") +
170                 "</b></td></tr>");
171         for (auto entry : context->drivers()) {
172                 s.append(QString("<tr><td><i>%1</i></td><td>%2</td></tr>")
173                         .arg(QString::fromUtf8(entry.first.c_str()),
174                                 QString::fromUtf8(entry.second->long_name().c_str())));
175         }
176
177         s.append("<tr><td colspan=\"2\"><b>" +
178                 tr("Supported input formats:") +
179                 "</b></td></tr>");
180         for (auto entry : context->input_formats()) {
181                 s.append(QString("<tr><td><i>%1</i></td><td>%2</td></tr>")
182                         .arg(QString::fromUtf8(entry.first.c_str()),
183                                 QString::fromUtf8(entry.second->description().c_str())));
184         }
185
186         s.append("<tr><td colspan=\"2\"><b>" +
187                 tr("Supported output formats:") +
188                 "</b></td></tr>");
189         for (auto entry : context->output_formats()) {
190                 s.append(QString("<tr><td><i>%1</i></td><td>%2</td></tr>")
191                         .arg(QString::fromUtf8(entry.first.c_str()),
192                                 QString::fromUtf8(entry.second->description().c_str())));
193         }
194
195 #ifdef ENABLE_DECODE
196         s.append("<tr><td colspan=\"2\"><b>" +
197                 tr("Supported protocol decoders:") +
198                 "</b></td></tr>");
199         for (const GSList *l = srd_decoder_list(); l; l = l->next) {
200                 dec = (struct srd_decoder *)l->data;
201                 s.append(QString("<tr><td><i>%1</i></td><td>%2</td></tr>")
202                         .arg(QString::fromUtf8(dec->id),
203                                 QString::fromUtf8(dec->longname)));
204         }
205 #endif
206
207         s.append("</table>");
208
209         QTextDocument *supported_doc = new QTextDocument();
210         supported_doc->setHtml(s);
211
212         QTextBrowser *support_list = new QTextBrowser();
213         support_list->setDocument(supported_doc);
214
215         QGridLayout *layout = new QGridLayout();
216         layout->addWidget(icon, 0, 0, 1, 1);
217         layout->addWidget(version_info, 0, 1, 1, 1);
218         layout->addWidget(support_list, 1, 1, 1, 1);
219
220         QWidget *page = new QWidget(parent);
221         page->setLayout(layout);
222
223         return page;
224 }
225
226 void Settings::accept()
227 {
228         GlobalSettings settings;
229         settings.stop_tracking();
230
231         QDialog::accept();
232 }
233
234 void Settings::reject()
235 {
236         GlobalSettings settings;
237         settings.undo_tracked_changes();
238
239         QDialog::reject();
240 }
241
242 void Settings::on_page_changed(QListWidgetItem *current, QListWidgetItem *previous)
243 {
244         if (!current)
245                 current = previous;
246
247         pages->setCurrentIndex(page_list->row(current));
248 }
249
250 void Settings::on_view_alwaysZoomToFit_changed(int state)
251 {
252         GlobalSettings settings;
253         settings.setValue(GlobalSettings::Key_View_AlwaysZoomToFit, state ? true : false);
254 }
255
256 void Settings::on_view_colouredBG_changed(int state)
257 {
258         GlobalSettings settings;
259         settings.setValue(GlobalSettings::Key_View_ColouredBG, state ? true : false);
260 }
261
262 void Settings::on_view_stickyScrolling_changed(int state)
263 {
264         GlobalSettings settings;
265         settings.setValue(GlobalSettings::Key_View_StickyScrolling, state ? true : false);
266 }
267
268 void Settings::on_view_showSamplingPoints_changed(int state)
269 {
270         GlobalSettings settings;
271         settings.setValue(GlobalSettings::Key_View_ShowSamplingPoints, state ? true : false);
272 }
273
274 } // namespace dialogs
275 } // namespace pv