Try to fix the font issue on the ruler.

This changes the ruler a bit, I hope nobody gets offended by it. :)

The main issue is that the scene is 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 <tomaz.canabrava@intel.com>
Signed-off-by: Dirk Hohndel <dirk@hohndel.org>
This commit is contained in:
Tomaz Canabrava 2014-02-27 22:52:03 -03:00 committed by Dirk Hohndel
parent 63f7f37e46
commit 006265d7a0
2 changed files with 22 additions and 33 deletions

View file

@ -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

View file

@ -54,5 +54,6 @@ private:
int paint_direction;
DiveCartesianAxis *timeAxis;
DiveCartesianAxis *depthAxis;
QGraphicsSimpleTextItem *textItem;
};
#endif