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