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-02-28 04:09:57 +00:00
|
|
|
DiveTextItem::DiveTextItem(QGraphicsItem *parent) : QGraphicsItemGroup(parent),
|
2014-02-09 18:00:03 +00:00
|
|
|
internalAlignFlags(Qt::AlignHCenter | Qt::AlignVCenter),
|
2014-01-19 18:16:04 +00:00
|
|
|
textBackgroundItem(NULL),
|
|
|
|
textItem(NULL),
|
2014-02-15 15:03:41 +00:00
|
|
|
colorIndex(SAC_DEFAULT),
|
|
|
|
scale(1.0)
|
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();
|
|
|
|
}
|
|
|
|
|
2014-02-28 04:09:57 +00:00
|
|
|
void DiveTextItem::setBrush(const QBrush &b)
|
2014-01-19 18:16:04 +00:00
|
|
|
{
|
|
|
|
brush = b;
|
|
|
|
updateText();
|
|
|
|
}
|
|
|
|
|
2014-02-15 15:03:41 +00:00
|
|
|
void DiveTextItem::setScale(double newscale)
|
|
|
|
{
|
|
|
|
scale = newscale;
|
|
|
|
}
|
|
|
|
|
2014-02-28 04:09:57 +00:00
|
|
|
void DiveTextItem::setText(const QString &t)
|
2014-01-19 18:16:04 +00:00
|
|
|
{
|
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-02-28 04:09:57 +00:00
|
|
|
const QString &DiveTextItem::text()
|
2014-01-21 12:48:26 +00:00
|
|
|
{
|
|
|
|
return internalText;
|
|
|
|
}
|
|
|
|
|
2014-01-19 18:16:04 +00:00
|
|
|
void DiveTextItem::updateText()
|
2014-01-14 17:23:57 +00:00
|
|
|
{
|
2014-02-15 15:03:41 +00:00
|
|
|
double size;
|
2014-01-19 18:16:04 +00:00
|
|
|
delete textItem;
|
2014-01-27 19:09:08 +00:00
|
|
|
textItem = NULL;
|
2014-01-19 18:16:04 +00:00
|
|
|
delete textBackgroundItem;
|
2014-01-27 19:09:08 +00:00
|
|
|
textBackgroundItem = NULL;
|
2014-02-28 04:09:57 +00:00
|
|
|
if (internalText.isEmpty()) {
|
2014-01-27 19:09:08 +00:00
|
|
|
return;
|
|
|
|
}
|
2014-01-19 18:16:04 +00:00
|
|
|
|
|
|
|
QFont fnt(qApp->font());
|
2014-02-15 15:03:41 +00:00
|
|
|
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;
|
|
|
|
fnt.setPixelSize(size);
|
|
|
|
} else {
|
|
|
|
size = fnt.pointSizeF();
|
|
|
|
size *= scale;
|
|
|
|
fnt.setPointSizeF(size);
|
|
|
|
}
|
2014-01-19 18:16:04 +00:00
|
|
|
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-29 15:48:06 +00:00
|
|
|
yPos = (internalAlignFlags & Qt::AlignTop) ? 0 :
|
2014-02-28 04:09:57 +00:00
|
|
|
(internalAlignFlags & Qt::AlignBottom) ? +rect.height() :
|
|
|
|
/*(internalAlignFlags & Qt::AlignVCenter ? */ +rect.height() / 4;
|
2014-01-19 18:16:04 +00:00
|
|
|
|
2014-02-28 04:09:57 +00:00
|
|
|
xPos = (internalAlignFlags & Qt::AlignLeft) ? -rect.width() :
|
|
|
|
(internalAlignFlags & Qt::AlignHCenter) ? -rect.width() / 2 :
|
|
|
|
/* (internalAlignFlags & Qt::AlignRight) */ 0;
|
2014-01-19 18:16:04 +00:00
|
|
|
|
2014-02-28 04:09:57 +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
|
|
|
}
|