2019-05-10 18:56:19 +02:00
|
|
|
#include <QFont>
|
|
|
|
#include <QFontMetrics>
|
|
|
|
#include <QImage>
|
|
|
|
#include <QPainter>
|
|
|
|
#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(QRect &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,
|
2019-07-08 21:19:25 +02:00
|
|
|
const QColor *bgColor) : _text(font ? text : 0), _font(font), _img(img),
|
|
|
|
_color(color), _bgColor(bgColor)
|
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);
|
|
|
|
}
|
|
|
|
|
|
|
|
void TextPointItem::setPos(const QPoint &point)
|
|
|
|
{
|
|
|
|
QPainterPath shape;
|
|
|
|
QRect iconRect;
|
|
|
|
|
|
|
|
if (_img) {
|
|
|
|
iconRect = QRect(QPoint(point.x() - _img->width()/2, point.y()
|
|
|
|
- _img->height()/2), _img->size());
|
|
|
|
_textRect.moveTopLeft(QPoint(point.x() + _img->width(), point.y()
|
2019-05-10 18:56:19 +02:00
|
|
|
- _textRect.height()/2));
|
|
|
|
} 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)
|
|
|
|
painter->drawImage(QPoint(_rect.left(), _rect.center().y()
|
|
|
|
- _img->height()/2), *_img);
|
|
|
|
|
|
|
|
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);
|
|
|
|
} else {
|
2019-07-08 21:19:25 +02:00
|
|
|
QImage img(_textRect.size(), QImage::Format_ARGB32_Premultiplied);
|
|
|
|
img.fill(Qt::transparent);
|
|
|
|
QPainter ip(&img);
|
|
|
|
ip.setPen(Qt::white);
|
|
|
|
ip.setFont(*_font);
|
|
|
|
ip.drawText(img.rect(), FLAGS, *_text);
|
|
|
|
|
|
|
|
painter->drawImage(_textRect.x() - 1, _textRect.y() - 1, img);
|
|
|
|
painter->drawImage(_textRect.x() + 1, _textRect.y() + 1, img);
|
|
|
|
painter->drawImage(_textRect.x() - 1, _textRect.y() + 1, img);
|
|
|
|
painter->drawImage(_textRect.x() + 1, _textRect.y() - 1, img);
|
|
|
|
painter->drawImage(_textRect.x(), _textRect.y() - 1, img);
|
|
|
|
painter->drawImage(_textRect.x(), _textRect.y() + 1, img);
|
|
|
|
painter->drawImage(_textRect.x() - 1, _textRect.y(), img);
|
|
|
|
painter->drawImage(_textRect.x() + 1, _textRect.y(), img);
|
|
|
|
|
|
|
|
if (_color) {
|
|
|
|
painter->setFont(*_font);
|
|
|
|
painter->setPen(*_color);
|
|
|
|
painter->drawText(_textRect, FLAGS, *_text);
|
|
|
|
} else {
|
2019-05-15 21:29:34 +02:00
|
|
|
#if QT_VERSION >= QT_VERSION_CHECK(5, 4, 0)
|
2019-07-08 21:19:25 +02:00
|
|
|
img.invertPixels();
|
|
|
|
painter->drawImage(_textRect, img);
|
2019-05-15 21:29:34 +02:00
|
|
|
#else // QT >= 5.4
|
2019-07-08 21:19:25 +02:00
|
|
|
QImage iimg(img.convertToFormat(QImage::Format_ARGB32));
|
|
|
|
iimg.invertPixels();
|
|
|
|
painter->drawImage(_textRect, iimg);
|
2019-05-15 21:29:34 +02:00
|
|
|
#endif // QT >= 5.4
|
2019-07-08 21:19:25 +02:00
|
|
|
}
|
2019-05-10 18:56:19 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
//painter->setPen(Qt::red);
|
|
|
|
//painter->drawRect(_rect);
|
|
|
|
}
|