]> sigrok.org Git - pulseview.git/blob - pv/subwindows/decoder_selector/subwindow.cpp
DecoderSelector: Put body label into a QScrollArea
[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 <QLineEdit>
26 #include <QPushButton>
27 #include <QScrollArea>
28 #include <QVBoxLayout>
29
30 #include "pv/session.hpp"
31 #include "pv/subwindows/decoder_selector/subwindow.hpp"
32
33 using std::reverse;
34 using std::shared_ptr;
35
36 namespace pv {
37 namespace subwindows {
38 namespace decoder_selector {
39
40
41 bool QCustomSortFilterProxyModel::filterAcceptsRow(int source_row,
42         const QModelIndex& source_parent) const
43 {
44         // Search model recursively
45
46         if (QSortFilterProxyModel::filterAcceptsRow(source_row, source_parent))
47                 return true;
48
49         const QModelIndex index = sourceModel()->index(source_row, 0, source_parent);
50
51         for (int i = 0; i < sourceModel()->rowCount(index); i++)
52                 if (filterAcceptsRow(i, index))
53                         return true;
54
55         return false;
56 }
57
58
59 void QCustomTreeView::currentChanged(const QModelIndex& current,
60         const QModelIndex& previous)
61 {
62         QTreeView::currentChanged(current, previous);
63         currentChanged(current);
64 }
65
66
67 SubWindow::SubWindow(Session& session, QWidget* parent) :
68         SubWindowBase(session, parent),
69         splitter_(new QSplitter()),
70         tree_view_(new QCustomTreeView()),
71         info_box_(new QWidget()),
72         info_label_header_(new QLabel()),
73         info_label_body_(new QLabel()),
74         info_label_footer_(new QLabel()),
75         model_(new DecoderCollectionModel()),
76         sort_filter_model_(new QCustomSortFilterProxyModel())
77 {
78         QVBoxLayout* root_layout = new QVBoxLayout(this);
79         root_layout->setContentsMargins(0, 0, 0, 0);
80         root_layout->addWidget(splitter_);
81
82         QWidget* upper_container = new QWidget();
83         QVBoxLayout* upper_layout = new QVBoxLayout(upper_container);
84         upper_layout->setContentsMargins(0, 5, 0, 0);
85         QLineEdit* filter = new QLineEdit();
86         upper_layout->addWidget(filter);
87         upper_layout->addWidget(tree_view_);
88
89         splitter_->setOrientation(Qt::Vertical);
90         splitter_->addWidget(upper_container);
91         splitter_->addWidget(info_box_);
92
93         const QIcon filter_icon(QIcon::fromTheme("search",
94                 QIcon(":/icons/search.svg")));
95         filter->setClearButtonEnabled(true);
96         filter->addAction(filter_icon, QLineEdit::LeadingPosition);
97
98         sort_filter_model_->setSourceModel(model_);
99         sort_filter_model_->setFilterCaseSensitivity(Qt::CaseInsensitive);
100
101         tree_view_->setModel(sort_filter_model_);
102         tree_view_->setRootIsDecorated(true);
103         tree_view_->setSortingEnabled(true);
104         tree_view_->sortByColumn(0, Qt::AscendingOrder);
105
106         // Hide the columns that hold the detailed item information
107         tree_view_->hideColumn(2);  // ID
108
109         // Ensure that all decoder tag names are fully visible by default
110         tree_view_->resizeColumnToContents(0);
111
112         tree_view_->setIndentation(10);
113
114         QScrollArea* info_label_body_container = new QScrollArea();
115         info_label_body_container->setWidget(info_label_body_);
116         info_label_body_container->setWidgetResizable(true);
117
118         info_box_->setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Expanding);
119         QVBoxLayout* info_box_layout = new QVBoxLayout(info_box_);
120         info_box_layout->addWidget(info_label_header_);
121         info_box_layout->addWidget(info_label_body_container);
122         info_box_layout->addWidget(info_label_footer_);
123         info_box_layout->setAlignment(Qt::AlignTop);
124         Qt::TextInteractionFlags flags = Qt::TextSelectableByMouse | Qt::TextSelectableByKeyboard;
125         info_label_header_->setWordWrap(true);
126         info_label_header_->setTextInteractionFlags(flags);
127         info_label_body_->setWordWrap(true);
128         info_label_body_->setTextInteractionFlags(flags);
129         info_label_body_->setText(tr("Select a decoder to see its description here."));
130         info_label_footer_->setWordWrap(true);
131         info_label_footer_->setTextInteractionFlags(flags);
132
133         connect(filter, SIGNAL(textChanged(const QString&)),
134                 this, SLOT(on_filter_changed(const QString&)));
135
136         connect(tree_view_, SIGNAL(currentChanged(const QModelIndex&)),
137                 this, SLOT(on_item_changed(const QModelIndex&)));
138         connect(tree_view_, SIGNAL(activated(const QModelIndex&)),
139                 this, SLOT(on_item_activated(const QModelIndex&)));
140
141         connect(this, SIGNAL(new_decoders_selected(vector<const srd_decoder*>)),
142                 &session, SLOT(on_new_decoders_selected(vector<const srd_decoder*>)));
143 }
144
145 bool SubWindow::has_toolbar() const
146 {
147         return true;
148 }
149
150 QToolBar* SubWindow::create_toolbar(QWidget *parent) const
151 {
152         QToolBar* toolbar = new QToolBar(parent);
153
154         return toolbar;
155 }
156
157 vector<const char*> SubWindow::get_decoder_inputs(const srd_decoder* d) const
158 {
159         vector<const char*> ret_val;
160
161         for (GSList* li = d->inputs; li; li = li->next)
162                 ret_val.push_back((const char*)li->data);
163
164         return ret_val;
165 }
166
167 vector<const srd_decoder*> SubWindow::get_decoders_providing(const char* output) const
168 {
169         vector<const srd_decoder*> ret_val;
170
171         for (GSList* li = (GSList*)srd_decoder_list(); li; li = li->next) {
172                 const srd_decoder* d = (srd_decoder*)li->data;
173                 assert(d);
174
175                 if (!d->outputs)
176                         continue;
177
178                 // TODO For now we ignore that d->outputs is actually a list
179                 if (strncmp((char*)(d->outputs->data), output, strlen(output)) == 0)
180                         ret_val.push_back(d);
181         }
182
183         return ret_val;
184 }
185
186 void SubWindow::on_item_changed(const QModelIndex& index)
187 {
188         if (!index.isValid())
189                 return;
190
191         QModelIndex id_index = index.model()->index(index.row(), 2, index.parent());
192         QString decoder_name = index.model()->data(id_index, Qt::DisplayRole).toString();
193
194         if (decoder_name.isEmpty())
195                 return;
196
197         const srd_decoder* d = srd_decoder_get_by_id(decoder_name.toUtf8());
198
199         const QString id = QString::fromUtf8(d->id);
200         const QString longname = QString::fromUtf8(d->longname);
201         const QString desc = QString::fromUtf8(d->desc);
202         const QString doc = QString::fromUtf8(srd_decoder_doc_get(d)).trimmed();
203
204         QString tags;
205         for (GSList* li = (GSList*)d->tags; li; li = li->next) {
206                 QString s = (li == (GSList*)d->tags) ?
207                         tr((char*)li->data) :
208                         QString(tr(", %1")).arg(tr((char*)li->data));
209                 tags.append(s);
210         }
211
212         info_label_header_->setText(QString("<span style='font-size:large'><b>%1 (%2)</b></span><br><i>%3</i>")
213                 .arg(longname, id, desc));
214         info_label_body_->setText(doc);
215         info_label_footer_->setText(tr("<p align='right'>Tags: %1</p>").arg(tags));
216 }
217
218 void SubWindow::on_item_activated(const QModelIndex& index)
219 {
220         if (!index.isValid())
221                 return;
222
223         QModelIndex id_index = index.model()->index(index.row(), 2, index.parent());
224         QString decoder_name = index.model()->data(id_index, Qt::DisplayRole).toString();
225
226         const srd_decoder* chosen_decoder = srd_decoder_get_by_id(decoder_name.toUtf8());
227         if (chosen_decoder == nullptr)
228                 return;
229
230         vector<const srd_decoder*> decoders;
231         decoders.push_back(chosen_decoder);
232
233         // If the decoder only depends on logic inputs, we add it and are done
234         vector<const char*> inputs = get_decoder_inputs(decoders.front());
235         if (inputs.size() == 0) {
236                 qWarning() << "Protocol decoder" << decoder_name << "cannot have 0 inputs!";
237                 return;
238         }
239
240         if (strncmp(inputs.at(0), "logic", 5) == 0) {
241                 new_decoders_selected(decoders);
242                 return;
243         }
244
245         // Check if we can automatically fulfill the stacking requirements
246         while (strncmp(inputs.at(0), "logic", 5) != 0) {
247                 vector<const srd_decoder*> prov_decoders = get_decoders_providing(inputs.at(0));
248
249                 if (prov_decoders.size() == 0) {
250                         // Emit warning and add the stack that we could gather so far
251                         qWarning() << "Protocol decoder" << QString::fromUtf8(decoders.back()->id) \
252                                 << "has input that no other decoder provides:" << QString::fromUtf8(inputs.at(0));
253                         break;
254                 }
255
256                 if (prov_decoders.size() == 1) {
257                         decoders.push_back(prov_decoders.front());
258                 } else {
259                         // Let user decide which one to use
260                         QString caption = QString(tr("Protocol decoder <b>%1</b> requires input type <b>%2</b> " \
261                                 "which several decoders provide.<br>Choose which one to use:<br>"))
262                                         .arg(QString::fromUtf8(decoders.back()->id), QString::fromUtf8(inputs.at(0)));
263
264                         QStringList items;
265                         for (const srd_decoder* d : prov_decoders)
266                                 items << QString::fromUtf8(d->id) + " (" + QString::fromUtf8(d->longname) + ")";
267                         bool ok_clicked;
268                         QString item = QInputDialog::getItem(this, tr("Choose Decoder"),
269                                 tr(caption.toUtf8()), items, 0, false, &ok_clicked);
270
271                         if ((!ok_clicked) || (item.isEmpty()))
272                                 return;
273
274                         QString d = item.section(' ', 0, 0);
275                         decoders.push_back(srd_decoder_get_by_id(d.toUtf8()));
276                 }
277
278                 inputs = get_decoder_inputs(decoders.back());
279         }
280
281         // Reverse decoder list and add the stack
282         reverse(decoders.begin(), decoders.end());
283         new_decoders_selected(decoders);
284 }
285
286 void SubWindow::on_filter_changed(const QString& text)
287 {
288         sort_filter_model_->setFilterFixedString(text);
289 }
290
291 } // namespace decoder_selector
292 } // namespace subwindows
293 } // namespace pv