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