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 SubWindow::SubWindow(Session& session, QWidget* parent) :
40 SubWindowBase(session, parent),
41 splitter_(new QSplitter()),
42 tree_view_(new QTreeView()),
43 info_box_(new QWidget()),
44 info_label_header_(new QLabel()),
45 info_label_body_(new QLabel()),
46 info_label_footer_(new QLabel()),
47 model_(new DecoderCollectionModel())
49 QVBoxLayout* root_layout = new QVBoxLayout(this);
50 root_layout->setContentsMargins(0, 0, 0, 0);
51 root_layout->addWidget(splitter_);
53 splitter_->setOrientation(Qt::Vertical);
54 splitter_->addWidget(tree_view_);
55 splitter_->addWidget(info_box_);
57 tree_view_->setModel(model_);
58 tree_view_->setRootIsDecorated(true);
60 // Hide the columns that hold the detailed item information
61 tree_view_->hideColumn(2); // ID
63 info_box_->setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Expanding);
64 QVBoxLayout* info_box_layout = new QVBoxLayout(info_box_);
65 info_box_layout->addWidget(info_label_header_);
66 info_box_layout->addWidget(info_label_body_);
67 info_box_layout->addWidget(info_label_footer_);
68 info_label_body_->setWordWrap(true);
69 info_label_body_->setText(tr("Select a decoder to see its description here."));
71 connect(tree_view_, SIGNAL(clicked(const QModelIndex&)),
72 this, SLOT(on_item_clicked(const QModelIndex&)));
73 connect(tree_view_, SIGNAL(doubleClicked(const QModelIndex&)),
74 this, SLOT(on_item_double_clicked(const QModelIndex&)));
76 connect(this, SIGNAL(new_decoders_selected(vector<const srd_decoder*>)),
77 &session, SLOT(on_new_decoders_selected(vector<const srd_decoder*>)));
80 bool SubWindow::has_toolbar() const
85 QToolBar* SubWindow::create_toolbar(QWidget *parent) const
87 QToolBar* toolbar = new QToolBar(parent);
92 const srd_decoder* SubWindow::get_srd_decoder_from_id(QString id) const
94 const srd_decoder* ret_val = nullptr;
96 GSList* l = g_slist_copy((GSList*)srd_decoder_list());
97 for (GSList* li = l; li; li = li->next) {
98 const srd_decoder* d = (srd_decoder*)li->data;
101 if (QString::fromUtf8(d->id) == id)
109 vector<const char*> SubWindow::decoder_inputs(const srd_decoder* d) const
111 vector<const char*> ret_val;
113 GSList* l = g_slist_copy(d->inputs);
114 for (GSList* li = l; li; li = li->next) {
115 const char* input = (const char*)li->data;
116 ret_val.push_back(input);
123 vector<const srd_decoder*> SubWindow::decoders_providing(const char* output) const
125 vector<const srd_decoder*> ret_val;
127 GSList* l = g_slist_copy((GSList*)srd_decoder_list());
128 for (GSList* li = l; li; li = li->next) {
129 const srd_decoder* d = (srd_decoder*)li->data;
135 // TODO For now we ignore that d->outputs is actually a list
136 if (strncmp((char*)(d->outputs->data), output, strlen(output)) == 0)
137 ret_val.push_back(d);
144 void SubWindow::on_item_clicked(const QModelIndex& index)
146 if (!index.isValid())
149 QModelIndex id_index = index.model()->index(index.row(), 2, index.parent());
150 QString decoder_name = index.model()->data(id_index, Qt::DisplayRole).toString();
152 if (decoder_name.isEmpty())
155 const srd_decoder* d = get_srd_decoder_from_id(decoder_name);
157 const QString id = QString::fromUtf8(d->id);
158 const QString longname = QString::fromUtf8(d->longname);
159 const QString desc = QString::fromUtf8(d->desc);
160 const QString doc = QString::fromUtf8(srd_decoder_doc_get(d));
163 GSList* l = g_slist_copy((GSList*)d->tags);
164 for (GSList* li = l; li; li = li->next) {
165 QString s = (li == l) ?
166 tr((char*)li->data) :
167 QString(tr(", %1")).arg(tr((char*)li->data));
172 info_label_header_->setText(QString("<span style='font-size:large;font-weight:bold'>%1 (%2)</span><br>%3")
173 .arg(longname, id, desc));
174 info_label_body_->setText(doc);
175 info_label_footer_->setText(tr("<p align='right'>Tags: %1</p>").arg(tags));
178 void SubWindow::on_item_double_clicked(const QModelIndex& index)
180 if (!index.isValid())
183 QModelIndex id_index = index.model()->index(index.row(), 2, index.parent());
184 QString decoder_name = index.model()->data(id_index, Qt::DisplayRole).toString();
186 const srd_decoder* chosen_decoder = get_srd_decoder_from_id(decoder_name);
187 if (chosen_decoder == nullptr)
190 vector<const srd_decoder*> decoders;
191 decoders.push_back(chosen_decoder);
193 // If the decoder only depends on logic inputs, we add it and are done
194 vector<const char*> inputs = decoder_inputs(decoders.front());
195 if (inputs.size() == 0) {
196 qWarning() << "Protocol decoder" << decoder_name << "cannot have 0 inputs!";
200 if (strncmp(inputs.at(0), "logic", 5) == 0) {
201 new_decoders_selected(decoders);
205 // Check if we can automatically fulfill the stacking requirements
206 while (strncmp(inputs.at(0), "logic", 5) != 0) {
207 vector<const srd_decoder*> prov_decoders = decoders_providing(inputs.at(0));
209 if (prov_decoders.size() == 0) {
210 // Emit warning and add the stack that we could gather so far
211 qWarning() << "Protocol decoder" << QString::fromUtf8(decoders.back()->id) \
212 << "has input that no other decoder provides:" << QString::fromUtf8(inputs.at(0));
216 if (prov_decoders.size() == 1) {
217 decoders.push_back(prov_decoders.front());
219 // Let user decide which one to use
220 QString caption = QString(tr("Protocol decoder <b>%1</b> requires input type <b>%2</b> " \
221 "which several decoders provide.<br>Choose which one to use:<br>"))
222 .arg(QString::fromUtf8(decoders.back()->id), QString::fromUtf8(inputs.at(0)));
225 for (const srd_decoder* d : prov_decoders)
226 items << QString::fromUtf8(d->id) + " (" + QString::fromUtf8(d->longname) + ")";
228 QString item = QInputDialog::getItem(this, tr("Choose Decoder"),
229 tr(caption.toUtf8()), items, 0, false, &ok_clicked);
231 if ((!ok_clicked) || (item.isEmpty()))
234 QString d = item.section(' ', 0, 0);
235 decoders.push_back(get_srd_decoder_from_id(d));
238 inputs = decoder_inputs(decoders.back());
241 // Reverse decoder list and add the stack
242 reverse(decoders.begin(), decoders.end());
243 new_decoders_selected(decoders);
246 } // namespace decoder_selector
247 } // namespace subwindows