X-Git-Url: https://sigrok.org/gitweb/?a=blobdiff_plain;f=pv%2Fviews%2Fdecoder_output%2Fview.cpp;h=8bce3745f1aa3e83ef932f3c05f5f45a6910e817;hb=628b45cc0ba7ac38ac6a003fedbbc746f15dd724;hp=8da1e84ae73499c506cff31e3bbf63e5d7c69e39;hpb=db29815836442ac83d453b3de2bf1755e9fd89f0;p=pulseview.git diff --git a/pv/views/decoder_output/view.cpp b/pv/views/decoder_output/view.cpp index 8da1e84a..8bce3745 100644 --- a/pv/views/decoder_output/view.cpp +++ b/pv/views/decoder_output/view.cpp @@ -17,20 +17,32 @@ * along with this program; if not, see . */ -#include +#include +#include +#include +#include #include #include #include +#include + #include "view.hpp" +#include "QHexView.hpp" #include "pv/session.hpp" #include "pv/util.hpp" +#include "pv/data/decode/decoder.hpp" +using pv::data::DecodeSignal; +using pv::data::SignalBase; +using pv::data::decode::Decoder; using pv::util::TimeUnit; using pv::util::Timestamp; +using std::dynamic_pointer_cast; +using std::numeric_limits; using std::shared_ptr; namespace pv { @@ -38,17 +50,48 @@ namespace views { namespace decoder_output { View::View(Session &session, bool is_main_view, QMainWindow *parent) : - ViewBase(session, is_main_view, parent) + ViewBase(session, is_main_view, parent), // Note: Place defaults in View::reset_view_state(), not here + decoder_selector_(new QComboBox()), + format_selector_(new QComboBox()), + class_selector_(new QComboBox()), + stacked_widget_(new QStackedWidget()), + hex_view_(new QHexView()), + signal_(nullptr) { QVBoxLayout *root_layout = new QVBoxLayout(this); root_layout->setContentsMargins(0, 0, 0, 0); - QToolBar* tool_bar = new QToolBar(); - tool_bar->setContextMenuPolicy(Qt::PreventContextMenu); + // Create toolbar + QToolBar* toolbar = new QToolBar(); + toolbar->setContextMenuPolicy(Qt::PreventContextMenu); + parent->addToolBar(toolbar); + + // Populate toolbar + toolbar->addWidget(new QLabel(tr("Decoder:"))); + toolbar->addWidget(decoder_selector_); + toolbar->addWidget(class_selector_); + toolbar->addSeparator(); + toolbar->addWidget(new QLabel(tr("Show data as"))); + toolbar->addWidget(format_selector_); + + // Add format types + format_selector_->addItem(tr("Hexdump"), qVariantFromValue(QString("text/hexdump"))); + + // Add widget stack + root_layout->addWidget(stacked_widget_); + stacked_widget_->addWidget(hex_view_); + stacked_widget_->setCurrentIndex(0); + + connect(decoder_selector_, SIGNAL(currentIndexChanged(int)), + this, SLOT(on_selected_decoder_changed(int))); + connect(class_selector_, SIGNAL(currentIndexChanged(int)), + this, SLOT(on_selected_class_changed(int))); - parent->addToolBar(tool_bar); + // Configure widgets + decoder_selector_->setSizeAdjustPolicy(QComboBox::AdjustToContents); + class_selector_->setSizeAdjustPolicy(QComboBox::AdjustToContents); reset_view_state(); } @@ -70,21 +113,65 @@ void View::reset_view_state() void View::clear_signals() { ViewBase::clear_signalbases(); + signal_ = nullptr; } void View::clear_decode_signals() { + ViewBase::clear_decode_signals(); + + decoder_selector_->clear(); + class_selector_->clear(); + format_selector_->setCurrentIndex(0); + signal_ = nullptr; } void View::add_decode_signal(shared_ptr signal) { + ViewBase::add_decode_signal(signal); + connect(signal.get(), SIGNAL(name_changed(const QString&)), - this, SLOT(on_signal_name_changed())); + this, SLOT(on_signal_name_changed(const QString&))); + connect(signal.get(), SIGNAL(decoder_stacked(void*)), + this, SLOT(on_decoder_stacked(void*))); + connect(signal.get(), SIGNAL(decoder_removed(void*)), + this, SLOT(on_decoder_removed(void*))); + + // Add all decoders provided by this signal + auto stack = signal->decoder_stack(); + if (stack.size() > 1) { + for (const shared_ptr& dec : stack) + // Only add the decoder if it has binary output + if (dec->get_binary_class_count() > 0) { + QString title = QString("%1 (%2)").arg(signal->name(), dec->name()); + decoder_selector_->addItem(title, QVariant::fromValue((void*)dec.get())); + } + } else + if (!stack.empty()) { + shared_ptr& dec = stack.at(0); + if (dec->get_binary_class_count() > 0) + decoder_selector_->addItem(signal->name(), QVariant::fromValue((void*)dec.get())); + } } void View::remove_decode_signal(shared_ptr signal) { - (void)signal; + // Remove all decoders provided by this signal + for (const shared_ptr& dec : signal->decoder_stack()) { + int index = decoder_selector_->findData(QVariant::fromValue((void*)dec.get())); + + if (index != -1) + decoder_selector_->removeItem(index); + } + + ViewBase::remove_decode_signal(signal); + + if (signal.get() == signal_) { + signal_ = nullptr; + decoder_ = nullptr; + bin_class_id_ = 0; + update_data(); + } } void View::save_settings(QSettings &settings) const @@ -99,10 +186,136 @@ void View::restore_settings(QSettings &settings) (void)settings; } -void View::on_signal_name_changed() +void View::update_data() +{ + if (!signal_) { + hex_view_->clear(); + return; + } + + if (signal_->get_binary_data_chunk_count(current_segment_, decoder_, bin_class_id_) == 0) { + hex_view_->clear(); + return; + } + + const DecodeBinaryClass* bin_class = + signal_->get_binary_data_class(current_segment_, decoder_, bin_class_id_); + + hex_view_->setData(bin_class); +} + +void View::on_selected_decoder_changed(int index) +{ + if (signal_) + disconnect(signal_, SIGNAL(new_binary_data(unsigned int, void*, unsigned int))); + + decoder_ = (Decoder*)decoder_selector_->itemData(index).value(); + + // Find the signal that contains the selected decoder + signal_ = nullptr; + + for (const shared_ptr& ds : decode_signals_) + for (const shared_ptr& dec : ds->decoder_stack()) + if (decoder_ == dec.get()) + signal_ = ds.get(); + + class_selector_->clear(); + + if (signal_) { + // Populate binary class selector + uint8_t bin_classes = decoder_->get_binary_class_count(); + for (uint8_t i = 0; i < bin_classes; i++) { + const data::decode::DecodeBinaryClassInfo* class_info = decoder_->get_binary_class(i); + class_selector_->addItem(class_info->name, 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) +{ + bin_class_id_ = class_selector_->itemData(index).value(); + + update_data(); +} + +void View::on_signal_name_changed(const QString &name) +{ + (void)name; + + SignalBase* sb = qobject_cast(QObject::sender()); + assert(sb); + + DecodeSignal* signal = dynamic_cast(sb); + assert(signal); + + // Update all decoder entries provided by this signal + auto stack = signal->decoder_stack(); + if (stack.size() > 1) { + for (const shared_ptr& dec : stack) { + QString title = QString("%1 (%2)").arg(signal->name(), dec->name()); + int index = decoder_selector_->findData(QVariant::fromValue((void*)dec.get())); + + if (index != -1) + decoder_selector_->setItemText(index, title); + } + } else + if (!stack.empty()) { + shared_ptr& dec = stack.at(0); + int index = decoder_selector_->findData(QVariant::fromValue((void*)dec.get())); + + if (index != -1) + decoder_selector_->setItemText(index, signal->name()); + } +} + +void View::on_new_binary_data(unsigned int segment_id, void* decoder, unsigned int bin_class_id) +{ + if ((segment_id == current_segment_) && (decoder == decoder_) && (bin_class_id == bin_class_id_)) + update_data(); +} + +void View::on_decoder_stacked(void* decoder) +{ + // TODO This doesn't change existing entries for the same signal - but it should as the naming scheme may change + + Decoder* d = static_cast(decoder); + + // Only add the decoder if it has binary output + if (d->get_binary_class_count() == 0) + return; + + // Find the signal that contains the selected decoder + DecodeSignal* signal = nullptr; + + for (const shared_ptr& ds : decode_signals_) + for (const shared_ptr& dec : ds->decoder_stack()) + if (d == dec.get()) + signal = ds.get(); + + assert(signal); + + // Add the decoder to the list + QString title = QString("%1 (%2)").arg(signal->name(), d->name()); + decoder_selector_->addItem(title, QVariant::fromValue((void*)d)); +} + +void View::on_decoder_removed(void* decoder) { + Decoder* d = static_cast(decoder); + + // Remove the decoder from the list + int index = decoder_selector_->findData(QVariant::fromValue((void*)d)); + + if (index != -1) + decoder_selector_->removeItem(index); } + } // namespace decoder_output } // namespace views } // namespace pv