]> sigrok.org Git - pulseview.git/blame - pv/subwindows/decoder_selector/subwindow.cpp
Backport recent changes from mainline.
[pulseview.git] / pv / subwindows / decoder_selector / subwindow.cpp
CommitLineData
10f5e6f5
UH
1/*
2 * This file is part of the PulseView project.
3 *
4 * Copyright (C) 2018 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 <algorithm>
21
22#include <QApplication>
23#include <QDebug>
24#include <QFontMetrics>
25#include <QInputDialog>
26#include <QLabel>
27#include <QLineEdit>
28#include <QPushButton>
29#include <QScrollArea>
30#include <QVBoxLayout>
31
32#include "pv/session.hpp"
33#include "pv/subwindows/decoder_selector/subwindow.hpp"
34
35#include <libsigrokdecode/libsigrokdecode.h>
36#include "subwindow.hpp" // Required only for lupdate since above include isn't recognized
37
38#define DECODERS_HAVE_TAGS \
39 ((SRD_PACKAGE_VERSION_MAJOR > 0) || \
40 (SRD_PACKAGE_VERSION_MAJOR == 0) && (SRD_PACKAGE_VERSION_MINOR > 5))
41
42using std::reverse;
43
44namespace pv {
45namespace subwindows {
46namespace decoder_selector {
47
48const char *initial_notice =
49 QT_TRANSLATE_NOOP("pv::subwindows::decoder_selector::SubWindow",
50 "Select a decoder to see its description here."); // clazy:exclude=non-pod-global-static
51
52const int min_width_margin = 75;
53
54
55bool QCustomSortFilterProxyModel::filterAcceptsRow(int source_row,
56 const QModelIndex& source_parent) const
57{
58 // Search model recursively
59
60 if (QSortFilterProxyModel::filterAcceptsRow(source_row, source_parent))
61 return true;
62
63 const QModelIndex index = sourceModel()->index(source_row, 0, source_parent);
64
65 for (int i = 0; i < sourceModel()->rowCount(index); i++)
66 if (filterAcceptsRow(i, index))
67 return true;
68
69 return false;
70}
71
72
73void QCustomTreeView::currentChanged(const QModelIndex& current,
74 const QModelIndex& previous)
75{
76 QTreeView::currentChanged(current, previous);
77 currentChanged(current);
78}
79
80
81SubWindow::SubWindow(Session& session, QWidget* parent) :
82 SubWindowBase(session, parent),
83 splitter_(new QSplitter()),
84 tree_view_(new QCustomTreeView()),
85 info_box_(new QWidget()),
86 info_label_header_(new QLabel()),
87 info_label_body_(new QLabel()),
88 info_label_footer_(new QLabel()),
89 model_(new DecoderCollectionModel()),
90 sort_filter_model_(new QCustomSortFilterProxyModel())
91{
92 QVBoxLayout* root_layout = new QVBoxLayout(this);
93 root_layout->setContentsMargins(0, 0, 0, 0);
94 root_layout->addWidget(splitter_);
95
96 QWidget* upper_container = new QWidget();
97 QVBoxLayout* upper_layout = new QVBoxLayout(upper_container);
98 upper_layout->setContentsMargins(0, 5, 0, 0);
99 QLineEdit* filter = new QLineEdit();
100 upper_layout->addWidget(filter);
101 upper_layout->addWidget(tree_view_);
102
103 splitter_->setOrientation(Qt::Vertical);
104 splitter_->addWidget(upper_container);
105 splitter_->addWidget(info_box_);
106
107 const QIcon filter_icon(QIcon::fromTheme("search",
108 QIcon(":/icons/search.svg")));
109 filter->setClearButtonEnabled(true);
110 filter->addAction(filter_icon, QLineEdit::LeadingPosition);
111
112
113 sort_filter_model_->setSourceModel(model_);
114 sort_filter_model_->setSortCaseSensitivity(Qt::CaseInsensitive);
115 sort_filter_model_->setFilterCaseSensitivity(Qt::CaseInsensitive);
116 sort_filter_model_->setFilterKeyColumn(-1);
117
118 tree_view_->setModel(sort_filter_model_);
119 tree_view_->setRootIsDecorated(true);
120 tree_view_->setSortingEnabled(true);
121 tree_view_->sortByColumn(0, Qt::AscendingOrder);
122
123 // Hide the columns that hold the detailed item information
124 tree_view_->hideColumn(2); // ID
125
126 // Ensure that all decoder tag names are fully visible by default
127 tree_view_->resizeColumnToContents(0);
128
129 tree_view_->setIndentation(10);
130
131#if (!DECODERS_HAVE_TAGS)
132 tree_view_->expandAll();
133 tree_view_->setItemsExpandable(false);
134#endif
135
136 QScrollArea* info_label_body_container = new QScrollArea();
137 info_label_body_container->setWidget(info_label_body_);
138 info_label_body_container->setWidgetResizable(true);
139
140 info_box_->setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Expanding);
141 QVBoxLayout* info_box_layout = new QVBoxLayout(info_box_);
142 info_box_layout->addWidget(info_label_header_);
143 info_box_layout->addWidget(info_label_body_container);
144 info_box_layout->addWidget(info_label_footer_);
145 info_box_layout->setAlignment(Qt::AlignTop);
146 Qt::TextInteractionFlags flags = Qt::TextSelectableByMouse | Qt::TextSelectableByKeyboard;
147 info_label_header_->setWordWrap(true);
148 info_label_header_->setTextInteractionFlags(flags);
149 info_label_body_->setWordWrap(true);
150 info_label_body_->setTextInteractionFlags(flags);
151 info_label_body_->setText(QString(tr(initial_notice)));
152 info_label_body_->setAlignment(Qt::AlignTop);
153 info_label_footer_->setWordWrap(true);
154 info_label_footer_->setTextInteractionFlags(flags);
155
156 connect(filter, SIGNAL(textChanged(const QString&)),
157 this, SLOT(on_filter_changed(const QString&)));
158 connect(filter, SIGNAL(returnPressed()),
159 this, SLOT(on_filter_return_pressed()));
160
161 connect(tree_view_, SIGNAL(currentChanged(const QModelIndex&)),
162 this, SLOT(on_item_changed(const QModelIndex&)));
163 connect(tree_view_, SIGNAL(activated(const QModelIndex&)),
164 this, SLOT(on_item_activated(const QModelIndex&)));
165
166 connect(this, SIGNAL(new_decoders_selected(vector<const srd_decoder*>)),
167 &session, SLOT(on_new_decoders_selected(vector<const srd_decoder*>)));
168
169 // Place the keyboard cursor in the filter QLineEdit initially
170 filter->setFocus();
171}
172
173bool SubWindow::has_toolbar() const
174{
175 return true;
176}
177
178QToolBar* SubWindow::create_toolbar(QWidget *parent) const
179{
180 QToolBar* toolbar = new QToolBar(parent);
181
182 return toolbar;
183}
184
185int SubWindow::minimum_width() const
186{
187 QFontMetrics m(info_label_body_->font());
188 const int label_width = m.width(QString(tr(initial_notice)));
189
190 return label_width + min_width_margin;
191}
192
193vector<const char*> SubWindow::get_decoder_inputs(const srd_decoder* d) const
194{
195 vector<const char*> ret_val;
196
197 for (GSList* li = d->inputs; li; li = li->next)
198 ret_val.push_back((const char*)li->data);
199
200 return ret_val;
201}
202
203vector<const srd_decoder*> SubWindow::get_decoders_providing(const char* output) const
204{
205 vector<const srd_decoder*> ret_val;
206
207 for (GSList* li = (GSList*)srd_decoder_list(); li; li = li->next) {
208 const srd_decoder* d = (srd_decoder*)li->data;
209 assert(d);
210
211 if (!d->outputs)
212 continue;
213
214 const int maxlen = 1024;
215
216 // TODO For now we ignore that d->outputs is actually a list
217 if (strncmp((char*)(d->outputs->data), output, maxlen) == 0)
218 ret_val.push_back(d);
219 }
220
221 return ret_val;
222}
223
224void SubWindow::on_item_changed(const QModelIndex& index)
225{
226 QString decoder_name, id, longname, desc, doc, tags;
227
228 // If the parent isn't valid, a category title was clicked
229 if (index.isValid() && index.parent().isValid()) {
230 QModelIndex id_index = index.model()->index(index.row(), 2, index.parent());
231 decoder_name = index.model()->data(id_index, Qt::DisplayRole).toString();
232
233 if (decoder_name.isEmpty())
234 return;
235
236 const srd_decoder* d = srd_decoder_get_by_id(decoder_name.toUtf8());
237
238 id = QString::fromUtf8(d->id);
239 longname = QString::fromUtf8(d->longname);
240 desc = QString::fromUtf8(d->desc);
241 doc = QString::fromUtf8(srd_decoder_doc_get(d)).trimmed();
242
243#if DECODERS_HAVE_TAGS
244 for (GSList* li = (GSList*)d->tags; li; li = li->next) {
245 QString s = (li == (GSList*)d->tags) ?
246 tr((char*)li->data) :
247 QString(tr(", %1")).arg(tr((char*)li->data));
248 tags.append(s);
249 }
250#endif
251 } else
252 doc = QString(tr(initial_notice));
253
254 if (!id.isEmpty())
255 info_label_header_->setText(
256 QString("<span style='font-size:large'><b>%1 (%2)</b></span><br><i>%3</i>")
257 .arg(longname, id, desc));
258 else
259 info_label_header_->clear();
260
261 info_label_body_->setText(doc);
262
263 if (!tags.isEmpty())
264 info_label_footer_->setText(tr("<p align='right'>Tags: %1</p>").arg(tags));
265 else
266 info_label_footer_->clear();
267}
268
269void SubWindow::on_item_activated(const QModelIndex& index)
270{
271 if (!index.isValid())
272 return;
273
274 QModelIndex id_index = index.model()->index(index.row(), 2, index.parent());
275 QString decoder_name = index.model()->data(id_index, Qt::DisplayRole).toString();
276
277 const srd_decoder* chosen_decoder = srd_decoder_get_by_id(decoder_name.toUtf8());
278 if (chosen_decoder == nullptr)
279 return;
280
281 vector<const srd_decoder*> decoders;
282 decoders.push_back(chosen_decoder);
283
284 // If the decoder only depends on logic inputs, we add it and are done
285 vector<const char*> inputs = get_decoder_inputs(decoders.front());
286 if (inputs.size() == 0) {
287 qWarning() << "Protocol decoder" << decoder_name << "cannot have 0 inputs!";
288 return;
289 }
290
291 if (strcmp(inputs.at(0), "logic") == 0) {
292 new_decoders_selected(decoders);
293 return;
294 }
295
296 // Check if we can automatically fulfill the stacking requirements
297 while (strcmp(inputs.at(0), "logic") != 0) {
298 vector<const srd_decoder*> prov_decoders = get_decoders_providing(inputs.at(0));
299
300 if (prov_decoders.size() == 0) {
301 // Emit warning and add the stack that we could gather so far
302 qWarning() << "Protocol decoder" << QString::fromUtf8(decoders.back()->id) \
303 << "has input that no other decoder provides:" << QString::fromUtf8(inputs.at(0));
304 break;
305 }
306
307 if (prov_decoders.size() == 1) {
308 decoders.push_back(prov_decoders.front());
309 } else {
310 // Let user decide which one to use
311 QString caption = QString(tr("Protocol decoder <b>%1</b> requires input type <b>%2</b> " \
312 "which several decoders provide.<br>Choose which one to use:<br>"))
313 .arg(QString::fromUtf8(decoders.back()->id), QString::fromUtf8(inputs.at(0)));
314
315 QStringList items;
316 for (const srd_decoder* d : prov_decoders)
317 items << QString::fromUtf8(d->id) + " (" + QString::fromUtf8(d->longname) + ")";
318 bool ok_clicked;
319 QString item = QInputDialog::getItem(this, tr("Choose Decoder"),
320 tr(caption.toUtf8()), items, 0, false, &ok_clicked);
321
322 if ((!ok_clicked) || (item.isEmpty()))
323 return;
324
325 QString d = item.section(' ', 0, 0);
326 decoders.push_back(srd_decoder_get_by_id(d.toUtf8()));
327 }
328
329 inputs = get_decoder_inputs(decoders.back());
330 }
331
332 // Reverse decoder list and add the stack
333 reverse(decoders.begin(), decoders.end());
334 new_decoders_selected(decoders);
335}
336
337void SubWindow::on_filter_changed(const QString& text)
338{
339 sort_filter_model_->setFilterFixedString(text);
340
341 // Expand the "All Decoders" category/tag if the user filtered
342 tree_view_->setExpanded(tree_view_->model()->index(0, 0), !text.isEmpty());
343}
344
345void SubWindow::on_filter_return_pressed()
346{
347 int num_visible_decoders = 0;
348 QModelIndex last_valid_index;
349
350 QModelIndex index = tree_view_->model()->index(0, 0);
351
352 while (index.isValid()) {
353 QModelIndex id_index = index.model()->index(index.row(), 2, index.parent());
354 QString decoder_name = index.model()->data(id_index, Qt::DisplayRole).toString();
355 if (!decoder_name.isEmpty()) {
356 last_valid_index = index;
357 num_visible_decoders++;
358 }
359 index = tree_view_->indexBelow(index);
360 }
361
362 // If only one decoder matches the filter, apply it when the user presses enter
363 if (num_visible_decoders == 1)
364 tree_view_->activated(last_valid_index);
365}
366
367} // namespace decoder_selector
368} // namespace subwindows
369} // namespace pv