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 model_(new DecoderCollectionModel())
45 QVBoxLayout* root_layout = new QVBoxLayout(this);
46 root_layout->setContentsMargins(0, 0, 0, 0);
47 root_layout->addWidget(splitter_);
49 splitter_->addWidget(tree_view_);
51 tree_view_->setModel(model_);
52 tree_view_->setRootIsDecorated(true);
54 // Hide the columns that hold the detailed item information
55 tree_view_->hideColumn(2); // ID
57 connect(tree_view_, SIGNAL(doubleClicked(const QModelIndex&)),
58 this, SLOT(on_item_double_clicked(const QModelIndex&)));
60 connect(this, SIGNAL(new_decoders_selected(vector<const srd_decoder*>)),
61 &session, SLOT(on_new_decoders_selected(vector<const srd_decoder*>)));
64 bool SubWindow::has_toolbar() const
69 QToolBar* SubWindow::create_toolbar(QWidget *parent) const
71 QToolBar* toolbar = new QToolBar(parent);
76 const srd_decoder* SubWindow::get_srd_decoder_from_id(QString id) const
78 const srd_decoder* ret_val = nullptr;
80 GSList* l = g_slist_copy((GSList*)srd_decoder_list());
81 for (GSList* li = l; li; li = li->next) {
82 const srd_decoder* d = (srd_decoder*)li->data;
85 if (QString::fromUtf8(d->id) == id)
93 vector<const char*> SubWindow::decoder_inputs(const srd_decoder* d) const
95 vector<const char*> ret_val;
97 GSList* l = g_slist_copy(d->inputs);
98 for (GSList* li = l; li; li = li->next) {
99 const char* input = (const char*)li->data;
100 ret_val.push_back(input);
107 vector<const srd_decoder*> SubWindow::decoders_providing(const char* output) const
109 vector<const srd_decoder*> ret_val;
111 GSList* l = g_slist_copy((GSList*)srd_decoder_list());
112 for (GSList* li = l; li; li = li->next) {
113 const srd_decoder* d = (srd_decoder*)li->data;
119 // TODO For now we ignore that d->outputs is actually a list
120 if (strncmp((char*)(d->outputs->data), output, strlen(output)) == 0)
121 ret_val.push_back(d);
128 void SubWindow::on_item_double_clicked(const QModelIndex& index)
130 if (!index.isValid())
133 QModelIndex id_index = index.model()->index(index.row(), 2, index.parent());
134 QString decoder_name = index.model()->data(id_index, Qt::DisplayRole).toString();
136 const srd_decoder* chosen_decoder = get_srd_decoder_from_id(decoder_name);
137 if (chosen_decoder == nullptr)
140 vector<const srd_decoder*> decoders;
141 decoders.push_back(chosen_decoder);
143 // If the decoder only depends on logic inputs, we add it and are done
144 vector<const char*> inputs = decoder_inputs(decoders.front());
145 if (inputs.size() == 0) {
146 qWarning() << "Protocol decoder" << decoder_name << "cannot have 0 inputs!";
150 if (strncmp(inputs.at(0), "logic", 5) == 0) {
151 new_decoders_selected(decoders);
155 // Check if we can automatically fulfill the stacking requirements
156 while (strncmp(inputs.at(0), "logic", 5) != 0) {
157 vector<const srd_decoder*> prov_decoders = decoders_providing(inputs.at(0));
159 if (prov_decoders.size() == 0) {
160 // Emit warning and add the stack that we could gather so far
161 qWarning() << "Protocol decoder" << QString::fromUtf8(decoders.back()->id) \
162 << "has input that no other decoder provides:" << QString::fromUtf8(inputs.at(0));
166 if (prov_decoders.size() == 1) {
167 decoders.push_back(prov_decoders.front());
169 // Let user decide which one to use
170 QString caption = QString(tr("Protocol decoder <b>%1</b> requires input type <b>%2</b> " \
171 "which several decoders provide.<br>Choose which one to use:<br>"))
172 .arg(QString::fromUtf8(decoders.back()->id), QString::fromUtf8(inputs.at(0)));
175 for (const srd_decoder* d : prov_decoders)
176 items << QString::fromUtf8(d->id) + " (" + QString::fromUtf8(d->longname) + ")";
178 QString item = QInputDialog::getItem(this, tr("Choose Decoder"),
179 tr(caption.toUtf8()), items, 0, false, &ok_clicked);
181 if ((!ok_clicked) || (item.isEmpty()))
184 QString d = item.section(' ', 0, 0);
185 decoders.push_back(get_srd_decoder_from_id(d));
188 inputs = decoder_inputs(decoders.back());
191 // Reverse decoder list and add the stack
192 reverse(decoders.begin(), decoders.end());
193 new_decoders_selected(decoders);
196 } // namespace decoder_selector
197 } // namespace subwindows