]> sigrok.org Git - pulseview.git/blobdiff - pv/view/decodetrace.cpp
Use SignalBase instead of LogicSignal for decoders
[pulseview.git] / pv / view / decodetrace.cpp
index b2ead1a66848600e1ae11425fe874d708b73f39a..b4201dd9c022005ce50d673c9ebc6747ee6c6d0a 100644 (file)
@@ -44,12 +44,13 @@ extern "C" {
 #include "decodetrace.hpp"
 
 #include <pv/session.hpp>
+#include <pv/strnatcmp.hpp>
 #include <pv/data/decoderstack.hpp>
 #include <pv/data/decode/decoder.hpp>
 #include <pv/data/logic.hpp>
 #include <pv/data/logicsegment.hpp>
 #include <pv/data/decode/annotation.hpp>
-#include <pv/view/logicsignal.hpp>
+#include <pv/view/signal.hpp>
 #include <pv/view/view.hpp>
 #include <pv/view/viewport.hpp>
 #include <pv/widgets/decodergroupbox.hpp>
@@ -128,10 +129,11 @@ const QColor DecodeTrace::OutlineColours[16] = {
 };
 
 DecodeTrace::DecodeTrace(pv::Session &session,
+       shared_ptr<data::SignalBase> signalbase,
        std::shared_ptr<pv::data::DecoderStack> decoder_stack, int index) :
-       Trace(QString::fromUtf8(
-               decoder_stack->stack().front()->decoder()->name)),
+       Trace(signalbase),
        session_(session),
+       signalbase_(signalbase),
        decoder_stack_(decoder_stack),
        row_height_(0),
        max_visible_rows_(0),
@@ -140,7 +142,12 @@ DecodeTrace::DecodeTrace(pv::Session &session,
 {
        assert(decoder_stack_);
 
-       set_colour(DecodeColours[index % countof(DecodeColours)]);
+       // Determine shortest string we want to see displayed in full
+       QFontMetrics m(QApplication::font());
+       min_useful_label_width_ = m.width("XX"); // e.g. two hex characters
+
+       signalbase_->set_name(QString::fromUtf8(decoder_stack->stack().front()->decoder()->name));
+       signalbase_->set_colour(DecodeColours[index % countof(DecodeColours)]);
 
        connect(decoder_stack_.get(), SIGNAL(new_decode_data()),
                this, SLOT(on_new_decode_data()));
@@ -164,7 +171,10 @@ pair<int, int> DecodeTrace::v_extents() const
 {
        const int row_height = (ViewItemPaintParams::text_height() * 6) / 4;
 
-       return make_pair(-row_height, row_height * max_visible_rows_);
+       // Make an empty decode trace appear symmetrical
+       const int row_count = max(1, max_visible_rows_);
+
+       return make_pair(-row_height, row_height * row_count);
 }
 
 void DecodeTrace::paint_back(QPainter &p, const ViewItemPaintParams &pp)
@@ -190,6 +200,9 @@ void DecodeTrace::paint_mid(QPainter &p, const ViewItemPaintParams &pp)
                return;
        }
 
+       // Set default pen to allow for text width calculation
+       p.setPen(Qt::black);
+
        // Iterate through the rows
        int y = get_visual_y();
        pair<uint64_t, uint64_t> sample_range = get_sample_range(
@@ -199,9 +212,7 @@ void DecodeTrace::paint_mid(QPainter &p, const ViewItemPaintParams &pp)
        const vector<Row> rows(decoder_stack_->get_visible_rows());
 
        visible_rows_.clear();
-       for (auto i : rows) {
-               const Row &row = i;
-
+       for (const Row& row : rows) {
                // Cache the row title widths
                int row_title_width;
                try {
@@ -229,13 +240,16 @@ void DecodeTrace::paint_mid(QPainter &p, const ViewItemPaintParams &pp)
 
                        y += row_height_;
 
-                       visible_rows_.push_back(i);
+                       visible_rows_.push_back(row);
                }
        }
 
        // Draw the hatching
        draw_unresolved_period(p, annotation_height, pp.left(), pp.right());
 
+       if ((int)visible_rows_.size() > max_visible_rows_)
+               owner_->extents_changed(false, true);
+
        // Update the maximum row count if needed
        max_visible_rows_ = std::max(max_visible_rows_, (int)visible_rows_.size());
 }
@@ -374,7 +388,7 @@ void DecodeTrace::draw_annotations(vector<pv::data::decode::Annotation> annotati
                bool a_is_separate = false;
 
                // Annotation wider than the threshold for a useful label width?
-               if (a_width > 20) {
+               if (a_width >= min_useful_label_width_) {
                        for (const QString &ann_text : a.annotations()) {
                                const int w = p.boundingRect(QRectF(), 0, ann_text).width();
                                // Annotation wide enough to fit a label? Don't put it in a block then
@@ -590,7 +604,7 @@ void DecodeTrace::draw_unresolved_period(QPainter &p, int h, int left,
        assert(decoder_stack_); 
 
        shared_ptr<Logic> data;
-       shared_ptr<LogicSignal> logic_signal;
+       shared_ptr<data::SignalBase> signalbase;
 
        const list< shared_ptr<Decoder> > &stack = decoder_stack_->stack();
 
@@ -599,8 +613,8 @@ void DecodeTrace::draw_unresolved_period(QPainter &p, int h, int left,
        // LogicSignals have the same data/segment
        for (const shared_ptr<Decoder> &dec : stack)
                if (dec && !dec->channels().empty() &&
-                       ((logic_signal = (*dec->channels().begin()).second)) &&
-                       ((data = logic_signal->logic_data())))
+                       ((signalbase = (*dec->channels().begin()).second)) &&
+                       ((data = signalbase->logic_data())))
                        break;
 
        if (!data || data->logic_segments().empty())
@@ -793,8 +807,8 @@ void DecodeTrace::create_decoder_form(int index,
                connect(combo, SIGNAL(currentIndexChanged(int)),
                        this, SLOT(on_channel_selected(int)));
                decoder_form->addRow(tr("<b>%1</b> (%2) *")
-                       .arg(QString::fromUtf8(pdch->name))
-                       .arg(QString::fromUtf8(pdch->desc)), combo);
+                       .arg(QString::fromUtf8(pdch->name),
+                            QString::fromUtf8(pdch->desc)), combo);
 
                const ChannelSelector s = {combo, dec, pdch};
                channel_selectors_.push_back(s);
@@ -808,8 +822,8 @@ void DecodeTrace::create_decoder_form(int index,
                connect(combo, SIGNAL(currentIndexChanged(int)),
                        this, SLOT(on_channel_selected(int)));
                decoder_form->addRow(tr("<b>%1</b> (%2)")
-                       .arg(QString::fromUtf8(pdch->name))
-                       .arg(QString::fromUtf8(pdch->desc)), combo);
+                       .arg(QString::fromUtf8(pdch->name),
+                            QString::fromUtf8(pdch->desc)), combo);
 
                const ChannelSelector s = {combo, dec, pdch};
                channel_selectors_.push_back(s);
@@ -837,7 +851,8 @@ QComboBox* DecodeTrace::create_channel_selector(
        vector< shared_ptr<Signal> > sig_list(sigs.begin(), sigs.end());
        std::sort(sig_list.begin(), sig_list.end(),
                [](const shared_ptr<Signal> &a, const shared_ptr<Signal> b) {
-                       return a->name().compare(b->name()) < 0; });
+                       return strnatcasecmp(a->base()->name().toStdString(),
+                               b->base()->name().toStdString()) < 0; });
 
        assert(decoder_stack_);
        const auto channel_iter = dec->channels().find(pdch);
@@ -851,12 +866,12 @@ QComboBox* DecodeTrace::create_channel_selector(
 
        for (const shared_ptr<view::Signal> &s : sig_list) {
                assert(s);
-               if (dynamic_pointer_cast<LogicSignal>(s) && s->enabled()) {
-                       selector->addItem(s->name(),
-                               qVariantFromValue((void*)s.get()));
+               if (s->base()->type() == sigrok::ChannelType::LOGIC && s->enabled()) {
+                       selector->addItem(s->base()->name(),
+                               qVariantFromValue((void*)s->base().get()));
 
                        if (channel_iter != dec->channels().end() &&
-                               (*channel_iter).second == s)
+                               (*channel_iter).second == s->base())
                                selector->setCurrentIndex(
                                        selector->count() - 1);
                }
@@ -869,7 +884,7 @@ void DecodeTrace::commit_decoder_channels(shared_ptr<data::decode::Decoder> &dec
 {
        assert(dec);
 
-       map<const srd_channel*, shared_ptr<LogicSignal> > channel_map;
+       map<const srd_channel*, shared_ptr<data::SignalBase> > channel_map;
 
        const unordered_set< shared_ptr<Signal> > sigs(session_.signals());
 
@@ -877,14 +892,13 @@ void DecodeTrace::commit_decoder_channels(shared_ptr<data::decode::Decoder> &dec
                if (s.decoder_ != dec)
                        break;
 
-               const LogicSignal *const selection =
-                       (LogicSignal*)s.combo_->itemData(
+               const data::SignalBase *const selection =
+                       (data::SignalBase*)s.combo_->itemData(
                                s.combo_->currentIndex()).value<void*>();
 
                for (shared_ptr<Signal> sig : sigs)
-                       if (sig.get() == selection) {
-                               channel_map[s.pdch_] =
-                                       dynamic_pointer_cast<LogicSignal>(sig);
+                       if (sig->base().get() == selection) {
+                               channel_map[s.pdch_] = sig->base();
                                break;
                        }
        }