]> sigrok.org Git - pulseview.git/blobdiff - pv/signal.cpp
Moved LabelPadding into View
[pulseview.git] / pv / signal.cpp
index 92ef808c2bdc29c3a620cf72cadd22293cab6dac..c23a3954455b155d74b042907c361fa245f18c3a 100644 (file)
  * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301 USA
  */
 
-#include "signal.h"
+#include <extdef.h>
 
-#include "extdef.h"
+#include "signal.h"
 
 namespace pv {
 
-const QSizeF Signal::LabelPadding(4, 0);
+const int Signal::LabelHitPadding = 2;
 
 Signal::Signal(QString name) :
        _name(name)
@@ -36,25 +36,20 @@ QString Signal::get_name() const
        return _name;
 }
 
-void Signal::paint_label(QPainter &p, const QRect &rect)
+void Signal::set_name(QString name)
+{
+       _name = name;
+}
+
+void Signal::paint_label(QPainter &p, const QRect &rect, bool hover)
 {
        p.setBrush(get_colour());
 
-       const QString text(_name);
        const QColor colour = get_colour();
-
-       const QSizeF text_size = p.boundingRect(
-               QRectF(0, 0, rect.width(), 0), 0, text).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;
-       const QRectF label_rect(
-               rect.right() - label_arrow_length - label_size.width(),
-               nominal_offset - label_size.height() / 2,
-               label_size.width(), label_size.height());
+
+       compute_text_size(p);
+       const QRectF label_rect = get_label_rect(rect);
 
        // Paint the label
        const QPointF points[] = {
@@ -74,7 +69,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());
@@ -87,7 +82,38 @@ void Signal::paint_label(QPainter &p, const QRect &rect)
 
        // Paint the text
        p.setPen((colour.lightness() > 64) ? Qt::black : Qt::white);
-       p.drawText(label_rect, Qt::AlignCenter | Qt::AlignVCenter, text);
+       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);
+       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(),
+               nominal_offset - label_size.height() / 2,
+               label_size.width(), label_size.height());
 }
 
 } // namespace pv