2014-01-14 17:23:57 +00:00
|
|
|
#include "divetextitem.h"
|
2015-02-09 21:51:31 +00:00
|
|
|
#include "profilewidget2.h"
|
2015-09-03 17:20:19 +00:00
|
|
|
#include "subsurface-core/color.h"
|
|
|
|
|
|
|
|
#include <QBrush>
|
2015-11-06 01:05:30 +00:00
|
|
|
#include <QDebug>
|
|
|
|
#include <QApplication>
|
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)),
|
2015-07-29 19:21:27 +00:00
|
|
|
printScale(1.0),
|
|
|
|
scale(1.0),
|
|
|
|
connected(false)
|
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
|
|
|
}
|
|
|
|
|
2015-07-29 17:57:05 +00:00
|
|
|
void DiveTextItem::paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget)
|
|
|
|
{
|
|
|
|
updateText();
|
|
|
|
QGraphicsItemGroup::paint(painter, option, widget);
|
|
|
|
}
|
|
|
|
|
2015-07-29 19:21:27 +00:00
|
|
|
void DiveTextItem::fontPrintScaleUpdate(double scale)
|
|
|
|
{
|
|
|
|
printScale = scale;
|
|
|
|
}
|
|
|
|
|
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;
|
|
|
|
}
|
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;
|
|
|
|
}
|
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) {
|
2015-07-29 19:21:27 +00:00
|
|
|
if (!connected) {
|
|
|
|
if (scene()) {
|
|
|
|
// by now we should be on a scene. grab the profile widget from it and setup our printScale
|
|
|
|
// and connect to the signal that makes sure we keep track if that changes
|
|
|
|
ProfileWidget2 *profile = qobject_cast<ProfileWidget2 *>(scene()->views().first());
|
|
|
|
connect(profile, SIGNAL(fontPrintScaleChanged(double)), this, SLOT(fontPrintScaleUpdate(double)), Qt::UniqueConnection);
|
|
|
|
fontPrintScaleUpdate(profile->getFontPrintScale());
|
|
|
|
connected = true;
|
|
|
|
} else {
|
|
|
|
qDebug() << "called before scene was set up" << t;
|
|
|
|
}
|
|
|
|
}
|
2015-01-16 19:22:30 +00:00
|
|
|
internalText = t;
|
2015-08-05 16:01:50 +00:00
|
|
|
updateText();
|
2015-01-16 19:22:30 +00:00
|
|
|
}
|
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
|
2015-07-29 19:21:27 +00:00
|
|
|
size *= scale * printScale;
|
2014-02-15 15:03:41 +00:00
|
|
|
fnt.setPixelSize(size);
|
|
|
|
} else {
|
|
|
|
size = fnt.pointSizeF();
|
2015-07-29 19:21:27 +00:00
|
|
|
size *= scale * printScale;
|
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
|
|
|
}
|