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