summaryrefslogtreecommitdiffstats
path: root/qt-ui/profile/divetextitem.cpp
blob: 5ace46b5e8cc19063e2c7140cefca56b1842e5bd (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
#include "divetextitem.h"
#include "animationfunctions.h"

#include <QPropertyAnimation>
#include <QApplication>
#include <QFont>
#include <QFontMetrics>
#include <QBrush>
#include <QPen>
#include <QDebug>

DiveTextItem::DiveTextItem(QGraphicsItem* parent): QGraphicsItemGroup(parent),
	textBackgroundItem(NULL),
	textItem(NULL),
	internalAlignFlags(0)
{
	setFlag(ItemIgnoresTransformations);
}

void DiveTextItem::setAlignment(int alignFlags)
{
	internalAlignFlags = alignFlags;
	updateText();
}

void DiveTextItem::setBrush(const QBrush& b)
{
	brush = b;
	updateText();
}

void DiveTextItem::setText(const QString& t)
{
	text = t;
	updateText();
}

void DiveTextItem::updateText()
{
	if(!internalAlignFlags || text.isEmpty())
		return;

	delete textItem;
	delete textBackgroundItem;

	QFont fnt(qApp->font());
	QFontMetrics fm(fnt);

	QPainterPath textPath;
	qreal xPos = 0, yPos = 0;

	QRectF rect = fm.boundingRect(text);
	yPos = (internalAlignFlags & Qt::AlignTop) ? -rect.height() :
			(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) */ -rect.width();

	textPath.addText( xPos, yPos, fnt, text);
	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);
}