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