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/>.
24 #include <QFileDialog>
27 #include <QMessageBox>
29 #include <QVBoxLayout>
31 #include <libsigrokdecode/libsigrokdecode.h>
34 #include "QHexView.hpp"
36 #include "pv/globalsettings.hpp"
37 #include "pv/session.hpp"
38 #include "pv/util.hpp"
39 #include "pv/data/decode/decoder.hpp"
41 using pv::data::DecodeSignal;
42 using pv::data::SignalBase;
43 using pv::data::decode::Decoder;
44 using pv::util::TimeUnit;
45 using pv::util::Timestamp;
47 using std::dynamic_pointer_cast;
48 using std::numeric_limits;
49 using std::shared_ptr;
53 namespace decoder_output {
55 const char* SaveTypeNames[SaveTypeCount] = {
58 "Hex Dump, with offset",
63 View::View(Session &session, bool is_main_view, QMainWindow *parent) :
64 ViewBase(session, is_main_view, parent),
66 // Note: Place defaults in View::reset_view_state(), not here
68 decoder_selector_(new QComboBox()),
69 format_selector_(new QComboBox()),
70 class_selector_(new QComboBox()),
71 stacked_widget_(new QStackedWidget()),
72 hex_view_(new QHexView()),
73 save_button_(new QToolButton()),
74 save_action_(new QAction(this)),
77 QVBoxLayout *root_layout = new QVBoxLayout(this);
78 root_layout->setContentsMargins(0, 0, 0, 0);
81 QToolBar* toolbar = new QToolBar();
82 toolbar->setContextMenuPolicy(Qt::PreventContextMenu);
83 parent->addToolBar(toolbar);
86 toolbar->addWidget(new QLabel(tr("Decoder:")));
87 toolbar->addWidget(decoder_selector_);
88 toolbar->addWidget(class_selector_);
89 toolbar->addSeparator();
90 toolbar->addWidget(new QLabel(tr("Show data as")));
91 toolbar->addWidget(format_selector_);
92 toolbar->addSeparator();
93 toolbar->addWidget(save_button_);
96 format_selector_->addItem(tr("Hexdump"), qVariantFromValue(QString("text/hexdump")));
99 root_layout->addWidget(stacked_widget_);
100 stacked_widget_->addWidget(hex_view_);
101 stacked_widget_->setCurrentIndex(0);
103 connect(decoder_selector_, SIGNAL(currentIndexChanged(int)),
104 this, SLOT(on_selected_decoder_changed(int)));
105 connect(class_selector_, SIGNAL(currentIndexChanged(int)),
106 this, SLOT(on_selected_class_changed(int)));
109 decoder_selector_->setSizeAdjustPolicy(QComboBox::AdjustToContents);
110 class_selector_->setSizeAdjustPolicy(QComboBox::AdjustToContents);
113 save_action_->setText(tr("&Save..."));
114 save_action_->setIcon(QIcon::fromTheme("document-save-as",
115 QIcon(":/icons/document-save-as.png")));
116 save_action_->setShortcut(QKeySequence(Qt::CTRL + Qt::Key_S));
117 connect(save_action_, SIGNAL(triggered(bool)),
118 this, SLOT(on_actionSave_triggered()));
120 QMenu *save_menu = new QMenu();
121 connect(save_menu, SIGNAL(triggered(QAction*)),
122 this, SLOT(on_actionSave_triggered(QAction*)));
124 for (int i = 0; i < SaveTypeCount; i++) {
125 QAction *const action = save_menu->addAction(tr(SaveTypeNames[i]));
126 action->setData(qVariantFromValue(i));
129 save_button_->setMenu(save_menu);
130 save_button_->setDefaultAction(save_action_);
131 save_button_->setPopupMode(QToolButton::MenuButtonPopup);
133 parent->setSizePolicy(hex_view_->sizePolicy()); // TODO Must be updated when selected widget changes
142 ViewType View::get_type() const
144 return ViewTypeDecoderOutput;
147 void View::reset_view_state()
149 ViewBase::reset_view_state();
151 decoder_selector_->clear();
152 class_selector_->clear();
153 format_selector_->setCurrentIndex(0);
154 save_button_->setEnabled(false);
159 void View::clear_decode_signals()
161 ViewBase::clear_decode_signals();
167 void View::add_decode_signal(shared_ptr<data::DecodeSignal> signal)
169 ViewBase::add_decode_signal(signal);
171 connect(signal.get(), SIGNAL(name_changed(const QString&)),
172 this, SLOT(on_signal_name_changed(const QString&)));
173 connect(signal.get(), SIGNAL(decoder_stacked(void*)),
174 this, SLOT(on_decoder_stacked(void*)));
175 connect(signal.get(), SIGNAL(decoder_removed(void*)),
176 this, SLOT(on_decoder_removed(void*)));
178 // Add all decoders provided by this signal
179 auto stack = signal->decoder_stack();
180 if (stack.size() > 1) {
181 for (const shared_ptr<Decoder>& dec : stack)
182 // Only add the decoder if it has binary output
183 if (dec->get_binary_class_count() > 0) {
184 QString title = QString("%1 (%2)").arg(signal->name(), dec->name());
185 decoder_selector_->addItem(title, QVariant::fromValue((void*)dec.get()));
188 if (!stack.empty()) {
189 shared_ptr<Decoder>& dec = stack.at(0);
190 if (dec->get_binary_class_count() > 0)
191 decoder_selector_->addItem(signal->name(), QVariant::fromValue((void*)dec.get()));
195 void View::remove_decode_signal(shared_ptr<data::DecodeSignal> signal)
197 // Remove all decoders provided by this signal
198 for (const shared_ptr<Decoder>& dec : signal->decoder_stack()) {
199 int index = decoder_selector_->findData(QVariant::fromValue((void*)dec.get()));
202 decoder_selector_->removeItem(index);
205 ViewBase::remove_decode_signal(signal);
207 if (signal.get() == signal_) {
214 void View::save_settings(QSettings &settings) const
219 void View::restore_settings(QSettings &settings)
221 // Note: It is assumed that this function is only called once,
222 // immediately after restoring a previous session.
226 void View::reset_data()
231 binary_data_exists_ = false;
236 void View::update_data()
241 const DecodeBinaryClass* bin_class =
242 signal_->get_binary_data_class(current_segment_, decoder_, bin_class_id_);
244 hex_view_->set_data(bin_class);
246 if (!binary_data_exists_)
249 if (!save_button_->isEnabled())
250 save_button_->setEnabled(true);
253 void View::save_data() const
261 GlobalSettings settings;
262 const QString dir = settings.value("MainWindow/SaveDirectory").toString();
264 const QString file_name = QFileDialog::getSaveFileName(
265 parent_, tr("Save Binary Data"), dir, tr("Binary Data Files (*.bin);;All Files (*)"));
267 if (file_name.isEmpty())
270 QFile file(file_name);
271 if (file.open(QIODevice::WriteOnly | QIODevice::Truncate)) {
272 pair<size_t, size_t> selection = hex_view_->get_selection();
274 vector<uint8_t> data;
275 signal_->get_merged_binary_data_chunks_by_offset(current_segment_, decoder_,
276 bin_class_id_, selection.first, selection.second, &data);
278 int64_t bytes_written = file.write((const char*)data.data(), data.size());
280 if ((bytes_written == -1) || ((uint64_t)bytes_written != data.size())) {
281 QMessageBox msg(parent_);
282 msg.setText(tr("Error") + "\n\n" + tr("File %1 could not be written to.").arg(file_name));
283 msg.setStandardButtons(QMessageBox::Ok);
284 msg.setIcon(QMessageBox::Warning);
291 void View::save_data_as_hex_dump(bool with_offset, bool with_ascii) const
299 GlobalSettings settings;
300 const QString dir = settings.value("MainWindow/SaveDirectory").toString();
302 const QString file_name = QFileDialog::getSaveFileName(
303 parent_, tr("Save Binary Data"), dir, tr("Hex Dumps (*.txt);;All Files (*)"));
305 if (file_name.isEmpty())
308 QFile file(file_name);
309 if (file.open(QIODevice::WriteOnly | QIODevice::Truncate | QIODevice::Text)) {
310 pair<size_t, size_t> selection = hex_view_->get_selection();
312 vector<uint8_t> data;
313 signal_->get_merged_binary_data_chunks_by_offset(current_segment_, decoder_,
314 bin_class_id_, selection.first, selection.second, &data);
316 QTextStream out_stream(&file);
318 uint64_t offset = selection.first;
319 unsigned int n = hex_view_->get_bytes_per_line();
322 while (offset < selection.second) {
323 size_t end = std::min(selection.second, offset + n);
324 offset = hex_view_->create_hex_line(offset, end, &s, with_offset, with_ascii);
325 out_stream << s << endl;
330 if (out_stream.status() != QTextStream::Ok) {
331 QMessageBox msg(parent_);
332 msg.setText(tr("Error") + "\n\n" + tr("File %1 could not be written to.").arg(file_name));
333 msg.setStandardButtons(QMessageBox::Ok);
334 msg.setIcon(QMessageBox::Warning);
341 void View::on_selected_decoder_changed(int index)
344 disconnect(signal_, SIGNAL(new_binary_data(unsigned int, void*, unsigned int)));
348 decoder_ = (Decoder*)decoder_selector_->itemData(index).value<void*>();
350 // Find the signal that contains the selected decoder
351 for (const shared_ptr<DecodeSignal>& ds : decode_signals_)
352 for (const shared_ptr<Decoder>& dec : ds->decoder_stack())
353 if (decoder_ == dec.get())
356 class_selector_->clear();
359 // Populate binary class selector
360 uint32_t bin_classes = decoder_->get_binary_class_count();
361 for (uint32_t i = 0; i < bin_classes; i++) {
362 const data::decode::DecodeBinaryClassInfo* class_info = decoder_->get_binary_class(i);
363 class_selector_->addItem(class_info->description, QVariant::fromValue(i));
366 connect(signal_, SIGNAL(new_binary_data(unsigned int, void*, unsigned int)),
367 this, SLOT(on_new_binary_data(unsigned int, void*, unsigned int)));
373 void View::on_selected_class_changed(int index)
375 bin_class_id_ = class_selector_->itemData(index).value<uint32_t>();
377 binary_data_exists_ =
378 signal_->get_binary_data_chunk_count(current_segment_, decoder_, bin_class_id_);
383 void View::on_signal_name_changed(const QString &name)
387 SignalBase* sb = qobject_cast<SignalBase*>(QObject::sender());
390 DecodeSignal* signal = dynamic_cast<DecodeSignal*>(sb);
393 // Update all decoder entries provided by this signal
394 auto stack = signal->decoder_stack();
395 if (stack.size() > 1) {
396 for (const shared_ptr<Decoder>& dec : stack) {
397 QString title = QString("%1 (%2)").arg(signal->name(), dec->name());
398 int index = decoder_selector_->findData(QVariant::fromValue((void*)dec.get()));
401 decoder_selector_->setItemText(index, title);
404 if (!stack.empty()) {
405 shared_ptr<Decoder>& dec = stack.at(0);
406 int index = decoder_selector_->findData(QVariant::fromValue((void*)dec.get()));
409 decoder_selector_->setItemText(index, signal->name());
413 void View::on_new_binary_data(unsigned int segment_id, void* decoder, unsigned int bin_class_id)
415 if ((segment_id == current_segment_) && (decoder == decoder_) && (bin_class_id == bin_class_id_))
416 if (!delayed_view_updater_.isActive())
417 delayed_view_updater_.start();
420 void View::on_decoder_stacked(void* decoder)
422 // TODO This doesn't change existing entries for the same signal - but it should as the naming scheme may change
424 Decoder* d = static_cast<Decoder*>(decoder);
426 // Only add the decoder if it has binary output
427 if (d->get_binary_class_count() == 0)
430 // Find the signal that contains the selected decoder
431 DecodeSignal* signal = nullptr;
433 for (const shared_ptr<DecodeSignal>& ds : decode_signals_)
434 for (const shared_ptr<Decoder>& dec : ds->decoder_stack())
440 // Add the decoder to the list
441 QString title = QString("%1 (%2)").arg(signal->name(), d->name());
442 decoder_selector_->addItem(title, QVariant::fromValue((void*)d));
445 void View::on_decoder_removed(void* decoder)
447 Decoder* d = static_cast<Decoder*>(decoder);
449 // Remove the decoder from the list
450 int index = decoder_selector_->findData(QVariant::fromValue((void*)d));
453 decoder_selector_->removeItem(index);
456 void View::on_actionSave_triggered(QAction* action)
458 int save_type = SaveTypeBinary;
460 save_type = action->data().toInt();
464 case SaveTypeBinary: save_data(); break;
465 case SaveTypeHexDumpPlain: save_data_as_hex_dump(false, false); break;
466 case SaveTypeHexDumpWithOffset: save_data_as_hex_dump(true, false); break;
467 case SaveTypeHexDumpComplete: save_data_as_hex_dump(true, true); break;
471 void View::perform_delayed_view_update()
473 if (!binary_data_exists_)
474 if (signal_->get_binary_data_chunk_count(current_segment_, decoder_, bin_class_id_))
475 binary_data_exists_ = true;
481 } // namespace decoder_output