2014-01-14 17:23:57 +00:00
|
|
|
#include "divetextitem.h"
|
2014-01-14 18:01:17 +00:00
|
|
|
#include "animationfunctions.h"
|
2014-01-19 18:16:04 +00:00
|
|
|
|
2014-01-14 17:23:57 +00:00
|
|
|
#include <QPropertyAnimation>
|
2014-01-19 18:16:04 +00:00
|
|
|
#include <QApplication>
|
|
|
|
#include <QFont>
|
|
|
|
#include <QFontMetrics>
|
|
|
|
#include <QBrush>
|
|
|
|
#include <QPen>
|
2014-01-19 19:19:00 +00:00
|
|
|
#include <QDebug>
|
2014-01-14 17:23:57 +00:00
|
|
|
|
2014-01-19 18:16:04 +00:00
|
|
|
DiveTextItem::DiveTextItem(QGraphicsItem* parent): QGraphicsItemGroup(parent),
|
|
|
|
textBackgroundItem(NULL),
|
|
|
|
textItem(NULL),
|
2014-01-21 12:48:26 +00:00
|
|
|
internalAlignFlags(Qt::AlignHCenter | Qt::AlignVCenter)
|
2014-01-14 17:23:57 +00:00
|
|
|
{
|
|
|
|
setFlag(ItemIgnoresTransformations);
|
|
|
|
}
|
|
|
|
|
|
|
|
void DiveTextItem::setAlignment(int alignFlags)
|
|
|
|
{
|
|
|
|
internalAlignFlags = alignFlags;
|
2014-01-19 18:16:04 +00:00
|
|
|
updateText();
|
|
|
|
}
|
|
|
|
|
|
|
|
void DiveTextItem::setBrush(const QBrush& b)
|
|
|
|
{
|
|
|
|
brush = b;
|
|
|
|
updateText();
|
|
|
|
}
|
|
|
|
|
|
|
|
void DiveTextItem::setText(const QString& t)
|
|
|
|
{
|
2014-01-21 12:48:26 +00:00
|
|
|
internalText = t;
|
2014-01-19 18:16:04 +00:00
|
|
|
updateText();
|
2014-01-14 17:23:57 +00:00
|
|
|
}
|
|
|
|
|
2014-01-21 12:48:26 +00:00
|
|
|
const QString& DiveTextItem::text()
|
|
|
|
{
|
|
|
|
return internalText;
|
|
|
|
}
|
|
|
|
|
2014-01-19 18:16:04 +00:00
|
|
|
void DiveTextItem::updateText()
|
2014-01-14 17:23:57 +00:00
|
|
|
{
|
2014-01-21 12:48:26 +00:00
|
|
|
if(internalText.isEmpty())
|
2014-01-19 18:16:04 +00:00
|
|
|
return;
|
|
|
|
|
|
|
|
delete textItem;
|
|
|
|
delete textBackgroundItem;
|
|
|
|
|
|
|
|
QFont fnt(qApp->font());
|
|
|
|
QFontMetrics fm(fnt);
|
|
|
|
|
|
|
|
QPainterPath textPath;
|
|
|
|
qreal xPos = 0, yPos = 0;
|
|
|
|
|
2014-01-21 12:48:26 +00:00
|
|
|
QRectF rect = fm.boundingRect(internalText);
|
2014-01-19 18:16:04 +00:00
|
|
|
yPos = (internalAlignFlags & Qt::AlignTop) ? -rect.height() :
|
2014-01-19 19:19:00 +00:00
|
|
|
(internalAlignFlags & Qt::AlignBottom) ? +rect.height() :
|
|
|
|
/*(internalAlignFlags & Qt::AlignVCenter ? */ +rect.height() / 4;
|
2014-01-19 18:16:04 +00:00
|
|
|
|
2014-01-19 19:19:00 +00:00
|
|
|
xPos = (internalAlignFlags & Qt::AlignLeft ) ? +rect.width() :
|
2014-01-19 18:16:04 +00:00
|
|
|
(internalAlignFlags & Qt::AlignHCenter) ? -rect.width()/2 :
|
|
|
|
/* (internalAlignFlags & Qt::AlignRight) */ -rect.width();
|
|
|
|
|
2014-01-21 12:48:26 +00:00
|
|
|
textPath.addText( xPos, yPos, fnt, internalText);
|
2014-01-19 18:16:04 +00:00
|
|
|
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);
|
2014-01-14 17:23:57 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void DiveTextItem::animatedHide()
|
|
|
|
{
|
2014-01-14 18:01:17 +00:00
|
|
|
Animations::hide(this);
|
2014-01-14 17:23:57 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void DiveTextItem::animateMoveTo(qreal x, qreal y)
|
|
|
|
{
|
2014-01-14 18:01:17 +00:00
|
|
|
Animations::moveTo(this, x, y);
|
2014-01-16 04:50:56 +00:00
|
|
|
}
|