[PATCH] Profile: add white outline to all text

Lubomir I. Ivanov neolit123 at gmail.com
Mon Jul 1 05:21:01 PDT 2013


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

To draw the white outline around profile text we trace a text path
using QPainterPath::addText() and then create a couple of
QGraphicsPathItem objects. One of the objects is outlined using
QPainterPathStroker and is placed at the bottom of a QGraphicsItemGroup
with a white brush. The other object holds the standard colored text
and is placed on top.

Notes:
- possibly quite expensive on older machines
- ProfileGraphicsView::plot_text() now returns a QGraphicsItemGroup
- QGraphicsSimpleTextItem uses a top-left baseline anchor
while QPainterPath::addText() uses a bottom-left baseline
which is a bit mysterious, requires the -3 offset for a match
and is possibly non-portable across fonts and sizes.

Signed-off-by: Lubomir I. Ivanov <neolit123 at gmail.com>
---
 qt-ui/profilegraphics.cpp | 31 ++++++++++++++++++++++---------
 qt-ui/profilegraphics.h   |  2 +-
 2 files changed, 23 insertions(+), 10 deletions(-)

diff --git a/qt-ui/profilegraphics.cpp b/qt-ui/profilegraphics.cpp
index cb69bc1..45ed932 100644
--- a/qt-ui/profilegraphics.cpp
+++ b/qt-ui/profilegraphics.cpp
@@ -1133,23 +1133,36 @@ void ProfileGraphicsView::plot_depth_profile()
 	}
 }
 
-QGraphicsSimpleTextItem *ProfileGraphicsView::plot_text(text_render_options_t *tro,const QPointF& pos, const QString& text, QGraphicsItem *parent)
+QGraphicsItemGroup *ProfileGraphicsView::plot_text(text_render_options_t *tro,const QPointF& pos, const QString& text, QGraphicsItem *parent)
 {
-	QFontMetrics fm(font());
+	QFont fnt(font());
+	QFontMetrics fm(fnt);
 
+	QPointF point(SCALEGC(pos.x(), pos.y())); // This is neded because of the SCALE macro.
 	double dx = tro->hpos * (fm.width(text));
 	double dy = tro->vpos * (fm.height());
 
-	QGraphicsSimpleTextItem *item = new QGraphicsSimpleTextItem(text, parent);
-	QPointF point(SCALEGC(pos.x(), pos.y())); // This is neded because of the SCALE macro.
+	QGraphicsItemGroup *group = new QGraphicsItemGroup(parent);
+	QPainterPath textPath;
+	/* addText() uses bottom-left text baseline and the -3 offset is probably slightly off
+	 * for different font sizes. */
+	textPath.addText(0, fm.height() - 3, fnt, text);
+	QPainterPathStroker stroker;
+	stroker.setWidth(3);
+	QGraphicsPathItem *strokedItem = new QGraphicsPathItem(stroker.createStroke(textPath), group);
+	strokedItem->setBrush(QBrush(profile_color[TEXT_BACKGROUND].first()));
+	strokedItem->setPen(Qt::NoPen);
 
-	item->setPos(point.x() + dx, point.y() + dy);
-	item->setBrush(QBrush(profile_color[tro->color].first()));
-	item->setFlag(QGraphicsItem::ItemIgnoresTransformations);
+	QGraphicsPathItem *textItem = new QGraphicsPathItem(textPath, group);
+	textItem->setBrush(QBrush(profile_color[tro->color].first()));
+	textItem->setPen(Qt::NoPen);
+
+	group->setPos(point.x() + dx, point.y() + dy);
+	group->setFlag(QGraphicsItem::ItemIgnoresTransformations);
 
 	if (!parent)
-		scene()->addItem(item);
-	return item;
+		scene()->addItem(group);
+	return group;
 }
 
 void ProfileGraphicsView::resizeEvent(QResizeEvent *event)
diff --git a/qt-ui/profilegraphics.h b/qt-ui/profilegraphics.h
index 6a5413d..c3cb4ef 100644
--- a/qt-ui/profilegraphics.h
+++ b/qt-ui/profilegraphics.h
@@ -104,7 +104,7 @@ public slots:
 
 private:
 	void plot_depth_profile();
-	QGraphicsSimpleTextItem* plot_text(text_render_options_t *tro, const QPointF& pos, const QString &text, QGraphicsItem *parent = 0);
+	QGraphicsItemGroup *plot_text(text_render_options_t *tro, const QPointF& pos, const QString &text, QGraphicsItem *parent = 0);
 	void plot_events(struct divecomputer *dc);
 	void plot_one_event(struct event *event);
 	void plot_temperature_profile();
-- 
1.7.11.msysgit.0



More information about the subsurface mailing list