]> sigrok.org Git - pulseview.git/blame - pv/subwindows/decoder_selector/subwindow.cpp
DecoderSelector: Use proxy model to permit sorting
[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
e10848e8
SA
39void QCustomTreeView::currentChanged(const QModelIndex& current,
40 const QModelIndex& previous)
41{
42 QTreeView::currentChanged(current, previous);
43 currentChanged(current);
44}
45
46
97378470
SA
47SubWindow::SubWindow(Session& session, QWidget* parent) :
48 SubWindowBase(session, parent),
49 splitter_(new QSplitter()),
e10848e8 50 tree_view_(new QCustomTreeView()),
c2b80ad9
SA
51 info_box_(new QWidget()),
52 info_label_header_(new QLabel()),
53 info_label_body_(new QLabel()),
54 info_label_footer_(new QLabel()),
a6fab024
SA
55 model_(new DecoderCollectionModel()),
56 sort_filter_model_(new QSortFilterProxyModel())
97378470
SA
57{
58 QVBoxLayout* root_layout = new QVBoxLayout(this);
59 root_layout->setContentsMargins(0, 0, 0, 0);
60 root_layout->addWidget(splitter_);
61
c2b80ad9 62 splitter_->setOrientation(Qt::Vertical);
97378470 63 splitter_->addWidget(tree_view_);
c2b80ad9 64 splitter_->addWidget(info_box_);
97378470 65
a6fab024
SA
66 sort_filter_model_->setSourceModel(model_);
67
68 tree_view_->setModel(sort_filter_model_);
97378470 69 tree_view_->setRootIsDecorated(true);
a6fab024
SA
70 tree_view_->setSortingEnabled(true);
71 tree_view_->sortByColumn(0, Qt::AscendingOrder);
97378470
SA
72
73 // Hide the columns that hold the detailed item information
74 tree_view_->hideColumn(2); // ID
75
c2b80ad9
SA
76 info_box_->setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Expanding);
77 QVBoxLayout* info_box_layout = new QVBoxLayout(info_box_);
78 info_box_layout->addWidget(info_label_header_);
79 info_box_layout->addWidget(info_label_body_);
80 info_box_layout->addWidget(info_label_footer_);
81 info_label_body_->setWordWrap(true);
82 info_label_body_->setText(tr("Select a decoder to see its description here."));
83
e10848e8
SA
84 connect(tree_view_, SIGNAL(currentChanged(const QModelIndex&)),
85 this, SLOT(on_item_changed(const QModelIndex&)));
86 connect(tree_view_, SIGNAL(activated(const QModelIndex&)),
87 this, SLOT(on_item_activated(const QModelIndex&)));
97378470
SA
88
89 connect(this, SIGNAL(new_decoders_selected(vector<const srd_decoder*>)),
90 &session, SLOT(on_new_decoders_selected(vector<const srd_decoder*>)));
91}
92
93bool SubWindow::has_toolbar() const
94{
95 return true;
96}
97
98QToolBar* SubWindow::create_toolbar(QWidget *parent) const
99{
100 QToolBar* toolbar = new QToolBar(parent);
101
102 return toolbar;
103}
104
105const srd_decoder* SubWindow::get_srd_decoder_from_id(QString id) const
106{
107 const srd_decoder* ret_val = nullptr;
108
109 GSList* l = g_slist_copy((GSList*)srd_decoder_list());
110 for (GSList* li = l; li; li = li->next) {
111 const srd_decoder* d = (srd_decoder*)li->data;
112 assert(d);
113
114 if (QString::fromUtf8(d->id) == id)
115 ret_val = d;
116 }
117 g_slist_free(l);
118
119 return ret_val;
120}
121
122vector<const char*> SubWindow::decoder_inputs(const srd_decoder* d) const
123{
124 vector<const char*> ret_val;
125
126 GSList* l = g_slist_copy(d->inputs);
127 for (GSList* li = l; li; li = li->next) {
128 const char* input = (const char*)li->data;
129 ret_val.push_back(input);
130 }
131 g_slist_free(l);
132
133 return ret_val;
134}
135
136vector<const srd_decoder*> SubWindow::decoders_providing(const char* output) const
137{
138 vector<const srd_decoder*> ret_val;
139
140 GSList* l = g_slist_copy((GSList*)srd_decoder_list());
141 for (GSList* li = l; li; li = li->next) {
142 const srd_decoder* d = (srd_decoder*)li->data;
143 assert(d);
144
145 if (!d->outputs)
146 continue;
147
148 // TODO For now we ignore that d->outputs is actually a list
149 if (strncmp((char*)(d->outputs->data), output, strlen(output)) == 0)
150 ret_val.push_back(d);
151 }
152 g_slist_free(l);
153
154 return ret_val;
155}
156
e10848e8 157void SubWindow::on_item_changed(const QModelIndex& index)
c2b80ad9
SA
158{
159 if (!index.isValid())
160 return;
161
162 QModelIndex id_index = index.model()->index(index.row(), 2, index.parent());
163 QString decoder_name = index.model()->data(id_index, Qt::DisplayRole).toString();
164
165 if (decoder_name.isEmpty())
166 return;
167
168 const srd_decoder* d = get_srd_decoder_from_id(decoder_name);
169
170 const QString id = QString::fromUtf8(d->id);
171 const QString longname = QString::fromUtf8(d->longname);
172 const QString desc = QString::fromUtf8(d->desc);
173 const QString doc = QString::fromUtf8(srd_decoder_doc_get(d));
174
175 QString tags;
176 GSList* l = g_slist_copy((GSList*)d->tags);
177 for (GSList* li = l; li; li = li->next) {
178 QString s = (li == l) ?
179 tr((char*)li->data) :
180 QString(tr(", %1")).arg(tr((char*)li->data));
181 tags.append(s);
182 }
183 g_slist_free(l);
184
185 info_label_header_->setText(QString("<span style='font-size:large;font-weight:bold'>%1 (%2)</span><br>%3")
186 .arg(longname, id, desc));
187 info_label_body_->setText(doc);
188 info_label_footer_->setText(tr("<p align='right'>Tags: %1</p>").arg(tags));
189}
190
e10848e8 191void SubWindow::on_item_activated(const QModelIndex& index)
97378470
SA
192{
193 if (!index.isValid())
194 return;
195
196 QModelIndex id_index = index.model()->index(index.row(), 2, index.parent());
197 QString decoder_name = index.model()->data(id_index, Qt::DisplayRole).toString();
198
199 const srd_decoder* chosen_decoder = get_srd_decoder_from_id(decoder_name);
200 if (chosen_decoder == nullptr)
201 return;
202
203 vector<const srd_decoder*> decoders;
204 decoders.push_back(chosen_decoder);
205
206 // If the decoder only depends on logic inputs, we add it and are done
207 vector<const char*> inputs = decoder_inputs(decoders.front());
208 if (inputs.size() == 0) {
209 qWarning() << "Protocol decoder" << decoder_name << "cannot have 0 inputs!";
210 return;
211 }
212
213 if (strncmp(inputs.at(0), "logic", 5) == 0) {
214 new_decoders_selected(decoders);
215 return;
216 }
217
218 // Check if we can automatically fulfill the stacking requirements
219 while (strncmp(inputs.at(0), "logic", 5) != 0) {
220 vector<const srd_decoder*> prov_decoders = decoders_providing(inputs.at(0));
221
222 if (prov_decoders.size() == 0) {
223 // Emit warning and add the stack that we could gather so far
224 qWarning() << "Protocol decoder" << QString::fromUtf8(decoders.back()->id) \
225 << "has input that no other decoder provides:" << QString::fromUtf8(inputs.at(0));
226 break;
227 }
228
229 if (prov_decoders.size() == 1) {
230 decoders.push_back(prov_decoders.front());
231 } else {
232 // Let user decide which one to use
233 QString caption = QString(tr("Protocol decoder <b>%1</b> requires input type <b>%2</b> " \
234 "which several decoders provide.<br>Choose which one to use:<br>"))
235 .arg(QString::fromUtf8(decoders.back()->id), QString::fromUtf8(inputs.at(0)));
236
237 QStringList items;
238 for (const srd_decoder* d : prov_decoders)
239 items << QString::fromUtf8(d->id) + " (" + QString::fromUtf8(d->longname) + ")";
240 bool ok_clicked;
241 QString item = QInputDialog::getItem(this, tr("Choose Decoder"),
242 tr(caption.toUtf8()), items, 0, false, &ok_clicked);
243
244 if ((!ok_clicked) || (item.isEmpty()))
245 return;
246
247 QString d = item.section(' ', 0, 0);
248 decoders.push_back(get_srd_decoder_from_id(d));
249 }
250
251 inputs = decoder_inputs(decoders.back());
252 }
253
254 // Reverse decoder list and add the stack
255 reverse(decoders.begin(), decoders.end());
256 new_decoders_selected(decoders);
257}
258
259} // namespace decoder_selector
260} // namespace subwindows
261} // namespace pv