2017-04-27 18:26:36 +00:00
|
|
|
// SPDX-License-Identifier: GPL-2.0
|
2014-01-14 17:23:57 +00:00
|
|
|
#include "divetextitem.h"
|
2015-02-09 21:51:31 +00:00
|
|
|
#include "profilewidget2.h"
|
2016-04-05 05:02:03 +00:00
|
|
|
#include "core/color.h"
|
2019-08-05 17:41:15 +00:00
|
|
|
#include "core/errorhelper.h"
|
2015-09-03 17:20:19 +00:00
|
|
|
|
|
|
|
#include <QBrush>
|
2015-11-06 01:05:30 +00:00
|
|
|
#include <QDebug>
|
|
|
|
#include <QApplication>
|
2014-01-19 18:16:04 +00:00
|
|
|
|
2021-08-12 20:57:57 +00:00
|
|
|
DiveTextItem::DiveTextItem(double dpr, double scale, int alignFlags, QGraphicsItem *parent) : QGraphicsItemGroup(parent),
|
|
|
|
internalAlignFlags(alignFlags),
|
2015-01-16 19:13:58 +00:00
|
|
|
textBackgroundItem(new QGraphicsPathItem(this)),
|
|
|
|
textItem(new QGraphicsPathItem(this)),
|
2021-08-09 14:48:08 +00:00
|
|
|
dpr(dpr),
|
2021-08-12 20:57:57 +00:00
|
|
|
scale(scale)
|
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);
|
|
|
|
}
|
|
|
|
|
2021-08-13 07:56:46 +00:00
|
|
|
void DiveTextItem::set(const QString &t, const QBrush &b)
|
2014-01-19 18:16:04 +00:00
|
|
|
{
|
2015-01-16 19:17:55 +00:00
|
|
|
textItem->setBrush(b);
|
2021-08-13 07:56:46 +00:00
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
2021-08-11 08:57:42 +00:00
|
|
|
QFont DiveTextItem::getFont(double dpr, double scale)
|
2014-01-14 17:23:57 +00:00
|
|
|
{
|
2014-01-19 18:16:04 +00:00
|
|
|
QFont fnt(qApp->font());
|
2021-08-11 08:57:42 +00:00
|
|
|
double size = fnt.pixelSize();
|
|
|
|
if (size > 0) {
|
2014-02-15 15:03:41 +00:00
|
|
|
// set in pixels - so the scale factor may not make a difference if it's too close to 1
|
2021-08-09 14:48:08 +00:00
|
|
|
size *= scale * dpr;
|
2017-03-23 01:13:49 +00:00
|
|
|
fnt.setPixelSize(lrint(size));
|
2014-02-15 15:03:41 +00:00
|
|
|
} else {
|
|
|
|
size = fnt.pointSizeF();
|
2021-08-09 14:48:08 +00:00
|
|
|
size *= scale * dpr;
|
2014-02-15 15:03:41 +00:00
|
|
|
fnt.setPointSizeF(size);
|
|
|
|
}
|
2021-08-11 08:57:42 +00:00
|
|
|
return fnt;
|
|
|
|
}
|
|
|
|
|
2021-08-11 20:18:52 +00:00
|
|
|
double DiveTextItem::fontHeight(double dpr, double scale)
|
|
|
|
{
|
|
|
|
QFont fnt = getFont(dpr, scale);
|
|
|
|
QFontMetrics fm(fnt);
|
|
|
|
return (double)fm.height();
|
|
|
|
}
|
|
|
|
|
|
|
|
double DiveTextItem::height() const
|
|
|
|
{
|
|
|
|
return fontHeight(dpr, scale);
|
|
|
|
}
|
|
|
|
|
2021-08-11 08:57:42 +00:00
|
|
|
void DiveTextItem::updateText()
|
|
|
|
{
|
|
|
|
if (internalText.isEmpty())
|
|
|
|
return;
|
|
|
|
|
|
|
|
QFont fnt = getFont(dpr, scale);
|
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
|
|
|
}
|