mirror of
https://github.com/tumic0/QtPBFImagePlugin.git
synced 2024-11-24 03:35:54 +01:00
Code cleanup/optimization
This commit is contained in:
parent
5d68f31124
commit
8d561ef3e9
@ -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)
|
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++)
|
for (int i = 0; i < layer.data->features_size(); i++)
|
||||||
feature(Feature(&(layer.data->features(i)), &(layer.keys),
|
feature(Feature(&(layer.data->features(i)), &(layer.keys),
|
||||||
&(layer.values)), style, styleLayer, factor, tile);
|
&(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;
|
vector_tile::Tile tile;
|
||||||
if (!tile.ParseFromArray(data.constData(), data.size())) {
|
if (!tile.ParseFromArray(data.constData(), data.size())) {
|
||||||
@ -136,7 +136,7 @@ QImage PBF::image(const QByteArray &data, int zoom, Style *style)
|
|||||||
return QImage();
|
return QImage();
|
||||||
}
|
}
|
||||||
|
|
||||||
Tile t;
|
Tile t(size);
|
||||||
|
|
||||||
style->setZoom(zoom);
|
style->setZoom(zoom);
|
||||||
style->drawBackground(t);
|
style->drawBackground(t);
|
||||||
|
@ -8,7 +8,7 @@ class Style;
|
|||||||
|
|
||||||
namespace PBF
|
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
|
#endif // PBF_H
|
||||||
|
@ -29,7 +29,7 @@ bool PBFHandler::read(QImage *image)
|
|||||||
|
|
||||||
bool ok;
|
bool ok;
|
||||||
int zoom = format().toInt(&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();
|
return !image->isNull();
|
||||||
}
|
}
|
||||||
|
@ -236,7 +236,7 @@ bool Style::Layer::Paint::antialias(Layer::Type type) const
|
|||||||
|
|
||||||
Style::Layer::Layout::Layout(const QJsonObject &json)
|
Style::Layer::Layout::Layout(const QJsonObject &json)
|
||||||
: _textSize(16), _textMaxWidth(10), _lineCap(Qt::FlatCap),
|
: _textSize(16), _textMaxWidth(10), _lineCap(Qt::FlatCap),
|
||||||
_lineJoin(Qt::MiterJoin)
|
_lineJoin(Qt::MiterJoin), _capitalize(false)
|
||||||
{
|
{
|
||||||
if (!(json.contains("text-field") && json["text-field"].isString()))
|
if (!(json.contains("text-field") && json["text-field"].isString()))
|
||||||
return;
|
return;
|
||||||
@ -261,6 +261,9 @@ Style::Layer::Layout::Layout(const QJsonObject &json)
|
|||||||
if (json.contains("text-max-width") && json["text-max-width"].isDouble())
|
if (json.contains("text-max-width") && json["text-max-width"].isDouble())
|
||||||
_textMaxWidth = json["text-max-width"].toDouble();
|
_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.contains("line-cap") && json["line-cap"].isString()) {
|
||||||
if (json["line-cap"].toString() == "round")
|
if (json["line-cap"].toString() == "round")
|
||||||
_lineCap = Qt::RoundCap;
|
_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,
|
void Style::Layer::drawPath(int zoom, const QPainterPath &path,
|
||||||
Tile &tile) const
|
Tile &tile) const
|
||||||
{
|
{
|
||||||
QPainter p(&(tile.background()));
|
QPainter *p = tile.painter();
|
||||||
|
|
||||||
QPen pen(_paint.pen(_type, zoom));
|
QPen pen(_paint.pen(_type, zoom));
|
||||||
pen.setJoinStyle(_layout.lineJoin());
|
pen.setJoinStyle(_layout.lineJoin());
|
||||||
pen.setCapStyle(_layout.lineCap());
|
pen.setCapStyle(_layout.lineCap());
|
||||||
|
|
||||||
p.setRenderHint(QPainter::Antialiasing,
|
p->setRenderHint(QPainter::Antialiasing, _paint.antialias(_type));
|
||||||
_paint.antialias(_type));
|
p->setPen(pen);
|
||||||
p.setPen(pen);
|
p->setBrush(_paint.brush(_type, zoom));
|
||||||
p.setBrush(_paint.brush(_type, zoom));
|
p->setOpacity(_paint.opacity(_type, zoom));
|
||||||
p.setOpacity(_paint.opacity(_type, zoom));
|
p->drawPath(path);
|
||||||
p.drawPath(path);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
void Style::Layer::drawSymbol(int zoom, const QPainterPath &path,
|
void Style::Layer::drawSymbol(int zoom, const QPainterPath &path,
|
||||||
const QVariantMap &tags, Tile &tile) const
|
const QVariantMap &tags, Tile &tile) const
|
||||||
{
|
{
|
||||||
if (!_layout.showText(zoom))
|
|
||||||
return;
|
|
||||||
|
|
||||||
QString text(_layout.field());
|
QString text(_layout.field());
|
||||||
for (int i = 0; i < _layout.keys().size(); i++) {
|
for (int i = 0; i < _layout.keys().size(); i++) {
|
||||||
const QString &key = _layout.keys().at(i);
|
const QString &key = _layout.keys().at(i);
|
||||||
const QVariant val = tags.value(key);
|
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));
|
QPen pen(_paint.pen(_type, zoom));
|
||||||
QFont font(_layout.font(zoom));
|
QFont font(_layout.font(zoom));
|
||||||
|
|
||||||
if (path.elementCount() == 1)
|
if (path.elementCount() == 1 && path.elementAt(0).isMoveTo())
|
||||||
tile.text().addLabel(text.trimmed(), QPointF(e.x, e.y), font, pen,
|
tile.text().addLabel(text.trimmed(), path.elementAt(0), font, pen,
|
||||||
_layout.maxTextWidth(zoom));
|
_layout.maxTextWidth(zoom));
|
||||||
else
|
else
|
||||||
tile.text().addLabel(text.trimmed(), path, font, pen);
|
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)
|
void Style::drawBackground(Tile &tile)
|
||||||
{
|
{
|
||||||
QPainterPath p;
|
QPainterPath path;
|
||||||
p.addRect(tile.background().rect());
|
path.addRect(QRectF(0, 0, tile.size(), tile.size()));
|
||||||
|
|
||||||
if (_styles.isEmpty()) {
|
if (_styles.isEmpty()) {
|
||||||
tile.background().fill(Qt::lightGray);
|
tile.painter()->setBrush(Qt::lightGray);
|
||||||
return;
|
tile.painter()->drawPath(path);
|
||||||
}
|
} else if (_styles.first().isBackground())
|
||||||
|
_styles.first().drawPath(_zoom, path, tile);
|
||||||
for (int i = 0; i < _styles.size(); i++)
|
|
||||||
if (_styles.at(i).isBackground())
|
|
||||||
_styles.at(i).drawPath(_zoom, p, tile);
|
|
||||||
}
|
}
|
||||||
|
@ -77,10 +77,10 @@ private:
|
|||||||
class Layout {
|
class Layout {
|
||||||
public:
|
public:
|
||||||
Layout() : _textSize(16), _textMaxWidth(10), _lineCap(Qt::FlatCap),
|
Layout() : _textSize(16), _textMaxWidth(10), _lineCap(Qt::FlatCap),
|
||||||
_lineJoin(Qt::MiterJoin) {}
|
_lineJoin(Qt::MiterJoin), _capitalize(false) {}
|
||||||
Layout(const QJsonObject &json);
|
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);}
|
qreal maxTextWidth(int zoom) const {return _textMaxWidth.value(zoom);}
|
||||||
const QString &field() const {return _textField;}
|
const QString &field() const {return _textField;}
|
||||||
const QStringList &keys() const {return _keys;}
|
const QStringList &keys() const {return _keys;}
|
||||||
@ -95,6 +95,7 @@ private:
|
|||||||
FunctionF _textMaxWidth;
|
FunctionF _textMaxWidth;
|
||||||
Qt::PenCapStyle _lineCap;
|
Qt::PenCapStyle _lineCap;
|
||||||
Qt::PenJoinStyle _lineJoin;
|
Qt::PenJoinStyle _lineJoin;
|
||||||
|
bool _capitalize;
|
||||||
};
|
};
|
||||||
|
|
||||||
class Paint {
|
class Paint {
|
||||||
|
@ -6,8 +6,8 @@
|
|||||||
class Text : public QGraphicsScene
|
class Text : public QGraphicsScene
|
||||||
{
|
{
|
||||||
public:
|
public:
|
||||||
Text(QObject *parent = 0) : QGraphicsScene(parent)
|
Text(int size, QObject *parent = 0) : QGraphicsScene(parent)
|
||||||
{setSceneRect(0, 0, 256, 256);}
|
{setSceneRect(0, 0, size, size);}
|
||||||
|
|
||||||
void addLabel(const QString &text, const QPointF &pos, const QFont &font,
|
void addLabel(const QString &text, const QPointF &pos, const QFont &font,
|
||||||
const QPen &pen, qreal maxTextWidth);
|
const QPen &pen, qreal maxTextWidth);
|
||||||
|
12
src/tile.h
12
src/tile.h
@ -7,20 +7,22 @@
|
|||||||
|
|
||||||
class Tile {
|
class Tile {
|
||||||
public:
|
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;}
|
Text &text() {return _text;}
|
||||||
QImage &background() {return _background;}
|
QPainter *painter() {return &_painter;}
|
||||||
|
|
||||||
QImage &render() {
|
QImage &render() {
|
||||||
QPainter p(&_background);
|
_text.render(painter());
|
||||||
_text.render(&p);
|
|
||||||
return _background;
|
return _background;
|
||||||
}
|
}
|
||||||
|
|
||||||
private:
|
private:
|
||||||
Text _text;
|
|
||||||
QImage _background;
|
QImage _background;
|
||||||
|
Text _text;
|
||||||
|
QPainter _painter;
|
||||||
};
|
};
|
||||||
|
|
||||||
#endif // TILE_H
|
#endif // TILE_H
|
||||||
|
Loading…
Reference in New Issue
Block a user