2014-01-14 17:23:57 +00:00
|
|
|
#include "divetextitem.h"
|
2014-07-09 20:09:52 +00:00
|
|
|
#include "mainwindow.h"
|
2015-02-09 21:51:31 +00:00
|
|
|
#include "profilewidget2.h"
|
2014-01-19 18:16:04 +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),
|
2015-01-16 19:13:58 +00:00
|
|
|
textBackgroundItem(new QGraphicsPathItem(this)),
|
|
|
|
textItem(new QGraphicsPathItem(this)),
|
2014-02-15 15:03:41 +00:00
|
|
|
scale(1.0)
|
2014-01-14 17:23:57 +00:00
|
|
|
{
|
|
|
|
setFlag(ItemIgnoresTransformations);
|
2015-01-16 19:13:58 +00:00
|
|
|
textBackgroundItem->setBrush(QBrush(getColor(TEXT_BACKGROUND)));
|
|
|
|
textBackgroundItem->setPen(Qt::NoPen);
|
|
|
|
textItem->setPen(Qt::NoPen);
|
2014-01-14 17:23:57 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void DiveTextItem::setAlignment(int alignFlags)
|
|
|
|
{
|
2015-01-16 19:22:30 +00:00
|
|
|
if (alignFlags != internalAlignFlags) {
|
|
|
|
internalAlignFlags = alignFlags;
|
|
|
|
updateText();
|
|
|
|
}
|
2014-01-19 18:16:04 +00:00
|
|
|
}
|
|
|
|
|
2014-02-28 04:09:57 +00:00
|
|
|
void DiveTextItem::setBrush(const QBrush &b)
|
2014-01-19 18:16:04 +00:00
|
|
|
{
|
2015-01-16 19:17:55 +00:00
|
|
|
textItem->setBrush(b);
|
2014-01-19 18:16:04 +00:00
|
|
|
}
|
|
|
|
|
2014-02-15 15:03:41 +00:00
|
|
|
void DiveTextItem::setScale(double newscale)
|
|
|
|
{
|
2015-01-16 19:22:30 +00:00
|
|
|
if (scale != newscale) {
|
|
|
|
scale = newscale;
|
|
|
|
updateText();
|
|
|
|
}
|
2014-02-15 15:03:41 +00:00
|
|
|
}
|
|
|
|
|
2014-02-28 04:09:57 +00:00
|
|
|
void DiveTextItem::setText(const QString &t)
|
2014-01-19 18:16:04 +00:00
|
|
|
{
|
2015-01-16 19:22:30 +00:00
|
|
|
if (internalText != t) {
|
|
|
|
internalText = t;
|
|
|
|
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-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
|
2014-07-09 20:09:52 +00:00
|
|
|
size *= scale * MainWindow::instance()->graphics()->getFontPrintScale();
|
2014-02-15 15:03:41 +00:00
|
|
|
fnt.setPixelSize(size);
|
|
|
|
} else {
|
|
|
|
size = fnt.pointSizeF();
|
2014-07-11 21:55:33 +00:00
|
|
|
size *= scale * MainWindow::instance()->graphics()->getFontPrintScale();
|
2014-02-15 15:03:41 +00:00
|
|
|
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 :
|
2015-01-16 19:13:58 +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() :
|
2015-01-16 19:13:58 +00:00
|
|
|
(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);
|
2015-01-16 19:13:58 +00:00
|
|
|
textBackgroundItem->setPath(stroker.createStroke(textPath));
|
|
|
|
textItem->setPath(textPath);
|
2014-01-14 17:23:57 +00:00
|
|
|
}
|