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"
37 using pv::data::DecodeSignal;
38 using pv::data::SignalBase;
39 using pv::util::TimeUnit;
40 using pv::util::Timestamp;
42 using std::numeric_limits;
43 using std::shared_ptr;
47 namespace decoder_output {
49 View::View(Session &session, bool is_main_view, QMainWindow *parent) :
50 ViewBase(session, is_main_view, parent),
52 // Note: Place defaults in View::reset_view_state(), not here
53 signal_selector_(new QComboBox()),
54 format_selector_(new QComboBox()),
55 stacked_widget_(new QStackedWidget()),
56 hex_view_(new QHexView()),
58 merged_data_(new QByteArray())
60 QVBoxLayout *root_layout = new QVBoxLayout(this);
61 root_layout->setContentsMargins(0, 0, 0, 0);
64 QToolBar* toolbar = new QToolBar();
65 toolbar->setContextMenuPolicy(Qt::PreventContextMenu);
66 parent->addToolBar(toolbar);
69 toolbar->addWidget(new QLabel(tr("Decoder:")));
70 toolbar->addWidget(signal_selector_);
71 toolbar->addSeparator();
72 toolbar->addWidget(new QLabel(tr("Show data as")));
73 toolbar->addWidget(format_selector_);
76 format_selector_->addItem(tr("Hexdump"), qVariantFromValue(QString("text/hexdump")));
79 root_layout->addWidget(stacked_widget_);
80 stacked_widget_->addWidget(hex_view_);
81 stacked_widget_->setCurrentIndex(0);
83 connect(signal_selector_, SIGNAL(currentIndexChanged(int)),
84 this, SLOT(on_selected_signal_changed(int)));
86 hex_view_->setData(merged_data_);
95 ViewType View::get_type() const
97 return ViewTypeDecoderOutput;
100 void View::reset_view_state()
102 ViewBase::reset_view_state();
105 void View::clear_signals()
107 ViewBase::clear_signalbases();
111 void View::clear_decode_signals()
113 signal_selector_->clear();
114 format_selector_->setCurrentIndex(0);
118 void View::add_decode_signal(shared_ptr<data::DecodeSignal> signal)
120 connect(signal.get(), SIGNAL(name_changed(const QString&)),
121 this, SLOT(on_signal_name_changed(const QString&)));
123 signal_selector_->addItem(signal->name(), QVariant::fromValue((void*)signal.get()));
126 void View::remove_decode_signal(shared_ptr<data::DecodeSignal> signal)
128 int index = signal_selector_->findData(QVariant::fromValue((void*)signal.get()));
131 signal_selector_->removeItem(index);
133 if (signal.get() == signal_) {
139 void View::save_settings(QSettings &settings) const
144 void View::restore_settings(QSettings &settings)
146 // Note: It is assumed that this function is only called once,
147 // immediately after restoring a previous session.
151 void View::update_data()
154 merged_data_->clear();
158 if (signal_->get_binary_data_chunk_count(current_segment_) == 0) {
159 merged_data_->clear();
163 vector<uint8_t> data;
164 signal_->get_binary_data_chunks_merged(current_segment_, 0,
165 numeric_limits<uint64_t>::max(), &data);
167 merged_data_->resize(data.size());
168 memcpy(merged_data_->data(), data.data(), data.size());
171 void View::on_selected_signal_changed(int index)
174 disconnect(signal_, SIGNAL(new_binary_data(unsigned int)));
176 signal_ = (DecodeSignal*)signal_selector_->itemData(index).value<void*>();
180 connect(signal_, SIGNAL(new_binary_data(unsigned int)),
181 this, SLOT(on_new_binary_data(unsigned int)));
184 void View::on_signal_name_changed(const QString &name)
186 SignalBase *sb = qobject_cast<SignalBase*>(QObject::sender());
189 int index = signal_selector_->findData(QVariant::fromValue(sb));
191 signal_selector_->setItemText(index, name);
194 void View::on_new_binary_data(unsigned int segment_id)
196 if (segment_id == current_segment_)
200 } // namespace decoder_output