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 <QApplication>
|
2014-01-19 18:16:04 +00:00
|
|
|
|
2021-08-22 17:48:49 +00:00
|
|
|
static const double outlineSize = 3.0;
|
|
|
|
|
|
|
|
DiveTextItem::DiveTextItem(double dpr, double scale, int alignFlags, QGraphicsItem *parent) : QGraphicsPixmapItem(parent),
|
2021-08-12 20:57:57 +00:00
|
|
|
internalAlignFlags(alignFlags),
|
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-07-29 17:57:05 +00:00
|
|
|
}
|
|
|
|
|
2021-11-13 17:05:31 +00:00
|
|
|
static QFont getFont(double dpr, double scale)
|
|
|
|
{
|
|
|
|
QFont fnt(qApp->font());
|
|
|
|
double size = fnt.pixelSize();
|
|
|
|
if (size > 0) {
|
|
|
|
// set in pixels - so the scale factor may not make a difference if it's too close to 1
|
|
|
|
size *= scale * dpr;
|
|
|
|
fnt.setPixelSize(lrint(size));
|
|
|
|
} else {
|
|
|
|
size = fnt.pointSizeF();
|
|
|
|
size *= scale * dpr;
|
|
|
|
fnt.setPointSizeF(size);
|
|
|
|
}
|
|
|
|
return fnt;
|
|
|
|
}
|
|
|
|
|
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
|
|
|
{
|
2021-08-13 07:56:46 +00:00
|
|
|
internalText = t;
|
2021-08-22 17:48:49 +00:00
|
|
|
if (internalText.isEmpty()) {
|
|
|
|
setPixmap(QPixmap());
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
QFont fnt = getFont(dpr, scale);
|
|
|
|
|
|
|
|
QPainterPath textPath;
|
|
|
|
textPath.addText(0.0, 0.0, fnt, internalText);
|
|
|
|
QPainterPathStroker stroker;
|
|
|
|
stroker.setWidth(outlineSize * dpr);
|
|
|
|
QPainterPath outlinePath = stroker.createStroke(textPath);
|
|
|
|
|
|
|
|
QRectF outlineRect = outlinePath.boundingRect();
|
|
|
|
textPath.translate(-outlineRect.topLeft());
|
|
|
|
outlinePath.translate(-outlineRect.topLeft());
|
|
|
|
|
|
|
|
QPixmap pixmap(outlineRect.size().toSize());
|
|
|
|
pixmap.fill(Qt::transparent);
|
|
|
|
{
|
|
|
|
QPainter painter(&pixmap);
|
|
|
|
painter.setRenderHints(QPainter::Antialiasing);
|
|
|
|
painter.setBrush(QBrush(getColor(TEXT_BACKGROUND)));
|
|
|
|
painter.setPen(Qt::NoPen);
|
|
|
|
painter.drawPath(outlinePath);
|
|
|
|
painter.setBrush(b);
|
|
|
|
painter.setPen(Qt::NoPen);
|
|
|
|
painter.drawPath(textPath);
|
|
|
|
}
|
|
|
|
setPixmap(pixmap);
|
|
|
|
|
|
|
|
double yOffset = (internalAlignFlags & Qt::AlignTop) ? 0.0 :
|
|
|
|
(internalAlignFlags & Qt::AlignBottom) ? -outlineRect.height() :
|
|
|
|
/*(internalAlignFlags & Qt::AlignVCenter ? */ -outlineRect.height() / 2.0;
|
|
|
|
|
|
|
|
double xOffset = (internalAlignFlags & Qt::AlignLeft) ? -outlineRect.width() :
|
|
|
|
(internalAlignFlags & Qt::AlignHCenter) ? -outlineRect.width() / 2.0 :
|
|
|
|
/* (internalAlignFlags & Qt::AlignRight) */ 0.0;
|
|
|
|
setOffset(xOffset, yOffset);
|
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 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
|
|
|
|
{
|
2021-08-22 17:48:49 +00:00
|
|
|
return fontHeight(dpr, scale) + outlineSize * dpr;
|
2014-01-14 17:23:57 +00:00
|
|
|
}
|
2021-11-13 17:05:31 +00:00
|
|
|
|
|
|
|
std::pair<double, double> DiveTextItem::getLabelSize(double dpr, double scale, const QString &label)
|
|
|
|
{
|
|
|
|
QFont fnt = getFont(dpr, scale);
|
|
|
|
double outlineSpace = outlineSize * dpr;
|
|
|
|
QFontMetrics fm(fnt);
|
|
|
|
/* Round up, because non-integers tend to give abysmal rendering. */
|
|
|
|
double width = ceil(fm.size(Qt::TextSingleLine, label).width() + outlineSpace);
|
|
|
|
double height = ceil(fm.height() + outlineSpace);
|
|
|
|
return std::make_pair(width, height);
|
|
|
|
}
|