]> sigrok.org Git - pulseview.git/blame - pv/views/decoder_output/view.cpp
Implement binary class selector
[pulseview.git] / pv / views / decoder_output / view.cpp
CommitLineData
2bdc5796
SA
1/*
2 * This file is part of the PulseView project.
3 *
4 * Copyright (C) 2019 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
4b97fe09 20#include <climits>
2bdc5796 21
4b97fe09
SA
22#include <QByteArray>
23#include <QDebug>
bdbc561f 24#include <QLabel>
2bdc5796 25#include <QMenu>
a24412db 26#include <QToolBar>
2bdc5796
SA
27#include <QVBoxLayout>
28
4b97fe09
SA
29#include <libsigrokdecode/libsigrokdecode.h>
30
2bdc5796 31#include "view.hpp"
560f8377 32#include "QHexView.hpp"
2bdc5796
SA
33
34#include "pv/session.hpp"
35#include "pv/util.hpp"
e77de61f 36#include "pv/data/decode/decoder.hpp"
2bdc5796 37
4b97fe09 38using pv::data::DecodeSignal;
bdbc561f 39using pv::data::SignalBase;
e77de61f 40using pv::data::decode::Decoder;
2bdc5796
SA
41using pv::util::TimeUnit;
42using pv::util::Timestamp;
43
e77de61f 44using std::dynamic_pointer_cast;
4b97fe09 45using std::numeric_limits;
2bdc5796
SA
46using std::shared_ptr;
47
48namespace pv {
49namespace views {
50namespace decoder_output {
51
a24412db 52View::View(Session &session, bool is_main_view, QMainWindow *parent) :
bdbc561f 53 ViewBase(session, is_main_view, parent),
2bdc5796
SA
54
55 // Note: Place defaults in View::reset_view_state(), not here
e77de61f 56 decoder_selector_(new QComboBox()),
560f8377 57 format_selector_(new QComboBox()),
e77de61f 58 class_selector_(new QComboBox()),
560f8377 59 stacked_widget_(new QStackedWidget()),
4b97fe09
SA
60 hex_view_(new QHexView()),
61 signal_(nullptr),
62 merged_data_(new QByteArray())
2bdc5796
SA
63{
64 QVBoxLayout *root_layout = new QVBoxLayout(this);
65 root_layout->setContentsMargins(0, 0, 0, 0);
66
bdbc561f
SA
67 // Create toolbar
68 QToolBar* toolbar = new QToolBar();
69 toolbar->setContextMenuPolicy(Qt::PreventContextMenu);
70 parent->addToolBar(toolbar);
a24412db 71
bdbc561f
SA
72 // Populate toolbar
73 toolbar->addWidget(new QLabel(tr("Decoder:")));
e77de61f
SA
74 toolbar->addWidget(decoder_selector_);
75 toolbar->addWidget(class_selector_);
861ab3a4
SA
76 toolbar->addSeparator();
77 toolbar->addWidget(new QLabel(tr("Show data as")));
78 toolbar->addWidget(format_selector_);
79
80 // Add format types
81 format_selector_->addItem(tr("Hexdump"), qVariantFromValue(QString("text/hexdump")));
a24412db 82
560f8377
SA
83 // Add widget stack
84 root_layout->addWidget(stacked_widget_);
85 stacked_widget_->addWidget(hex_view_);
4b97fe09
SA
86 stacked_widget_->setCurrentIndex(0);
87
e77de61f
SA
88 connect(decoder_selector_, SIGNAL(currentIndexChanged(int)),
89 this, SLOT(on_selected_decoder_changed(int)));
b2b18d3a
SA
90 connect(class_selector_, SIGNAL(currentIndexChanged(int)),
91 this, SLOT(on_selected_class_changed(int)));
4b97fe09
SA
92
93 hex_view_->setData(merged_data_);
560f8377 94
2bdc5796
SA
95 reset_view_state();
96}
97
98View::~View()
99{
100}
101
db298158
SA
102ViewType View::get_type() const
103{
104 return ViewTypeDecoderOutput;
105}
106
2bdc5796
SA
107void View::reset_view_state()
108{
109 ViewBase::reset_view_state();
110}
111
112void View::clear_signals()
113{
114 ViewBase::clear_signalbases();
4b97fe09 115 signal_ = nullptr;
2bdc5796
SA
116}
117
118void View::clear_decode_signals()
119{
e77de61f
SA
120 ViewBase::clear_decode_signals();
121
122 decoder_selector_->clear();
b2b18d3a 123 class_selector_->clear();
861ab3a4 124 format_selector_->setCurrentIndex(0);
4b97fe09 125 signal_ = nullptr;
2bdc5796
SA
126}
127
128void View::add_decode_signal(shared_ptr<data::DecodeSignal> signal)
129{
e77de61f
SA
130 ViewBase::add_decode_signal(signal);
131
2bdc5796 132 connect(signal.get(), SIGNAL(name_changed(const QString&)),
bdbc561f 133 this, SLOT(on_signal_name_changed(const QString&)));
e77de61f
SA
134 connect(signal.get(), SIGNAL(decoder_stacked(void*)),
135 this, SLOT(on_decoder_stacked(void*)));
136 connect(signal.get(), SIGNAL(decoder_removed(void*)),
137 this, SLOT(on_decoder_removed(void*)));
138
139 // Add all decoders provided by this signal
140 auto stack = signal->decoder_stack();
141 if (stack.size() > 1) {
b2b18d3a
SA
142 for (const shared_ptr<Decoder>& dec : stack)
143 // Only add the decoder if it has binary output
144 if (dec->get_binary_class_count() > 0) {
145 QString title = QString("%1 (%2)").arg(signal->name(), dec->name());
146 decoder_selector_->addItem(title, QVariant::fromValue((void*)dec.get()));
147 }
e77de61f
SA
148 } else
149 if (!stack.empty()) {
150 shared_ptr<Decoder>& dec = stack.at(0);
b2b18d3a
SA
151 if (dec->get_binary_class_count() > 0)
152 decoder_selector_->addItem(signal->name(), QVariant::fromValue((void*)dec.get()));
e77de61f 153 }
2bdc5796
SA
154}
155
156void View::remove_decode_signal(shared_ptr<data::DecodeSignal> signal)
157{
e77de61f
SA
158 // Remove all decoders provided by this signal
159 for (const shared_ptr<Decoder>& dec : signal->decoder_stack()) {
160 int index = decoder_selector_->findData(QVariant::fromValue((void*)dec.get()));
bdbc561f 161
e77de61f
SA
162 if (index != -1)
163 decoder_selector_->removeItem(index);
164 }
165
166 ViewBase::remove_decode_signal(signal);
4b97fe09
SA
167
168 if (signal.get() == signal_) {
169 signal_ = nullptr;
e77de61f
SA
170 decoder_ = nullptr;
171 bin_class_id_ = 0;
4b97fe09
SA
172 update_data();
173 }
2bdc5796
SA
174}
175
176void View::save_settings(QSettings &settings) const
177{
178 (void)settings;
179}
180
181void View::restore_settings(QSettings &settings)
182{
183 // Note: It is assumed that this function is only called once,
184 // immediately after restoring a previous session.
185 (void)settings;
186}
187
4b97fe09
SA
188void View::update_data()
189{
190 if (!signal_) {
191 merged_data_->clear();
192 return;
193 }
194
e77de61f 195 if (signal_->get_binary_data_chunk_count(current_segment_, decoder_, bin_class_id_) == 0) {
4b97fe09
SA
196 merged_data_->clear();
197 return;
198 }
199
200 vector<uint8_t> data;
e77de61f
SA
201 signal_->get_binary_data_chunks_merged(current_segment_, decoder_, bin_class_id_,
202 0, numeric_limits<uint64_t>::max(), &data);
4b97fe09
SA
203
204 merged_data_->resize(data.size());
205 memcpy(merged_data_->data(), data.data(), data.size());
b2b18d3a
SA
206
207 hex_view_->setData(merged_data_);
4b97fe09
SA
208}
209
e77de61f 210void View::on_selected_decoder_changed(int index)
4b97fe09
SA
211{
212 if (signal_)
b2b18d3a 213 disconnect(signal_, SIGNAL(new_binary_data(unsigned int, void*, unsigned int)));
4b97fe09 214
e77de61f
SA
215 decoder_ = (Decoder*)decoder_selector_->itemData(index).value<void*>();
216
217 // Find the signal that contains the selected decoder
218 signal_ = nullptr;
219
b2b18d3a
SA
220 for (const shared_ptr<DecodeSignal>& ds : decode_signals_)
221 for (const shared_ptr<Decoder>& dec : ds->decoder_stack())
222 if (decoder_ == dec.get())
223 signal_ = ds.get();
e77de61f 224
b2b18d3a
SA
225 class_selector_->clear();
226
227 if (signal_) {
228 // Populate binary class selector
229 uint8_t bin_classes = decoder_->get_binary_class_count();
230 for (uint8_t i = 0; i < bin_classes; i++) {
231 const data::decode::DecodeBinaryClassInfo* class_info = decoder_->get_binary_class(i);
232 class_selector_->addItem(class_info->name, QVariant::fromValue(i));
233 }
234
235 connect(signal_, SIGNAL(new_binary_data(unsigned int, void*, unsigned int)),
236 this, SLOT(on_new_binary_data(unsigned int, void*, unsigned int)));
e77de61f 237 }
4b97fe09 238
b2b18d3a
SA
239 update_data();
240}
241
242void View::on_selected_class_changed(int index)
243{
244 bin_class_id_ = class_selector_->itemData(index).value<uint8_t>();
e77de61f
SA
245
246 update_data();
4b97fe09
SA
247}
248
bdbc561f 249void View::on_signal_name_changed(const QString &name)
2bdc5796 250{
e77de61f
SA
251 (void)name;
252
253 SignalBase* sb = qobject_cast<SignalBase*>(QObject::sender());
bdbc561f
SA
254 assert(sb);
255
e77de61f
SA
256 DecodeSignal* signal = dynamic_cast<DecodeSignal*>(sb);
257 assert(signal);
258
259 // Update all decoder entries provided by this signal
260 auto stack = signal->decoder_stack();
261 if (stack.size() > 1) {
262 for (const shared_ptr<Decoder>& dec : stack) {
263 QString title = QString("%1 (%2)").arg(signal->name(), dec->name());
264 int index = decoder_selector_->findData(QVariant::fromValue((void*)dec.get()));
265
266 if (index != -1)
267 decoder_selector_->setItemText(index, title);
268 }
269 } else
270 if (!stack.empty()) {
271 shared_ptr<Decoder>& dec = stack.at(0);
272 int index = decoder_selector_->findData(QVariant::fromValue((void*)dec.get()));
273
274 if (index != -1)
275 decoder_selector_->setItemText(index, signal->name());
276 }
2bdc5796
SA
277}
278
b2b18d3a 279void View::on_new_binary_data(unsigned int segment_id, void* decoder, unsigned int bin_class_id)
4b97fe09 280{
b2b18d3a 281 if ((segment_id == current_segment_) && (decoder == decoder_) && (bin_class_id == bin_class_id_))
4b97fe09
SA
282 update_data();
283}
284
e77de61f
SA
285void View::on_decoder_stacked(void* decoder)
286{
287 // TODO This doesn't change existing entries for the same signal - but it should as the naming scheme may change
288
289 Decoder* d = static_cast<Decoder*>(decoder);
290
b2b18d3a
SA
291 // Only add the decoder if it has binary output
292 if (d->get_binary_class_count() == 0)
293 return;
294
e77de61f
SA
295 // Find the signal that contains the selected decoder
296 DecodeSignal* signal = nullptr;
297
298 for (const shared_ptr<DecodeSignal>& ds : decode_signals_)
299 for (const shared_ptr<Decoder>& dec : ds->decoder_stack())
300 if (d == dec.get())
301 signal = ds.get();
302
303 assert(signal);
304
305 // Add the decoder to the list
306 QString title = QString("%1 (%2)").arg(signal->name(), d->name());
307 decoder_selector_->addItem(title, QVariant::fromValue((void*)d));
308}
309
310void View::on_decoder_removed(void* decoder)
311{
312 Decoder* d = static_cast<Decoder*>(decoder);
313
314 // Remove the decoder from the list
315 int index = decoder_selector_->findData(QVariant::fromValue((void*)d));
316
317 if (index != -1)
318 decoder_selector_->removeItem(index);
319}
320
321
2bdc5796
SA
322} // namespace decoder_output
323} // namespace views
324} // namespace pv