2 * This file is part of the PulseView project.
4 * Copyright (C) 2019 Soeren Apel <soeren@apelpie.net>
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.
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.
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/>.
27 #include <QVBoxLayout>
29 #include <libsigrokdecode/libsigrokdecode.h>
32 #include "QHexView.hpp"
34 #include "pv/session.hpp"
35 #include "pv/util.hpp"
36 #include "pv/data/decode/decoder.hpp"
38 using pv::data::DecodeSignal;
39 using pv::data::SignalBase;
40 using pv::data::decode::Decoder;
41 using pv::util::TimeUnit;
42 using pv::util::Timestamp;
44 using std::dynamic_pointer_cast;
45 using std::numeric_limits;
46 using std::shared_ptr;
50 namespace decoder_output {
52 View::View(Session &session, bool is_main_view, QMainWindow *parent) :
53 ViewBase(session, is_main_view, parent),
55 // Note: Place defaults in View::reset_view_state(), not here
56 decoder_selector_(new QComboBox()),
57 format_selector_(new QComboBox()),
58 class_selector_(new QComboBox()),
59 stacked_widget_(new QStackedWidget()),
60 hex_view_(new QHexView()),
62 merged_data_(new QByteArray())
64 QVBoxLayout *root_layout = new QVBoxLayout(this);
65 root_layout->setContentsMargins(0, 0, 0, 0);
68 QToolBar* toolbar = new QToolBar();
69 toolbar->setContextMenuPolicy(Qt::PreventContextMenu);
70 parent->addToolBar(toolbar);
73 toolbar->addWidget(new QLabel(tr("Decoder:")));
74 toolbar->addWidget(decoder_selector_);
75 toolbar->addWidget(class_selector_);
76 toolbar->addSeparator();
77 toolbar->addWidget(new QLabel(tr("Show data as")));
78 toolbar->addWidget(format_selector_);
81 format_selector_->addItem(tr("Hexdump"), qVariantFromValue(QString("text/hexdump")));
84 root_layout->addWidget(stacked_widget_);
85 stacked_widget_->addWidget(hex_view_);
86 stacked_widget_->setCurrentIndex(0);
88 connect(decoder_selector_, SIGNAL(currentIndexChanged(int)),
89 this, SLOT(on_selected_decoder_changed(int)));
90 connect(class_selector_, SIGNAL(currentIndexChanged(int)),
91 this, SLOT(on_selected_class_changed(int)));
93 hex_view_->setData(merged_data_);
102 ViewType View::get_type() const
104 return ViewTypeDecoderOutput;
107 void View::reset_view_state()
109 ViewBase::reset_view_state();
112 void View::clear_signals()
114 ViewBase::clear_signalbases();
118 void View::clear_decode_signals()
120 ViewBase::clear_decode_signals();
122 decoder_selector_->clear();
123 class_selector_->clear();
124 format_selector_->setCurrentIndex(0);
128 void View::add_decode_signal(shared_ptr<data::DecodeSignal> signal)
130 ViewBase::add_decode_signal(signal);
132 connect(signal.get(), SIGNAL(name_changed(const QString&)),
133 this, SLOT(on_signal_name_changed(const QString&)));
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*)));
139 // Add all decoders provided by this signal
140 auto stack = signal->decoder_stack();
141 if (stack.size() > 1) {
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()));
149 if (!stack.empty()) {
150 shared_ptr<Decoder>& dec = stack.at(0);
151 if (dec->get_binary_class_count() > 0)
152 decoder_selector_->addItem(signal->name(), QVariant::fromValue((void*)dec.get()));
156 void View::remove_decode_signal(shared_ptr<data::DecodeSignal> signal)
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()));
163 decoder_selector_->removeItem(index);
166 ViewBase::remove_decode_signal(signal);
168 if (signal.get() == signal_) {
176 void View::save_settings(QSettings &settings) const
181 void View::restore_settings(QSettings &settings)
183 // Note: It is assumed that this function is only called once,
184 // immediately after restoring a previous session.
188 void View::update_data()
191 merged_data_->clear();
195 if (signal_->get_binary_data_chunk_count(current_segment_, decoder_, bin_class_id_) == 0) {
196 merged_data_->clear();
200 vector<uint8_t> data;
201 signal_->get_binary_data_chunks_merged(current_segment_, decoder_, bin_class_id_,
202 0, numeric_limits<uint64_t>::max(), &data);
204 merged_data_->resize(data.size());
205 memcpy(merged_data_->data(), data.data(), data.size());
207 hex_view_->setData(merged_data_);
210 void View::on_selected_decoder_changed(int index)
213 disconnect(signal_, SIGNAL(new_binary_data(unsigned int, void*, unsigned int)));
215 decoder_ = (Decoder*)decoder_selector_->itemData(index).value<void*>();
217 // Find the signal that contains the selected decoder
220 for (const shared_ptr<DecodeSignal>& ds : decode_signals_)
221 for (const shared_ptr<Decoder>& dec : ds->decoder_stack())
222 if (decoder_ == dec.get())
225 class_selector_->clear();
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));
235 connect(signal_, SIGNAL(new_binary_data(unsigned int, void*, unsigned int)),
236 this, SLOT(on_new_binary_data(unsigned int, void*, unsigned int)));
242 void View::on_selected_class_changed(int index)
244 bin_class_id_ = class_selector_->itemData(index).value<uint8_t>();
249 void View::on_signal_name_changed(const QString &name)
253 SignalBase* sb = qobject_cast<SignalBase*>(QObject::sender());
256 DecodeSignal* signal = dynamic_cast<DecodeSignal*>(sb);
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()));
267 decoder_selector_->setItemText(index, title);
270 if (!stack.empty()) {
271 shared_ptr<Decoder>& dec = stack.at(0);
272 int index = decoder_selector_->findData(QVariant::fromValue((void*)dec.get()));
275 decoder_selector_->setItemText(index, signal->name());
279 void View::on_new_binary_data(unsigned int segment_id, void* decoder, unsigned int bin_class_id)
281 if ((segment_id == current_segment_) && (decoder == decoder_) && (bin_class_id == bin_class_id_))
285 void View::on_decoder_stacked(void* decoder)
287 // TODO This doesn't change existing entries for the same signal - but it should as the naming scheme may change
289 Decoder* d = static_cast<Decoder*>(decoder);
291 // Only add the decoder if it has binary output
292 if (d->get_binary_class_count() == 0)
295 // Find the signal that contains the selected decoder
296 DecodeSignal* signal = nullptr;
298 for (const shared_ptr<DecodeSignal>& ds : decode_signals_)
299 for (const shared_ptr<Decoder>& dec : ds->decoder_stack())
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));
310 void View::on_decoder_removed(void* decoder)
312 Decoder* d = static_cast<Decoder*>(decoder);
314 // Remove the decoder from the list
315 int index = decoder_selector_->findData(QVariant::fromValue((void*)d));
318 decoder_selector_->removeItem(index);
322 } // namespace decoder_output