Code cleanup/optimization

This commit is contained in:
Martin Tůma 2018-10-29 18:42:04 +01:00
parent 5d68f31124
commit 8d561ef3e9
7 changed files with 37 additions and 38 deletions

View File

@ -121,14 +121,14 @@ static void feature(const Feature &feature, Style *style, int styleLayer,
static void layer(const Layer &layer, Style *style, int styleLayer, Tile &tile)
{
qreal factor = layer.data->extent() / 256.0;
qreal factor = layer.data->extent() / (qreal)tile.size();
for (int i = 0; i < layer.data->features_size(); i++)
feature(Feature(&(layer.data->features(i)), &(layer.keys),
&(layer.values)), style, styleLayer, factor, tile);
}
QImage PBF::image(const QByteArray &data, int zoom, Style *style)
QImage PBF::image(const QByteArray &data, int zoom, Style *style, int size)
{
vector_tile::Tile tile;
if (!tile.ParseFromArray(data.constData(), data.size())) {
@ -136,7 +136,7 @@ QImage PBF::image(const QByteArray &data, int zoom, Style *style)
return QImage();
}
Tile t;
Tile t(size);
style->setZoom(zoom);
style->drawBackground(t);

View File

@ -8,7 +8,7 @@ class Style;
namespace PBF
{
QImage image(const QByteArray &data, int zoom, Style *style);
QImage image(const QByteArray &data, int zoom, Style *style, int size);
}
#endif // PBF_H

View File

@ -29,7 +29,7 @@ bool PBFHandler::read(QImage *image)
bool ok;
int zoom = format().toInt(&ok);
*image = PBF::image(ba, ok ? zoom : -1, _style);
*image = PBF::image(ba, ok ? zoom : -1, _style, 256);
return !image->isNull();
}

View File

@ -236,7 +236,7 @@ bool Style::Layer::Paint::antialias(Layer::Type type) const
Style::Layer::Layout::Layout(const QJsonObject &json)
: _textSize(16), _textMaxWidth(10), _lineCap(Qt::FlatCap),
_lineJoin(Qt::MiterJoin)
_lineJoin(Qt::MiterJoin), _capitalize(false)
{
if (!(json.contains("text-field") && json["text-field"].isString()))
return;
@ -261,6 +261,9 @@ Style::Layer::Layout::Layout(const QJsonObject &json)
if (json.contains("text-max-width") && json["text-max-width"].isDouble())
_textMaxWidth = json["text-max-width"].toDouble();
if (json.contains("text-transform") && json["text-transform"].isString())
_capitalize = json["text-transform"].toString() == "uppercase";
if (json.contains("line-cap") && json["line-cap"].isString()) {
if (json["line-cap"].toString() == "round")
_lineCap = Qt::RoundCap;
@ -341,39 +344,35 @@ bool Style::Layer::match(int zoom, const QVariantMap &tags) const
void Style::Layer::drawPath(int zoom, const QPainterPath &path,
Tile &tile) const
{
QPainter p(&(tile.background()));
QPainter *p = tile.painter();
QPen pen(_paint.pen(_type, zoom));
pen.setJoinStyle(_layout.lineJoin());
pen.setCapStyle(_layout.lineCap());
p.setRenderHint(QPainter::Antialiasing,
_paint.antialias(_type));
p.setPen(pen);
p.setBrush(_paint.brush(_type, zoom));
p.setOpacity(_paint.opacity(_type, zoom));
p.drawPath(path);
p->setRenderHint(QPainter::Antialiasing, _paint.antialias(_type));
p->setPen(pen);
p->setBrush(_paint.brush(_type, zoom));
p->setOpacity(_paint.opacity(_type, zoom));
p->drawPath(path);
}
void Style::Layer::drawSymbol(int zoom, const QPainterPath &path,
const QVariantMap &tags, Tile &tile) const
{
if (!_layout.showText(zoom))
return;
QString text(_layout.field());
for (int i = 0; i < _layout.keys().size(); i++) {
const QString &key = _layout.keys().at(i);
const QVariant val = tags.value(key);
text.replace(QString("{%1}").arg(key), val.toString());
text.replace(QString("{%1}").arg(key), _layout.capitalize()
? val.toString().toUpper() : val.toString());
}
const QPainterPath::Element &e = path.elementAt(0);
QPen pen(_paint.pen(_type, zoom));
QFont font(_layout.font(zoom));
if (path.elementCount() == 1)
tile.text().addLabel(text.trimmed(), QPointF(e.x, e.y), font, pen,
if (path.elementCount() == 1 && path.elementAt(0).isMoveTo())
tile.text().addLabel(text.trimmed(), path.elementAt(0), font, pen,
_layout.maxTextWidth(zoom));
else
tile.text().addLabel(text.trimmed(), path, font, pen);
@ -419,15 +418,12 @@ void Style::drawFeature(int layer, const QPainterPath &path,
void Style::drawBackground(Tile &tile)
{
QPainterPath p;
p.addRect(tile.background().rect());
QPainterPath path;
path.addRect(QRectF(0, 0, tile.size(), tile.size()));
if (_styles.isEmpty()) {
tile.background().fill(Qt::lightGray);
return;
}
for (int i = 0; i < _styles.size(); i++)
if (_styles.at(i).isBackground())
_styles.at(i).drawPath(_zoom, p, tile);
tile.painter()->setBrush(Qt::lightGray);
tile.painter()->drawPath(path);
} else if (_styles.first().isBackground())
_styles.first().drawPath(_zoom, path, tile);
}

View File

@ -77,10 +77,10 @@ private:
class Layout {
public:
Layout() : _textSize(16), _textMaxWidth(10), _lineCap(Qt::FlatCap),
_lineJoin(Qt::MiterJoin) {}
_lineJoin(Qt::MiterJoin), _capitalize(false) {}
Layout(const QJsonObject &json);
bool showText(int zoom) const {return _textSize.value(zoom) > 0;}
bool capitalize() const {return _capitalize;}
qreal maxTextWidth(int zoom) const {return _textMaxWidth.value(zoom);}
const QString &field() const {return _textField;}
const QStringList &keys() const {return _keys;}
@ -95,6 +95,7 @@ private:
FunctionF _textMaxWidth;
Qt::PenCapStyle _lineCap;
Qt::PenJoinStyle _lineJoin;
bool _capitalize;
};
class Paint {

View File

@ -6,8 +6,8 @@
class Text : public QGraphicsScene
{
public:
Text(QObject *parent = 0) : QGraphicsScene(parent)
{setSceneRect(0, 0, 256, 256);}
Text(int size, QObject *parent = 0) : QGraphicsScene(parent)
{setSceneRect(0, 0, size, size);}
void addLabel(const QString &text, const QPointF &pos, const QFont &font,
const QPen &pen, qreal maxTextWidth);

View File

@ -7,20 +7,22 @@
class Tile {
public:
Tile() : _background(256, 256, QImage::Format_ARGB32) {}
Tile(int size) : _background(size, size, QImage::Format_ARGB32),
_text(size), _painter(&_background) {}
int size() const {return _background.width();}
Text &text() {return _text;}
QImage &background() {return _background;}
QPainter *painter() {return &_painter;}
QImage &render() {
QPainter p(&_background);
_text.render(&p);
_text.render(painter());
return _background;
}
private:
Text _text;
QImage _background;
Text _text;
QPainter _painter;
};
#endif // TILE_H