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