From 5d68f311240bc7a219e0644e8e3e001edef777fb Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Martin=20T=C5=AFma?= Date: Mon, 29 Oct 2018 18:30:55 +0100 Subject: [PATCH] Use a dedicated graphics item for drawing the text instead a pixmap --- pbfplugin.pro | 6 ++++-- src/text.cpp | 30 ++++++++++-------------------- src/textitem.cpp | 29 +++++++++++++++++++++++++++++ src/textitem.h | 28 ++++++++++++++++++++++++++++ 4 files changed, 71 insertions(+), 22 deletions(-) create mode 100644 src/textitem.cpp create mode 100644 src/textitem.h diff --git a/pbfplugin.pro b/pbfplugin.pro index 16e475b..6d2121f 100644 --- a/pbfplugin.pro +++ b/pbfplugin.pro @@ -18,7 +18,8 @@ HEADERS += src/pbfhandler.h \ src/text.h \ src/tile.h \ src/function.h \ - src/textpathitem.h + src/textpathitem.h \ + src/textitem.h SOURCES += src/pbfplugin.cpp \ src/pbfhandler.cpp \ src/gzip.cpp \ @@ -27,7 +28,8 @@ SOURCES += src/pbfplugin.cpp \ src/color.cpp \ src/text.cpp \ src/function.cpp \ - src/textpathitem.cpp + src/textpathitem.cpp \ + src/textitem.cpp LIBS += -lprotobuf-lite diff --git a/src/text.cpp b/src/text.cpp index a02accc..a7d554a 100644 --- a/src/text.cpp +++ b/src/text.cpp @@ -2,6 +2,7 @@ #include #include #include "text.h" +#include "textitem.h" #include "textpathitem.h" @@ -51,27 +52,16 @@ void Text::addLabel(const QString &text, const QPointF &pos, const QFont &font, if (text.isEmpty()) return; - QFontMetrics fm(font); - int limit = fm.width('M') * maxTextWidth; - int flags = Qt::AlignCenter | Qt::TextWordWrap | Qt::TextDontClip; - - QRect br = fm.boundingRect(QRect(0, 0, limit, 0), flags, text); - if (!br.isValid()) + TextItem *ti = new TextItem(text, pos, font, maxTextWidth); + addItem(ti); + if (!sceneRect().contains(ti->sceneBoundingRect())) { + delete ti; return; - br.moveTo((pos - QPointF(br.width() / 2.0, br.height() / 2.0)).toPoint()); - if (!sceneRect().contains(br)) - return; - QPixmap pm(br.size()); - pm.fill(Qt::transparent); - QPainter p(&pm); - p.setFont(font); - p.setPen(pen); - p.drawText(pm.rect(), flags, text); + } - QGraphicsPixmapItem *pi = addPixmap(pm); - pi->setPos(br.topLeft()); + ti->setPen(pen); - QList ci = collidingItems(pi); + QList ci = collidingItems(ti); for (int i = 0; i < ci.size(); i++) ci[i]->setVisible(false); } @@ -95,14 +85,14 @@ void Text::addLabel(const QString &text, const QPainterPath &path, const QPainterPath &segment = list.at(i); TextPathItem *pi = new TextPathItem(text, reverse(segment) ? segment.toReversed() : segment, font); - pi->setPen(pen); addItem(pi); - if (!sceneRect().contains(pi->sceneBoundingRect())) { delete pi; continue; } + pi->setPen(pen); + QList ci = collidingItems(pi); for (int j = 0; j < ci.size(); j++) ci[j]->setVisible(false); diff --git a/src/textitem.cpp b/src/textitem.cpp new file mode 100644 index 0000000..c996eed --- /dev/null +++ b/src/textitem.cpp @@ -0,0 +1,29 @@ +#include +#include "textitem.h" + + +#define FLAGS (Qt::AlignCenter | Qt::TextWordWrap | Qt::TextDontClip) + +TextItem::TextItem(const QString &text, const QPointF &pos, const QFont &font, + int maxTextWidth, QGraphicsItem *parent) : QGraphicsItem(parent), _text(text), + _font(font) +{ + QFontMetrics fm(font); + int limit = fm.width('M') * maxTextWidth; + + QRect br = fm.boundingRect(QRect(0, 0, limit, 0), FLAGS, text); + Q_ASSERT(br.isValid()); + setPos((pos - QPointF(br.width() / 2.0, br.height() / 2.0)).toPoint()); + _boundingRect = QRectF(0, 0, br.width(), br.height()); +} + +void TextItem::paint(QPainter *painter, const QStyleOptionGraphicsItem *option, + QWidget *widget) +{ + Q_UNUSED(option); + Q_UNUSED(widget); + + painter->setFont(_font); + painter->setPen(_pen); + painter->drawText(_boundingRect, FLAGS, _text); +} diff --git a/src/textitem.h b/src/textitem.h new file mode 100644 index 0000000..503da89 --- /dev/null +++ b/src/textitem.h @@ -0,0 +1,28 @@ +#ifndef TEXTITEM_H +#define TEXTITEM_H + +#include +#include +#include +#include + +class TextItem : public QGraphicsItem +{ +public: + TextItem(const QString &text, const QPointF &pos, const QFont &font, + int maxTextWidth, QGraphicsItem *parent = 0); + + QRectF boundingRect() const {return _boundingRect;} + void paint(QPainter *painter, const QStyleOptionGraphicsItem *option, + QWidget *widget); + + void setPen(const QPen &pen) {_pen = pen;} + +private: + QString _text; + QRectF _boundingRect; + QFont _font; + QPen _pen; +}; + +#endif // TEXTITEM_H