1
0
mirror of https://github.com/tumic0/GPXSee.git synced 2024-12-04 16:29:09 +01:00
GPXSee/src/map/textpointitem.cpp

114 lines
3.4 KiB
C++
Raw Normal View History

2019-05-10 18:56:19 +02:00
#include <QFont>
#include <QFontMetrics>
#include <QImage>
#include <QPainter>
2021-04-18 12:20:07 +02:00
#include <QStaticText>
2019-05-10 18:56:19 +02:00
#include "textpointitem.h"
#define FLAGS (Qt::AlignCenter | Qt::TextWordWrap | Qt::TextDontClip)
#define MAX_TEXT_WIDTH 8
2019-06-30 20:39:22 +02:00
#define MIN_BOX_WIDTH 2
2019-05-10 18:56:19 +02:00
2019-06-30 20:39:22 +02:00
static void expand(QRectF &rect, int width)
2019-05-10 18:56:19 +02:00
{
2019-06-30 20:39:22 +02:00
rect.adjust(-(width/2 - rect.width()/2), 0, width/2 - rect.width()/2, 0);
}
2019-05-10 18:56:19 +02:00
2019-06-30 20:39:22 +02:00
TextPointItem::TextPointItem(const QPoint &point, const QString *text,
const QFont *font, const QImage *img, const QColor *color,
const QColor *haloColor, const QColor *bgColor, int padding, double rotate)
2021-04-18 12:20:07 +02:00
: TextItem(font ? text : 0), _font(font), _img(img), _color(color),
_haloColor(haloColor), _bgColor(bgColor), _rotate(rotate)
2019-06-30 20:39:22 +02:00
{
2019-07-08 21:19:25 +02:00
if (_text) {
QFontMetrics fm(*_font);
int limit = _font->pixelSize() * MAX_TEXT_WIDTH;
_textRect = fm.boundingRect(QRect(0, 0, limit, 0), FLAGS, *_text);
2019-06-08 12:01:35 +02:00
_textRect.adjust(0, 0, 1, 1);
2019-06-30 20:39:22 +02:00
2019-07-08 21:19:25 +02:00
if (_bgColor && _textRect.width() < _font->pixelSize() * MIN_BOX_WIDTH)
expand(_textRect, _font->pixelSize() * MIN_BOX_WIDTH);
}
2019-06-30 20:39:22 +02:00
setPos(point, padding);
2019-06-30 20:39:22 +02:00
}
void TextPointItem::setPos(const QPoint &point, int padding)
2019-06-30 20:39:22 +02:00
{
QPainterPath shape;
QRectF iconRect;
2019-06-30 20:39:22 +02:00
if (_img && !_img->isNull()) {
QSizeF s(_img->size() / _img->devicePixelRatioF());
iconRect = QRectF(QPointF(point.x() - s.width() / 2,
point.y() - s.height()/2), s);
_textRect.moveTopLeft(QPointF(point.x() + s.width()/2 + padding,
point.y() - _textRect.height()/2));
2019-05-10 18:56:19 +02:00
} else
_textRect.moveCenter(point);
_rect = _textRect | iconRect;
2019-06-30 20:39:22 +02:00
shape.addRect(_rect);
_shape = shape;
2019-05-10 18:56:19 +02:00
}
void TextPointItem::paint(QPainter *painter) const
{
if (_img && !_img->isNull()) {
QSizeF s(_img->size() / _img->devicePixelRatioF());
if (std::isnan(_rotate))
painter->drawImage(QPointF(_rect.left(), _rect.center().y()
- s.height()/2), *_img);
else {
painter->save();
painter->translate(QPointF(_rect.left() + s.width()/2,
_rect.center().y()));
painter->rotate(_rotate);
painter->drawImage(QPointF(-s.width()/2, -s.height()/2), *_img);
painter->restore();
}
2021-04-18 18:10:23 +02:00
}
2019-05-10 18:56:19 +02:00
if (_text) {
2019-06-30 20:39:22 +02:00
if (_bgColor) {
painter->setPen(*_color);
painter->setBrush(*_bgColor);
painter->drawRect(_textRect);
painter->setBrush(Qt::NoBrush);
2019-05-10 18:56:19 +02:00
painter->setFont(*_font);
painter->drawText(_textRect, FLAGS, *_text);
2021-04-18 12:20:07 +02:00
} else if (_haloColor) {
QStaticText st(*_text);
st.setTextFormat(Qt::PlainText);
st.setTextWidth(_textRect.width());
st.setTextOption(QTextOption(Qt::AlignHCenter));
st.setPerformanceHint(QStaticText::AggressiveCaching);
painter->setPen(*_haloColor);
painter->setFont(*_font);
painter->drawStaticText(_textRect.topLeft() + QPointF(-1, -1), st);
painter->drawStaticText(_textRect.topLeft() + QPointF(+1, +1), st);
painter->drawStaticText(_textRect.topLeft() + QPointF(-1, +1), st);
painter->drawStaticText(_textRect.topLeft() + QPointF(+1, -1), st);
painter->drawStaticText(_textRect.topLeft() + QPointF(0, -1), st);
painter->drawStaticText(_textRect.topLeft() + QPointF(0, +1), st);
painter->drawStaticText(_textRect.topLeft() + QPointF(-1, 0), st);
painter->drawStaticText(_textRect.topLeft() + QPointF(+1, 0), st);
painter->setPen(*_color);
painter->drawStaticText(_textRect.topLeft(), st);
2019-05-10 18:56:19 +02:00
} else {
2021-04-18 12:20:07 +02:00
painter->setPen(*_color);
painter->setFont(*_font);
painter->drawText(_textRect, FLAGS, *_text);
2019-05-10 18:56:19 +02:00
}
}
//painter->setPen(Qt::red);
//painter->setRenderHint(QPainter::Antialiasing, false);
2019-05-10 18:56:19 +02:00
//painter->drawRect(_rect);
}