Ugh, I thought I had send this earlier, sorry. crash fix.
On Thu, Feb 27, 2014 at 3:41 PM, Tomaz Canabrava <[email protected]> wrote: > >
From 6cad0b77c57bcce71473220ef7928f53a95f61a1 Mon Sep 17 00:00:00 2001 From: Tomaz Canabrava <[email protected]> Date: Thu, 27 Feb 2014 17:25:50 -0300 Subject: [PATCH 9/9] Try to fix the font issue on the ruler. This changes a bit the ruler, I hope nobody get's offended by it. :) The main issue is that the scene has now 100x100 pixels wide, so the font was *really* huge. and setting itemIgnoresTransformations on the ruler broke a lot of stuff. I removed the code that painted the text and created a QGraphics TextItem for that - that will hold the text for the ruler. then I played with the view to get the correct angle of the line, that was in scene coordinates and thus, could not be used directly on the item that had ignore transformation changes. Signed-off-by: Tomaz Canabrava <[email protected]> --- qt-ui/profile/ruleritem.cpp | 54 ++++++++++++++++++--------------------------- qt-ui/profile/ruleritem.h | 1 + 2 files changed, 22 insertions(+), 33 deletions(-) diff --git a/qt-ui/profile/ruleritem.cpp b/qt-ui/profile/ruleritem.cpp index d2b06f7..04b7fd9 100644 --- a/qt-ui/profile/ruleritem.cpp +++ b/qt-ui/profile/ruleritem.cpp @@ -1,8 +1,11 @@ #include "ruleritem.h" +#include "divetextitem.h" + #include <QFont> #include <QFontMetrics> #include <QPainter> #include <QGraphicsScene> +#include <QGraphicsView> #include <QDebug> #include <stdint.h> @@ -62,11 +65,13 @@ RulerItem2::RulerItem2(): timeAxis(NULL), depthAxis(NULL), source(new RulerNodeItem2(pInfo)), - dest(new RulerNodeItem2(pInfo)) + dest(new RulerNodeItem2(pInfo)), + textItem(new QGraphicsSimpleTextItem(this)) { memset(&pInfo, 0, sizeof(pInfo)); source->setRuler(this); dest->setRuler(this); + textItem->setFlag(QGraphicsItem::ItemIgnoresTransformations); } void RulerItem2::recalculate() @@ -82,6 +87,7 @@ void RulerItem2::recalculate() prepareGeometryChange(); startPoint = mapFromItem(source, 0, 0); endPoint = mapFromItem(dest, 0, 0); + if (startPoint.x() > endPoint.x()) { tmp = endPoint; endPoint = startPoint; @@ -92,22 +98,21 @@ void RulerItem2::recalculate() compare_samples(source->entry, dest->entry, buffer, 500, 1); text = QString(buffer); - QRect r = fm.boundingRect(QRect(QPoint(10,-1*INT_MAX), QPoint(line.length()-10, 0)), Qt::TextWordWrap, text); - if (r.height() < 10) - height = 10; - else - height = r.height(); + //Draw Text + // This text item ignores transformations, so we cant use + // the line.angle(), we need to calculate the angle based + // on the view. + + 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); - QLineF line_n = line.normalVector(); - line_n.setLength(height); - if (scene()) { - /* Determine whether we draw down or upwards */ - if (scene()->sceneRect().contains(line_n.p2()) && - scene()->sceneRect().contains(endPoint+QPointF(line_n.dx(),line_n.dy()))) - paint_direction = -1; - else - paint_direction = 1; - } } RulerNodeItem2 *RulerItem2::sourceNode() const @@ -125,26 +130,9 @@ void RulerItem2::paint(QPainter *painter, const QStyleOptionGraphicsItem *option Q_UNUSED(option); Q_UNUSED(widget); QLineF line(startPoint, endPoint); - QLineF line_n = line.normalVector(); painter->setPen(QColor(Qt::black)); painter->setBrush(Qt::NoBrush); - line_n.setLength(height); - - if (paint_direction == 1) - line_n.setAngle(line_n.angle()+180); painter->drawLine(line); - painter->drawLine(line_n); - painter->drawLine(line_n.p1() + QPointF(line.dx(), line.dy()), line_n.p2() + QPointF(line.dx(), line.dy())); - - //Draw Text - painter->save(); - painter->translate(startPoint.x(), startPoint.y()); - painter->rotate(line.angle()*-1); - if (paint_direction == 1) - painter->translate(0, height); - painter->setPen(Qt::black); - painter->drawText(QRectF(QPointF(10,-1*height), QPointF(line.length()-10, 0)), Qt::TextWordWrap, text); - painter->restore(); } QRectF RulerItem2::boundingRect() const diff --git a/qt-ui/profile/ruleritem.h b/qt-ui/profile/ruleritem.h index 714bf09..f9cc985 100644 --- a/qt-ui/profile/ruleritem.h +++ b/qt-ui/profile/ruleritem.h @@ -54,5 +54,6 @@ private: int paint_direction; DiveCartesianAxis *timeAxis; DiveCartesianAxis *depthAxis; + QGraphicsSimpleTextItem *textItem; }; #endif \ No newline at end of file -- 1.9.0
_______________________________________________ subsurface mailing list [email protected] http://lists.hohndel.org/cgi-bin/mailman/listinfo/subsurface
