X-Git-Url: https://sigrok.org/gitweb/?p=pulseview.git;a=blobdiff_plain;f=pv%2Fsignal.cpp;h=04c579232e4c6a2966680b0ad68a39ddf796aacf;hp=1a11cfeec1909d39c7a2ffeca54c24b91899dc80;hb=b3b57abc4fcfea1471529e9d116eaf11905f6639;hpb=3b258424833cc681b7f3a0571a00dcc42df6192f;ds=sidebyside diff --git a/pv/signal.cpp b/pv/signal.cpp index 1a11cfee..04c57923 100644 --- a/pv/signal.cpp +++ b/pv/signal.cpp @@ -21,10 +21,11 @@ #include #include "signal.h" +#include "view/view.h" namespace pv { -const QSizeF Signal::LabelPadding(4, 0); +const int Signal::LabelHitPadding = 2; Signal::Signal(QString name) : _name(name) @@ -36,29 +37,30 @@ QString Signal::get_name() const return _name; } -QRectF Signal::get_label_rect(QPainter &p, const QRect &rect) +void Signal::set_name(QString name) { - const QSizeF text_size = p.boundingRect( - QRectF(0, 0, rect.width(), 0), 0, _name).size(); + _name = name; +} - 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()); +QColor Signal::get_colour() const +{ + return _colour; +} + +void Signal::set_colour(QColor colour) +{ + _colour = colour; } -void Signal::paint_label(QPainter &p, const QRect &rect) +void Signal::paint_label(QPainter &p, const QRect &rect, bool hover) { - p.setBrush(get_colour()); + p.setBrush(_colour); const QColor colour = get_colour(); const float nominal_offset = get_nominal_offset(rect); - const QRectF label_rect = get_label_rect(p, rect); + + compute_text_size(p); + const QRectF label_rect = get_label_rect(rect); // Paint the label const QPointF points[] = { @@ -78,7 +80,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 +96,36 @@ void Signal::paint_label(QPainter &p, const QRect &rect) p.drawText(label_rect, Qt::AlignCenter | Qt::AlignVCenter, _name); } +bool Signal::pt_in_label_rect(const QRect &rect, const QPoint &point) +{ + const QRectF label = get_label_rect(rect); + return QRectF( + QPointF(label.left() - LabelHitPadding, + label.top() - LabelHitPadding), + QPointF(rect.right(), + label.bottom() + LabelHitPadding) + ).contains(point); +} + +void Signal::compute_text_size(QPainter &p) +{ + _text_size = p.boundingRect(QRectF(), 0, _name).size(); +} + +QRectF Signal::get_label_rect(const QRect &rect) +{ + 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); + 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, + label_size.width(), label_size.height()); +} + } // namespace pv