]> sigrok.org Git - pulseview.git/blame - pv/subwindows/decoder_selector/subwindow.cpp
Fix #1375 by searching across all model columns
[pulseview.git] / pv / subwindows / decoder_selector / subwindow.cpp
CommitLineData
97378470
SA
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
c52493c9 22#include <QApplication>
97378470 23#include <QDebug>
c52493c9 24#include <QFontMetrics>
97378470
SA
25#include <QInputDialog>
26#include <QLabel>
b229a1b7 27#include <QLineEdit>
97378470 28#include <QPushButton>
9c4ba1c1 29#include <QScrollArea>
97378470
SA
30#include <QVBoxLayout>
31
32#include "pv/session.hpp"
33#include "pv/subwindows/decoder_selector/subwindow.hpp"
34
35using std::reverse;
36using std::shared_ptr;
37
38namespace pv {
39namespace subwindows {
40namespace decoder_selector {
41
c52493c9
SA
42const QString initial_notice = QApplication::tr("Select a decoder to see its description here.");
43const int min_width_margin = 75;
44
97378470 45
b229a1b7
SA
46bool QCustomSortFilterProxyModel::filterAcceptsRow(int source_row,
47 const QModelIndex& source_parent) const
48{
49 // Search model recursively
50
51 if (QSortFilterProxyModel::filterAcceptsRow(source_row, source_parent))
52 return true;
53
54 const QModelIndex index = sourceModel()->index(source_row, 0, source_parent);
55
56 for (int i = 0; i < sourceModel()->rowCount(index); i++)
57 if (filterAcceptsRow(i, index))
58 return true;
59
60 return false;
61}
62
63
e10848e8
SA
64void QCustomTreeView::currentChanged(const QModelIndex& current,
65 const QModelIndex& previous)
66{
67 QTreeView::currentChanged(current, previous);
68 currentChanged(current);
69}
70
71
97378470
SA
72SubWindow::SubWindow(Session& session, QWidget* parent) :
73 SubWindowBase(session, parent),
74 splitter_(new QSplitter()),
e10848e8 75 tree_view_(new QCustomTreeView()),
c2b80ad9
SA
76 info_box_(new QWidget()),
77 info_label_header_(new QLabel()),
78 info_label_body_(new QLabel()),
79 info_label_footer_(new QLabel()),
a6fab024 80 model_(new DecoderCollectionModel()),
b229a1b7 81 sort_filter_model_(new QCustomSortFilterProxyModel())
97378470
SA
82{
83 QVBoxLayout* root_layout = new QVBoxLayout(this);
84 root_layout->setContentsMargins(0, 0, 0, 0);
85 root_layout->addWidget(splitter_);
86
b229a1b7
SA
87 QWidget* upper_container = new QWidget();
88 QVBoxLayout* upper_layout = new QVBoxLayout(upper_container);
89 upper_layout->setContentsMargins(0, 5, 0, 0);
90 QLineEdit* filter = new QLineEdit();
91 upper_layout->addWidget(filter);
92 upper_layout->addWidget(tree_view_);
93
c2b80ad9 94 splitter_->setOrientation(Qt::Vertical);
b229a1b7 95 splitter_->addWidget(upper_container);
c2b80ad9 96 splitter_->addWidget(info_box_);
97378470 97
b229a1b7
SA
98 const QIcon filter_icon(QIcon::fromTheme("search",
99 QIcon(":/icons/search.svg")));
100 filter->setClearButtonEnabled(true);
101 filter->addAction(filter_icon, QLineEdit::LeadingPosition);
102
a6fab024 103 sort_filter_model_->setSourceModel(model_);
b229a1b7 104 sort_filter_model_->setFilterCaseSensitivity(Qt::CaseInsensitive);
b032da62 105 sort_filter_model_->setFilterKeyColumn(-1);
a6fab024
SA
106
107 tree_view_->setModel(sort_filter_model_);
97378470 108 tree_view_->setRootIsDecorated(true);
a6fab024
SA
109 tree_view_->setSortingEnabled(true);
110 tree_view_->sortByColumn(0, Qt::AscendingOrder);
97378470
SA
111
112 // Hide the columns that hold the detailed item information
113 tree_view_->hideColumn(2); // ID
114
b54aeaea
UH
115 // Ensure that all decoder tag names are fully visible by default
116 tree_view_->resizeColumnToContents(0);
117
5da32ad6
UH
118 tree_view_->setIndentation(10);
119
9c4ba1c1
SA
120 QScrollArea* info_label_body_container = new QScrollArea();
121 info_label_body_container->setWidget(info_label_body_);
122 info_label_body_container->setWidgetResizable(true);
123
c2b80ad9
SA
124 info_box_->setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Expanding);
125 QVBoxLayout* info_box_layout = new QVBoxLayout(info_box_);
126 info_box_layout->addWidget(info_label_header_);
9c4ba1c1 127 info_box_layout->addWidget(info_label_body_container);
c2b80ad9 128 info_box_layout->addWidget(info_label_footer_);
a7961b36 129 info_box_layout->setAlignment(Qt::AlignTop);
0a7cdbf7 130 Qt::TextInteractionFlags flags = Qt::TextSelectableByMouse | Qt::TextSelectableByKeyboard;
a8ea0620 131 info_label_header_->setWordWrap(true);
0a7cdbf7 132 info_label_header_->setTextInteractionFlags(flags);
c2b80ad9 133 info_label_body_->setWordWrap(true);
0a7cdbf7 134 info_label_body_->setTextInteractionFlags(flags);
c52493c9 135 info_label_body_->setText(initial_notice);
e0cfcb0e 136 info_label_body_->setAlignment(Qt::AlignTop);
a8ea0620 137 info_label_footer_->setWordWrap(true);
0a7cdbf7 138 info_label_footer_->setTextInteractionFlags(flags);
c2b80ad9 139
b229a1b7
SA
140 connect(filter, SIGNAL(textChanged(const QString&)),
141 this, SLOT(on_filter_changed(const QString&)));
142
e10848e8
SA
143 connect(tree_view_, SIGNAL(currentChanged(const QModelIndex&)),
144 this, SLOT(on_item_changed(const QModelIndex&)));
145 connect(tree_view_, SIGNAL(activated(const QModelIndex&)),
146 this, SLOT(on_item_activated(const QModelIndex&)));
97378470
SA
147
148 connect(this, SIGNAL(new_decoders_selected(vector<const srd_decoder*>)),
149 &session, SLOT(on_new_decoders_selected(vector<const srd_decoder*>)));
150}
151
152bool SubWindow::has_toolbar() const
153{
154 return true;
155}
156
157QToolBar* SubWindow::create_toolbar(QWidget *parent) const
158{
159 QToolBar* toolbar = new QToolBar(parent);
160
161 return toolbar;
162}
163
c52493c9
SA
164int SubWindow::minimum_width() const
165{
166 QFontMetrics m(info_label_body_->font());
167 const int label_width = m.width(initial_notice);
168
169 return label_width + min_width_margin;
170}
171
8f7c2dce 172vector<const char*> SubWindow::get_decoder_inputs(const srd_decoder* d) const
97378470
SA
173{
174 vector<const char*> ret_val;
175
486bcf01
SA
176 for (GSList* li = d->inputs; li; li = li->next)
177 ret_val.push_back((const char*)li->data);
97378470
SA
178
179 return ret_val;
180}
181
8f7c2dce 182vector<const srd_decoder*> SubWindow::get_decoders_providing(const char* output) const
97378470
SA
183{
184 vector<const srd_decoder*> ret_val;
185
486bcf01 186 for (GSList* li = (GSList*)srd_decoder_list(); li; li = li->next) {
97378470
SA
187 const srd_decoder* d = (srd_decoder*)li->data;
188 assert(d);
189
190 if (!d->outputs)
191 continue;
192
193 // TODO For now we ignore that d->outputs is actually a list
194 if (strncmp((char*)(d->outputs->data), output, strlen(output)) == 0)
195 ret_val.push_back(d);
196 }
97378470
SA
197
198 return ret_val;
199}
200
e10848e8 201void SubWindow::on_item_changed(const QModelIndex& index)
c2b80ad9
SA
202{
203 if (!index.isValid())
204 return;
205
206 QModelIndex id_index = index.model()->index(index.row(), 2, index.parent());
207 QString decoder_name = index.model()->data(id_index, Qt::DisplayRole).toString();
208
209 if (decoder_name.isEmpty())
210 return;
211
644462a7 212 const srd_decoder* d = srd_decoder_get_by_id(decoder_name.toUtf8());
c2b80ad9
SA
213
214 const QString id = QString::fromUtf8(d->id);
215 const QString longname = QString::fromUtf8(d->longname);
216 const QString desc = QString::fromUtf8(d->desc);
9c4ba1c1 217 const QString doc = QString::fromUtf8(srd_decoder_doc_get(d)).trimmed();
c2b80ad9
SA
218
219 QString tags;
486bcf01
SA
220 for (GSList* li = (GSList*)d->tags; li; li = li->next) {
221 QString s = (li == (GSList*)d->tags) ?
c2b80ad9
SA
222 tr((char*)li->data) :
223 QString(tr(", %1")).arg(tr((char*)li->data));
224 tags.append(s);
225 }
c2b80ad9 226
a51c7f7d 227 info_label_header_->setText(QString("<span style='font-size:large'><b>%1 (%2)</b></span><br><i>%3</i>")
c2b80ad9
SA
228 .arg(longname, id, desc));
229 info_label_body_->setText(doc);
230 info_label_footer_->setText(tr("<p align='right'>Tags: %1</p>").arg(tags));
231}
232
e10848e8 233void SubWindow::on_item_activated(const QModelIndex& index)
97378470
SA
234{
235 if (!index.isValid())
236 return;
237
238 QModelIndex id_index = index.model()->index(index.row(), 2, index.parent());
239 QString decoder_name = index.model()->data(id_index, Qt::DisplayRole).toString();
240
644462a7 241 const srd_decoder* chosen_decoder = srd_decoder_get_by_id(decoder_name.toUtf8());
97378470
SA
242 if (chosen_decoder == nullptr)
243 return;
244
245 vector<const srd_decoder*> decoders;
246 decoders.push_back(chosen_decoder);
247
248 // If the decoder only depends on logic inputs, we add it and are done
8f7c2dce 249 vector<const char*> inputs = get_decoder_inputs(decoders.front());
97378470
SA
250 if (inputs.size() == 0) {
251 qWarning() << "Protocol decoder" << decoder_name << "cannot have 0 inputs!";
252 return;
253 }
254
255 if (strncmp(inputs.at(0), "logic", 5) == 0) {
256 new_decoders_selected(decoders);
257 return;
258 }
259
260 // Check if we can automatically fulfill the stacking requirements
261 while (strncmp(inputs.at(0), "logic", 5) != 0) {
8f7c2dce 262 vector<const srd_decoder*> prov_decoders = get_decoders_providing(inputs.at(0));
97378470
SA
263
264 if (prov_decoders.size() == 0) {
265 // Emit warning and add the stack that we could gather so far
266 qWarning() << "Protocol decoder" << QString::fromUtf8(decoders.back()->id) \
267 << "has input that no other decoder provides:" << QString::fromUtf8(inputs.at(0));
268 break;
269 }
270
271 if (prov_decoders.size() == 1) {
272 decoders.push_back(prov_decoders.front());
273 } else {
274 // Let user decide which one to use
275 QString caption = QString(tr("Protocol decoder <b>%1</b> requires input type <b>%2</b> " \
276 "which several decoders provide.<br>Choose which one to use:<br>"))
277 .arg(QString::fromUtf8(decoders.back()->id), QString::fromUtf8(inputs.at(0)));
278
279 QStringList items;
280 for (const srd_decoder* d : prov_decoders)
281 items << QString::fromUtf8(d->id) + " (" + QString::fromUtf8(d->longname) + ")";
282 bool ok_clicked;
283 QString item = QInputDialog::getItem(this, tr("Choose Decoder"),
284 tr(caption.toUtf8()), items, 0, false, &ok_clicked);
285
286 if ((!ok_clicked) || (item.isEmpty()))
287 return;
288
289 QString d = item.section(' ', 0, 0);
644462a7 290 decoders.push_back(srd_decoder_get_by_id(d.toUtf8()));
97378470
SA
291 }
292
8f7c2dce 293 inputs = get_decoder_inputs(decoders.back());
97378470
SA
294 }
295
296 // Reverse decoder list and add the stack
297 reverse(decoders.begin(), decoders.end());
298 new_decoders_selected(decoders);
299}
300
b229a1b7
SA
301void SubWindow::on_filter_changed(const QString& text)
302{
303 sort_filter_model_->setFilterFixedString(text);
304}
305
97378470
SA
306} // namespace decoder_selector
307} // namespace subwindows
308} // namespace pv