QtPBFImagePlugin/src/textpointitem.cpp

137 lines
3.3 KiB
C++
Raw Normal View History

2018-11-22 21:57:21 +01:00
#include <QPainter>
#include <QtMath>
#include "textpointitem.h"
#define FLAGS (Qt::AlignCenter | Qt::TextWordWrap | Qt::TextDontClip)
2018-11-27 19:53:08 +01:00
QRectF TextPointItem::exactBoundingRect(const QString &str,
const QFont &font, int maxWidth)
{
QFontMetrics fm(font);
2018-11-25 12:46:38 +01:00
int limit = font.pixelSize() * maxWidth;
// Italic fonts overflow the computed bounding rect, so reduce it
// a little bit.
if (font.italic())
limit -= font.pixelSize() / 2.0;
QRect br = fm.boundingRect(QRect(0, 0, limit, 0), FLAGS, str);
Q_ASSERT(br.isValid());
// Expand the bounding rect back to the real content size
if (font.italic())
br.adjust(-font.pixelSize() / 4.0, 0, font.pixelSize() / 4.0, 0);
return br;
}
2018-11-27 19:53:08 +01:00
QRectF TextPointItem::fuzzyBoundingRect(const QString &str,
const QFont &font, int maxWidth)
{
2018-11-25 12:46:38 +01:00
int limit = font.pixelSize() * maxWidth;
2018-11-27 19:53:08 +01:00
qreal cw = avgCharWidth(str, font);
qreal lh = font.pixelSize() * 1.25;
int width = 0, lines = 0;
if (font.italic())
limit -= font.pixelSize() / 2.0;
QStringList l(str.split('\n'));
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
QRectF TextPointItem::computeTextRect(BoundingRectFunction brf) const
{
QRectF iconRect = _icon.isNull() ? QRectF() : _icon.rect();
2018-11-28 23:39:33 +01:00
QRectF textRect = brf(text(), font(), _maxWidth);
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;
}
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-11-28 23:39:33 +01:00
: TextItem(text), _pos(pos), _icon(icon), _maxWidth(maxWidth),
2018-11-25 12:46:38 +01:00
_anchor(anchor)
{
2018-11-28 23:39:33 +01:00
setFont(font);
2018-11-22 21:57:21 +01:00
_boundingRect = computeTextRect(fuzzyBoundingRect);
if (!_icon.isNull()) {
QRectF iconRect(_icon.rect());
iconRect.moveCenter(pos);
_boundingRect |= iconRect;
}
_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()) {
textRect = computeTextRect(exactBoundingRect);
painter->drawImage(_pos - QPointF(_icon.width() / 2,
_icon.height() / 2), _icon);
} else
textRect = computeTextRect(fuzzyBoundingRect);
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());
}