From: Joel Holdsworth Date: Wed, 17 Oct 2012 20:11:41 +0000 (+0100) Subject: Cache signal label text size at draw time for later use X-Git-Tag: pulseview-0.1.0~242 X-Git-Url: https://sigrok.org/gitaction?a=commitdiff_plain;h=ed6f0f4f84a26aadacfd4d3a745a8d7f1094efc5;p=pulseview.git Cache signal label text size at draw time for later use --- diff --git a/pv/signal.cpp b/pv/signal.cpp index 7d3dd663..091dd8d4 100644 --- a/pv/signal.cpp +++ b/pv/signal.cpp @@ -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(), diff --git a/pv/signal.h b/pv/signal.h index 8faff77c..b4c4244c 100644 --- a/pv/signal.h +++ b/pv/signal.h @@ -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 diff --git a/pv/view/header.cpp b/pv/view/header.cpp index 4eebf1f5..8fe9d485 100644 --- a/pv/view/header.cpp +++ b/pv/view/header.cpp @@ -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; }