From: Joel Holdsworth Date: Wed, 17 Oct 2012 19:31:21 +0000 (+0100) Subject: Added mouse-over behaviour to signal labels X-Git-Tag: pulseview-0.1.0~244 X-Git-Url: https://sigrok.org/gitaction?a=commitdiff_plain;h=a29bb7fb6aa3665c21da1e06a3d3c4ca80f4437f;p=pulseview.git Added mouse-over behaviour to signal labels --- diff --git a/pv/signal.cpp b/pv/signal.cpp index 1a11cfee..7d3dd663 100644 --- a/pv/signal.cpp +++ b/pv/signal.cpp @@ -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 diff --git a/pv/signal.h b/pv/signal.h index 1b2f5df9..8faff77c 100644 --- a/pv/signal.h +++ b/pv/signal.h @@ -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 */ diff --git a/pv/view/header.cpp b/pv/view/header.cpp index aaa98ab8..4eebf1f5 100644 --- a/pv/view/header.cpp +++ b/pv/view/header.cpp @@ -28,6 +28,7 @@ #include +#include #include #include @@ -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 diff --git a/pv/view/header.h b/pv/view/header.h index f7979c6d..5693a8a0 100644 --- a/pv/view/header.h +++ b/pv/view/header.h @@ -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