2 * This file is part of the PulseView project.
4 * Copyright (C) 2018 Soeren Apel <soeren@apelpie.net>
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.
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.
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/>.
23 #include <QInputDialog>
25 #include <QPushButton>
26 #include <QVBoxLayout>
28 #include "pv/session.hpp"
29 #include "pv/subwindows/decoder_selector/subwindow.hpp"
32 using std::shared_ptr;
35 namespace subwindows {
36 namespace decoder_selector {
39 void QCustomTreeView::currentChanged(const QModelIndex& current,
40 const QModelIndex& previous)
42 QTreeView::currentChanged(current, previous);
43 currentChanged(current);
47 SubWindow::SubWindow(Session& session, QWidget* parent) :
48 SubWindowBase(session, parent),
49 splitter_(new QSplitter()),
50 tree_view_(new QCustomTreeView()),
51 info_box_(new QWidget()),
52 info_label_header_(new QLabel()),
53 info_label_body_(new QLabel()),
54 info_label_footer_(new QLabel()),
55 model_(new DecoderCollectionModel()),
56 sort_filter_model_(new QSortFilterProxyModel())
58 QVBoxLayout* root_layout = new QVBoxLayout(this);
59 root_layout->setContentsMargins(0, 0, 0, 0);
60 root_layout->addWidget(splitter_);
62 splitter_->setOrientation(Qt::Vertical);
63 splitter_->addWidget(tree_view_);
64 splitter_->addWidget(info_box_);
66 sort_filter_model_->setSourceModel(model_);
68 tree_view_->setModel(sort_filter_model_);
69 tree_view_->setRootIsDecorated(true);
70 tree_view_->setSortingEnabled(true);
71 tree_view_->sortByColumn(0, Qt::AscendingOrder);
73 // Hide the columns that hold the detailed item information
74 tree_view_->hideColumn(2); // ID
76 info_box_->setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Expanding);
77 QVBoxLayout* info_box_layout = new QVBoxLayout(info_box_);
78 info_box_layout->addWidget(info_label_header_);
79 info_box_layout->addWidget(info_label_body_);
80 info_box_layout->addWidget(info_label_footer_);
81 info_label_body_->setWordWrap(true);
82 info_label_body_->setText(tr("Select a decoder to see its description here."));
84 connect(tree_view_, SIGNAL(currentChanged(const QModelIndex&)),
85 this, SLOT(on_item_changed(const QModelIndex&)));
86 connect(tree_view_, SIGNAL(activated(const QModelIndex&)),
87 this, SLOT(on_item_activated(const QModelIndex&)));
89 connect(this, SIGNAL(new_decoders_selected(vector<const srd_decoder*>)),
90 &session, SLOT(on_new_decoders_selected(vector<const srd_decoder*>)));
93 bool SubWindow::has_toolbar() const
98 QToolBar* SubWindow::create_toolbar(QWidget *parent) const
100 QToolBar* toolbar = new QToolBar(parent);
105 const srd_decoder* SubWindow::get_srd_decoder_from_id(QString id) const
107 const srd_decoder* ret_val = nullptr;
109 GSList* l = g_slist_copy((GSList*)srd_decoder_list());
110 for (GSList* li = l; li; li = li->next) {
111 const srd_decoder* d = (srd_decoder*)li->data;
114 if (QString::fromUtf8(d->id) == id)
122 vector<const char*> SubWindow::decoder_inputs(const srd_decoder* d) const
124 vector<const char*> ret_val;
126 GSList* l = g_slist_copy(d->inputs);
127 for (GSList* li = l; li; li = li->next) {
128 const char* input = (const char*)li->data;
129 ret_val.push_back(input);
136 vector<const srd_decoder*> SubWindow::decoders_providing(const char* output) const
138 vector<const srd_decoder*> ret_val;
140 GSList* l = g_slist_copy((GSList*)srd_decoder_list());
141 for (GSList* li = l; li; li = li->next) {
142 const srd_decoder* d = (srd_decoder*)li->data;
148 // TODO For now we ignore that d->outputs is actually a list
149 if (strncmp((char*)(d->outputs->data), output, strlen(output)) == 0)
150 ret_val.push_back(d);
157 void SubWindow::on_item_changed(const QModelIndex& index)
159 if (!index.isValid())
162 QModelIndex id_index = index.model()->index(index.row(), 2, index.parent());
163 QString decoder_name = index.model()->data(id_index, Qt::DisplayRole).toString();
165 if (decoder_name.isEmpty())
168 const srd_decoder* d = get_srd_decoder_from_id(decoder_name);
170 const QString id = QString::fromUtf8(d->id);
171 const QString longname = QString::fromUtf8(d->longname);
172 const QString desc = QString::fromUtf8(d->desc);
173 const QString doc = QString::fromUtf8(srd_decoder_doc_get(d));
176 GSList* l = g_slist_copy((GSList*)d->tags);
177 for (GSList* li = l; li; li = li->next) {
178 QString s = (li == l) ?
179 tr((char*)li->data) :
180 QString(tr(", %1")).arg(tr((char*)li->data));
185 info_label_header_->setText(QString("<span style='font-size:large;font-weight:bold'>%1 (%2)</span><br>%3")
186 .arg(longname, id, desc));
187 info_label_body_->setText(doc);
188 info_label_footer_->setText(tr("<p align='right'>Tags: %1</p>").arg(tags));
191 void SubWindow::on_item_activated(const QModelIndex& index)
193 if (!index.isValid())
196 QModelIndex id_index = index.model()->index(index.row(), 2, index.parent());
197 QString decoder_name = index.model()->data(id_index, Qt::DisplayRole).toString();
199 const srd_decoder* chosen_decoder = get_srd_decoder_from_id(decoder_name);
200 if (chosen_decoder == nullptr)
203 vector<const srd_decoder*> decoders;
204 decoders.push_back(chosen_decoder);
206 // If the decoder only depends on logic inputs, we add it and are done
207 vector<const char*> inputs = decoder_inputs(decoders.front());
208 if (inputs.size() == 0) {
209 qWarning() << "Protocol decoder" << decoder_name << "cannot have 0 inputs!";
213 if (strncmp(inputs.at(0), "logic", 5) == 0) {
214 new_decoders_selected(decoders);
218 // Check if we can automatically fulfill the stacking requirements
219 while (strncmp(inputs.at(0), "logic", 5) != 0) {
220 vector<const srd_decoder*> prov_decoders = decoders_providing(inputs.at(0));
222 if (prov_decoders.size() == 0) {
223 // Emit warning and add the stack that we could gather so far
224 qWarning() << "Protocol decoder" << QString::fromUtf8(decoders.back()->id) \
225 << "has input that no other decoder provides:" << QString::fromUtf8(inputs.at(0));
229 if (prov_decoders.size() == 1) {
230 decoders.push_back(prov_decoders.front());
232 // Let user decide which one to use
233 QString caption = QString(tr("Protocol decoder <b>%1</b> requires input type <b>%2</b> " \
234 "which several decoders provide.<br>Choose which one to use:<br>"))
235 .arg(QString::fromUtf8(decoders.back()->id), QString::fromUtf8(inputs.at(0)));
238 for (const srd_decoder* d : prov_decoders)
239 items << QString::fromUtf8(d->id) + " (" + QString::fromUtf8(d->longname) + ")";
241 QString item = QInputDialog::getItem(this, tr("Choose Decoder"),
242 tr(caption.toUtf8()), items, 0, false, &ok_clicked);
244 if ((!ok_clicked) || (item.isEmpty()))
247 QString d = item.section(' ', 0, 0);
248 decoders.push_back(get_srd_decoder_from_id(d));
251 inputs = decoder_inputs(decoders.back());
254 // Reverse decoder list and add the stack
255 reverse(decoders.begin(), decoders.end());
256 new_decoders_selected(decoders);
259 } // namespace decoder_selector
260 } // namespace subwindows