2018-11-22 21:57:21 +01:00
|
|
|
#include <QPainter>
|
2018-11-11 14:10:38 +01:00
|
|
|
#include <QtMath>
|
|
|
|
#include "textpointitem.h"
|
|
|
|
|
|
|
|
|
|
|
|
#define FLAGS (Qt::AlignCenter | Qt::TextWordWrap | Qt::TextDontClip)
|
|
|
|
|
2018-12-11 18:48:12 +01:00
|
|
|
QRectF TextPointItem::exactBoundingRect() const
|
2018-11-19 23:09:34 +01:00
|
|
|
{
|
2018-12-11 18:48:12 +01:00
|
|
|
QFontMetrics fm(font());
|
|
|
|
int limit = font().pixelSize() * _maxWidth;
|
2018-11-19 23:09:34 +01:00
|
|
|
// Italic fonts overflow the computed bounding rect, so reduce it
|
|
|
|
// a little bit.
|
2018-12-11 18:48:12 +01:00
|
|
|
if (font().italic())
|
|
|
|
limit -= font().pixelSize() / 2.0;
|
2018-11-19 23:09:34 +01:00
|
|
|
|
2018-12-11 18:48:12 +01:00
|
|
|
QRect br = fm.boundingRect(QRect(0, 0, limit, 0), FLAGS, text());
|
2018-11-19 23:09:34 +01:00
|
|
|
Q_ASSERT(br.isValid());
|
|
|
|
// Expand the bounding rect back to the real content size
|
2018-12-11 18:48:12 +01:00
|
|
|
if (font().italic())
|
|
|
|
br.adjust(-font().pixelSize() / 4.0, 0, font().pixelSize() / 4.0, 0);
|
2018-11-19 23:09:34 +01:00
|
|
|
|
|
|
|
return br;
|
|
|
|
}
|
|
|
|
|
2018-12-11 18:48:12 +01:00
|
|
|
QRectF TextPointItem::fuzzyBoundingRect() const
|
2018-11-11 14:10:38 +01:00
|
|
|
{
|
2018-12-11 18:48:12 +01:00
|
|
|
int limit = font().pixelSize() * _maxWidth;
|
|
|
|
qreal cw = avgCharWidth();
|
|
|
|
qreal lh = font().pixelSize() * 1.25;
|
2018-11-11 14:10:38 +01:00
|
|
|
int width = 0, lines = 0;
|
|
|
|
|
2018-12-11 18:48:12 +01:00
|
|
|
QStringList l(text().split('\n'));
|
2018-11-11 14:10:38 +01:00
|
|
|
for (int i = 0; i < l.size(); i++) {
|
|
|
|
int lw = (int)(l.at(i).length() * cw);
|
|
|
|
if (lw > limit) {
|
|
|
|
l[i].replace('-', ' ');
|
|
|
|
l[i].replace('/', ' ');
|
|
|
|
QStringList words(l.at(i).split(' '));
|
|
|
|
int pl = 0;
|
|
|
|
for (int j = 0; j < words.size(); j++) {
|
|
|
|
int wl = (int)(words.at(j).length() * cw);
|
|
|
|
if (wl + pl < limit) {
|
|
|
|
pl += wl + cw;
|
|
|
|
} else {
|
|
|
|
if (wl > limit) {
|
|
|
|
if (pl > 0)
|
|
|
|
lines++;
|
|
|
|
} else
|
|
|
|
lines++;
|
|
|
|
width = qMax(width, qMax(pl, wl));
|
|
|
|
pl = wl;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
width = qMax(width, pl);
|
|
|
|
lines++;
|
|
|
|
} else {
|
|
|
|
width = qMax(width, lw);
|
|
|
|
lines++;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return QRectF(0, 0, width, lines * lh);
|
|
|
|
}
|
2018-11-22 21:57:21 +01:00
|
|
|
|
|
|
|
|
2018-12-11 18:48:12 +01:00
|
|
|
QRectF TextPointItem::computeTextRect(bool exact) const
|
2018-11-22 21:57:21 +01:00
|
|
|
{
|
2018-12-04 00:09:23 +01:00
|
|
|
QRectF iconRect = _icon.isNull() ? QRectF()
|
|
|
|
: QRectF(QPointF(0, 0), QSizeF(_icon.size()) / _icon.devicePixelRatioF());
|
2018-12-11 18:48:12 +01:00
|
|
|
QRectF textRect = exact ? exactBoundingRect() : fuzzyBoundingRect();
|
2018-11-22 21:57:21 +01:00
|
|
|
|
2018-11-25 12:46:38 +01:00
|
|
|
switch (_anchor) {
|
2018-11-22 21:57:21 +01:00
|
|
|
case Text::Center:
|
|
|
|
textRect.moveCenter(_pos);
|
|
|
|
break;
|
|
|
|
case Text::Left:
|
|
|
|
textRect.moveTopLeft(_pos - QPointF(-iconRect.width() / 2,
|
|
|
|
textRect.height() / 2));
|
|
|
|
break;
|
|
|
|
case Text::Right:
|
|
|
|
textRect.moveTopRight(_pos - QPointF(iconRect.width() / 2,
|
|
|
|
textRect.height() / 2));
|
|
|
|
break;
|
|
|
|
case Text::Bottom:
|
|
|
|
textRect.moveTopLeft(_pos - QPointF(textRect.width() / 2,
|
|
|
|
iconRect.height() / 2));
|
|
|
|
break;
|
|
|
|
case Text::Top:
|
|
|
|
textRect.moveTopLeft(_pos - QPointF(textRect.width() / 2,
|
|
|
|
-iconRect.height() / 2));
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
return textRect;
|
|
|
|
}
|
2018-11-11 14:10:38 +01:00
|
|
|
|
|
|
|
TextPointItem::TextPointItem(const QString &text, const QPointF &pos,
|
2018-11-25 12:46:38 +01:00
|
|
|
const QFont &font, int maxWidth, Text::Anchor anchor, const QImage &icon)
|
2018-12-11 18:48:12 +01:00
|
|
|
: TextItem(text, font), _pos(pos), _icon(icon), _maxWidth(maxWidth),
|
2018-11-25 12:46:38 +01:00
|
|
|
_anchor(anchor)
|
2018-11-11 14:10:38 +01:00
|
|
|
{
|
2018-12-11 18:48:12 +01:00
|
|
|
_boundingRect = computeTextRect(false);
|
2018-11-22 21:57:21 +01:00
|
|
|
|
|
|
|
if (!_icon.isNull()) {
|
2018-12-04 00:09:23 +01:00
|
|
|
QRectF iconRect(QPointF(0, 0), QSizeF(_icon.size())
|
|
|
|
/ _icon.devicePixelRatioF());
|
2018-11-22 21:57:21 +01:00
|
|
|
iconRect.moveCenter(pos);
|
|
|
|
_boundingRect |= iconRect;
|
|
|
|
}
|
2018-11-11 14:10:38 +01:00
|
|
|
|
|
|
|
_shape.addRect(_boundingRect);
|
|
|
|
}
|
|
|
|
|
|
|
|
void TextPointItem::paint(QPainter *painter) const
|
|
|
|
{
|
|
|
|
//painter->setPen(Qt::red);
|
|
|
|
//painter->drawRect(_boundingRect);
|
2018-11-22 21:57:21 +01:00
|
|
|
|
|
|
|
QRectF textRect;
|
|
|
|
|
|
|
|
if (!_icon.isNull()) {
|
2018-12-11 18:48:12 +01:00
|
|
|
textRect = computeTextRect(true);
|
2018-12-04 00:09:23 +01:00
|
|
|
painter->drawImage(_pos - QPointF(_icon.width()
|
|
|
|
/ _icon.devicePixelRatioF() / 2, _icon.height()
|
|
|
|
/ _icon.devicePixelRatioF() / 2), _icon);
|
2018-11-22 21:57:21 +01:00
|
|
|
} else
|
2018-12-11 18:48:12 +01:00
|
|
|
textRect = _boundingRect;
|
2018-11-22 21:57:21 +01:00
|
|
|
|
2018-11-28 23:39:33 +01:00
|
|
|
painter->setFont(font());
|
|
|
|
painter->setPen(pen());
|
2018-11-22 21:57:21 +01:00
|
|
|
painter->drawText(textRect, FLAGS, text());
|
2018-11-11 14:10:38 +01:00
|
|
|
}
|