+void View::reset_data()
+{
+ signal_ = nullptr;
+ decoder_ = nullptr;
+ bin_class_id_ = 0;
+ binary_data_exists_ = false;
+
+ hex_view_->clear();
+}
+
+void View::update_data()
+{
+ if (!signal_)
+ return;
+
+ const DecodeBinaryClass* bin_class =
+ signal_->get_binary_data_class(current_segment_, decoder_, bin_class_id_);
+
+ hex_view_->set_data(bin_class);
+
+ if (!binary_data_exists_)
+ return;
+
+ if (!save_button_->isEnabled())
+ save_button_->setEnabled(true);
+}
+
+void View::save_data() const
+{
+ assert(decoder_);
+ assert(signal_);
+
+ if (!signal_)
+ return;
+
+ GlobalSettings settings;
+ const QString dir = settings.value("MainWindow/SaveDirectory").toString();
+
+ const QString file_name = QFileDialog::getSaveFileName(
+ parent_, tr("Save Binary Data"), dir, tr("Binary Data Files (*.bin);;All Files (*)"));
+
+ if (file_name.isEmpty())
+ return;
+
+ QFile file(file_name);
+ if (file.open(QIODevice::WriteOnly | QIODevice::Truncate)) {
+ pair<size_t, size_t> selection = hex_view_->get_selection();
+
+ vector<uint8_t> data;
+ data.resize(selection.second - selection.first + 1);
+
+ signal_->get_merged_binary_data_chunks_by_offset(current_segment_, decoder_,
+ bin_class_id_, selection.first, selection.second, &data);
+
+ int64_t bytes_written = file.write((const char*)data.data(), data.size());
+
+ if ((bytes_written == -1) || ((uint64_t)bytes_written != data.size())) {
+ QMessageBox msg(parent_);
+ msg.setText(tr("Error") + "\n\n" + tr("File %1 could not be written to.").arg(file_name));
+ msg.setStandardButtons(QMessageBox::Ok);
+ msg.setIcon(QMessageBox::Warning);
+ msg.exec();
+ return;
+ }
+ }
+}
+
+void View::save_data_as_hex_dump(bool with_offset, bool with_ascii) const
+{
+ assert(decoder_);
+ assert(signal_);
+
+ if (!signal_)
+ return;
+
+ GlobalSettings settings;
+ const QString dir = settings.value("MainWindow/SaveDirectory").toString();
+
+ const QString file_name = QFileDialog::getSaveFileName(
+ parent_, tr("Save Binary Data"), dir, tr("Hex Dumps (*.txt);;All Files (*)"));
+
+ if (file_name.isEmpty())
+ return;
+
+ QFile file(file_name);
+ if (file.open(QIODevice::WriteOnly | QIODevice::Truncate | QIODevice::Text)) {
+ pair<size_t, size_t> selection = hex_view_->get_selection();
+
+ vector<uint8_t> data;
+ data.resize(selection.second - selection.first + 1);
+
+ signal_->get_merged_binary_data_chunks_by_offset(current_segment_, decoder_,
+ bin_class_id_, selection.first, selection.second, &data);
+
+ QTextStream out_stream(&file);
+
+ uint64_t offset = selection.first;
+ uint64_t n = hex_view_->get_bytes_per_line();
+ QString s;
+
+ while (offset < selection.second) {
+ size_t end = std::min((uint64_t)(selection.second), offset + n);
+ offset = hex_view_->create_hex_line(offset, end, &s, with_offset, with_ascii);
+ out_stream << s << endl;
+ }
+
+ out_stream << endl;
+
+ if (out_stream.status() != QTextStream::Ok) {
+ QMessageBox msg(parent_);
+ msg.setText(tr("Error") + "\n\n" + tr("File %1 could not be written to.").arg(file_name));
+ msg.setStandardButtons(QMessageBox::Ok);
+ msg.setIcon(QMessageBox::Warning);
+ msg.exec();
+ return;
+ }
+ }
+}
+
+void View::on_selected_decoder_changed(int index)
+{
+ if (signal_)
+ disconnect(signal_, SIGNAL(new_binary_data(unsigned int, void*, unsigned int)));
+
+ reset_data();
+
+ decoder_ = (Decoder*)decoder_selector_->itemData(index).value<void*>();
+
+ // Find the signal that contains the selected decoder
+ for (const shared_ptr<DecodeSignal>& ds : decode_signals_)
+ for (const shared_ptr<Decoder>& dec : ds->decoder_stack())
+ if (decoder_ == dec.get())
+ signal_ = ds.get();
+
+ class_selector_->clear();
+
+ if (signal_) {
+ // Populate binary class selector
+ uint32_t bin_classes = decoder_->get_binary_class_count();
+ for (uint32_t i = 0; i < bin_classes; i++) {
+ const data::decode::DecodeBinaryClassInfo* class_info = decoder_->get_binary_class(i);
+ class_selector_->addItem(class_info->description, QVariant::fromValue(i));
+ }
+
+ connect(signal_, SIGNAL(new_binary_data(unsigned int, void*, unsigned int)),
+ this, SLOT(on_new_binary_data(unsigned int, void*, unsigned int)));
+ }
+
+ update_data();
+}
+
+void View::on_selected_class_changed(int index)