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