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(),
tile.size().height() / scale.y() / (qreal)layer.data()->extent());
style->setPainter(tile, styleLayer);
style->setTextProperties(tile, styleLayer);
style->setupLayer(tile, styleLayer);
for (int i = 0; i < layer.features().size(); i++)
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()));
}
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
{
Text::Properties prop;
prop.maxWidth = _layout.maxTextWidth(tile.zoom());
prop.maxAngle = _layout.maxTextAngle(tile.zoom());
prop.anchor = _layout.textAnchor(tile.zoom());
tile.text().setProperties(prop);
tile.text().setMaxWidth(_layout.maxTextWidth(tile.zoom()));
tile.text().setMaxAngle(_layout.maxTextAngle(tile.zoom()));
tile.text().setAnchor(_layout.textAnchor(tile.zoom()));
tile.text().setPen(_paint.pen(_type, tile.zoom()));
tile.text().setFont(_layout.font(tile.zoom()));
}
void Style::Layer::addSymbol(Tile &tile, const QPainterPath &path,
const QVariantHash &tags, const Sprites &sprites) const
{
QString text = _layout.text(tile.zoom(), tags);
QString tt(text.trimmed());
if (tt.isEmpty())
if (text.isEmpty())
return;
QString icon = _layout.icon(tile.zoom(), tags);
if (_layout.viewportAlignment())
tile.text().addLabel(tt, path.elementAt(0), tile.painter(), false,
sprites.icon(icon));
tile.text().addLabel(text, path.elementAt(0), false, sprites.icon(icon));
else if (path.elementCount() == 1 && path.elementAt(0).isMoveTo())
tile.text().addLabel(tt, path.elementAt(0), tile.painter(), true,
sprites.icon(icon));
tile.text().addLabel(text, path.elementAt(0), true, sprites.icon(icon));
else
tile.text().addLabel(tt, path, tile.painter());
tile.text().addLabel(text, path);
}
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);
}
void Style::setTextProperties(Tile &tile, int layer) const
{
const Layer &sl = _layers.at(layer);
sl.setTextProperties(tile);
}
void Style::setPainter(Tile &tile, int layer) const
void Style::setupLayer(Tile &tile, int layer) const
{
const Layer &sl = _layers.at(layer);
if (sl.isPath())
if (sl.isSymbol())
sl.setTextProperties(tile);
else if (sl.isPath())
sl.setPathPainter(tile, _sprites);
else if (sl.isSymbol())
sl.setSymbolPainter(tile);
}
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;
void drawBackground(Tile &tile) const;
void setPainter(Tile &tile, int layer) const;
void setTextProperties(Tile &tile, int layer) const;
void setupLayer(Tile &tile, int layer) const;
void drawFeature(Tile &tile, int layer, const QPainterPath &path,
const QVariantHash &tags) const;
@ -48,7 +47,6 @@ private:
bool match(int zoom, const QVariantHash &tags) const;
void setPathPainter(Tile &tile, const Sprites &sprites) const;
void setSymbolPainter(Tile &tile) const;
void setTextProperties(Tile &tile) const;
void addSymbol(Tile &tile, const QPainterPath &path,
const QVariantHash &tags, const Sprites &sprites) const;
@ -104,7 +102,7 @@ private:
qreal maxTextAngle(int zoom) const
{return _textMaxAngle.value(zoom);}
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
{return _icon.value(zoom, tags);}
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,
const QPainter &painter, bool overlap, const QImage &icon)
void Text::addLabel(const QString &text, const QPointF &pos, bool overlap,
const QImage &icon)
{
TextPointItem *ti = new TextPointItem(text, pos, painter.font(),
_properties, icon);
TextPointItem *ti = new TextPointItem(text, pos, _font, _maxWidth, _anchor,
icon);
if (!overlap && !_sceneRect.contains(ti->boundingRect())) {
delete ti;
return;
}
ti->setPen(painter.pen());
ti->setPen(_pen);
addItem(ti);
QList<TextItem*> ci = collidingItems(ti);
@ -166,25 +166,24 @@ void Text::addLabel(const QString &text, const QPointF &pos,
ci[i]->setVisible(false);
}
void Text::addLabel(const QString &text, const QPainterPath &path,
const QPainter &painter)
void Text::addLabel(const QString &text, const QPainterPath &path)
{
int textWidth = text.size() * painter.font().pixelSize() * 0.6;
int textWidth = text.size() * _font.pixelSize() * 0.6;
if (textWidth > path.length())
return;
QPainterPath tp(textPath(path, textWidth, _properties.maxAngle,
painter.font().pixelSize() / 2, _sceneRect));
QPainterPath tp(textPath(path, textWidth, _maxAngle, _font.pixelSize() / 2,
_sceneRect));
if (tp.isEmpty())
return;
TextPathItem *ti = new TextPathItem(text, reverse(tp) ? tp.toReversed()
: tp, painter.font());
: tp, _font);
if (!_sceneRect.contains(ti->boundingRect())) {
delete ti;
return;
}
ti->setPen(painter.pen());
ti->setPen(_pen);
addItem(ti);

View File

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

View File

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

View File

@ -11,7 +11,7 @@ class TextPointItem : public TextItem
{
public:
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;}
QPainterPath shape() const {return _shape;}
@ -20,8 +20,7 @@ public:
void setPen(const QPen &pen) {_pen = pen;}
private:
typedef QRectF (*BoundingRectFunction)(const QString &, const QFont &,
const Text::Properties &);
typedef QRectF (*BoundingRectFunction)(const QString &, const QFont &, int);
QRectF computeTextRect(BoundingRectFunction brf) const;
QPointF _pos;
@ -30,7 +29,8 @@ private:
QFont _font;
QPen _pen;
QImage _icon;
Text::Properties _properties;
int _maxWidth;
Text::Anchor _anchor;
};
#endif // TEXTPOINTITEM_H