From 630a5cea838c19282f3dcafae64af5a83c01bc7d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Martin=20T=C5=AFma?= Date: Sat, 17 Oct 2020 14:26:59 +0200 Subject: [PATCH] Improved polygon labels layout logic --- src/map/IMG/rastertile.cpp | 30 +++++++++++++++++++++++++++--- src/map/IMG/textitem.h | 5 +++++ src/map/IMG/textpathitem.cpp | 2 +- src/map/IMG/textpathitem.h | 3 +-- src/map/IMG/textpointitem.cpp | 2 +- src/map/IMG/textpointitem.h | 3 +-- 6 files changed, 36 insertions(+), 9 deletions(-) diff --git a/src/map/IMG/rastertile.cpp b/src/map/IMG/rastertile.cpp index 3bf573b7..3a0acbce 100644 --- a/src/map/IMG/rastertile.cpp +++ b/src/map/IMG/rastertile.cpp @@ -237,10 +237,27 @@ void RasterTile::drawTextItems(QPainter *painter, textItems.at(i)->paint(painter); } +static void removeDuplicitLabel(QList &labels, const QString &text, + const QRectF &tileRect) +{ + for (int j = 0; j < labels.size(); j++) { + TextItem *item = labels.at(j); + if (tileRect.contains(item->boundingRect()) && *(item->text()) == text) { + labels.removeOne(item); + return; + } + } +} + void RasterTile::processPolygons(QList &textItems) { + QRectF tileRect(_xy, _img.size()); + QSet set; + QList labels; + for (int i = 0; i < _polygons.size(); i++) { MapData::Poly &poly = _polygons[i]; + bool exists = set.contains(poly.label.text()); if (poly.label.text().isEmpty()) continue; @@ -253,12 +270,19 @@ void RasterTile::processPolygons(QList &textItems) centroid(poly.points).toPoint(), &poly.label.text(), poiFont(), 0, &style.brush().color()); if (item->isValid() && !item->collides(textItems) - && rectNearPolygon(poly.points, item->boundingRect())) - textItems.append(item); - else + && !(exists && tileRect.contains(item->boundingRect())) + && rectNearPolygon(poly.points, item->boundingRect())) { + if (exists) + removeDuplicitLabel(labels, poly.label.text(), tileRect); + else + set.insert(poly.label.text()); + labels.append(item); + } else delete item; } } + + textItems.append(labels); } void RasterTile::processLines(QList &textItems) diff --git a/src/map/IMG/textitem.h b/src/map/IMG/textitem.h index e3121e7b..2baca50e 100644 --- a/src/map/IMG/textitem.h +++ b/src/map/IMG/textitem.h @@ -10,13 +10,18 @@ class QPainter; class TextItem { public: + TextItem(const QString *text) : _text(text) {} virtual ~TextItem() {} virtual QPainterPath shape() const = 0; virtual QRectF boundingRect() const = 0; virtual void paint(QPainter *painter) const = 0; + const QString *text() const {return _text;} bool collides(const QList &list) const; + +protected: + const QString *_text; }; #endif // TEXTITEM_H diff --git a/src/map/IMG/textpathitem.cpp b/src/map/IMG/textpathitem.cpp index 504b45dd..6af30ef3 100644 --- a/src/map/IMG/textpathitem.cpp +++ b/src/map/IMG/textpathitem.cpp @@ -137,7 +137,7 @@ static bool reverse(const QPainterPath &path) TextPathItem::TextPathItem(const QPolygonF &line, const QString *label, const QRect &tileRect, const QFont *font, const QColor *color) - : _text(label), _font(font), _color(color) + : TextItem(label), _font(font), _color(color) { qreal cw = font->pixelSize() * 0.7; qreal textWidth = _text->size() * cw; diff --git a/src/map/IMG/textpathitem.h b/src/map/IMG/textpathitem.h index cc33cd1a..3ef7a1e4 100644 --- a/src/map/IMG/textpathitem.h +++ b/src/map/IMG/textpathitem.h @@ -8,7 +8,7 @@ class TextPathItem : public TextItem { public: - TextPathItem() : _text(0), _font(0), _color(0) {} + TextPathItem() : TextItem(0), _font(0), _color(0) {} TextPathItem(const QPolygonF &line, const QString *label, const QRect &tileRect, const QFont *font, const QColor *color); @@ -19,7 +19,6 @@ public: void paint(QPainter *painter) const; private: - const QString *_text; const QFont *_font; const QColor *_color; QPainterPath _path; diff --git a/src/map/IMG/textpointitem.cpp b/src/map/IMG/textpointitem.cpp index dfed98e3..9d90c281 100644 --- a/src/map/IMG/textpointitem.cpp +++ b/src/map/IMG/textpointitem.cpp @@ -17,7 +17,7 @@ static void expand(QRect &rect, int width) TextPointItem::TextPointItem(const QPoint &point, const QString *text, const QFont *font, const QImage *img, const QColor *color, - const QColor *bgColor) : _text(font ? text : 0), _font(font), _img(img), + const QColor *bgColor) : TextItem(font ? text : 0), _font(font), _img(img), _color(color), _bgColor(bgColor) { if (_text) { diff --git a/src/map/IMG/textpointitem.h b/src/map/IMG/textpointitem.h index c4190a7d..7aa9b5ca 100644 --- a/src/map/IMG/textpointitem.h +++ b/src/map/IMG/textpointitem.h @@ -14,7 +14,7 @@ class QColor; class TextPointItem : public TextItem { public: - TextPointItem() : _text(0), _font(0), _img(0) {} + TextPointItem() : TextItem(0), _font(0), _img(0) {} TextPointItem(const QPoint &point, const QString *text, const QFont *font, const QImage *img, const QColor *color, const QColor *bgColor = 0); @@ -27,7 +27,6 @@ public: void setPos(const QPoint &point); private: - const QString *_text; const QFont *_font; const QImage *_img; const QColor *_color, *_bgColor;