Added a label context bar action
[pulseview.git] / pv / view / signal.cpp
index 02732ffc7a19112d3e6fc236b7beea57ccf6ed8c..bb86d8cb6d0add9c3585c8690dee22c60d06579a 100644 (file)
 
 #include <extdef.h>
 
+#include <assert.h>
+#include <math.h>
+
+#include <QApplication>
+
 #include "signal.h"
 #include "view.h"
 
@@ -28,10 +33,44 @@ namespace view {
 
 const int Signal::LabelHitPadding = 2;
 
-Signal::Signal(QString name) :
-       _name(name),
-       _v_offset(0)
+const QPen Signal::SignalAxisPen(QColor(128, 128, 128, 64));
+
+const char *const ProbeNames[] = {
+       "CLK",
+       "DATA",
+       "IN",
+       "OUT",
+       "RST",
+       "Tx",
+       "Rx",
+       "EN",
+       "SCLK",
+       "MOSI",
+       "MISO",
+       "/SS",
+       "SDA",
+       "SCL"
+};
+
+Signal::Signal(const sr_probe *const probe) :
+       _probe(probe),
+       _name(probe->name),
+       _v_offset(0),
+       _name_action(NULL),
+       _name_widget(),
+       _updating_name_widget(false)
 {
+       assert(_probe);
+
+       _name_action.setDefaultWidget(&_name_widget);
+
+       _name_widget.setEditable(true);
+       for(unsigned int i = 0; i < countof(ProbeNames); i++)
+               _name_widget.insertItem(i, ProbeNames[i]);
+       _name_widget.setEditText(probe->name);
+
+       connect(&_name_widget, SIGNAL(editTextChanged(const QString&)),
+               this, SLOT(on_text_changed(const QString&)));
 }
 
 QString Signal::get_name() const
@@ -42,6 +81,9 @@ QString Signal::get_name() const
 void Signal::set_name(QString name)
 {
        _name = name;
+       _updating_name_widget = true;
+       _name_widget.setEditText(name);
+       _updating_name_widget = false;
 }
 
 QColor Signal::get_colour() const
@@ -64,21 +106,23 @@ void Signal::set_v_offset(int v_offset)
        _v_offset = v_offset;
 }
 
-void Signal::paint_label(QPainter &p, const QRect &rect, bool hover)
+void Signal::paint_label(QPainter &p, int y, int right, bool hover)
 {
        p.setBrush(_colour);
 
+       if (!_probe->enabled)
+               return;
+
        const QColor colour = get_colour();
-       const float nominal_offset = get_nominal_offset(rect);
 
        compute_text_size(p);
-       const QRectF label_rect = get_label_rect(rect);
+       const QRectF label_rect = get_label_rect(y, right);
 
        // Paint the label
        const QPointF points[] = {
                label_rect.topLeft(),
                label_rect.topRight(),
-               QPointF(rect.right(), nominal_offset),
+               QPointF(right, y),
                label_rect.bottomRight(),
                label_rect.bottomLeft()
        };
@@ -86,11 +130,17 @@ void Signal::paint_label(QPainter &p, const QRect &rect, bool hover)
        const QPointF highlight_points[] = {
                QPointF(label_rect.left() + 1, label_rect.top() + 1),
                QPointF(label_rect.right(), label_rect.top() + 1),
-               QPointF(rect.right() - 1, nominal_offset),
+               QPointF(right - 1, y),
                QPointF(label_rect.right(), label_rect.bottom() - 1),
                QPointF(label_rect.left() + 1, label_rect.bottom() - 1)
        };
 
+       if (selected()) {
+               p.setPen(highlight_pen());
+               p.setBrush(Qt::transparent);
+               p.drawPolygon(points, countof(points));
+       }
+
        p.setPen(Qt::transparent);
        p.setBrush(hover ? colour.lighter() : colour);
        p.drawPolygon(points, countof(points));
@@ -108,37 +158,51 @@ void Signal::paint_label(QPainter &p, const QRect &rect, bool hover)
        p.drawText(label_rect, Qt::AlignCenter | Qt::AlignVCenter, _name);
 }
 
-bool Signal::pt_in_label_rect(const QRect &rect, const QPoint &point)
+bool Signal::pt_in_label_rect(int y, int left, int right,
+       const QPoint &point)
 {
-       const QRectF label = get_label_rect(rect);
+       (void)left;
+
+       const QRectF label = get_label_rect(y, right);
        return QRectF(
                QPointF(label.left() - LabelHitPadding,
                        label.top() - LabelHitPadding),
-               QPointF(rect.right(),
-                       label.bottom() + LabelHitPadding)
-               ).contains(point);
+               QPointF(right, label.bottom() + LabelHitPadding)
+                       ).contains(point);
+}
+
+void Signal::paint_axis(QPainter &p, int y, int left, int right)
+{
+       p.setPen(SignalAxisPen);
+       p.drawLine(QPointF(left, y + 0.5f), QPointF(right, y + 0.5f));
 }
 
 void Signal::compute_text_size(QPainter &p)
 {
-       _text_size = p.boundingRect(QRectF(), 0, _name).size();
+       _text_size = QSize(
+               p.boundingRect(QRectF(), 0, _name).width(),
+               p.boundingRect(QRectF(), 0, "Tg").height());
 }
 
-QRectF Signal::get_label_rect(const QRect &rect)
+QRectF Signal::get_label_rect(int y, int right)
 {
        using pv::view::View;
 
-       const float nominal_offset = get_nominal_offset(rect) + 0.5;
        const QSizeF label_size(
                _text_size.width() + View::LabelPadding.width() * 2,
-               _text_size.height() + View::LabelPadding.height() * 2);
+               ceilf((_text_size.height() + View::LabelPadding.height() * 2) / 2) * 2);
        const float label_arrow_length = label_size.height() / 2;
        return QRectF(
-               rect.right() - label_arrow_length -
-                       label_size.width() - 0.5,
-               nominal_offset - label_size.height() / 2,
+               right - label_arrow_length - label_size.width() - 0.5,
+               y + 0.5f - label_size.height() / 2,
                label_size.width(), label_size.height());
 }
 
+void Signal::on_text_changed(const QString &text)
+{
+       _name = text;
+       text_changed();
+}
+
 } // namespace view
 } // namespace pv