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:
parent
7c6174a8ee
commit
630a5cea83
@ -237,10 +237,27 @@ void RasterTile::drawTextItems(QPainter *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)
|
||||
{
|
||||
QRectF tileRect(_xy, _img.size());
|
||||
QSet<QString> set;
|
||||
QList<TextItem *> 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<TextItem*> &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<TextItem*> &textItems)
|
||||
|
@ -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<TextItem*> &list) const;
|
||||
|
||||
protected:
|
||||
const QString *_text;
|
||||
};
|
||||
|
||||
#endif // TEXTITEM_H
|
||||
|
@ -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;
|
||||
|
@ -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;
|
||||
|
@ -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) {
|
||||
|
@ -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;
|
||||
|
Loading…
Reference in New Issue
Block a user