Ruler: change how the ruler text drawing works

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@gmail.com>
Signed-off-by: Dirk Hohndel <dirk@hohndel.org>
This commit is contained in:
Lubomir I. Ivanov 2014-03-12 00:09:53 +02:00 committed by Dirk Hohndel
parent 07cd62425d
commit f2bfc72648
2 changed files with 22 additions and 12 deletions

View file

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

View file

@ -50,6 +50,7 @@ private:
int paint_direction;
DiveCartesianAxis *timeAxis;
DiveCartesianAxis *depthAxis;
QGraphicsRectItem *textItemBack;
QGraphicsSimpleTextItem *textItem;
};
#endif
#endif