]> sigrok.org Git - pulseview.git/blame - pv/views/decoder_output/view.cpp
Various PD-related changes
[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)));
4b97fe09
SA
90
91 hex_view_->setData(merged_data_);
560f8377 92
2bdc5796
SA
93 reset_view_state();
94}
95
96View::~View()
97{
98}
99
db298158
SA
100ViewType View::get_type() const
101{
102 return ViewTypeDecoderOutput;
103}
104
2bdc5796
SA
105void View::reset_view_state()
106{
107 ViewBase::reset_view_state();
108}
109
110void View::clear_signals()
111{
112 ViewBase::clear_signalbases();
4b97fe09 113 signal_ = nullptr;
2bdc5796
SA
114}
115
116void View::clear_decode_signals()
117{
e77de61f
SA
118 ViewBase::clear_decode_signals();
119
120 decoder_selector_->clear();
861ab3a4 121 format_selector_->setCurrentIndex(0);
4b97fe09 122 signal_ = nullptr;
2bdc5796
SA
123}
124
125void View::add_decode_signal(shared_ptr<data::DecodeSignal> signal)
126{
e77de61f
SA
127 ViewBase::add_decode_signal(signal);
128
2bdc5796 129 connect(signal.get(), SIGNAL(name_changed(const QString&)),
bdbc561f 130 this, SLOT(on_signal_name_changed(const QString&)));
e77de61f
SA
131 connect(signal.get(), SIGNAL(decoder_stacked(void*)),
132 this, SLOT(on_decoder_stacked(void*)));
133 connect(signal.get(), SIGNAL(decoder_removed(void*)),
134 this, SLOT(on_decoder_removed(void*)));
135
136 // Add all decoders provided by this signal
137 auto stack = signal->decoder_stack();
138 if (stack.size() > 1) {
139 for (const shared_ptr<Decoder>& dec : stack) {
140 QString title = QString("%1 (%2)").arg(signal->name(), dec->name());
141 decoder_selector_->addItem(title, QVariant::fromValue((void*)dec.get()));
142 }
143 } else
144 if (!stack.empty()) {
145 shared_ptr<Decoder>& dec = stack.at(0);
146 decoder_selector_->addItem(signal->name(), QVariant::fromValue((void*)dec.get()));
147 }
2bdc5796
SA
148}
149
150void View::remove_decode_signal(shared_ptr<data::DecodeSignal> signal)
151{
e77de61f
SA
152 // Remove all decoders provided by this signal
153 for (const shared_ptr<Decoder>& dec : signal->decoder_stack()) {
154 int index = decoder_selector_->findData(QVariant::fromValue((void*)dec.get()));
bdbc561f 155
e77de61f
SA
156 if (index != -1)
157 decoder_selector_->removeItem(index);
158 }
159
160 ViewBase::remove_decode_signal(signal);
4b97fe09
SA
161
162 if (signal.get() == signal_) {
163 signal_ = nullptr;
e77de61f
SA
164 decoder_ = nullptr;
165 bin_class_id_ = 0;
4b97fe09
SA
166 update_data();
167 }
2bdc5796
SA
168}
169
170void View::save_settings(QSettings &settings) const
171{
172 (void)settings;
173}
174
175void View::restore_settings(QSettings &settings)
176{
177 // Note: It is assumed that this function is only called once,
178 // immediately after restoring a previous session.
179 (void)settings;
180}
181
4b97fe09
SA
182void View::update_data()
183{
184 if (!signal_) {
185 merged_data_->clear();
186 return;
187 }
188
e77de61f 189 if (signal_->get_binary_data_chunk_count(current_segment_, decoder_, bin_class_id_) == 0) {
4b97fe09
SA
190 merged_data_->clear();
191 return;
192 }
193
194 vector<uint8_t> data;
e77de61f
SA
195 signal_->get_binary_data_chunks_merged(current_segment_, decoder_, bin_class_id_,
196 0, numeric_limits<uint64_t>::max(), &data);
4b97fe09
SA
197
198 merged_data_->resize(data.size());
199 memcpy(merged_data_->data(), data.data(), data.size());
200}
201
e77de61f 202void View::on_selected_decoder_changed(int index)
4b97fe09
SA
203{
204 if (signal_)
e77de61f 205 disconnect(signal_, SIGNAL(new_binary_data(unsigned int, unsigned int)));
4b97fe09 206
e77de61f
SA
207 decoder_ = (Decoder*)decoder_selector_->itemData(index).value<void*>();
208
209 // Find the signal that contains the selected decoder
210 signal_ = nullptr;
211
212 for (const shared_ptr<data::SignalBase>& sb : signalbases_) {
213 shared_ptr<DecodeSignal> ds = dynamic_pointer_cast<DecodeSignal>(sb);
214
215 if (ds)
216 for (const shared_ptr<Decoder>& dec : ds->decoder_stack())
217 if (decoder_ == dec.get())
218 signal_ = ds.get();
219 }
4b97fe09
SA
220
221 if (signal_)
e77de61f
SA
222 connect(signal_, SIGNAL(new_binary_data(unsigned int, unsigned int)),
223 this, SLOT(on_new_binary_data(unsigned int, unsigned int)));
224
225 update_data();
4b97fe09
SA
226}
227
bdbc561f 228void View::on_signal_name_changed(const QString &name)
2bdc5796 229{
e77de61f
SA
230 (void)name;
231
232 SignalBase* sb = qobject_cast<SignalBase*>(QObject::sender());
bdbc561f
SA
233 assert(sb);
234
e77de61f
SA
235 DecodeSignal* signal = dynamic_cast<DecodeSignal*>(sb);
236 assert(signal);
237
238 // Update all decoder entries provided by this signal
239 auto stack = signal->decoder_stack();
240 if (stack.size() > 1) {
241 for (const shared_ptr<Decoder>& dec : stack) {
242 QString title = QString("%1 (%2)").arg(signal->name(), dec->name());
243 int index = decoder_selector_->findData(QVariant::fromValue((void*)dec.get()));
244
245 if (index != -1)
246 decoder_selector_->setItemText(index, title);
247 }
248 } else
249 if (!stack.empty()) {
250 shared_ptr<Decoder>& dec = stack.at(0);
251 int index = decoder_selector_->findData(QVariant::fromValue((void*)dec.get()));
252
253 if (index != -1)
254 decoder_selector_->setItemText(index, signal->name());
255 }
2bdc5796
SA
256}
257
e77de61f 258void View::on_new_binary_data(unsigned int segment_id, unsigned int bin_class_id)
4b97fe09 259{
e77de61f 260 if ((segment_id == current_segment_) && (bin_class_id == bin_class_id_))
4b97fe09
SA
261 update_data();
262}
263
e77de61f
SA
264void View::on_decoder_stacked(void* decoder)
265{
266 // TODO This doesn't change existing entries for the same signal - but it should as the naming scheme may change
267
268 Decoder* d = static_cast<Decoder*>(decoder);
269
270 // Find the signal that contains the selected decoder
271 DecodeSignal* signal = nullptr;
272
273 for (const shared_ptr<DecodeSignal>& ds : decode_signals_)
274 for (const shared_ptr<Decoder>& dec : ds->decoder_stack())
275 if (d == dec.get())
276 signal = ds.get();
277
278 assert(signal);
279
280 // Add the decoder to the list
281 QString title = QString("%1 (%2)").arg(signal->name(), d->name());
282 decoder_selector_->addItem(title, QVariant::fromValue((void*)d));
283}
284
285void View::on_decoder_removed(void* decoder)
286{
287 Decoder* d = static_cast<Decoder*>(decoder);
288
289 // Remove the decoder from the list
290 int index = decoder_selector_->findData(QVariant::fromValue((void*)d));
291
292 if (index != -1)
293 decoder_selector_->removeItem(index);
294}
295
296
2bdc5796
SA
297} // namespace decoder_output
298} // namespace views
299} // namespace pv