mirror of
https://github.com/subsurface/subsurface.git
synced 2024-11-28 21:20:19 +00:00
76a8e83a54
This seem to work better, but it misses a couple of items at times (for example the highest label on some of the axis). Needs lots more testing. See #590 Signed-off-by: Dirk Hohndel <dirk@hohndel.org>
106 lines
2.5 KiB
C++
106 lines
2.5 KiB
C++
#include "divetextitem.h"
|
|
#include "animationfunctions.h"
|
|
#include "mainwindow.h"
|
|
|
|
#include <QPropertyAnimation>
|
|
#include <QApplication>
|
|
#include <QFont>
|
|
#include <QFontMetrics>
|
|
#include <QBrush>
|
|
#include <QPen>
|
|
#include <QDebug>
|
|
|
|
DiveTextItem::DiveTextItem(QGraphicsItem *parent) : QGraphicsItemGroup(parent),
|
|
internalAlignFlags(Qt::AlignHCenter | Qt::AlignVCenter),
|
|
textBackgroundItem(NULL),
|
|
textItem(NULL),
|
|
colorIndex(SAC_DEFAULT),
|
|
scale(1.0)
|
|
{
|
|
setFlag(ItemIgnoresTransformations);
|
|
}
|
|
|
|
void DiveTextItem::setAlignment(int alignFlags)
|
|
{
|
|
internalAlignFlags = alignFlags;
|
|
updateText();
|
|
}
|
|
|
|
void DiveTextItem::setBrush(const QBrush &b)
|
|
{
|
|
brush = b;
|
|
updateText();
|
|
}
|
|
|
|
void DiveTextItem::setScale(double newscale)
|
|
{
|
|
scale = newscale;
|
|
}
|
|
|
|
void DiveTextItem::setText(const QString &t)
|
|
{
|
|
internalText = t;
|
|
updateText();
|
|
}
|
|
|
|
const QString &DiveTextItem::text()
|
|
{
|
|
return internalText;
|
|
}
|
|
|
|
void DiveTextItem::updateText()
|
|
{
|
|
double size;
|
|
delete textItem;
|
|
textItem = NULL;
|
|
delete textBackgroundItem;
|
|
textBackgroundItem = NULL;
|
|
if (internalText.isEmpty()) {
|
|
return;
|
|
}
|
|
|
|
QFont fnt(qApp->font());
|
|
if ((size = fnt.pixelSize()) > 0) {
|
|
// set in pixels - so the scale factor may not make a difference if it's too close to 1
|
|
size *= scale * MainWindow::instance()->graphics()->getFontPrintScale();
|
|
fnt.setPixelSize(size);
|
|
} else {
|
|
size = fnt.pointSizeF();
|
|
size *= scale * MainWindow::instance()->graphics()->getFontPrintScale();;
|
|
fnt.setPointSizeF(size);
|
|
}
|
|
QFontMetrics fm(fnt);
|
|
|
|
QPainterPath textPath;
|
|
qreal xPos = 0, yPos = 0;
|
|
|
|
QRectF rect = fm.boundingRect(internalText);
|
|
yPos = (internalAlignFlags & Qt::AlignTop) ? 0 :
|
|
(internalAlignFlags & Qt::AlignBottom) ? +rect.height() :
|
|
/*(internalAlignFlags & Qt::AlignVCenter ? */ +rect.height() / 4;
|
|
|
|
xPos = (internalAlignFlags & Qt::AlignLeft) ? -rect.width() :
|
|
(internalAlignFlags & Qt::AlignHCenter) ? -rect.width() / 2 :
|
|
/* (internalAlignFlags & Qt::AlignRight) */ 0;
|
|
|
|
textPath.addText(xPos, yPos, fnt, internalText);
|
|
QPainterPathStroker stroker;
|
|
stroker.setWidth(3);
|
|
textBackgroundItem = new QGraphicsPathItem(stroker.createStroke(textPath), this);
|
|
textBackgroundItem->setBrush(QBrush(getColor(TEXT_BACKGROUND)));
|
|
textBackgroundItem->setPen(Qt::NoPen);
|
|
|
|
textItem = new QGraphicsPathItem(textPath, this);
|
|
textItem->setBrush(brush);
|
|
textItem->setPen(Qt::NoPen);
|
|
}
|
|
|
|
void DiveTextItem::animatedHide()
|
|
{
|
|
Animations::hide(this);
|
|
}
|
|
|
|
void DiveTextItem::animateMoveTo(qreal x, qreal y)
|
|
{
|
|
Animations::moveTo(this, x, y);
|
|
}
|