Some more code cleanup

This commit is contained in:
Martin Tůma 2018-11-25 12:46:38 +01:00
parent 2637594c0a
commit 9a314c408e
7 changed files with 61 additions and 78 deletions

View File

@ -164,8 +164,7 @@ static void drawLayer(const Layer &layer, Style *style, int styleLayer,
QSizeF factor(tile.size().width() / scale.x() / (qreal)layer.data()->extent(), QSizeF factor(tile.size().width() / scale.x() / (qreal)layer.data()->extent(),
tile.size().height() / scale.y() / (qreal)layer.data()->extent()); tile.size().height() / scale.y() / (qreal)layer.data()->extent());
style->setPainter(tile, styleLayer); style->setupLayer(tile, styleLayer);
style->setTextProperties(tile, styleLayer);
for (int i = 0; i < layer.features().size(); i++) for (int i = 0; i < layer.features().size(); i++)
drawFeature(layer.features().at(i), style, styleLayer, factor, tile); drawFeature(layer.features().at(i), style, styleLayer, factor, tile);

View File

@ -430,44 +430,30 @@ void Style::Layer::setPathPainter(Tile &tile, const Sprites &sprites) const
p.setOpacity(_paint.opacity(_type, tile.zoom())); p.setOpacity(_paint.opacity(_type, tile.zoom()));
} }
void Style::Layer::setSymbolPainter(Tile &tile) const
{
QPen pen(_paint.pen(_type, tile.zoom()));
QFont font(_layout.font(tile.zoom()));
QPainter &p = tile.painter();
p.setPen(pen);
p.setFont(font);
}
void Style::Layer::setTextProperties(Tile &tile) const void Style::Layer::setTextProperties(Tile &tile) const
{ {
Text::Properties prop; tile.text().setMaxWidth(_layout.maxTextWidth(tile.zoom()));
prop.maxWidth = _layout.maxTextWidth(tile.zoom()); tile.text().setMaxAngle(_layout.maxTextAngle(tile.zoom()));
prop.maxAngle = _layout.maxTextAngle(tile.zoom()); tile.text().setAnchor(_layout.textAnchor(tile.zoom()));
prop.anchor = _layout.textAnchor(tile.zoom()); tile.text().setPen(_paint.pen(_type, tile.zoom()));
tile.text().setFont(_layout.font(tile.zoom()));
tile.text().setProperties(prop);
} }
void Style::Layer::addSymbol(Tile &tile, const QPainterPath &path, void Style::Layer::addSymbol(Tile &tile, const QPainterPath &path,
const QVariantHash &tags, const Sprites &sprites) const const QVariantHash &tags, const Sprites &sprites) const
{ {
QString text = _layout.text(tile.zoom(), tags); QString text = _layout.text(tile.zoom(), tags);
QString tt(text.trimmed()); if (text.isEmpty())
if (tt.isEmpty())
return; return;
QString icon = _layout.icon(tile.zoom(), tags); QString icon = _layout.icon(tile.zoom(), tags);
if (_layout.viewportAlignment()) if (_layout.viewportAlignment())
tile.text().addLabel(tt, path.elementAt(0), tile.painter(), false, tile.text().addLabel(text, path.elementAt(0), false, sprites.icon(icon));
sprites.icon(icon));
else if (path.elementCount() == 1 && path.elementAt(0).isMoveTo()) else if (path.elementCount() == 1 && path.elementAt(0).isMoveTo())
tile.text().addLabel(tt, path.elementAt(0), tile.painter(), true, tile.text().addLabel(text, path.elementAt(0), true, sprites.icon(icon));
sprites.icon(icon));
else else
tile.text().addLabel(tt, path, tile.painter()); tile.text().addLabel(text, path);
} }
bool Style::load(const QString &fileName) bool Style::load(const QString &fileName)
@ -516,20 +502,14 @@ bool Style::match(int zoom, int layer, const QVariantHash &tags) const
return _layers.at(layer).match(zoom, tags); return _layers.at(layer).match(zoom, tags);
} }
void Style::setTextProperties(Tile &tile, int layer) const void Style::setupLayer(Tile &tile, int layer) const
{ {
const Layer &sl = _layers.at(layer); const Layer &sl = _layers.at(layer);
if (sl.isSymbol())
sl.setTextProperties(tile); sl.setTextProperties(tile);
} else if (sl.isPath())
void Style::setPainter(Tile &tile, int layer) const
{
const Layer &sl = _layers.at(layer);
if (sl.isPath())
sl.setPathPainter(tile, _sprites); sl.setPathPainter(tile, _sprites);
else if (sl.isSymbol())
sl.setSymbolPainter(tile);
} }
void Style::drawFeature(Tile &tile, int layer, const QPainterPath &path, void Style::drawFeature(Tile &tile, int layer, const QPainterPath &path,

View File

@ -30,8 +30,7 @@ public:
bool match(int zoom, int layer, const QVariantHash &tags) const; bool match(int zoom, int layer, const QVariantHash &tags) const;
void drawBackground(Tile &tile) const; void drawBackground(Tile &tile) const;
void setPainter(Tile &tile, int layer) const; void setupLayer(Tile &tile, int layer) const;
void setTextProperties(Tile &tile, int layer) const;
void drawFeature(Tile &tile, int layer, const QPainterPath &path, void drawFeature(Tile &tile, int layer, const QPainterPath &path,
const QVariantHash &tags) const; const QVariantHash &tags) const;
@ -48,7 +47,6 @@ private:
bool match(int zoom, const QVariantHash &tags) const; bool match(int zoom, const QVariantHash &tags) const;
void setPathPainter(Tile &tile, const Sprites &sprites) const; void setPathPainter(Tile &tile, const Sprites &sprites) const;
void setSymbolPainter(Tile &tile) const;
void setTextProperties(Tile &tile) const; void setTextProperties(Tile &tile) const;
void addSymbol(Tile &tile, const QPainterPath &path, void addSymbol(Tile &tile, const QPainterPath &path,
const QVariantHash &tags, const Sprites &sprites) const; const QVariantHash &tags, const Sprites &sprites) const;
@ -104,7 +102,7 @@ private:
qreal maxTextAngle(int zoom) const qreal maxTextAngle(int zoom) const
{return _textMaxAngle.value(zoom);} {return _textMaxAngle.value(zoom);}
QString text(int zoom, const QVariantHash &tags) const QString text(int zoom, const QVariantHash &tags) const
{return _text.value(zoom, tags);} {return _text.value(zoom, tags).trimmed();}
QString icon(int zoom, const QVariantHash &tags) const QString icon(int zoom, const QVariantHash &tags) const
{return _icon.value(zoom, tags);} {return _icon.value(zoom, tags);}
QFont font(int zoom) const; QFont font(int zoom) const;

View File

@ -149,16 +149,16 @@ void Text::render(QPainter *painter) const
} }
} }
void Text::addLabel(const QString &text, const QPointF &pos, void Text::addLabel(const QString &text, const QPointF &pos, bool overlap,
const QPainter &painter, bool overlap, const QImage &icon) const QImage &icon)
{ {
TextPointItem *ti = new TextPointItem(text, pos, painter.font(), TextPointItem *ti = new TextPointItem(text, pos, _font, _maxWidth, _anchor,
_properties, icon); icon);
if (!overlap && !_sceneRect.contains(ti->boundingRect())) { if (!overlap && !_sceneRect.contains(ti->boundingRect())) {
delete ti; delete ti;
return; return;
} }
ti->setPen(painter.pen()); ti->setPen(_pen);
addItem(ti); addItem(ti);
QList<TextItem*> ci = collidingItems(ti); QList<TextItem*> ci = collidingItems(ti);
@ -166,25 +166,24 @@ void Text::addLabel(const QString &text, const QPointF &pos,
ci[i]->setVisible(false); ci[i]->setVisible(false);
} }
void Text::addLabel(const QString &text, const QPainterPath &path, void Text::addLabel(const QString &text, const QPainterPath &path)
const QPainter &painter)
{ {
int textWidth = text.size() * painter.font().pixelSize() * 0.6; int textWidth = text.size() * _font.pixelSize() * 0.6;
if (textWidth > path.length()) if (textWidth > path.length())
return; return;
QPainterPath tp(textPath(path, textWidth, _properties.maxAngle, QPainterPath tp(textPath(path, textWidth, _maxAngle, _font.pixelSize() / 2,
painter.font().pixelSize() / 2, _sceneRect)); _sceneRect));
if (tp.isEmpty()) if (tp.isEmpty())
return; return;
TextPathItem *ti = new TextPathItem(text, reverse(tp) ? tp.toReversed() TextPathItem *ti = new TextPathItem(text, reverse(tp) ? tp.toReversed()
: tp, painter.font()); : tp, _font);
if (!_sceneRect.contains(ti->boundingRect())) { if (!_sceneRect.contains(ti->boundingRect())) {
delete ti; delete ti;
return; return;
} }
ti->setPen(painter.pen()); ti->setPen(_pen);
addItem(ti); addItem(ti);

View File

@ -1,8 +1,14 @@
#ifndef TEXT_H #ifndef TEXT_H
#define TEXT_H #define TEXT_H
#include <QImage> #include <QList>
#include <QRectF>
#include <QFont>
#include <QPen>
class QImage;
class QPainterPath;
class QPainter;
class TextItem; class TextItem;
class Text class Text
@ -16,24 +22,19 @@ public:
Bottom Bottom
}; };
struct Properties {
int maxWidth;
int maxAngle;
Anchor anchor;
};
Text(const QSize &size) Text(const QSize &size)
: _sceneRect(QRectF(QPointF(0, 0), size)) {} : _sceneRect(QRectF(QPointF(0, 0), size)) {}
~Text(); ~Text();
void setProperties(const Properties &prop) void setFont(const QFont &font) {_font = font;}
{_properties = prop;} void setPen(const QPen &pen) {_pen = pen;}
void setAnchor(Anchor anchor) {_anchor = anchor;}
void setMaxWidth(int width) {_maxWidth = width;}
void setMaxAngle(int angle) {_maxAngle = angle;}
void addLabel(const QString &text, const QPointF &pos, void addLabel(const QString &text, const QPointF &pos, bool overlap,
const QPainter &painter, bool overlap, const QImage &icon); const QImage &icon);
void addLabel(const QString &text, const QPainterPath &path, void addLabel(const QString &text, const QPainterPath &path);
const QPainter &painter);
void render(QPainter *painter) const; void render(QPainter *painter) const;
@ -43,7 +44,12 @@ private:
QRectF _sceneRect; QRectF _sceneRect;
QList<TextItem *> _items; QList<TextItem *> _items;
Properties _properties;
int _maxWidth;
int _maxAngle;
Anchor _anchor;
QFont _font;
QPen _pen;
}; };
#endif // TEXT_H #endif // TEXT_H

View File

@ -6,10 +6,10 @@
#define FLAGS (Qt::AlignCenter | Qt::TextWordWrap | Qt::TextDontClip) #define FLAGS (Qt::AlignCenter | Qt::TextWordWrap | Qt::TextDontClip)
static QRectF exactBoundingRect(const QString &str, const QFont &font, static QRectF exactBoundingRect(const QString &str, const QFont &font,
const Text::Properties &prop) int maxWidth)
{ {
QFontMetrics fm(font); QFontMetrics fm(font);
int limit = font.pixelSize() * prop.maxWidth; int limit = font.pixelSize() * maxWidth;
// Italic fonts overflow the computed bounding rect, so reduce it // Italic fonts overflow the computed bounding rect, so reduce it
// a little bit. // a little bit.
if (font.italic()) if (font.italic())
@ -25,9 +25,9 @@ static QRectF exactBoundingRect(const QString &str, const QFont &font,
} }
static QRectF fuzzyBoundingRect(const QString &str, const QFont &font, static QRectF fuzzyBoundingRect(const QString &str, const QFont &font,
const Text::Properties &prop) int maxWidth)
{ {
int limit = font.pixelSize() * prop.maxWidth; int limit = font.pixelSize() * maxWidth;
qreal acw = (font.capitalization() == QFont::AllUppercase) ? 0.66 : 0.55; qreal acw = (font.capitalization() == QFont::AllUppercase) ? 0.66 : 0.55;
qreal cw = font.pixelSize() * acw; qreal cw = font.pixelSize() * acw;
if (font.bold()) if (font.bold())
@ -72,9 +72,9 @@ static QRectF fuzzyBoundingRect(const QString &str, const QFont &font,
QRectF TextPointItem::computeTextRect(BoundingRectFunction brf) const QRectF TextPointItem::computeTextRect(BoundingRectFunction brf) const
{ {
QRectF iconRect = _icon.isNull() ? QRectF() : _icon.rect(); QRectF iconRect = _icon.isNull() ? QRectF() : _icon.rect();
QRectF textRect = brf(text(), _font, _properties); QRectF textRect = brf(text(), _font, _maxWidth);
switch (_properties.anchor) { switch (_anchor) {
case Text::Center: case Text::Center:
textRect.moveCenter(_pos); textRect.moveCenter(_pos);
break; break;
@ -100,8 +100,9 @@ QRectF TextPointItem::computeTextRect(BoundingRectFunction brf) const
} }
TextPointItem::TextPointItem(const QString &text, const QPointF &pos, TextPointItem::TextPointItem(const QString &text, const QPointF &pos,
const QFont &font, const Text::Properties &prop, const QImage &icon) const QFont &font, int maxWidth, Text::Anchor anchor, const QImage &icon)
: TextItem(text), _pos(pos), _font(font), _icon(icon), _properties(prop) : TextItem(text), _pos(pos), _font(font), _icon(icon), _maxWidth(maxWidth),
_anchor(anchor)
{ {
_boundingRect = computeTextRect(fuzzyBoundingRect); _boundingRect = computeTextRect(fuzzyBoundingRect);

View File

@ -11,7 +11,7 @@ class TextPointItem : public TextItem
{ {
public: public:
TextPointItem(const QString &text, const QPointF &pos, const QFont &font, TextPointItem(const QString &text, const QPointF &pos, const QFont &font,
const Text::Properties &prop, const QImage &icon); int maxWidth, Text::Anchor anchor, const QImage &icon);
QRectF boundingRect() const {return _boundingRect;} QRectF boundingRect() const {return _boundingRect;}
QPainterPath shape() const {return _shape;} QPainterPath shape() const {return _shape;}
@ -20,8 +20,7 @@ public:
void setPen(const QPen &pen) {_pen = pen;} void setPen(const QPen &pen) {_pen = pen;}
private: private:
typedef QRectF (*BoundingRectFunction)(const QString &, const QFont &, typedef QRectF (*BoundingRectFunction)(const QString &, const QFont &, int);
const Text::Properties &);
QRectF computeTextRect(BoundingRectFunction brf) const; QRectF computeTextRect(BoundingRectFunction brf) const;
QPointF _pos; QPointF _pos;
@ -30,7 +29,8 @@ private:
QFont _font; QFont _font;
QPen _pen; QPen _pen;
QImage _icon; QImage _icon;
Text::Properties _properties; int _maxWidth;
Text::Anchor _anchor;
}; };
#endif // TEXTPOINTITEM_H #endif // TEXTPOINTITEM_H