]> sigrok.org Git - pulseview.git/blobdiff - pv/view/logicsignal.cpp
Signal: Save and load signal names as UTF-8 strings.
[pulseview.git] / pv / view / logicsignal.cpp
index 5beb39df4fa6b4f126686b97aec6d35ecab731a2..fc81a4a92668663b26a9cf2ad7fa0113f72258f4 100644 (file)
@@ -31,7 +31,7 @@
 #include "logicsignal.hpp"
 #include "view.hpp"
 
-#include <pv/sigsession.hpp>
+#include <pv/session.hpp>
 #include <pv/devicemanager.hpp>
 #include <pv/data/logic.hpp>
 #include <pv/data/logicsnapshot.hpp>
@@ -81,8 +81,24 @@ const QColor LogicSignal::SignalColours[10] = {
        QColor(0xEE, 0xEE, 0xEC),       // White
 };
 
+QColor LogicSignal::TriggerMarkerBackgroundColour = QColor(0xED, 0xD4, 0x00);
+const int LogicSignal::TriggerMarkerPadding = 2;
+const char* LogicSignal::TriggerMarkerIcons[8] = {
+       nullptr,
+       ":/icons/trigger-marker-low.svg",
+       ":/icons/trigger-marker-high.svg",
+       ":/icons/trigger-marker-rising.svg",
+       ":/icons/trigger-marker-falling.svg",
+       ":/icons/trigger-marker-change.svg",
+       nullptr,
+       nullptr
+};
+
+QCache<QString, const QIcon> LogicSignal::icon_cache_;
+QCache<QString, const QPixmap> LogicSignal::pixmap_cache_;
+
 LogicSignal::LogicSignal(
-       pv::SigSession &session,
+       pv::Session &session,
        shared_ptr<Device> device,
        shared_ptr<Channel> channel,
        shared_ptr<data::Logic> data) :
@@ -220,6 +236,45 @@ void LogicSignal::paint_mid(QPainter &p, int left, int right)
        delete[] cap_lines;
 }
 
+void LogicSignal::paint_fore(QPainter &p, int left, int right)
+{
+       (void)left;
+
+       // Draw the trigger marker
+       if (!trigger_match_)
+               return;
+
+       const int y = get_visual_y();
+       const vector<int32_t> trig_types = get_trigger_types();
+       for (int32_t type_id : trig_types) {
+               const TriggerMatchType *const type =
+                       TriggerMatchType::get(type_id);
+               if (trigger_match_ != type || type_id < 0 ||
+                       (size_t)type_id >= countof(TriggerMarkerIcons) ||
+                       !TriggerMarkerIcons[type_id])
+                       continue;
+
+               const QPixmap *const pixmap = get_pixmap(
+                       TriggerMarkerIcons[type_id]);
+               if (!pixmap)
+                       continue;
+
+               const int pad = TriggerMarkerPadding;
+               const QSize size = pixmap->size();
+               const QPoint point(
+                       right - size.width() - pad * 2,
+                       y - (SignalHeight + size.height()) / 2);
+
+               p.setPen(QPen(Qt::NoPen));
+               p.setBrush(TriggerMarkerBackgroundColour);
+               p.drawRoundedRect(QRect(point, size).adjusted(
+                       -pad, -pad, pad, pad), pad, pad);
+               p.drawPixmap(point, *pixmap);
+
+               break;
+       }
+}
+
 void LogicSignal::paint_caps(QPainter &p, QLineF *const lines,
        vector< pair<int64_t, bool> > &edges, bool level,
        double samples_per_pixel, double pixels_offset, float x_offset,
@@ -241,38 +296,50 @@ void LogicSignal::paint_caps(QPainter &p, QLineF *const lines,
 
 void LogicSignal::init_trigger_actions(QWidget *parent)
 {
-       trigger_none_ = new QAction(QIcon(":/icons/trigger-none.svg"),
+       trigger_none_ = new QAction(*get_icon(":/icons/trigger-none.svg"),
                tr("No trigger"), parent);
        trigger_none_->setCheckable(true);
        connect(trigger_none_, SIGNAL(triggered()), this, SLOT(on_trigger()));
 
-       trigger_rising_ = new QAction(QIcon(":/icons/trigger-rising.svg"),
+       trigger_rising_ = new QAction(*get_icon(":/icons/trigger-rising.svg"),
                tr("Trigger on rising edge"), parent);
        trigger_rising_->setCheckable(true);
        connect(trigger_rising_, SIGNAL(triggered()), this, SLOT(on_trigger()));
 
-       trigger_high_ = new QAction(QIcon(":/icons/trigger-high.svg"),
+       trigger_high_ = new QAction(*get_icon(":/icons/trigger-high.svg"),
                tr("Trigger on high level"), parent);
        trigger_high_->setCheckable(true);
        connect(trigger_high_, SIGNAL(triggered()), this, SLOT(on_trigger()));
 
-       trigger_falling_ = new QAction(QIcon(":/icons/trigger-falling.svg"),
+       trigger_falling_ = new QAction(*get_icon(":/icons/trigger-falling.svg"),
                tr("Trigger on falling edge"), parent);
        trigger_falling_->setCheckable(true);
        connect(trigger_falling_, SIGNAL(triggered()), this, SLOT(on_trigger()));
 
-       trigger_low_ = new QAction(QIcon(":/icons/trigger-low.svg"),
+       trigger_low_ = new QAction(*get_icon(":/icons/trigger-low.svg"),
                tr("Trigger on low level"), parent);
        trigger_low_->setCheckable(true);
        connect(trigger_low_, SIGNAL(triggered()), this, SLOT(on_trigger()));
 
-       trigger_change_ = new QAction(QIcon(":/icons/trigger-change.svg"),
+       trigger_change_ = new QAction(*get_icon(":/icons/trigger-change.svg"),
                tr("Trigger on rising or falling edge"), parent);
        trigger_change_->setCheckable(true);
        connect(trigger_change_, SIGNAL(triggered()), this, SLOT(on_trigger()));
 }
 
-QAction* LogicSignal::match_action(const TriggerMatchType *type)
+const vector<int32_t> LogicSignal::get_trigger_types() const
+{
+       try {
+               const Glib::VariantContainerBase gvar =
+                       device_->config_list(ConfigKey::TRIGGER_MATCH);
+               return Glib::VariantBase::cast_dynamic<
+                       Glib::Variant<vector<int32_t>>>(gvar).get();
+       } catch (Error e) {
+               return vector<int32_t>();
+       }
+}
+
+QAction* LogicSignal::action_from_trigger_type(const TriggerMatchType *type)
 {
        QAction *action;
 
@@ -302,7 +369,7 @@ QAction* LogicSignal::match_action(const TriggerMatchType *type)
        return action;
 }
 
-const TriggerMatchType *LogicSignal::action_match(QAction *action)
+const TriggerMatchType *LogicSignal::trigger_type_from_action(QAction *action)
 {
        if (action == trigger_low_)
                return TriggerMatchType::ZERO;
@@ -320,28 +387,20 @@ const TriggerMatchType *LogicSignal::action_match(QAction *action)
 
 void LogicSignal::populate_popup_form(QWidget *parent, QFormLayout *form)
 {
-       Glib::VariantContainerBase gvar;
-       vector<int32_t> trig_types;
-
        Signal::populate_popup_form(parent, form);
 
-       try {
-               gvar = device_->config_list(ConfigKey::TRIGGER_MATCH);
-       } catch (Error e) {
-               return;
-       }
-
        trigger_bar_ = new QToolBar(parent);
        init_trigger_actions(trigger_bar_);
        trigger_bar_->addAction(trigger_none_);
        trigger_none_->setChecked(!trigger_match_);
-       trig_types =
-               Glib::VariantBase::cast_dynamic<Glib::Variant<vector<int32_t>>>(
-                       gvar).get();
+
+       const vector<int32_t> trig_types = get_trigger_types();
        for (auto type_id : trig_types) {
-               auto type = TriggerMatchType::get(type_id);
-               trigger_bar_->addAction(match_action(type));
-               match_action(type)->setChecked(trigger_match_ == type);
+               const TriggerMatchType *const type =
+                       TriggerMatchType::get(type_id);
+               QAction *const action = action_from_trigger_type(type);
+               trigger_bar_->addAction(action);
+               action->setChecked(trigger_match_ == type);
        }
        form->addRow(tr("Trigger"), trigger_bar_);
 
@@ -374,17 +433,42 @@ void LogicSignal::modify_trigger()
 
        session_.session()->set_trigger(
                new_trigger->stages().empty() ? nullptr : new_trigger);
+
+       if (owner_)
+               owner_->appearance_changed(false, true);
+}
+
+const QIcon* LogicSignal::get_icon(const char *path)
+{
+       const QIcon *icon = icon_cache_.take(path);
+       if (!icon) {
+               icon = new QIcon(path);
+               icon_cache_.insert(path, icon);
+       }
+
+       return icon;
+}
+
+const QPixmap* LogicSignal::get_pixmap(const char *path)
+{
+       const QPixmap *pixmap = pixmap_cache_.take(path);
+       if (!pixmap) {
+               pixmap = new QPixmap(path);
+               pixmap_cache_.insert(path, pixmap);
+       }
+
+       return pixmap;
 }
 
 void LogicSignal::on_trigger()
 {
        QAction *action;
 
-       match_action(trigger_match_)->setChecked(false);
+       action_from_trigger_type(trigger_match_)->setChecked(false);
 
        action = (QAction *)sender();
        action->setChecked(true);
-       trigger_match_ = action_match(action);
+       trigger_match_ = trigger_type_from_action(action);
 
        modify_trigger();
 }