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()),
63 QVBoxLayout *root_layout = new QVBoxLayout(this);
64 root_layout->setContentsMargins(0, 0, 0, 0);
67 QToolBar* toolbar = new QToolBar();
68 toolbar->setContextMenuPolicy(Qt::PreventContextMenu);
69 parent->addToolBar(toolbar);
72 toolbar->addWidget(new QLabel(tr("Decoder:")));
73 toolbar->addWidget(decoder_selector_);
74 toolbar->addWidget(class_selector_);
75 toolbar->addSeparator();
76 toolbar->addWidget(new QLabel(tr("Show data as")));
77 toolbar->addWidget(format_selector_);
80 format_selector_->addItem(tr("Hexdump"), qVariantFromValue(QString("text/hexdump")));
83 root_layout->addWidget(stacked_widget_);
84 stacked_widget_->addWidget(hex_view_);
85 stacked_widget_->setCurrentIndex(0);
87 connect(decoder_selector_, SIGNAL(currentIndexChanged(int)),
88 this, SLOT(on_selected_decoder_changed(int)));
89 connect(class_selector_, SIGNAL(currentIndexChanged(int)),
90 this, SLOT(on_selected_class_changed(int)));
93 decoder_selector_->setSizeAdjustPolicy(QComboBox::AdjustToContents);
94 class_selector_->setSizeAdjustPolicy(QComboBox::AdjustToContents);
103 ViewType View::get_type() const
105 return ViewTypeDecoderOutput;
108 void View::reset_view_state()
110 ViewBase::reset_view_state();
113 void View::clear_signals()
115 ViewBase::clear_signalbases();
119 void View::clear_decode_signals()
121 ViewBase::clear_decode_signals();
123 decoder_selector_->clear();
124 class_selector_->clear();
125 format_selector_->setCurrentIndex(0);
129 void View::add_decode_signal(shared_ptr<data::DecodeSignal> signal)
131 ViewBase::add_decode_signal(signal);
133 connect(signal.get(), SIGNAL(name_changed(const QString&)),
134 this, SLOT(on_signal_name_changed(const QString&)));
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*)));
140 // Add all decoders provided by this signal
141 auto stack = signal->decoder_stack();
142 if (stack.size() > 1) {
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()));
150 if (!stack.empty()) {
151 shared_ptr<Decoder>& dec = stack.at(0);
152 if (dec->get_binary_class_count() > 0)
153 decoder_selector_->addItem(signal->name(), QVariant::fromValue((void*)dec.get()));
157 void View::remove_decode_signal(shared_ptr<data::DecodeSignal> signal)
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()));
164 decoder_selector_->removeItem(index);
167 ViewBase::remove_decode_signal(signal);
169 if (signal.get() == signal_) {
177 void View::save_settings(QSettings &settings) const
182 void View::restore_settings(QSettings &settings)
184 // Note: It is assumed that this function is only called once,
185 // immediately after restoring a previous session.
189 void View::update_data()
196 if (signal_->get_binary_data_chunk_count(current_segment_, decoder_, bin_class_id_) == 0) {
201 const DecodeBinaryClass* bin_class =
202 signal_->get_binary_data_class(current_segment_, decoder_, bin_class_id_);
204 hex_view_->setData(bin_class);
207 void View::on_selected_decoder_changed(int index)
210 disconnect(signal_, SIGNAL(new_binary_data(unsigned int, void*, unsigned int)));
212 decoder_ = (Decoder*)decoder_selector_->itemData(index).value<void*>();
214 // Find the signal that contains the selected decoder
217 for (const shared_ptr<DecodeSignal>& ds : decode_signals_)
218 for (const shared_ptr<Decoder>& dec : ds->decoder_stack())
219 if (decoder_ == dec.get())
222 class_selector_->clear();
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));
232 connect(signal_, SIGNAL(new_binary_data(unsigned int, void*, unsigned int)),
233 this, SLOT(on_new_binary_data(unsigned int, void*, unsigned int)));
239 void View::on_selected_class_changed(int index)
241 bin_class_id_ = class_selector_->itemData(index).value<uint8_t>();
246 void View::on_signal_name_changed(const QString &name)
250 SignalBase* sb = qobject_cast<SignalBase*>(QObject::sender());
253 DecodeSignal* signal = dynamic_cast<DecodeSignal*>(sb);
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()));
264 decoder_selector_->setItemText(index, title);
267 if (!stack.empty()) {
268 shared_ptr<Decoder>& dec = stack.at(0);
269 int index = decoder_selector_->findData(QVariant::fromValue((void*)dec.get()));
272 decoder_selector_->setItemText(index, signal->name());
276 void View::on_new_binary_data(unsigned int segment_id, void* decoder, unsigned int bin_class_id)
278 if ((segment_id == current_segment_) && (decoder == decoder_) && (bin_class_id == bin_class_id_))
282 void View::on_decoder_stacked(void* decoder)
284 // TODO This doesn't change existing entries for the same signal - but it should as the naming scheme may change
286 Decoder* d = static_cast<Decoder*>(decoder);
288 // Only add the decoder if it has binary output
289 if (d->get_binary_class_count() == 0)
292 // Find the signal that contains the selected decoder
293 DecodeSignal* signal = nullptr;
295 for (const shared_ptr<DecodeSignal>& ds : decode_signals_)
296 for (const shared_ptr<Decoder>& dec : ds->decoder_stack())
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));
307 void View::on_decoder_removed(void* decoder)
309 Decoder* d = static_cast<Decoder*>(decoder);
311 // Remove the decoder from the list
312 int index = decoder_selector_->findData(QVariant::fromValue((void*)d));
315 decoder_selector_->removeItem(index);
319 } // namespace decoder_output