From: Joel Holdsworth Date: Sat, 23 Mar 2013 11:28:31 +0000 (+0000) Subject: Improved painting of cursor values X-Git-Tag: pulseview-0.1.0~65 X-Git-Url: https://sigrok.org/gitaction?a=commitdiff_plain;h=3a6fe0818affcb40b2821eab3f633e536377dd81;p=pulseview.git Improved painting of cursor values --- diff --git a/pv/view/cursor.cpp b/pv/view/cursor.cpp index 35c2ed45..c4f4b6d7 100644 --- a/pv/view/cursor.cpp +++ b/pv/view/cursor.cpp @@ -20,6 +20,7 @@ #include "cursor.h" +#include "ruler.h" #include "view.h" #include @@ -62,9 +63,10 @@ QRectF Cursor::get_label_rect(const QRect &rect) const label_size.width() + 1, label_size.height() + 1); } -void Cursor::paint_label(QPainter &p, const QRect &rect) +void Cursor::paint_label(QPainter &p, const QRect &rect, + unsigned int prefix) { - compute_text_size(p); + compute_text_size(p, prefix); const QRectF r(get_label_rect(rect)); const float h_centre = (r.left() + r.right()) / 2; @@ -88,9 +90,6 @@ void Cursor::paint_label(QPainter &p, const QRect &rect) QPointF(r.left() + 1, r.top() + 1), }; - char text[16]; - format_text(text); - p.setPen(Qt::transparent); p.setBrush(FillColour); p.drawPolygon(points, countof(points)); @@ -104,19 +103,14 @@ void Cursor::paint_label(QPainter &p, const QRect &rect) p.drawPolygon(points, countof(points)); p.setPen(TextColour); - p.drawText(r, Qt::AlignCenter | Qt::AlignVCenter, text); -} - -void Cursor::compute_text_size(QPainter &p) -{ - char text[16]; - format_text(text); - _text_size = p.boundingRect(QRectF(), 0, text).size(); + p.drawText(r, Qt::AlignCenter | Qt::AlignVCenter, + Ruler::format_time(_time, prefix, 2)); } -void Cursor::format_text(char *text) +void Cursor::compute_text_size(QPainter &p, unsigned int prefix) { - sprintf(text, "%gs", _time); + _text_size = p.boundingRect(QRectF(), 0, + Ruler::format_time(_time, prefix, 2)).size(); } } // namespace view diff --git a/pv/view/cursor.h b/pv/view/cursor.h index dd8ef242..be779c00 100644 --- a/pv/view/cursor.h +++ b/pv/view/cursor.h @@ -65,13 +65,13 @@ public: * Paints the cursor's label to the ruler. * @param p The painter to draw with. * @param rect The rectangle of the ruler client area. + * @param prefix The index of the SI prefix to use. */ - void paint_label(QPainter &p, const QRect &rect); + void paint_label(QPainter &p, const QRect &rect, + unsigned int prefix); private: - void compute_text_size(QPainter &p); - - void format_text(char *text); + void compute_text_size(QPainter &p, unsigned int prefix); private: QSizeF _text_size; diff --git a/pv/view/ruler.cpp b/pv/view/ruler.cpp index 4649b10d..adab57db 100644 --- a/pv/view/ruler.cpp +++ b/pv/view/ruler.cpp @@ -59,6 +59,20 @@ Ruler::Ruler(View &parent) : this, SLOT(hover_point_changed())); } +QString Ruler::format_time(double t, unsigned int prefix, + unsigned int precision) +{ + const double multiplier = pow(10.0, + - prefix * 3 - FirstSIPrefixPower); + + QString s; + QTextStream ts(&s); + ts.setRealNumberPrecision(precision); + ts << fixed << forcesign << (t * multiplier) << + SIPrefixes[prefix] << "s"; + return s; +} + void Ruler::paintEvent(QPaintEvent*) { using namespace Qt; @@ -73,7 +87,6 @@ void Ruler::paintEvent(QPaintEvent*) double min_width = SpacingIncrement, typical_width; double tick_period; unsigned int prefix; - double multiplier; // Find tick spacing, and number formatting that does not cause // value to collide. @@ -94,11 +107,10 @@ void Ruler::paintEvent(QPaintEvent*) prefix = (order - FirstSIPrefixPower) / 3; assert(prefix < countof(SIPrefixes)); - multiplier = pow(10.0, - prefix * 3 - FirstSIPrefixPower); typical_width = p.boundingRect(0, 0, INT_MAX, INT_MAX, AlignLeft | AlignTop, format_time(_view.offset(), - multiplier, prefix)).width() + MinValueSpacing; + prefix)).width() + MinValueSpacing; min_width += SpacingIncrement; @@ -137,7 +149,7 @@ void Ruler::paintEvent(QPaintEvent*) // Draw a major tick p.drawText(x, ValueMargin, 0, text_height, AlignCenter | AlignTop | TextDontClip, - format_time(t, multiplier, prefix)); + format_time(t, prefix)); p.drawLine(QPointF(x, major_tick_y1), QPointF(x, tick_y2)); } @@ -152,7 +164,7 @@ void Ruler::paintEvent(QPaintEvent*) } // Draw the cursors - draw_cursors(p); + draw_cursors(p, prefix); // Draw the hover mark draw_hover_mark(p); @@ -192,26 +204,15 @@ void Ruler::mouseReleaseEvent(QMouseEvent *) _grabbed_marker = NULL; } -QString Ruler::format_time(double t, double multiplier, - unsigned int prefix) -{ - QString s; - QTextStream ts(&s); - ts.setRealNumberPrecision(0); - ts << fixed << forcesign << (t * multiplier) << - SIPrefixes[prefix] << "s"; - return s; -} - -void Ruler::draw_cursors(QPainter &p) +void Ruler::draw_cursors(QPainter &p, unsigned int prefix) { if (!_view.cursors_shown()) return; const QRect r = rect(); pair &cursors = _view.cursors(); - cursors.first.paint_label(p, r); - cursors.second.paint_label(p, r); + cursors.first.paint_label(p, r, prefix); + cursors.second.paint_label(p, r, prefix); } void Ruler::draw_hover_mark(QPainter &p) diff --git a/pv/view/ruler.h b/pv/view/ruler.h index 7bdde99c..f40f8255 100644 --- a/pv/view/ruler.h +++ b/pv/view/ruler.h @@ -45,6 +45,9 @@ private: public: Ruler(View &parent); + static QString format_time(double t, unsigned int prefix, + unsigned precision = 0); + private: void paintEvent(QPaintEvent *event); @@ -53,10 +56,7 @@ private: void mouseReleaseEvent(QMouseEvent *); private: - static QString format_time(double t, double multiplier, - unsigned int prefix); - - void draw_cursors(QPainter &p); + void draw_cursors(QPainter &p, unsigned int prefix); /** * Draw a hover arrow under the cursor position. diff --git a/pv/view/timemarker.h b/pv/view/timemarker.h index 34e936c1..3c919889 100644 --- a/pv/view/timemarker.h +++ b/pv/view/timemarker.h @@ -80,8 +80,10 @@ public: * Paints the marker's label to the ruler. * @param p The painter to draw with. * @param rect The rectangle of the ruler client area. + * @param prefix The SI prefix to paint time value with. */ - virtual void paint_label(QPainter &p, const QRect &rect) = 0; + virtual void paint_label(QPainter &p, const QRect &rect, + unsigned int prefix) = 0; signals: void time_changed();