]> sigrok.org Git - pulseview.git/blame - pv/subwindows/decoder_selector/subwindow.cpp
Fix #1147 by implementing decoder selector subwindow
[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>
25#include <QPushButton>
26#include <QVBoxLayout>
27
28#include "pv/session.hpp"
29#include "pv/subwindows/decoder_selector/subwindow.hpp"
30
31using std::reverse;
32using std::shared_ptr;
33
34namespace pv {
35namespace subwindows {
36namespace decoder_selector {
37
38
39SubWindow::SubWindow(Session& session, QWidget* parent) :
40 SubWindowBase(session, parent),
41 splitter_(new QSplitter()),
42 tree_view_(new QTreeView()),
43 model_(new DecoderCollectionModel())
44{
45 QVBoxLayout* root_layout = new QVBoxLayout(this);
46 root_layout->setContentsMargins(0, 0, 0, 0);
47 root_layout->addWidget(splitter_);
48
49 splitter_->addWidget(tree_view_);
50
51 tree_view_->setModel(model_);
52 tree_view_->setRootIsDecorated(true);
53
54 // Hide the columns that hold the detailed item information
55 tree_view_->hideColumn(2); // ID
56
57 connect(tree_view_, SIGNAL(doubleClicked(const QModelIndex&)),
58 this, SLOT(on_item_double_clicked(const QModelIndex&)));
59
60 connect(this, SIGNAL(new_decoders_selected(vector<const srd_decoder*>)),
61 &session, SLOT(on_new_decoders_selected(vector<const srd_decoder*>)));
62}
63
64bool SubWindow::has_toolbar() const
65{
66 return true;
67}
68
69QToolBar* SubWindow::create_toolbar(QWidget *parent) const
70{
71 QToolBar* toolbar = new QToolBar(parent);
72
73 return toolbar;
74}
75
76const srd_decoder* SubWindow::get_srd_decoder_from_id(QString id) const
77{
78 const srd_decoder* ret_val = nullptr;
79
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;
83 assert(d);
84
85 if (QString::fromUtf8(d->id) == id)
86 ret_val = d;
87 }
88 g_slist_free(l);
89
90 return ret_val;
91}
92
93vector<const char*> SubWindow::decoder_inputs(const srd_decoder* d) const
94{
95 vector<const char*> ret_val;
96
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);
101 }
102 g_slist_free(l);
103
104 return ret_val;
105}
106
107vector<const srd_decoder*> SubWindow::decoders_providing(const char* output) const
108{
109 vector<const srd_decoder*> ret_val;
110
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;
114 assert(d);
115
116 if (!d->outputs)
117 continue;
118
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);
122 }
123 g_slist_free(l);
124
125 return ret_val;
126}
127
128void SubWindow::on_item_double_clicked(const QModelIndex& index)
129{
130 if (!index.isValid())
131 return;
132
133 QModelIndex id_index = index.model()->index(index.row(), 2, index.parent());
134 QString decoder_name = index.model()->data(id_index, Qt::DisplayRole).toString();
135
136 const srd_decoder* chosen_decoder = get_srd_decoder_from_id(decoder_name);
137 if (chosen_decoder == nullptr)
138 return;
139
140 vector<const srd_decoder*> decoders;
141 decoders.push_back(chosen_decoder);
142
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!";
147 return;
148 }
149
150 if (strncmp(inputs.at(0), "logic", 5) == 0) {
151 new_decoders_selected(decoders);
152 return;
153 }
154
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));
158
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));
163 break;
164 }
165
166 if (prov_decoders.size() == 1) {
167 decoders.push_back(prov_decoders.front());
168 } else {
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)));
173
174 QStringList items;
175 for (const srd_decoder* d : prov_decoders)
176 items << QString::fromUtf8(d->id) + " (" + QString::fromUtf8(d->longname) + ")";
177 bool ok_clicked;
178 QString item = QInputDialog::getItem(this, tr("Choose Decoder"),
179 tr(caption.toUtf8()), items, 0, false, &ok_clicked);
180
181 if ((!ok_clicked) || (item.isEmpty()))
182 return;
183
184 QString d = item.section(' ', 0, 0);
185 decoders.push_back(get_srd_decoder_from_id(d));
186 }
187
188 inputs = decoder_inputs(decoders.back());
189 }
190
191 // Reverse decoder list and add the stack
192 reverse(decoders.begin(), decoders.end());
193 new_decoders_selected(decoders);
194}
195
196} // namespace decoder_selector
197} // namespace subwindows
198} // namespace pv