]> sigrok.org Git - pulseview.git/commitdiff
Cache signal label text size at draw time for later use
authorJoel Holdsworth <redacted>
Wed, 17 Oct 2012 20:11:41 +0000 (21:11 +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

index 7d3dd66362f4c5ab182b4503ab2e0294d5f93066..091dd8d4406ef62dfc56b325c96f770890342323 100644 (file)
@@ -43,7 +43,9 @@ void Signal::paint_label(QPainter &p, const QRect &rect, bool hover)
 
        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[] = {
@@ -79,10 +81,9 @@ 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(QPainter &p,
-       const QRect &rect, const QPoint &point)
+bool Signal::pt_in_label_rect(const QRect &rect, const QPoint &point)
 {
-       const QRectF label = get_label_rect(p, rect);
+       const QRectF label = get_label_rect(rect);
        return QRectF(
                QPointF(label.left() - LabelHitPadding,
                        label.top() - LabelHitPadding),
@@ -91,15 +92,17 @@ bool Signal::pt_in_label_rect(QPainter &p,
                ).contains(point);
 }
 
-QRectF Signal::get_label_rect(QPainter &p, const QRect &rect)
+void Signal::compute_text_size(QPainter &p)
 {
-       const QSizeF text_size = p.boundingRect(
-               QRectF(0, 0, rect.width(), 0), 0, _name).size();
+       _text_size = p.boundingRect(QRectF(), 0, _name).size();
+}
 
+QRectF Signal::get_label_rect(const QRect &rect)
+{
        const float nominal_offset = get_nominal_offset(rect);
        const QSizeF label_size(
-               text_size.width() + LabelPadding.width() * 2,
-               text_size.height() + LabelPadding.height() * 2);
+               _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(),
index 8faff77c6a11ba1dc5550b4be9cb23527724f900..b4c4244ca3fc438f7a71441bfc0a7de80fec0674 100644 (file)
@@ -65,21 +65,25 @@ public:
 
        /**
         * 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);
+       bool pt_in_label_rect(const QRect &rect, const QPoint &point);
 
 private:
+
+       /**
+        * Computes an caches the size of the label text.
+        */
+       void compute_text_size(QPainter &p);
+
        /**
         * Computes the outline rectangle of a label.
         * @param p the QPainter to lay out text with.
         * @param rect The rectangle of the signal header.
         * @return Returns the rectangle of the signal label.
         */
-       virtual QRectF get_label_rect(QPainter &p, const QRect &rect);
+       QRectF get_label_rect(const QRect &rect);
 
 protected:
        /**
@@ -95,6 +99,7 @@ protected:
 
 protected:
        QString _name;
+       QSizeF _text_size;
 };
 
 } // namespace pv
index 4eebf1f5dcb25d29224a6885d850ab1f25b4a2e4..8fe9d485b761c5874625836b1aa469669bf135ba 100644 (file)
@@ -63,8 +63,7 @@ void Header::paintEvent(QPaintEvent *event)
                        0, offset, w, View::SignalHeight);
 
                s->paint_label(painter, signal_heading_rect,
-                       s->pt_in_label_rect(painter,
-                               signal_heading_rect, _mouse_point));
+                       s->pt_in_label_rect(signal_heading_rect, _mouse_point));
 
                offset += View::SignalHeight;
        }