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