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())
57 QVBoxLayout* root_layout = new QVBoxLayout(this);
58 root_layout->setContentsMargins(0, 0, 0, 0);
59 root_layout->addWidget(splitter_);
61 splitter_->setOrientation(Qt::Vertical);
62 splitter_->addWidget(tree_view_);
63 splitter_->addWidget(info_box_);
65 tree_view_->setModel(model_);
66 tree_view_->setRootIsDecorated(true);
68 // Hide the columns that hold the detailed item information
69 tree_view_->hideColumn(2); // ID
71 info_box_->setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Expanding);
72 QVBoxLayout* info_box_layout = new QVBoxLayout(info_box_);
73 info_box_layout->addWidget(info_label_header_);
74 info_box_layout->addWidget(info_label_body_);
75 info_box_layout->addWidget(info_label_footer_);
76 info_label_body_->setWordWrap(true);
77 info_label_body_->setText(tr("Select a decoder to see its description here."));
79 connect(tree_view_, SIGNAL(currentChanged(const QModelIndex&)),
80 this, SLOT(on_item_changed(const QModelIndex&)));
81 connect(tree_view_, SIGNAL(activated(const QModelIndex&)),
82 this, SLOT(on_item_activated(const QModelIndex&)));
84 connect(this, SIGNAL(new_decoders_selected(vector<const srd_decoder*>)),
85 &session, SLOT(on_new_decoders_selected(vector<const srd_decoder*>)));
88 bool SubWindow::has_toolbar() const
93 QToolBar* SubWindow::create_toolbar(QWidget *parent) const
95 QToolBar* toolbar = new QToolBar(parent);
100 const srd_decoder* SubWindow::get_srd_decoder_from_id(QString id) const
102 const srd_decoder* ret_val = nullptr;
104 GSList* l = g_slist_copy((GSList*)srd_decoder_list());
105 for (GSList* li = l; li; li = li->next) {
106 const srd_decoder* d = (srd_decoder*)li->data;
109 if (QString::fromUtf8(d->id) == id)
117 vector<const char*> SubWindow::decoder_inputs(const srd_decoder* d) const
119 vector<const char*> ret_val;
121 GSList* l = g_slist_copy(d->inputs);
122 for (GSList* li = l; li; li = li->next) {
123 const char* input = (const char*)li->data;
124 ret_val.push_back(input);
131 vector<const srd_decoder*> SubWindow::decoders_providing(const char* output) const
133 vector<const srd_decoder*> ret_val;
135 GSList* l = g_slist_copy((GSList*)srd_decoder_list());
136 for (GSList* li = l; li; li = li->next) {
137 const srd_decoder* d = (srd_decoder*)li->data;
143 // TODO For now we ignore that d->outputs is actually a list
144 if (strncmp((char*)(d->outputs->data), output, strlen(output)) == 0)
145 ret_val.push_back(d);
152 void SubWindow::on_item_changed(const QModelIndex& index)
154 if (!index.isValid())
157 QModelIndex id_index = index.model()->index(index.row(), 2, index.parent());
158 QString decoder_name = index.model()->data(id_index, Qt::DisplayRole).toString();
160 if (decoder_name.isEmpty())
163 const srd_decoder* d = get_srd_decoder_from_id(decoder_name);
165 const QString id = QString::fromUtf8(d->id);
166 const QString longname = QString::fromUtf8(d->longname);
167 const QString desc = QString::fromUtf8(d->desc);
168 const QString doc = QString::fromUtf8(srd_decoder_doc_get(d));
171 GSList* l = g_slist_copy((GSList*)d->tags);
172 for (GSList* li = l; li; li = li->next) {
173 QString s = (li == l) ?
174 tr((char*)li->data) :
175 QString(tr(", %1")).arg(tr((char*)li->data));
180 info_label_header_->setText(QString("<span style='font-size:large;font-weight:bold'>%1 (%2)</span><br>%3")
181 .arg(longname, id, desc));
182 info_label_body_->setText(doc);
183 info_label_footer_->setText(tr("<p align='right'>Tags: %1</p>").arg(tags));
186 void SubWindow::on_item_activated(const QModelIndex& index)
188 if (!index.isValid())
191 QModelIndex id_index = index.model()->index(index.row(), 2, index.parent());
192 QString decoder_name = index.model()->data(id_index, Qt::DisplayRole).toString();
194 const srd_decoder* chosen_decoder = get_srd_decoder_from_id(decoder_name);
195 if (chosen_decoder == nullptr)
198 vector<const srd_decoder*> decoders;
199 decoders.push_back(chosen_decoder);
201 // If the decoder only depends on logic inputs, we add it and are done
202 vector<const char*> inputs = decoder_inputs(decoders.front());
203 if (inputs.size() == 0) {
204 qWarning() << "Protocol decoder" << decoder_name << "cannot have 0 inputs!";
208 if (strncmp(inputs.at(0), "logic", 5) == 0) {
209 new_decoders_selected(decoders);
213 // Check if we can automatically fulfill the stacking requirements
214 while (strncmp(inputs.at(0), "logic", 5) != 0) {
215 vector<const srd_decoder*> prov_decoders = decoders_providing(inputs.at(0));
217 if (prov_decoders.size() == 0) {
218 // Emit warning and add the stack that we could gather so far
219 qWarning() << "Protocol decoder" << QString::fromUtf8(decoders.back()->id) \
220 << "has input that no other decoder provides:" << QString::fromUtf8(inputs.at(0));
224 if (prov_decoders.size() == 1) {
225 decoders.push_back(prov_decoders.front());
227 // Let user decide which one to use
228 QString caption = QString(tr("Protocol decoder <b>%1</b> requires input type <b>%2</b> " \
229 "which several decoders provide.<br>Choose which one to use:<br>"))
230 .arg(QString::fromUtf8(decoders.back()->id), QString::fromUtf8(inputs.at(0)));
233 for (const srd_decoder* d : prov_decoders)
234 items << QString::fromUtf8(d->id) + " (" + QString::fromUtf8(d->longname) + ")";
236 QString item = QInputDialog::getItem(this, tr("Choose Decoder"),
237 tr(caption.toUtf8()), items, 0, false, &ok_clicked);
239 if ((!ok_clicked) || (item.isEmpty()))
242 QString d = item.section(' ', 0, 0);
243 decoders.push_back(get_srd_decoder_from_id(d));
246 inputs = decoder_inputs(decoders.back());
249 // Reverse decoder list and add the stack
250 reverse(decoders.begin(), decoders.end());
251 new_decoders_selected(decoders);
254 } // namespace decoder_selector
255 } // namespace subwindows