[PATCH 5/6] Ruler: change how the ruler text drawing works

Lubomir I. Ivanov neolit123 at gmail.com
Tue Mar 11 15:09:53 PDT 2014


From: "Lubomir I. Ivanov" <neolit123 at gmail.com>

A couple of problems with the ruler:
- the rotated text doesn't look very well at all and
interpolation doesn't help it much
- measuring towards the right most part of the profile makes
the text go out of the screen

To solve these issues and attempt to improve the ruler
this patch does the following:
- place the text at the bottom of the lowest of the start
and end points. this way the line will never intersect with
the text
- clamp the x position, so that the text doesn't ever leave
the screen horizontally
- place a white background behind the text so that it
will cover text and graphics under the ruler item
(TODO: place the ruler on top of everything else)

Signed-off-by: Lubomir I. Ivanov <neolit123 at gmail.com>
---
more or less a proposal.

the patch also adds a newline at the bottom of ruleritem.h,
so that this isn't a separate whitespace patch.
---
 qt-ui/profile/ruleritem.cpp | 31 ++++++++++++++++++++-----------
 qt-ui/profile/ruleritem.h   |  3 ++-
 2 files changed, 22 insertions(+), 12 deletions(-)

diff --git a/qt-ui/profile/ruleritem.cpp b/qt-ui/profile/ruleritem.cpp
index 66fb67a..627958e 100644
--- a/qt-ui/profile/ruleritem.cpp
+++ b/qt-ui/profile/ruleritem.cpp
@@ -68,12 +68,15 @@ RulerItem2::RulerItem2() : source(new RulerNodeItem2()),
 	dest(new RulerNodeItem2()),
 	timeAxis(NULL),
 	depthAxis(NULL),
+	textItemBack(new QGraphicsRectItem(this)),
 	textItem(new QGraphicsSimpleTextItem(this))
 {
 	memset(&pInfo, 0, sizeof(pInfo));
 	source->setRuler(this);
 	dest->setRuler(this);
 	textItem->setFlag(QGraphicsItem::ItemIgnoresTransformations);
+	textItemBack->setBrush(QColor(0xff, 0xff, 0xff, 190));
+	textItemBack->setPen(QColor(Qt::white));
 }
 
 void RulerItem2::recalculate()
@@ -100,20 +103,26 @@ void RulerItem2::recalculate()
 	compare_samples(source->entry, dest->entry, buffer, 500, 1);
 	text = QString(buffer);
 
-	//Draw Text
-	// This text item ignores transformations, so we cant use
-	// the line.angle(), we need to calculate the angle based
-	// on the view.
-
+	// draw text
 	QGraphicsView *view = scene()->views().first();
 	QPoint begin = view->mapFromScene(mapToScene(startPoint));
-	QPoint end = view->mapFromScene(mapToScene(endPoint));
-	QLineF globalLine(begin, end);
 	textItem->setText(text);
-	textItem->resetMatrix();
-	textItem->resetTransform();
-	textItem->setPos(startPoint);
-	textItem->rotate(globalLine.angle() * -1);
+	qreal tgtX = startPoint.x();
+	const qreal diff = begin.x() + textItem->boundingRect().width();
+	// clamp so that the text doesn't go out of the screen to the right
+	if (diff > view->width()) {
+		begin.setX(begin.x() - (diff - view->width()));
+		tgtX = mapFromScene(view->mapToScene(begin)).x();
+	}
+	// always show the text bellow the lowest of the start and end points
+	qreal tgtY = (startPoint.y() >= endPoint.y()) ? startPoint.y() : endPoint.y();
+	textItem->setPos(tgtX - 1, tgtY + 4);
+
+	// setup the text background
+	textItemBack->setVisible(startPoint.x() != endPoint.x());
+	QPointF wh = mapFromScene(view->mapToScene(QPoint(textItem->boundingRect().width(),
+		textItem->boundingRect().height())));
+	textItemBack->setRect(tgtX - 2, tgtY + 3, wh.x() + 2, wh.y() + 3);
 }
 
 RulerNodeItem2 *RulerItem2::sourceNode() const
diff --git a/qt-ui/profile/ruleritem.h b/qt-ui/profile/ruleritem.h
index 6ab86f4..7bfc63e 100644
--- a/qt-ui/profile/ruleritem.h
+++ b/qt-ui/profile/ruleritem.h
@@ -50,6 +50,7 @@ private:
 	int paint_direction;
 	DiveCartesianAxis *timeAxis;
 	DiveCartesianAxis *depthAxis;
+	QGraphicsRectItem *textItemBack;
 	QGraphicsSimpleTextItem *textItem;
 };
-#endif
\ No newline at end of file
+#endif
-- 
1.7.11.msysgit.0



More information about the subsurface mailing list