1
0
mirror of https://github.com/tumic0/GPXSee.git synced 2024-11-28 05:34:47 +01:00

Improved polygon labels layout logic

This commit is contained in:
Martin Tůma 2020-10-17 14:26:59 +02:00
parent 7c6174a8ee
commit 630a5cea83
6 changed files with 36 additions and 9 deletions

View File

@ -237,10 +237,27 @@ void RasterTile::drawTextItems(QPainter *painter,
textItems.at(i)->paint(painter); textItems.at(i)->paint(painter);
} }
static void removeDuplicitLabel(QList<TextItem *> &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<TextItem*> &textItems) void RasterTile::processPolygons(QList<TextItem*> &textItems)
{ {
QRectF tileRect(_xy, _img.size());
QSet<QString> set;
QList<TextItem *> labels;
for (int i = 0; i < _polygons.size(); i++) { for (int i = 0; i < _polygons.size(); i++) {
MapData::Poly &poly = _polygons[i]; MapData::Poly &poly = _polygons[i];
bool exists = set.contains(poly.label.text());
if (poly.label.text().isEmpty()) if (poly.label.text().isEmpty())
continue; continue;
@ -253,12 +270,19 @@ void RasterTile::processPolygons(QList<TextItem*> &textItems)
centroid(poly.points).toPoint(), &poly.label.text(), centroid(poly.points).toPoint(), &poly.label.text(),
poiFont(), 0, &style.brush().color()); poiFont(), 0, &style.brush().color());
if (item->isValid() && !item->collides(textItems) if (item->isValid() && !item->collides(textItems)
&& rectNearPolygon(poly.points, item->boundingRect())) && !(exists && tileRect.contains(item->boundingRect()))
textItems.append(item); && rectNearPolygon(poly.points, item->boundingRect())) {
else if (exists)
removeDuplicitLabel(labels, poly.label.text(), tileRect);
else
set.insert(poly.label.text());
labels.append(item);
} else
delete item; delete item;
} }
} }
textItems.append(labels);
} }
void RasterTile::processLines(QList<TextItem*> &textItems) void RasterTile::processLines(QList<TextItem*> &textItems)

View File

@ -10,13 +10,18 @@ class QPainter;
class TextItem class TextItem
{ {
public: public:
TextItem(const QString *text) : _text(text) {}
virtual ~TextItem() {} virtual ~TextItem() {}
virtual QPainterPath shape() const = 0; virtual QPainterPath shape() const = 0;
virtual QRectF boundingRect() const = 0; virtual QRectF boundingRect() const = 0;
virtual void paint(QPainter *painter) const = 0; virtual void paint(QPainter *painter) const = 0;
const QString *text() const {return _text;}
bool collides(const QList<TextItem*> &list) const; bool collides(const QList<TextItem*> &list) const;
protected:
const QString *_text;
}; };
#endif // TEXTITEM_H #endif // TEXTITEM_H

View File

@ -137,7 +137,7 @@ static bool reverse(const QPainterPath &path)
TextPathItem::TextPathItem(const QPolygonF &line, const QString *label, TextPathItem::TextPathItem(const QPolygonF &line, const QString *label,
const QRect &tileRect, const QFont *font, const QColor *color) 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 cw = font->pixelSize() * 0.7;
qreal textWidth = _text->size() * cw; qreal textWidth = _text->size() * cw;

View File

@ -8,7 +8,7 @@
class TextPathItem : public TextItem class TextPathItem : public TextItem
{ {
public: public:
TextPathItem() : _text(0), _font(0), _color(0) {} TextPathItem() : TextItem(0), _font(0), _color(0) {}
TextPathItem(const QPolygonF &line, const QString *label, TextPathItem(const QPolygonF &line, const QString *label,
const QRect &tileRect, const QFont *font, const QColor *color); const QRect &tileRect, const QFont *font, const QColor *color);
@ -19,7 +19,6 @@ public:
void paint(QPainter *painter) const; void paint(QPainter *painter) const;
private: private:
const QString *_text;
const QFont *_font; const QFont *_font;
const QColor *_color; const QColor *_color;
QPainterPath _path; QPainterPath _path;

View File

@ -17,7 +17,7 @@ static void expand(QRect &rect, int width)
TextPointItem::TextPointItem(const QPoint &point, const QString *text, TextPointItem::TextPointItem(const QPoint &point, const QString *text,
const QFont *font, const QImage *img, const QColor *color, 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) _color(color), _bgColor(bgColor)
{ {
if (_text) { if (_text) {

View File

@ -14,7 +14,7 @@ class QColor;
class TextPointItem : public TextItem class TextPointItem : public TextItem
{ {
public: 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, TextPointItem(const QPoint &point, const QString *text, const QFont *font,
const QImage *img, const QColor *color, const QColor *bgColor = 0); const QImage *img, const QColor *color, const QColor *bgColor = 0);
@ -27,7 +27,6 @@ public:
void setPos(const QPoint &point); void setPos(const QPoint &point);
private: private:
const QString *_text;
const QFont *_font; const QFont *_font;
const QImage *_img; const QImage *_img;
const QColor *_color, *_bgColor; const QColor *_color, *_bgColor;