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 const char* SaveTypeNames[SaveTypeCount] = {
58 View::View(Session &session, bool is_main_view, QMainWindow *parent) :
59 ViewBase(session, is_main_view, parent),
61 // Note: Place defaults in View::reset_view_state(), not here
63 decoder_selector_(new QComboBox()),
64 format_selector_(new QComboBox()),
65 class_selector_(new QComboBox()),
66 stacked_widget_(new QStackedWidget()),
67 hex_view_(new QHexView()),
68 save_button_(new QToolButton()),
69 save_action_(new QAction(this)),
72 QVBoxLayout *root_layout = new QVBoxLayout(this);
73 root_layout->setContentsMargins(0, 0, 0, 0);
76 QToolBar* toolbar = new QToolBar();
77 toolbar->setContextMenuPolicy(Qt::PreventContextMenu);
78 parent->addToolBar(toolbar);
81 toolbar->addWidget(new QLabel(tr("Decoder:")));
82 toolbar->addWidget(decoder_selector_);
83 toolbar->addWidget(class_selector_);
84 toolbar->addSeparator();
85 toolbar->addWidget(new QLabel(tr("Show data as")));
86 toolbar->addWidget(format_selector_);
87 toolbar->addSeparator();
88 toolbar->addWidget(save_button_);
91 format_selector_->addItem(tr("Hexdump"), qVariantFromValue(QString("text/hexdump")));
94 root_layout->addWidget(stacked_widget_);
95 stacked_widget_->addWidget(hex_view_);
96 stacked_widget_->setCurrentIndex(0);
98 connect(decoder_selector_, SIGNAL(currentIndexChanged(int)),
99 this, SLOT(on_selected_decoder_changed(int)));
100 connect(class_selector_, SIGNAL(currentIndexChanged(int)),
101 this, SLOT(on_selected_class_changed(int)));
104 decoder_selector_->setSizeAdjustPolicy(QComboBox::AdjustToContents);
105 class_selector_->setSizeAdjustPolicy(QComboBox::AdjustToContents);
108 save_action_->setText(tr("&Save..."));
109 save_action_->setIcon(QIcon::fromTheme("document-save-as",
110 QIcon(":/icons/document-save-as.png")));
111 save_action_->setShortcut(QKeySequence(Qt::CTRL + Qt::Key_S));
112 connect(save_action_, SIGNAL(triggered(bool)),
113 this, SLOT(on_actionSave_triggered()));
115 QMenu *save_menu = new QMenu();
116 connect(save_menu, SIGNAL(triggered(QAction*)),
117 this, SLOT(on_actionSave_triggered(QAction*)));
119 for (int i = 0; i < SaveTypeCount; i++) {
120 QAction *const action = save_menu->addAction(tr(SaveTypeNames[i]));
121 action->setData(qVariantFromValue(i));
124 save_button_->setMenu(save_menu);
125 save_button_->setDefaultAction(save_action_);
126 save_button_->setPopupMode(QToolButton::MenuButtonPopup);
128 parent->setSizePolicy(hex_view_->sizePolicy()); // TODO Must be updated when selected widget changes
137 ViewType View::get_type() const
139 return ViewTypeDecoderOutput;
142 void View::reset_view_state()
144 ViewBase::reset_view_state();
146 decoder_selector_->clear();
147 class_selector_->clear();
148 format_selector_->setCurrentIndex(0);
149 save_button_->setEnabled(false);
154 void View::clear_decode_signals()
156 ViewBase::clear_decode_signals();
162 void View::add_decode_signal(shared_ptr<data::DecodeSignal> signal)
164 ViewBase::add_decode_signal(signal);
166 connect(signal.get(), SIGNAL(name_changed(const QString&)),
167 this, SLOT(on_signal_name_changed(const QString&)));
168 connect(signal.get(), SIGNAL(decoder_stacked(void*)),
169 this, SLOT(on_decoder_stacked(void*)));
170 connect(signal.get(), SIGNAL(decoder_removed(void*)),
171 this, SLOT(on_decoder_removed(void*)));
173 // Add all decoders provided by this signal
174 auto stack = signal->decoder_stack();
175 if (stack.size() > 1) {
176 for (const shared_ptr<Decoder>& dec : stack)
177 // Only add the decoder if it has binary output
178 if (dec->get_binary_class_count() > 0) {
179 QString title = QString("%1 (%2)").arg(signal->name(), dec->name());
180 decoder_selector_->addItem(title, QVariant::fromValue((void*)dec.get()));
183 if (!stack.empty()) {
184 shared_ptr<Decoder>& dec = stack.at(0);
185 if (dec->get_binary_class_count() > 0)
186 decoder_selector_->addItem(signal->name(), QVariant::fromValue((void*)dec.get()));
190 void View::remove_decode_signal(shared_ptr<data::DecodeSignal> signal)
192 // Remove all decoders provided by this signal
193 for (const shared_ptr<Decoder>& dec : signal->decoder_stack()) {
194 int index = decoder_selector_->findData(QVariant::fromValue((void*)dec.get()));
197 decoder_selector_->removeItem(index);
200 ViewBase::remove_decode_signal(signal);
202 if (signal.get() == signal_) {
209 void View::save_settings(QSettings &settings) const
214 void View::restore_settings(QSettings &settings)
216 // Note: It is assumed that this function is only called once,
217 // immediately after restoring a previous session.
221 void View::reset_data()
226 binary_data_exists_ = false;
231 void View::update_data()
236 if (!binary_data_exists_)
239 const DecodeBinaryClass* bin_class =
240 signal_->get_binary_data_class(current_segment_, decoder_, bin_class_id_);
242 hex_view_->setData(bin_class);
245 void View::on_selected_decoder_changed(int index)
248 disconnect(signal_, SIGNAL(new_binary_data(unsigned int, void*, unsigned int)));
252 decoder_ = (Decoder*)decoder_selector_->itemData(index).value<void*>();
254 // Find the signal that contains the selected decoder
255 for (const shared_ptr<DecodeSignal>& ds : decode_signals_)
256 for (const shared_ptr<Decoder>& dec : ds->decoder_stack())
257 if (decoder_ == dec.get())
260 class_selector_->clear();
263 // Populate binary class selector
264 uint32_t bin_classes = decoder_->get_binary_class_count();
265 for (uint32_t i = 0; i < bin_classes; i++) {
266 const data::decode::DecodeBinaryClassInfo* class_info = decoder_->get_binary_class(i);
267 class_selector_->addItem(class_info->description, QVariant::fromValue(i));
270 connect(signal_, SIGNAL(new_binary_data(unsigned int, void*, unsigned int)),
271 this, SLOT(on_new_binary_data(unsigned int, void*, unsigned int)));
277 void View::on_selected_class_changed(int index)
279 bin_class_id_ = class_selector_->itemData(index).value<uint32_t>();
281 binary_data_exists_ =
282 signal_->get_binary_data_chunk_count(current_segment_, decoder_, bin_class_id_);
287 void View::on_signal_name_changed(const QString &name)
291 SignalBase* sb = qobject_cast<SignalBase*>(QObject::sender());
294 DecodeSignal* signal = dynamic_cast<DecodeSignal*>(sb);
297 // Update all decoder entries provided by this signal
298 auto stack = signal->decoder_stack();
299 if (stack.size() > 1) {
300 for (const shared_ptr<Decoder>& dec : stack) {
301 QString title = QString("%1 (%2)").arg(signal->name(), dec->name());
302 int index = decoder_selector_->findData(QVariant::fromValue((void*)dec.get()));
305 decoder_selector_->setItemText(index, title);
308 if (!stack.empty()) {
309 shared_ptr<Decoder>& dec = stack.at(0);
310 int index = decoder_selector_->findData(QVariant::fromValue((void*)dec.get()));
313 decoder_selector_->setItemText(index, signal->name());
317 void View::on_new_binary_data(unsigned int segment_id, void* decoder, unsigned int bin_class_id)
319 if ((segment_id == current_segment_) && (decoder == decoder_) && (bin_class_id == bin_class_id_))
320 if (!delayed_view_updater_.isActive())
321 delayed_view_updater_.start();
324 void View::on_decoder_stacked(void* decoder)
326 // TODO This doesn't change existing entries for the same signal - but it should as the naming scheme may change
328 Decoder* d = static_cast<Decoder*>(decoder);
330 // Only add the decoder if it has binary output
331 if (d->get_binary_class_count() == 0)
334 // Find the signal that contains the selected decoder
335 DecodeSignal* signal = nullptr;
337 for (const shared_ptr<DecodeSignal>& ds : decode_signals_)
338 for (const shared_ptr<Decoder>& dec : ds->decoder_stack())
344 // Add the decoder to the list
345 QString title = QString("%1 (%2)").arg(signal->name(), d->name());
346 decoder_selector_->addItem(title, QVariant::fromValue((void*)d));
349 void View::on_decoder_removed(void* decoder)
351 Decoder* d = static_cast<Decoder*>(decoder);
353 // Remove the decoder from the list
354 int index = decoder_selector_->findData(QVariant::fromValue((void*)d));
357 decoder_selector_->removeItem(index);
360 void View::on_actionSave_triggered(QAction* action)
365 void View::perform_delayed_view_update()
367 if (!binary_data_exists_)
368 if (signal_->get_binary_data_chunk_count(current_segment_, decoder_, bin_class_id_)) {
369 binary_data_exists_ = true;
371 save_button_->setEnabled(true);
378 } // namespace decoder_output