]> sigrok.org Git - pulseview.git/commitdiff
Added mouse-over behaviour to signal labels
authorJoel Holdsworth <redacted>
Wed, 17 Oct 2012 19:31:21 +0000 (20:31 +0100)
committerJoel Holdsworth <redacted>
Sat, 20 Oct 2012 17:29:25 +0000 (18:29 +0100)
pv/signal.cpp
pv/signal.h
pv/view/header.cpp
pv/view/header.h

index 1a11cfeec1909d39c7a2ffeca54c24b91899dc80..7d3dd66362f4c5ab182b4503ab2e0294d5f93066 100644 (file)
@@ -25,6 +25,7 @@
 namespace pv {
 
 const QSizeF Signal::LabelPadding(4, 0);
+const int Signal::LabelHitPadding = 2;
 
 Signal::Signal(QString name) :
        _name(name)
@@ -36,23 +37,7 @@ QString Signal::get_name() const
        return _name;
 }
 
-QRectF Signal::get_label_rect(QPainter &p, const QRect &rect)
-{
-       const QSizeF text_size = p.boundingRect(
-               QRectF(0, 0, rect.width(), 0), 0, _name).size();
-
-       const float nominal_offset = get_nominal_offset(rect);
-       const QSizeF label_size(
-               text_size.width() + LabelPadding.width() * 2,
-               text_size.height() + LabelPadding.height() * 2);
-       const float label_arrow_length = label_size.height() / 2;
-       return QRectF(
-               rect.right() - label_arrow_length - label_size.width(),
-               nominal_offset - label_size.height() / 2,
-               label_size.width(), label_size.height());
-}
-
-void Signal::paint_label(QPainter &p, const QRect &rect)
+void Signal::paint_label(QPainter &p, const QRect &rect, bool hover)
 {
        p.setBrush(get_colour());
 
@@ -78,7 +63,7 @@ void Signal::paint_label(QPainter &p, const QRect &rect)
        };
 
        p.setPen(Qt::transparent);
-       p.setBrush(colour);
+       p.setBrush(hover ? colour.lighter() : colour);
        p.drawPolygon(points, countof(points));
 
        p.setPen(colour.lighter());
@@ -94,4 +79,32 @@ void Signal::paint_label(QPainter &p, const QRect &rect)
        p.drawText(label_rect, Qt::AlignCenter | Qt::AlignVCenter, _name);
 }
 
+bool Signal::pt_in_label_rect(QPainter &p,
+       const QRect &rect, const QPoint &point)
+{
+       const QRectF label = get_label_rect(p, rect);
+       return QRectF(
+               QPointF(label.left() - LabelHitPadding,
+                       label.top() - LabelHitPadding),
+               QPointF(rect.right(),
+                       label.bottom() + LabelHitPadding)
+               ).contains(point);
+}
+
+QRectF Signal::get_label_rect(QPainter &p, const QRect &rect)
+{
+       const QSizeF text_size = p.boundingRect(
+               QRectF(0, 0, rect.width(), 0), 0, _name).size();
+
+       const float nominal_offset = get_nominal_offset(rect);
+       const QSizeF label_size(
+               text_size.width() + LabelPadding.width() * 2,
+               text_size.height() + LabelPadding.height() * 2);
+       const float label_arrow_length = label_size.height() / 2;
+       return QRectF(
+               rect.right() - label_arrow_length - label_size.width(),
+               nominal_offset - label_size.height() / 2,
+               label_size.width(), label_size.height());
+}
+
 } // namespace pv
index 1b2f5df92a7221813e0dbb6afbdb7de74eac172c..8faff77c6a11ba1dc5550b4be9cb23527724f900 100644 (file)
@@ -34,6 +34,7 @@ class Signal
 {
 private:
        static const QSizeF LabelPadding;
+       static const int LabelHitPadding;
 
 protected:
        Signal(QString name);
@@ -52,6 +53,26 @@ public:
        virtual void paint(QPainter &p, const QRect &rect, double scale,
                double offset) = 0;
 
+
+       /**
+        * Paints the signal label into a QGLWidget.
+        * @param p the QPainter to paint into.
+        * @param rect the rectangular area to draw the label into.
+        * @param hover true if the label is being hovered over by the mouse.
+        */
+       virtual void paint_label(QPainter &p, const QRect &rect,
+               bool hover);
+
+       /**
+        * Determines if a point is in the header label rect.
+        * @param p the QPainter to paint into.
+        * @param rect the rectangular area to draw the label into.
+        * @param point the point to test.
+        */
+       bool pt_in_label_rect(QPainter &p, const QRect &rect,
+               const QPoint &point);
+
+private:
        /**
         * Computes the outline rectangle of a label.
         * @param p the QPainter to lay out text with.
@@ -59,16 +80,8 @@ public:
         * @return Returns the rectangle of the signal label.
         */
        virtual QRectF get_label_rect(QPainter &p, const QRect &rect);
-       
-       /**
-        * Paints the signal label into a QGLWidget.
-        * @param p the QPainter to paint into.
-        * @param rect the rectangular area to draw the label into.
-        */
-       virtual void paint_label(QPainter &p, const QRect &rect);
 
 protected:
-
        /**
         * Get the colour of the logic signal
         */
index aaa98ab85fb59ab6da9583ac277d758736850da3..4eebf1f5dcb25d29224a6885d850ab1f25b4a2e4 100644 (file)
@@ -28,6 +28,7 @@
 
 #include <boost/foreach.hpp>
 
+#include <QMouseEvent>
 #include <QPainter>
 #include <QRect>
 
@@ -41,6 +42,7 @@ Header::Header(View &parent) :
        QWidget(&parent),
        _view(parent)
 {
+       setMouseTracking(true);
 }
 
 void Header::paintEvent(QPaintEvent *event)
@@ -57,8 +59,12 @@ void Header::paintEvent(QPaintEvent *event)
        {
                assert(s);
 
-               const QRect label_rect(0, offset, w, View::SignalHeight);
-               s->paint_label(painter, label_rect);
+               const QRect signal_heading_rect(
+                       0, offset, w, View::SignalHeight);
+
+               s->paint_label(painter, signal_heading_rect,
+                       s->pt_in_label_rect(painter,
+                               signal_heading_rect, _mouse_point));
 
                offset += View::SignalHeight;
        }
@@ -66,5 +72,18 @@ void Header::paintEvent(QPaintEvent *event)
        painter.end();
 }
 
+void Header::mouseMoveEvent(QMouseEvent *event)
+{
+       assert(event);
+       _mouse_point = event->pos();
+       update();
+}
+
+void Header::leaveEvent(QEvent *event)
+{
+       _mouse_point = QPoint(-1, -1);
+       update();
+}
+
 } // namespace view
 } // namespace pv
index f7979c6d2ea2f70b60263ed888971fe5d175bfe5..5693a8a0d4f1bfc0d5a16e294d4f6d745e3c1a7b 100644 (file)
@@ -38,8 +38,15 @@ public:
 private:
        void paintEvent(QPaintEvent *event);
 
+private:
+       void mouseMoveEvent(QMouseEvent *event);
+
+       void leaveEvent(QEvent *event);
+
 private:
        View &_view;
+
+       QPoint _mouse_point;
 };
 
 } // namespace view