]> sigrok.org Git - pulseview.git/blob - pv/subwindows/decoder_selector/subwindow.cpp
781e494fbc85d5196bcb823cd63098db6d123df9
[pulseview.git] / pv / subwindows / decoder_selector / subwindow.cpp
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
31 using std::reverse;
32 using std::shared_ptr;
33
34 namespace pv {
35 namespace subwindows {
36 namespace decoder_selector {
37
38
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())
48 {
49         QVBoxLayout* root_layout = new QVBoxLayout(this);
50         root_layout->setContentsMargins(0, 0, 0, 0);
51         root_layout->addWidget(splitter_);
52
53         splitter_->setOrientation(Qt::Vertical);
54         splitter_->addWidget(tree_view_);
55         splitter_->addWidget(info_box_);
56
57         tree_view_->setModel(model_);
58         tree_view_->setRootIsDecorated(true);
59
60         // Hide the columns that hold the detailed item information
61         tree_view_->hideColumn(2);  // ID
62
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."));
70
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&)));
75
76         connect(this, SIGNAL(new_decoders_selected(vector<const srd_decoder*>)),
77                 &session, SLOT(on_new_decoders_selected(vector<const srd_decoder*>)));
78 }
79
80 bool SubWindow::has_toolbar() const
81 {
82         return true;
83 }
84
85 QToolBar* SubWindow::create_toolbar(QWidget *parent) const
86 {
87         QToolBar* toolbar = new QToolBar(parent);
88
89         return toolbar;
90 }
91
92 const srd_decoder* SubWindow::get_srd_decoder_from_id(QString id) const
93 {
94         const srd_decoder* ret_val = nullptr;
95
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;
99                 assert(d);
100
101                 if (QString::fromUtf8(d->id) == id)
102                         ret_val = d;
103         }
104         g_slist_free(l);
105
106         return ret_val;
107 }
108
109 vector<const char*> SubWindow::decoder_inputs(const srd_decoder* d) const
110 {
111         vector<const char*> ret_val;
112
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);
117         }
118         g_slist_free(l);
119
120         return ret_val;
121 }
122
123 vector<const srd_decoder*> SubWindow::decoders_providing(const char* output) const
124 {
125         vector<const srd_decoder*> ret_val;
126
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;
130                 assert(d);
131
132                 if (!d->outputs)
133                         continue;
134
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);
138         }
139         g_slist_free(l);
140
141         return ret_val;
142 }
143
144 void SubWindow::on_item_clicked(const QModelIndex& index)
145 {
146         if (!index.isValid())
147                 return;
148
149         QModelIndex id_index = index.model()->index(index.row(), 2, index.parent());
150         QString decoder_name = index.model()->data(id_index, Qt::DisplayRole).toString();
151
152         if (decoder_name.isEmpty())
153                 return;
154
155         const srd_decoder* d = get_srd_decoder_from_id(decoder_name);
156
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));
161
162         QString tags;
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));
168                 tags.append(s);
169         }
170         g_slist_free(l);
171
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));
176 }
177
178 void SubWindow::on_item_double_clicked(const QModelIndex& index)
179 {
180         if (!index.isValid())
181                 return;
182
183         QModelIndex id_index = index.model()->index(index.row(), 2, index.parent());
184         QString decoder_name = index.model()->data(id_index, Qt::DisplayRole).toString();
185
186         const srd_decoder* chosen_decoder = get_srd_decoder_from_id(decoder_name);
187         if (chosen_decoder == nullptr)
188                 return;
189
190         vector<const srd_decoder*> decoders;
191         decoders.push_back(chosen_decoder);
192
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!";
197                 return;
198         }
199
200         if (strncmp(inputs.at(0), "logic", 5) == 0) {
201                 new_decoders_selected(decoders);
202                 return;
203         }
204
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));
208
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));
213                         break;
214                 }
215
216                 if (prov_decoders.size() == 1) {
217                         decoders.push_back(prov_decoders.front());
218                 } else {
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)));
223
224                         QStringList items;
225                         for (const srd_decoder* d : prov_decoders)
226                                 items << QString::fromUtf8(d->id) + " (" + QString::fromUtf8(d->longname) + ")";
227                         bool ok_clicked;
228                         QString item = QInputDialog::getItem(this, tr("Choose Decoder"),
229                                 tr(caption.toUtf8()), items, 0, false, &ok_clicked);
230
231                         if ((!ok_clicked) || (item.isEmpty()))
232                                 return;
233
234                         QString d = item.section(' ', 0, 0);
235                         decoders.push_back(get_srd_decoder_from_id(d));
236                 }
237
238                 inputs = decoder_inputs(decoders.back());
239         }
240
241         // Reverse decoder list and add the stack
242         reverse(decoders.begin(), decoders.end());
243         new_decoders_selected(decoders);
244 }
245
246 } // namespace decoder_selector
247 } // namespace subwindows
248 } // namespace pv