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