mirror of
https://github.com/tumic0/QtPBFImagePlugin.git
synced 2025-07-01 05:29:15 +02:00
Compare commits
54 Commits
Author | SHA1 | Date | |
---|---|---|---|
d8df5cb595 | |||
543bf63017 | |||
7ea19d260c | |||
0185cd904f | |||
ec6302d524 | |||
0916d6330c | |||
cc0c16cfd6 | |||
8da172c0ff | |||
148f79eaef | |||
1c42f104d1 | |||
536cad2d61 | |||
ff1c29e078 | |||
e8e9139740 | |||
a58962ab93 | |||
aacc42c25d | |||
3f6b5faa2c | |||
5b6a222999 | |||
e00c8feab6 | |||
82308857ba | |||
044e95e061 | |||
f5c15ece52 | |||
df4134c03a | |||
c28d10eafc | |||
76fb129651 | |||
3dfcf35e53 | |||
5ccb93bd59 | |||
c3df78a455 | |||
f96b530fa3 | |||
9fdfd35752 | |||
20f2e7c7d2 | |||
75e6da8903 | |||
c8e8f0013f | |||
a7294b30f8 | |||
2a89ef8dfd | |||
54176640d0 | |||
4042b3ccdf | |||
c1af3d82a5 | |||
368c53307d | |||
75e61e956e | |||
094641eb4b | |||
cdf40f774b | |||
0950caae0d | |||
0188273c29 | |||
13d682bdf6 | |||
738ada91c2 | |||
6d02dafeb6 | |||
a559b5237b | |||
12b91c9c6f | |||
1677eb7a67 | |||
03f4315a5b | |||
a3d555bb6a | |||
a83d4909ed | |||
e45e86e5d7 | |||
0476f5740e |
22
.travis.yml
Normal file
22
.travis.yml
Normal file
@ -0,0 +1,22 @@
|
||||
language: c++
|
||||
dist: xenial
|
||||
|
||||
os:
|
||||
- linux
|
||||
- osx
|
||||
|
||||
env:
|
||||
- QT_SELECT=qt5
|
||||
|
||||
before_install:
|
||||
- if [[ "$TRAVIS_OS_NAME" == "linux" ]]; then sudo apt-get -qq update; fi
|
||||
|
||||
install:
|
||||
- if [[ "$TRAVIS_OS_NAME" == "osx" ]]; then brew install qt protobuf; fi
|
||||
- if [[ "$TRAVIS_OS_NAME" == "linux" ]]; then sudo apt-get install qtbase5-dev qtbase5-dev-tools qt5-qmake libprotobuf-dev protobuf-compiler; fi
|
||||
|
||||
script:
|
||||
- if [[ "$TRAVIS_OS_NAME" == "osx" ]]; then PATH=/usr/local/opt/qt/bin/:${PATH}; fi
|
||||
- if [[ "$TRAVIS_OS_NAME" == "osx" ]]; then qmake PROTOBUF=/usr/local pbfplugin.pro; fi
|
||||
- if [[ "$TRAVIS_OS_NAME" == "linux" ]]; then qmake pbfplugin.pro; fi
|
||||
- make
|
27
README.md
27
README.md
@ -3,8 +3,8 @@ Qt image plugin for displaying Mapbox vector tiles
|
||||
|
||||
## Description
|
||||
QtPBFImagePlugin is a Qt image plugin that enables applications capable of
|
||||
displaying raster MBTiles maps or raster XYZ online maps to also display PBF
|
||||
vector tiles without (almost, see usage) any application modifications.
|
||||
displaying raster MBTiles maps or raster XYZ online maps to also display
|
||||
PBF(MVT) vector tiles without (almost, see usage) any application modifications.
|
||||
|
||||
Standard Mapbox GL Styles are used for styling the maps. Most relevant style
|
||||
features used by [Maputnik](http://editor.openmaptiles.org) are supported.
|
||||
@ -16,7 +16,8 @@ placed in the same directory as the style itself. A default fallback style
|
||||
(OSM-Liberty) for OpenMapTiles is part of the plugin.
|
||||
|
||||
"Plain" PBF files as well as gzip compressed files (as used in MBTiles) are
|
||||
supported by the plugin.
|
||||
supported by the plugin. The tile size is (since version 2.0 of the plugin) 512px
|
||||
to fit the styles and available data (OpenMapTiles, Mapbox tiles).
|
||||
|
||||
## Usage
|
||||
Due to a major design flaw in the Mapbox vector tiles specification - the zoom
|
||||
@ -33,14 +34,14 @@ The plugin supports vector scaling using QImageReader's setScaledSize() method,
|
||||
so when used like in the following example:
|
||||
```cpp
|
||||
QImageReader reader(file, QString::number(zoom).toLatin1());
|
||||
reader.setScaledSize(QSize(512, 512));
|
||||
reader.setScaledSize(QSize(1024, 1024));
|
||||
reader.read(&image);
|
||||
```
|
||||
you will get 512x512px tiles with a pixel ratio of 2 (= HiDPI tiles).
|
||||
you will get 1024x1024px tiles with a pixel ratio of 2 (= HiDPI tiles).
|
||||
|
||||
## Build
|
||||
### Requirements
|
||||
* Qt 5
|
||||
* Qt >= 5.4 (5.6 for HiDPI support)
|
||||
* Google Protocol Buffers (protobuf-lite)
|
||||
* Zlib
|
||||
|
||||
@ -63,11 +64,19 @@ make
|
||||
|
||||
## Install
|
||||
Copy the plugin to the system Qt image plugins path to make it work. You may
|
||||
also set the QT_PLUGIN_PATH system variable before starting the application.
|
||||
also set the QT_PLUGIN_PATH system variable before starting the application. For
|
||||
Linux, there are RPM and DEB [packages](https://build.opensuse.org/project/show/home:tumic:QtPBFImagePlugin)
|
||||
for most common distros available on OBS.
|
||||
|
||||
## Limitations
|
||||
Text PBF features must have a unique id (OpenMapTiles >= v3.7) for the text layout
|
||||
* Only data that is part of the PBF file is displayed. External layers defined in the
|
||||
style are ignored.
|
||||
* Text PBF features must have a unique id (OpenMapTiles >= v3.7) for the text layout
|
||||
algorithm to work properly.
|
||||
* Expressions not supported in the styles, only property functions are implemented.
|
||||
|
||||
## Changelog
|
||||
[Changelog](https://build.opensuse.org/package/view_file/home:tumic:QtPBFImagePlugin/QtPBFImagePlugin/libqt5-qtpbfimageformat.changes)
|
||||
|
||||
## Status
|
||||
A picture is worth a thousand words. Data and styles from https://openmaptiles.org.
|
||||
@ -85,3 +94,5 @@ A picture is worth a thousand words. Data and styles from https://openmaptiles.o
|
||||

|
||||

|
||||
|
||||
## Applications using QtPBFImagePlugin
|
||||
* [GPXSee](https://www.gpxsee.org)
|
||||
|
@ -1,4 +1,4 @@
|
||||
{
|
||||
"Keys": [ "pbf" ],
|
||||
"MimeTypes": [ "image/pbf" ]
|
||||
"Keys": [ "mvt" ],
|
||||
"MimeTypes": [ "application/vnd.mapbox-vector-tile" ]
|
||||
}
|
||||
|
@ -20,7 +20,8 @@ HEADERS += src/pbfhandler.h \
|
||||
src/textpointitem.h \
|
||||
src/font.h \
|
||||
src/textitem.h \
|
||||
src/sprites.h
|
||||
src/sprites.h \
|
||||
src/config.h
|
||||
SOURCES += src/pbfplugin.cpp \
|
||||
src/pbfhandler.cpp \
|
||||
src/gzip.cpp \
|
||||
@ -32,7 +33,8 @@ SOURCES += src/pbfplugin.cpp \
|
||||
src/textpathitem.cpp \
|
||||
src/textpointitem.cpp \
|
||||
src/font.cpp \
|
||||
src/sprites.cpp
|
||||
src/sprites.cpp \
|
||||
src/textitem.cpp
|
||||
RESOURCES += pbfplugin.qrc
|
||||
|
||||
unix:!macx{
|
||||
|
9
pkg/pbfplugin.xml
Normal file
9
pkg/pbfplugin.xml
Normal file
@ -0,0 +1,9 @@
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<mime-info xmlns="http://www.freedesktop.org/standards/shared-mime-info">
|
||||
<mime-type type="application/vnd.mapbox-vector-tile">
|
||||
<comment>Mapbox Vector Tile</comment>
|
||||
<sub-class-of type="application/octet-stream"/>
|
||||
<generic-icon name="application/octet-stream"/>
|
||||
<glob pattern="*.mvt"/>
|
||||
</mime-type>
|
||||
</mime-info>
|
@ -13,9 +13,7 @@ QColor Color::fromJsonString(const QString &str)
|
||||
{
|
||||
QColor ret;
|
||||
|
||||
if (str.startsWith('#'))
|
||||
return QColor(str);
|
||||
else if (str.startsWith("rgb(")) {
|
||||
if (str.startsWith("rgb(")) {
|
||||
QStringList comp(str.mid(4, str.size() - 5).split(','));
|
||||
if (comp.size() != 3)
|
||||
return QColor();
|
||||
@ -39,7 +37,8 @@ QColor Color::fromJsonString(const QString &str)
|
||||
return QColor();
|
||||
ret = QColor::fromHslF(comp.at(0).toFloat() / 360.0, pval(comp.at(1)),
|
||||
pval(comp.at(2)), comp.at(3).toFloat());
|
||||
}
|
||||
} else
|
||||
ret = QColor(str);
|
||||
|
||||
if (!ret.isValid())
|
||||
qWarning() << str << ": invalid color";
|
||||
|
10
src/config.h
Normal file
10
src/config.h
Normal file
@ -0,0 +1,10 @@
|
||||
#ifndef CONFIG_H
|
||||
#define CONFIG_H
|
||||
|
||||
#include <QtGlobal>
|
||||
|
||||
#if QT_VERSION >= QT_VERSION_CHECK(5, 6, 0)
|
||||
#define ENABLE_HIDPI
|
||||
#endif // QT >= 5.6
|
||||
|
||||
#endif // CONFIG_H
|
@ -40,7 +40,9 @@ static QColor interpolate(const QPair<qreal, QColor> &p0,
|
||||
qreal p1h, p1s, p1l, p1a;
|
||||
p1.second.getHslF(&p1h, &p1s, &p1l, &p1a);
|
||||
|
||||
return QColor::fromHslF(f(p0h, p1h, ratio), f(p0s, p1s, ratio),
|
||||
/* Qt returns a hue of -1 for achromatic colors. We convert it to a hue of 1
|
||||
using qAbs() to enable interpolation of grayscale colors */
|
||||
return QColor::fromHslF(f(qAbs(p0h), qAbs(p1h), ratio), f(p0s, p1s, ratio),
|
||||
f(p0l, p1l, ratio), f(p0a, p1a, ratio));
|
||||
}
|
||||
|
||||
|
@ -4,6 +4,8 @@
|
||||
#include <QList>
|
||||
#include <QPointF>
|
||||
#include <QColor>
|
||||
#include <QPair>
|
||||
#include <QString>
|
||||
|
||||
class QJsonObject;
|
||||
|
||||
|
187
src/pbf.cpp
187
src/pbf.cpp
@ -1,21 +1,20 @@
|
||||
#include <QByteArray>
|
||||
#include <QPainter>
|
||||
#include <QDebug>
|
||||
#include <QVariantHash>
|
||||
#include "vector_tile.pb.h"
|
||||
#include "style.h"
|
||||
#include "tile.h"
|
||||
#include "pbf.h"
|
||||
|
||||
|
||||
#define MOVE_TO 1
|
||||
#define LINE_TO 2
|
||||
#define CLOSE_PATH 7
|
||||
|
||||
#define POLYGON vector_tile::Tile_GeomType_POLYGON
|
||||
#define LINESTRING vector_tile::Tile_GeomType_LINESTRING
|
||||
#define POINT vector_tile::Tile_GeomType_POINT
|
||||
|
||||
static inline qint32 zigzag32decode(quint32 value)
|
||||
{
|
||||
return static_cast<qint32>((value >> 1u) ^ static_cast<quint32>(
|
||||
-static_cast<qint32>(value & 1u)));
|
||||
}
|
||||
|
||||
static inline QPoint parameters(quint32 v1, quint32 v2)
|
||||
{
|
||||
return QPoint(zigzag32decode(v1), zigzag32decode(v2));
|
||||
}
|
||||
|
||||
static QVariant value(const vector_tile::Tile_Value &val)
|
||||
{
|
||||
@ -37,100 +36,35 @@ static QVariant value(const vector_tile::Tile_Value &val)
|
||||
return QVariant();
|
||||
}
|
||||
|
||||
class Feature
|
||||
const QVariant *PBF::Feature::value(const QString &key) const
|
||||
{
|
||||
public:
|
||||
Feature() : _data(0) {}
|
||||
Feature(const vector_tile::Tile_Feature *data, const QVector<QString> *keys,
|
||||
const QVector<QVariant> *values) : _data(data)
|
||||
{
|
||||
for (int i = 0; i < data->tags_size(); i = i + 2)
|
||||
_tags.insert(keys->at(data->tags(i)), values->at(data->tags(i+1)));
|
||||
const KeyHash &keys(_layer->keys());
|
||||
KeyHash::const_iterator it(keys.find(key));
|
||||
if (it == keys.constEnd())
|
||||
return 0;
|
||||
|
||||
switch (data->type()) {
|
||||
case POLYGON:
|
||||
_tags.insert("$type", QVariant("Polygon"));
|
||||
break;
|
||||
case LINESTRING:
|
||||
_tags.insert("$type", QVariant("LineString"));
|
||||
break;
|
||||
case POINT:
|
||||
_tags.insert("$type", QVariant("Point"));
|
||||
break;
|
||||
default:
|
||||
break;
|
||||
}
|
||||
}
|
||||
google::protobuf::uint32 index = *it;
|
||||
for (int i = 0; i < _data->tags_size(); i = i + 2)
|
||||
if (_data->tags(i) == index)
|
||||
return &(_layer->values().at(_data->tags(i+1)));
|
||||
|
||||
const QVariantHash &tags() const {return _tags;}
|
||||
const vector_tile::Tile_Feature *data() const {return _data;}
|
||||
|
||||
private:
|
||||
QVariantHash _tags;
|
||||
const vector_tile::Tile_Feature *_data;
|
||||
};
|
||||
|
||||
bool cmp(const Feature &f1, const Feature &f2)
|
||||
{
|
||||
return f1.data()->id() < f2.data()->id();
|
||||
return 0;
|
||||
}
|
||||
|
||||
class Layer
|
||||
QPainterPath PBF::Feature::path(const QSizeF &factor) const
|
||||
{
|
||||
public:
|
||||
Layer(const vector_tile::Tile_Layer *data) : _data(data)
|
||||
{
|
||||
QVector<QString> keys;
|
||||
QVector<QVariant> values;
|
||||
|
||||
for (int i = 0; i < data->keys_size(); i++)
|
||||
keys.append(QString::fromStdString(data->keys(i)));
|
||||
for (int i = 0; i < data->values_size(); i++)
|
||||
values.append(value(data->values(i)));
|
||||
|
||||
_features.reserve(data->features_size());
|
||||
for (int i = 0; i < data->features_size(); i++)
|
||||
_features.append(Feature(&(data->features(i)), &keys, &values));
|
||||
qSort(_features.begin(), _features.end(), cmp);
|
||||
}
|
||||
|
||||
const QVector<Feature> &features() const {return _features;}
|
||||
const vector_tile::Tile_Layer *data() const {return _data;}
|
||||
|
||||
private:
|
||||
const vector_tile::Tile_Layer *_data;
|
||||
QVector<Feature> _features;
|
||||
};
|
||||
|
||||
static inline qint32 zigzag32decode(quint32 value)
|
||||
{
|
||||
return static_cast<qint32>((value >> 1u) ^ static_cast<quint32>(
|
||||
-static_cast<qint32>(value & 1u)));
|
||||
}
|
||||
|
||||
static inline QPoint parameters(quint32 v1, quint32 v2)
|
||||
{
|
||||
return QPoint(zigzag32decode(v1), zigzag32decode(v2));
|
||||
}
|
||||
|
||||
static void drawFeature(const Feature &feature, Style *style, int styleLayer,
|
||||
const QSizeF &factor, Tile &tile)
|
||||
{
|
||||
if (!style->match(tile.zoom(), styleLayer, feature.tags()))
|
||||
return;
|
||||
|
||||
QPoint cursor;
|
||||
QPainterPath path;
|
||||
|
||||
for (int i = 0; i < feature.data()->geometry_size(); i++) {
|
||||
quint32 g = feature.data()->geometry(i);
|
||||
for (int i = 0; i < _data->geometry_size(); i++) {
|
||||
quint32 g = _data->geometry(i);
|
||||
unsigned cmdId = g & 0x7;
|
||||
unsigned cmdCount = g >> 3;
|
||||
|
||||
if (cmdId == MOVE_TO) {
|
||||
for (unsigned j = 0; j < cmdCount; j++) {
|
||||
QPoint offset = parameters(feature.data()->geometry(i+1),
|
||||
feature.data()->geometry(i+2));
|
||||
QPoint offset = parameters(_data->geometry(i+1),
|
||||
_data->geometry(i+2));
|
||||
i += 2;
|
||||
cursor += offset;
|
||||
path.moveTo(QPointF(cursor.x() * factor.width(),
|
||||
@ -138,8 +72,8 @@ static void drawFeature(const Feature &feature, Style *style, int styleLayer,
|
||||
}
|
||||
} else if (cmdId == LINE_TO) {
|
||||
for (unsigned j = 0; j < cmdCount; j++) {
|
||||
QPoint offset = parameters(feature.data()->geometry(i+1),
|
||||
feature.data()->geometry(i+2));
|
||||
QPoint offset = parameters(_data->geometry(i+1),
|
||||
_data->geometry(i+2));
|
||||
i += 2;
|
||||
cursor += offset;
|
||||
path.lineTo(QPointF(cursor.x() * factor.width(),
|
||||
@ -151,60 +85,35 @@ static void drawFeature(const Feature &feature, Style *style, int styleLayer,
|
||||
}
|
||||
}
|
||||
|
||||
if (path.elementCount())
|
||||
style->drawFeature(tile, styleLayer, path, feature.tags());
|
||||
return path;
|
||||
}
|
||||
|
||||
static void drawLayer(const Layer &layer, Style *style, int styleLayer,
|
||||
Tile &tile)
|
||||
PBF::Layer::Layer(const vector_tile::Tile_Layer *data) : _data(data)
|
||||
{
|
||||
if (layer.data()->version() > 2)
|
||||
return;
|
||||
_keys.reserve(data->keys_size());
|
||||
for (int i = 0; i < data->keys_size(); i++)
|
||||
_keys.insert(QString::fromStdString(data->keys(i)), i);
|
||||
_values.reserve(data->values_size());
|
||||
for (int i = 0; i < data->values_size(); i++)
|
||||
_values.append(value(data->values(i)));
|
||||
|
||||
QSizeF factor(tile.size().width() / tile.scale().x() /
|
||||
(qreal)layer.data()->extent(), tile.size().height() / tile.scale().y()
|
||||
/ (qreal)layer.data()->extent());
|
||||
|
||||
tile.painter().save();
|
||||
style->setupLayer(tile, styleLayer);
|
||||
for (int i = 0; i < layer.features().size(); i++)
|
||||
drawFeature(layer.features().at(i), style, styleLayer, factor, tile);
|
||||
tile.painter().restore();
|
||||
_features.reserve(data->features_size());
|
||||
for (int i = 0; i < data->features_size(); i++)
|
||||
_features.append(Feature(&(data->features(i)), this));
|
||||
qSort(_features.begin(), _features.end());
|
||||
}
|
||||
|
||||
bool PBF::render(const QByteArray &data, int zoom, Style *style,
|
||||
const QPointF &scale, QImage *image)
|
||||
PBF::PBF(const vector_tile::Tile &tile)
|
||||
{
|
||||
vector_tile::Tile tile;
|
||||
if (!tile.ParseFromArray(data.constData(), data.size())) {
|
||||
qCritical() << "Invalid tile protocol buffer data";
|
||||
return false;
|
||||
}
|
||||
|
||||
Tile t(image, zoom, scale);
|
||||
|
||||
style->drawBackground(t);
|
||||
|
||||
// Prepare source layers
|
||||
QMap<QString, Layer> layers;
|
||||
for (int i = 0; i < tile.layers_size(); i++) {
|
||||
const vector_tile::Tile_Layer &layer = tile.layers(i);
|
||||
QString name(QString::fromStdString(layer.name()));
|
||||
if (style->sourceLayers().contains(name))
|
||||
layers.insert(name, Layer(&layer));
|
||||
_layers.insert(QString::fromStdString(layer.name()), new Layer(&layer));
|
||||
}
|
||||
|
||||
// Process source layers in order of style layers
|
||||
for (int i = 0; i < style->sourceLayers().size(); i++) {
|
||||
QMap<QString, Layer>::const_iterator it = layers.find(
|
||||
style->sourceLayers().at(i));
|
||||
if (it == layers.constEnd())
|
||||
continue;
|
||||
|
||||
drawLayer(*it, style, i, t);
|
||||
}
|
||||
|
||||
t.text().render(&t.painter());
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
PBF::~PBF()
|
||||
{
|
||||
for (QHash<QString, Layer*>::iterator it = _layers.begin();
|
||||
it != _layers.end(); it++)
|
||||
delete *it;
|
||||
}
|
||||
|
66
src/pbf.h
66
src/pbf.h
@ -1,15 +1,67 @@
|
||||
#ifndef PBF_H
|
||||
#define PBF_H
|
||||
|
||||
#include <QImage>
|
||||
#include <QVariant>
|
||||
#include <QVector>
|
||||
#include <QHash>
|
||||
#include <QPainterPath>
|
||||
#include "vector_tile.pb.h"
|
||||
|
||||
class QByteArray;
|
||||
class Style;
|
||||
|
||||
namespace PBF
|
||||
typedef QHash<QString, google::protobuf::uint32> KeyHash;
|
||||
|
||||
class PBF
|
||||
{
|
||||
bool render(const QByteArray &data, int zoom, Style *style,
|
||||
const QPointF &scale, QImage *render);
|
||||
}
|
||||
public:
|
||||
class Layer;
|
||||
|
||||
class Feature
|
||||
{
|
||||
public:
|
||||
Feature() : _data(0), _layer(0) {}
|
||||
Feature(const vector_tile::Tile_Feature *data, const Layer *layer)
|
||||
: _data(data), _layer(layer) {}
|
||||
|
||||
const QVariant *value(const QString &key) const;
|
||||
vector_tile::Tile_GeomType type() const {return _data->type();}
|
||||
QPainterPath path(const QSizeF &factor) const;
|
||||
|
||||
friend bool operator<(const Feature &f1, const Feature &f2);
|
||||
|
||||
private:
|
||||
const vector_tile::Tile_Feature *_data;
|
||||
const Layer *_layer;
|
||||
};
|
||||
|
||||
class Layer
|
||||
{
|
||||
public:
|
||||
|
||||
Layer(const vector_tile::Tile_Layer *data);
|
||||
|
||||
const QVector<Feature> &features() const {return _features;}
|
||||
const QVector<QVariant> &values() const {return _values;}
|
||||
const KeyHash &keys() const {return _keys;}
|
||||
const vector_tile::Tile_Layer *data() const {return _data;}
|
||||
|
||||
private:
|
||||
const vector_tile::Tile_Layer *_data;
|
||||
QVector<Feature> _features;
|
||||
QVector<QVariant> _values;
|
||||
KeyHash _keys;
|
||||
};
|
||||
|
||||
|
||||
PBF(const vector_tile::Tile &tile);
|
||||
~PBF();
|
||||
|
||||
const QHash<QString, Layer*> &layers() const {return _layers;}
|
||||
|
||||
private:
|
||||
QHash<QString, Layer*> _layers;
|
||||
};
|
||||
|
||||
inline bool operator<(const PBF::Feature &f1, const PBF::Feature &f2)
|
||||
{return f1._data->id() < f2._data->id();}
|
||||
|
||||
#endif // PBF_H
|
||||
|
@ -1,12 +1,14 @@
|
||||
#include <QImage>
|
||||
#include <QIODevice>
|
||||
#include <QtEndian>
|
||||
#include <QDebug>
|
||||
#include "gzip.h"
|
||||
#include "pbf.h"
|
||||
#include "tile.h"
|
||||
#include "style.h"
|
||||
#include "pbfhandler.h"
|
||||
|
||||
|
||||
#define TILE_SIZE 256
|
||||
#define TILE_SIZE 512
|
||||
|
||||
#define GZIP_MAGIC 0x1F8B0800
|
||||
#define GZIP_MAGIC_MASK 0xFFFFFF00
|
||||
@ -61,6 +63,11 @@ bool PBFHandler::read(QImage *image)
|
||||
ba = device()->readAll();
|
||||
if (ba.isNull())
|
||||
return false;
|
||||
vector_tile::Tile data;
|
||||
if (!data.ParseFromArray(ba.constData(), ba.size())) {
|
||||
qCritical() << "Invalid PBF data";
|
||||
return false;
|
||||
}
|
||||
|
||||
bool ok;
|
||||
int zoom = format().toInt(&ok);
|
||||
@ -68,11 +75,14 @@ bool PBFHandler::read(QImage *image)
|
||||
QSize size = _scaledSize.isValid()
|
||||
? _scaledSize : QSize(TILE_SIZE, TILE_SIZE);
|
||||
QPointF scale = _scaledSize.isValid()
|
||||
? QPointF(_scaledSize.width() / TILE_SIZE, _scaledSize.height() / TILE_SIZE)
|
||||
: QPointF(1.0, 1.0);
|
||||
? QPointF((qreal)_scaledSize.width() / TILE_SIZE,
|
||||
(qreal)_scaledSize.height() / TILE_SIZE) : QPointF(1.0, 1.0);
|
||||
*image = QImage(size, QImage::Format_ARGB32_Premultiplied);
|
||||
Tile tile(image, ok ? zoom : -1, scale);
|
||||
|
||||
return PBF::render(ba, ok ? zoom : -1, _style, scale, image);
|
||||
_style->render(data, tile);
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
bool PBFHandler::supportsOption(ImageOption option) const
|
||||
|
@ -2,16 +2,16 @@
|
||||
#define PBFHANDLER_H
|
||||
|
||||
#include <QImageIOHandler>
|
||||
#include <QImage>
|
||||
#include <QVariant>
|
||||
#include <QSize>
|
||||
|
||||
class QImage;
|
||||
class Style;
|
||||
|
||||
class PBFHandler : public QImageIOHandler
|
||||
{
|
||||
public:
|
||||
PBFHandler(Style *style) : _style(style) {}
|
||||
PBFHandler(const Style *style) : _style(style) {}
|
||||
~PBFHandler() {}
|
||||
|
||||
bool canRead() const;
|
||||
@ -19,12 +19,12 @@ public:
|
||||
|
||||
QVariant option(ImageOption option) const;
|
||||
bool supportsOption(ImageOption option) const;
|
||||
void setOption(QImageIOHandler::ImageOption option, const QVariant &value);
|
||||
void setOption(ImageOption option, const QVariant &value);
|
||||
|
||||
static bool canRead(QIODevice *device);
|
||||
|
||||
private:
|
||||
Style *_style;
|
||||
const Style *_style;
|
||||
QSize _scaledSize;
|
||||
};
|
||||
|
||||
|
@ -21,7 +21,7 @@ QImageIOPlugin::Capabilities PBFPlugin::capabilities(QIODevice *device,
|
||||
const QByteArray &format) const
|
||||
{
|
||||
if (device == 0)
|
||||
return (format == "pbf") ? Capabilities(CanRead) : Capabilities();
|
||||
return (format == "mvt") ? Capabilities(CanRead) : Capabilities();
|
||||
else
|
||||
return (device->isReadable() && PBFHandler::canRead(device))
|
||||
? Capabilities(CanRead) : Capabilities();
|
||||
|
@ -83,6 +83,9 @@ bool Sprites::load(const QString &jsonFile, const QString &imageFile)
|
||||
|
||||
QImage Sprites::icon(const QString &name) const
|
||||
{
|
||||
if (_imageFile.isEmpty())
|
||||
return QImage();
|
||||
|
||||
const QImage *img = atlas(_imageFile);
|
||||
if (img->isNull())
|
||||
return QImage();
|
||||
|
@ -4,6 +4,9 @@
|
||||
#include <QRect>
|
||||
#include <QMap>
|
||||
#include <QImage>
|
||||
#include <QString>
|
||||
|
||||
class QJsonObject;
|
||||
|
||||
class Sprites
|
||||
{
|
||||
|
195
src/style.cpp
195
src/style.cpp
@ -10,9 +10,22 @@
|
||||
#include "color.h"
|
||||
#include "font.h"
|
||||
#include "tile.h"
|
||||
#include "pbf.h"
|
||||
#include "style.h"
|
||||
|
||||
|
||||
static vector_tile::Tile_GeomType geometryType(const QString &str)
|
||||
{
|
||||
if (str == "Point")
|
||||
return vector_tile::Tile_GeomType_POINT;
|
||||
else if (str == "LineString")
|
||||
return vector_tile::Tile_GeomType_LINESTRING;
|
||||
else if (str == "Polygon")
|
||||
return vector_tile::Tile_GeomType_POLYGON;
|
||||
else
|
||||
return vector_tile::Tile_GeomType_UNKNOWN;
|
||||
}
|
||||
|
||||
Style::Layer::Filter::Filter(const QJsonArray &json)
|
||||
: _type(Unknown), _not(false)
|
||||
{
|
||||
@ -27,9 +40,15 @@ Style::Layer::Filter::Filter(const QJsonArray &json)
|
||||
if (type == "==") {
|
||||
if (json.size() != 3)
|
||||
INVALID_FILTER(json);
|
||||
_type = EQ;
|
||||
_kv = QPair<QString, QVariant>(json.at(1).toString(),
|
||||
json.at(2).toVariant());
|
||||
if (json.at(1).toString() == "$type") {
|
||||
_type = GeometryType;
|
||||
_kv = QPair<QString, QVariant>(QString(),
|
||||
QVariant(geometryType(json.at(2).toString())));
|
||||
} else {
|
||||
_type = EQ;
|
||||
_kv = QPair<QString, QVariant>(json.at(1).toString(),
|
||||
json.at(2).toVariant());
|
||||
}
|
||||
} else if (type == "!=") {
|
||||
if (json.size() != 3)
|
||||
INVALID_FILTER(json);
|
||||
@ -98,45 +117,68 @@ Style::Layer::Filter::Filter(const QJsonArray &json)
|
||||
INVALID_FILTER(json);
|
||||
}
|
||||
|
||||
bool Style::Layer::Filter::match(const QVariantHash &tags) const
|
||||
bool Style::Layer::Filter::match(const PBF::Feature &feature) const
|
||||
{
|
||||
const QVariant *v;
|
||||
|
||||
switch (_type) {
|
||||
case None:
|
||||
return true;
|
||||
case EQ:
|
||||
return tags.value(_kv.first) == _kv.second;
|
||||
if (!(v = feature.value(_kv.first)))
|
||||
return false;
|
||||
else
|
||||
return *v == _kv.second;
|
||||
case NE:
|
||||
return tags.value(_kv.first) != _kv.second;
|
||||
if (!(v = feature.value(_kv.first)))
|
||||
return true;
|
||||
else
|
||||
return *v != _kv.second;
|
||||
case GT:
|
||||
return tags.value(_kv.first) > _kv.second;
|
||||
if (!(v = feature.value(_kv.first)))
|
||||
return false;
|
||||
else
|
||||
return *v > _kv.second;
|
||||
case GE:
|
||||
return tags.value(_kv.first) >= _kv.second;
|
||||
if (!(v = feature.value(_kv.first)))
|
||||
return false;
|
||||
else
|
||||
return *v >= _kv.second;
|
||||
case LT:
|
||||
return tags.value(_kv.first) < _kv.second;
|
||||
if (!(v = feature.value(_kv.first)))
|
||||
return false;
|
||||
else
|
||||
return *v < _kv.second;
|
||||
case LE:
|
||||
return tags.value(_kv.first) <= _kv.second;
|
||||
if (!(v = feature.value(_kv.first)))
|
||||
return false;
|
||||
else
|
||||
return *v <= _kv.second;
|
||||
case In:
|
||||
return _set.contains(tags.value(_kv.first).toString()) ^ _not;
|
||||
if (!(v = feature.value(_kv.first)))
|
||||
return _not;
|
||||
else
|
||||
return _set.contains((*v).toString()) ^ _not;
|
||||
case Has:
|
||||
return tags.contains(_kv.first) ^ _not;
|
||||
return (feature.value(_kv.first) ? true : false) ^ _not;
|
||||
case All:
|
||||
for (int i = 0; i < _filters.size(); i++) {
|
||||
if (!_filters.at(i).match(tags))
|
||||
for (int i = 0; i < _filters.size(); i++)
|
||||
if (!_filters.at(i).match(feature))
|
||||
return false;
|
||||
}
|
||||
return true;
|
||||
case Any:
|
||||
for (int i = 0; i < _filters.size(); i++) {
|
||||
if (_filters.at(i).match(tags))
|
||||
for (int i = 0; i < _filters.size(); i++)
|
||||
if (_filters.at(i).match(feature))
|
||||
return true;
|
||||
}
|
||||
return false;
|
||||
case GeometryType:
|
||||
return feature.type() == _kv.second.toUInt();
|
||||
default:
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
QString Style::Layer::Template::value(int zoom, const QVariantHash &tags) const
|
||||
QString Style::Layer::Template::value(int zoom, const PBF::Feature &feature) const
|
||||
{
|
||||
QRegExp rx = QRegExp("\\{[^\\}]*\\}");
|
||||
QString text(_field.value(zoom));
|
||||
@ -150,8 +192,8 @@ QString Style::Layer::Template::value(int zoom, const QVariantHash &tags) const
|
||||
}
|
||||
for (int i = 0; i < keys.size(); i++) {
|
||||
const QString &key = keys.at(i);
|
||||
const QVariant val = tags.value(key);
|
||||
text.replace(QString("{%1}").arg(key), val.toString());
|
||||
const QVariant *val = feature.value(key);
|
||||
text.replace(QString("{%1}").arg(key), val ? val->toString() : "");
|
||||
}
|
||||
|
||||
return text;
|
||||
@ -188,6 +230,9 @@ Style::Layer::Paint::Paint(const QJsonObject &json)
|
||||
|
||||
// text
|
||||
_textColor = FunctionC(json["text-color"]);
|
||||
_textHaloColor = FunctionC(json["text-halo-color"], QColor());
|
||||
_textHaloWidth = FunctionF(json["text-halo-width"]);
|
||||
_textHaloBlur = FunctionF(json["text-halo-blur"]);
|
||||
}
|
||||
|
||||
QPen Style::Layer::Paint::pen(Type type, int zoom) const
|
||||
@ -392,7 +437,7 @@ Text::RotationAlignment Style::Layer::Layout::textRotationAlignment(int zoom)
|
||||
}
|
||||
|
||||
Style::Layer::Layer(const QJsonObject &json)
|
||||
: _type(Unknown), _minZoom(-1), _maxZoom(-1)
|
||||
: _type(Unknown), _minZoom(0), _maxZoom(24)
|
||||
{
|
||||
// type
|
||||
QString type(json["type"].toString());
|
||||
@ -428,16 +473,12 @@ Style::Layer::Layer(const QJsonObject &json)
|
||||
_paint = Paint(json["paint"].toObject());
|
||||
}
|
||||
|
||||
bool Style::Layer::match(int zoom, const QVariantHash &tags) const
|
||||
bool Style::Layer::match(int zoom, const PBF::Feature &feature) const
|
||||
{
|
||||
if (zoom >= 0) {
|
||||
if (_minZoom > 0 && zoom < _minZoom)
|
||||
return false;
|
||||
if (_maxZoom > 0 && zoom > _maxZoom)
|
||||
return false;
|
||||
}
|
||||
if (zoom >= 0 && (zoom < _minZoom || zoom > _maxZoom))
|
||||
return false;
|
||||
|
||||
return _filter.match(tags);
|
||||
return _filter.match(feature);
|
||||
}
|
||||
|
||||
void Style::Layer::setPathPainter(Tile &tile, const Sprites &sprites) const
|
||||
@ -469,16 +510,17 @@ void Style::Layer::setTextProperties(Tile &tile) const
|
||||
t.setFont(_layout.font(zoom));
|
||||
t.setSymbolPlacement(_layout.symbolPlacement(zoom));
|
||||
t.setRotationAlignment(_layout.textRotationAlignment(zoom));
|
||||
t.setHalo(_paint.halo(zoom));
|
||||
}
|
||||
|
||||
void Style::Layer::addSymbol(Tile &tile, const QPainterPath &path,
|
||||
const QVariantHash &tags, const Sprites &sprites) const
|
||||
const PBF::Feature &feature, const Sprites &sprites) const
|
||||
{
|
||||
QString text = _layout.text(tile.zoom(), tags);
|
||||
QString text = _layout.text(tile.zoom(), feature);
|
||||
if (text.isEmpty())
|
||||
return;
|
||||
|
||||
QString icon = _layout.icon(tile.zoom(), tags);
|
||||
QString icon = _layout.icon(tile.zoom(), feature);
|
||||
tile.text().addLabel(text, sprites.icon(icon), path);
|
||||
}
|
||||
|
||||
@ -525,44 +567,38 @@ bool Style::load(const QString &fileName)
|
||||
_layers.append(Layer(layers[i].toObject()));
|
||||
}
|
||||
|
||||
for (int i = 0; i < _layers.size(); i++)
|
||||
_sourceLayers.append(_layers.at(i).sourceLayer());
|
||||
|
||||
QDir styleDir = QFileInfo(fileName).absoluteDir();
|
||||
loadSprites(styleDir, "sprite.json", "sprite.png", _sprites);
|
||||
#ifdef ENABLE_HIDPI
|
||||
loadSprites(styleDir, "sprite@2x.json", "sprite@2x.png", _sprites2x);
|
||||
#endif // ENABLE_HIDPI
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
void Style::setupLayer(Tile &tile, int layer) const
|
||||
const Sprites &Style::sprites(const QPointF &scale) const
|
||||
{
|
||||
const Layer &sl = _layers.at(layer);
|
||||
const Sprites &sprites = (tile.scale().x() > 1.0 || tile.scale().y() > 1.0)
|
||||
#ifdef ENABLE_HIDPI
|
||||
return (scale.x() > 1.0 || scale.y() > 1.0)
|
||||
&& !_sprites2x.isNull() ? _sprites2x : _sprites;
|
||||
|
||||
if (sl.isSymbol())
|
||||
sl.setTextProperties(tile);
|
||||
else if (sl.isPath())
|
||||
sl.setPathPainter(tile, sprites);
|
||||
#else // ENABLE_HIDPI
|
||||
Q_UNUSED(scale);
|
||||
return _sprites;
|
||||
#endif // ENABLE_HIDPI
|
||||
}
|
||||
|
||||
void Style::drawFeature(Tile &tile, int layer, const QPainterPath &path,
|
||||
const QVariantHash &tags) const
|
||||
void Style::setupLayer(Tile &tile, const Layer &layer) const
|
||||
{
|
||||
const Layer &sl = _layers.at(layer);
|
||||
const Sprites &sprites = (tile.scale().x() > 1.0 || tile.scale().y() > 1.0)
|
||||
&& !_sprites2x.isNull() ? _sprites2x : _sprites;
|
||||
|
||||
if (sl.isPath())
|
||||
tile.painter().drawPath(path);
|
||||
else if (sl.isSymbol())
|
||||
sl.addSymbol(tile, path, tags, sprites);
|
||||
if (layer.isSymbol())
|
||||
layer.setTextProperties(tile);
|
||||
else if (layer.isPath())
|
||||
layer.setPathPainter(tile, sprites(tile.scale()));
|
||||
}
|
||||
|
||||
void Style::drawBackground(Tile &tile) const
|
||||
{
|
||||
QRectF rect(QPointF(0, 0), tile.size());
|
||||
QRectF rect(QPointF(0, 0), QSizeF(tile.size().width() / tile.scale().x(),
|
||||
tile.size().height() / tile.scale().y()));
|
||||
QPainterPath path;
|
||||
path.addRect(rect);
|
||||
|
||||
@ -578,3 +614,52 @@ void Style::drawBackground(Tile &tile) const
|
||||
//tile.painter().setPen(Qt::red);
|
||||
//tile.painter().drawRect(rect);
|
||||
}
|
||||
|
||||
void Style::drawFeature(const PBF::Feature &feature, const Layer &layer,
|
||||
Tile &tile, const QSizeF &factor) const
|
||||
{
|
||||
if (!layer.match(tile.zoom(), feature))
|
||||
return;
|
||||
|
||||
QPainterPath path(feature.path(factor));
|
||||
if (!path.elementCount())
|
||||
return;
|
||||
|
||||
if (layer.isPath())
|
||||
tile.painter().drawPath(path);
|
||||
else if (layer.isSymbol())
|
||||
layer.addSymbol(tile, path, feature, sprites(tile.scale()));
|
||||
}
|
||||
|
||||
void Style::drawLayer(const PBF::Layer &pbfLayer, const Layer &styleLayer,
|
||||
Tile &tile) const
|
||||
{
|
||||
if (pbfLayer.data()->version() > 2)
|
||||
return;
|
||||
|
||||
QSizeF factor(tile.size().width() / tile.scale().x() /
|
||||
(qreal)pbfLayer.data()->extent(), tile.size().height() / tile.scale().y()
|
||||
/ (qreal)pbfLayer.data()->extent());
|
||||
|
||||
tile.painter().save();
|
||||
setupLayer(tile, styleLayer);
|
||||
for (int i = 0; i < pbfLayer.features().size(); i++)
|
||||
drawFeature(pbfLayer.features().at(i), styleLayer, tile, factor);
|
||||
tile.painter().restore();
|
||||
}
|
||||
|
||||
void Style::render(const PBF &data, Tile &tile) const
|
||||
{
|
||||
drawBackground(tile);
|
||||
|
||||
for (int i = 0; i < _layers.size(); i++) {
|
||||
QHash<QString, PBF::Layer*>::const_iterator it = data.layers().find(
|
||||
_layers.at(i).sourceLayer());
|
||||
if (it == data.layers().constEnd())
|
||||
continue;
|
||||
|
||||
drawLayer(**it, _layers.at(i), tile);
|
||||
}
|
||||
|
||||
tile.text().render(&tile.painter());
|
||||
}
|
||||
|
60
src/style.h
60
src/style.h
@ -3,12 +3,16 @@
|
||||
|
||||
#include <QObject>
|
||||
#include <QString>
|
||||
#include <QVector>
|
||||
#include <QPair>
|
||||
#include <QVariantHash>
|
||||
#include <QStringList>
|
||||
#include <QSet>
|
||||
#include <QPen>
|
||||
#include <QBrush>
|
||||
#include <QFont>
|
||||
#include "pbf.h"
|
||||
#include "config.h"
|
||||
#include "text.h"
|
||||
#include "function.h"
|
||||
#include "sprites.h"
|
||||
@ -16,6 +20,8 @@
|
||||
|
||||
class QPainter;
|
||||
class QPainterPath;
|
||||
class QJsonArray;
|
||||
class QJsonObject;
|
||||
class Tile;
|
||||
|
||||
class Style : public QObject
|
||||
@ -24,21 +30,12 @@ public:
|
||||
Style(QObject *parent = 0) : QObject(parent) {}
|
||||
|
||||
bool load(const QString &fileName);
|
||||
|
||||
const QStringList &sourceLayers() const
|
||||
{return _sourceLayers;}
|
||||
|
||||
bool match(int zoom, int layer, const QVariantHash &tags) const
|
||||
{return _layers.at(layer).match(zoom, tags);}
|
||||
void drawBackground(Tile &tile) const;
|
||||
void setupLayer(Tile &tile, int layer) const;
|
||||
void drawFeature(Tile &tile, int layer, const QPainterPath &path,
|
||||
const QVariantHash &tags) const;
|
||||
void render(const PBF &data, Tile &tile) const;
|
||||
|
||||
private:
|
||||
class Layer {
|
||||
public:
|
||||
Layer() : _type(Unknown), _minZoom(-1), _maxZoom(-1) {}
|
||||
Layer() : _type(Unknown), _minZoom(0), _maxZoom(24) {}
|
||||
Layer(const QJsonObject &json);
|
||||
|
||||
const QString &sourceLayer() const {return _sourceLayer;}
|
||||
@ -46,11 +43,11 @@ private:
|
||||
bool isBackground() const {return (_type == Background);}
|
||||
bool isSymbol() const {return (_type == Symbol);}
|
||||
|
||||
bool match(int zoom, const QVariantHash &tags) const;
|
||||
bool match(int zoom, const PBF::Feature &feature) const;
|
||||
void setPathPainter(Tile &tile, const Sprites &sprites) const;
|
||||
void setTextProperties(Tile &tile) const;
|
||||
void addSymbol(Tile &tile, const QPainterPath &path,
|
||||
const QVariantHash &tags, const Sprites &sprites) const;
|
||||
const PBF::Feature &feature, const Sprites &sprites) const;
|
||||
|
||||
private:
|
||||
enum Type {
|
||||
@ -66,13 +63,13 @@ private:
|
||||
Filter() : _type(None) {}
|
||||
Filter(const QJsonArray &json);
|
||||
|
||||
bool match(const QVariantHash &tags) const;
|
||||
bool match(const PBF::Feature &feature) const;
|
||||
private:
|
||||
enum Type {
|
||||
None, Unknown,
|
||||
EQ, NE, GE, GT, LE, LT,
|
||||
All, Any,
|
||||
In, Has
|
||||
In, Has, GeometryType
|
||||
};
|
||||
|
||||
Type _type;
|
||||
@ -86,7 +83,7 @@ private:
|
||||
public:
|
||||
Template() {}
|
||||
Template(const FunctionS &str) : _field(str) {}
|
||||
QString value(int zoom, const QVariantHash &tags) const;
|
||||
QString value(int zoom, const PBF::Feature &feature) const;
|
||||
|
||||
private:
|
||||
FunctionS _field;
|
||||
@ -102,10 +99,10 @@ private:
|
||||
{return _textMaxWidth.value(zoom);}
|
||||
qreal maxTextAngle(int zoom) const
|
||||
{return _textMaxAngle.value(zoom);}
|
||||
QString text(int zoom, const QVariantHash &tags) const
|
||||
{return _text.value(zoom, tags).trimmed();}
|
||||
QString icon(int zoom, const QVariantHash &tags) const
|
||||
{return _icon.value(zoom, tags);}
|
||||
QString text(int zoom, const PBF::Feature &feature) const
|
||||
{return _text.value(zoom, feature).trimmed();}
|
||||
QString icon(int zoom, const PBF::Feature &feature) const
|
||||
{return _icon.value(zoom, feature);}
|
||||
QFont font(int zoom) const;
|
||||
Qt::PenCapStyle lineCap(int zoom) const;
|
||||
Qt::PenJoinStyle lineJoin(int zoom) const;
|
||||
@ -140,11 +137,13 @@ private:
|
||||
const;
|
||||
qreal opacity(Layer::Type type, int zoom) const;
|
||||
bool antialias(Layer::Type type, int zoom) const;
|
||||
QString fillPattern(int zoom) const
|
||||
{return _fillPattern.value(zoom);}
|
||||
Text::Halo halo(int zoom) const
|
||||
{return Text::Halo(_textHaloColor.value(zoom),
|
||||
_textHaloWidth.value(zoom), _textHaloBlur.value(zoom));}
|
||||
|
||||
private:
|
||||
FunctionC _textColor;
|
||||
FunctionC _textHaloColor;
|
||||
FunctionC _lineColor;
|
||||
FunctionC _fillColor;
|
||||
FunctionC _fillOutlineColor;
|
||||
@ -152,6 +151,8 @@ private:
|
||||
FunctionF _fillOpacity;
|
||||
FunctionF _lineOpacity;
|
||||
FunctionF _lineWidth;
|
||||
FunctionF _textHaloWidth;
|
||||
FunctionF _textHaloBlur;
|
||||
FunctionB _fillAntialias;
|
||||
QVector<qreal> _lineDasharray;
|
||||
FunctionS _fillPattern;
|
||||
@ -165,9 +166,20 @@ private:
|
||||
Paint _paint;
|
||||
};
|
||||
|
||||
const Sprites &sprites(const QPointF &scale) const;
|
||||
|
||||
void drawBackground(Tile &tile) const;
|
||||
void setupLayer(Tile &tile, const Layer &layer) const;
|
||||
void drawFeature(const PBF::Feature &feature, const Layer &layer,
|
||||
Tile &tile, const QSizeF &factor) const;
|
||||
void drawLayer(const PBF::Layer &pbfLayer, const Layer &styleLayer,
|
||||
Tile &tile) const;
|
||||
|
||||
QVector<Layer> _layers;
|
||||
QStringList _sourceLayers;
|
||||
Sprites _sprites, _sprites2x;
|
||||
Sprites _sprites;
|
||||
#ifdef ENABLE_HIDPI
|
||||
Sprites _sprites2x;
|
||||
#endif // QT >= 5.6
|
||||
};
|
||||
|
||||
#endif // STYLE_H
|
||||
|
45
src/text.cpp
45
src/text.cpp
@ -1,5 +1,7 @@
|
||||
#include <QFontMetrics>
|
||||
#include <QPainter>
|
||||
#include <QSet>
|
||||
#include <QMap>
|
||||
#include "text.h"
|
||||
#include "textpointitem.h"
|
||||
#include "textpathitem.h"
|
||||
@ -30,22 +32,36 @@ void Text::addLabel(const QString &text, const QImage &icon,
|
||||
{
|
||||
TextItem *ti;
|
||||
|
||||
switch (_placement) {
|
||||
case Line:
|
||||
if (_alignment == Viewport)
|
||||
if (_alignment == Viewport) {
|
||||
QMap<qreal, int> map;
|
||||
for (int j = 0; j < path.elementCount(); j++) {
|
||||
QLineF l(path.elementAt(j), _sceneRect.center());
|
||||
map.insert(l.length(), j);
|
||||
}
|
||||
QMap<qreal, int>::const_iterator jt = map.constBegin();
|
||||
ti = new TextPointItem(text, path.elementAt(jt.value()), _font,
|
||||
_maxWidth, _anchor, icon);
|
||||
while (true) {
|
||||
if (_sceneRect.contains(ti->boundingRect()))
|
||||
break;
|
||||
if (++jt == map.constEnd())
|
||||
break;
|
||||
static_cast<TextPointItem*>(ti)->setPos(path.elementAt(
|
||||
jt.value()));
|
||||
}
|
||||
} else {
|
||||
switch (_placement) {
|
||||
case Line:
|
||||
ti = new TextPathItem(text, path, _font, _maxAngle, _sceneRect);
|
||||
break;
|
||||
case LineCenter:
|
||||
ti = new TextPointItem(text, path.pointAtPercent(0.5), _font,
|
||||
_maxWidth, _anchor, icon);
|
||||
break;
|
||||
default:
|
||||
ti = new TextPointItem(text, path.elementAt(0), _font,
|
||||
_maxWidth, _anchor, icon);
|
||||
else
|
||||
ti = new TextPathItem(text, path, _font, _maxAngle, _sceneRect);
|
||||
break;
|
||||
case LineCenter:
|
||||
ti = new TextPointItem(text, path.pointAtPercent(0.5), _font,
|
||||
_maxWidth, _anchor, icon);
|
||||
break;
|
||||
default:
|
||||
ti = new TextPointItem(text, path.elementAt(0), _font, _maxWidth,
|
||||
_anchor, icon);
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
// Note: empty path == point geometry (single move)
|
||||
@ -55,6 +71,7 @@ void Text::addLabel(const QString &text, const QImage &icon,
|
||||
}
|
||||
|
||||
ti->setPen(_pen);
|
||||
ti->setHalo(_halo);
|
||||
addItem(ti);
|
||||
|
||||
QList<TextItem*> ci = collidingItems(ti);
|
||||
|
18
src/text.h
18
src/text.h
@ -34,6 +34,22 @@ public:
|
||||
Auto
|
||||
};
|
||||
|
||||
class Halo {
|
||||
public:
|
||||
Halo() : _width(0), _blur(0) {}
|
||||
Halo(const QColor &color, qreal width, qreal blur)
|
||||
: _color(color), _width(width), _blur(blur) {}
|
||||
|
||||
const QColor &color() const {return _color;}
|
||||
qreal width() const {return _width;}
|
||||
qreal blur() const {return _blur;}
|
||||
|
||||
private:
|
||||
QColor _color;
|
||||
qreal _width;
|
||||
qreal _blur;
|
||||
};
|
||||
|
||||
Text(const QSize &size) : _sceneRect(QRectF(QPointF(0, 0), size)) {}
|
||||
~Text();
|
||||
|
||||
@ -45,6 +61,7 @@ public:
|
||||
void setSymbolPlacement(SymbolPlacement placement);
|
||||
void setRotationAlignment(RotationAlignment alignment)
|
||||
{_alignment = alignment;}
|
||||
void setHalo(const Halo &halo) {_halo = halo;}
|
||||
|
||||
void addLabel(const QString &text, const QImage &icon,
|
||||
const QPainterPath &path);
|
||||
@ -65,6 +82,7 @@ private:
|
||||
RotationAlignment _alignment;
|
||||
QFont _font;
|
||||
QPen _pen;
|
||||
Halo _halo;
|
||||
};
|
||||
|
||||
#endif // TEXT_H
|
||||
|
39
src/textitem.cpp
Normal file
39
src/textitem.cpp
Normal file
@ -0,0 +1,39 @@
|
||||
#include "textitem.h"
|
||||
|
||||
bool TextItem::collidesWithItem(const TextItem *other) const
|
||||
{
|
||||
QRectF r1(boundingRect());
|
||||
QRectF r2(other->boundingRect());
|
||||
|
||||
if (r1.isEmpty() || r2.isEmpty() || !r1.intersects(r2))
|
||||
return false;
|
||||
|
||||
return other->shape().intersects(shape());
|
||||
}
|
||||
|
||||
qreal TextItem::avgCharWidth() const
|
||||
{
|
||||
qreal ratio;
|
||||
ushort cp = _text.at(0).unicode();
|
||||
|
||||
// CJK & East Asian scripts
|
||||
if (cp >= 0x2E80)
|
||||
ratio = 1.0;
|
||||
// Greek & Cyrilic
|
||||
else if (cp >= 0x03FF && cp <= 0x04FF) {
|
||||
ratio = (_font.capitalization() == QFont::AllUppercase) ? 0.80 : 0.73;
|
||||
if (_font.bold())
|
||||
ratio *= 1.1;
|
||||
if (_font.italic())
|
||||
ratio *= 0.9;
|
||||
// The rest (Latin scripts, Arabic, ...)
|
||||
} else {
|
||||
ratio = (_font.capitalization() == QFont::AllUppercase) ? 0.75 : 0.63;
|
||||
if (_font.bold())
|
||||
ratio *= 1.1;
|
||||
if (_font.italic())
|
||||
ratio *= 0.9;
|
||||
}
|
||||
|
||||
return ratio * _font.pixelSize();
|
||||
}
|
@ -1,20 +1,28 @@
|
||||
#ifndef TEXTITEM_H
|
||||
#define TEXTITEM_H
|
||||
|
||||
#include <QString>
|
||||
#include <QPainterPath>
|
||||
#include <QPen>
|
||||
#include <QFont>
|
||||
#include <QRectF>
|
||||
#include "text.h"
|
||||
|
||||
class QPainter;
|
||||
|
||||
class TextItem
|
||||
{
|
||||
public:
|
||||
TextItem(const QString &text) : _text(text), _visible(true) {}
|
||||
TextItem(const QString &text, const QFont &font)
|
||||
: _text(text), _font(font), _visible(true) {}
|
||||
virtual ~TextItem() {}
|
||||
|
||||
const QString &text() const {return _text;}
|
||||
const QFont &font() const {return _font;}
|
||||
void setFont(const QFont &font) {_font = font;}
|
||||
const QPen &pen() const {return _pen;}
|
||||
const Text::Halo &halo() const {return _halo;}
|
||||
void setPen(const QPen &pen) {_pen = pen;}
|
||||
void setHalo(const Text::Halo &halo) {_halo = halo;}
|
||||
|
||||
virtual QPainterPath shape() const = 0;
|
||||
virtual QRectF boundingRect() const = 0;
|
||||
@ -23,38 +31,16 @@ public:
|
||||
bool isVisible() const {return _visible;}
|
||||
void setVisible(bool visible) {_visible = visible;}
|
||||
|
||||
bool collidesWithItem(const TextItem *other) const
|
||||
{
|
||||
QRectF r1(boundingRect());
|
||||
QRectF r2(other->boundingRect());
|
||||
|
||||
if (r1.isEmpty() || r2.isEmpty() || !r1.intersects(r2))
|
||||
return false;
|
||||
|
||||
return other->shape().intersects(shape());
|
||||
}
|
||||
bool collidesWithItem(const TextItem *other) const;
|
||||
|
||||
protected:
|
||||
static int avgCharWidth(const QString &str, const QFont &font)
|
||||
{
|
||||
qreal ratio;
|
||||
|
||||
if (str.at(0).unicode() >= 0x2E80)
|
||||
ratio = 1.0;
|
||||
else {
|
||||
ratio = (font.capitalization() == QFont::AllUppercase)
|
||||
? 0.66 : 0.55;
|
||||
if (font.bold())
|
||||
ratio *= 1.1;
|
||||
}
|
||||
|
||||
return ratio * font.pixelSize();
|
||||
}
|
||||
qreal avgCharWidth() const;
|
||||
|
||||
private:
|
||||
QString _text;
|
||||
QFont _font;
|
||||
QPen _pen;
|
||||
Text::Halo _halo;
|
||||
bool _visible;
|
||||
};
|
||||
|
||||
|
@ -3,23 +3,23 @@
|
||||
#include "textpathitem.h"
|
||||
|
||||
|
||||
static QPointF intersection(const QLineF &line, const QRectF &rect)
|
||||
static bool intersection(const QLineF &line, const QRectF &rect,
|
||||
QPointF *p)
|
||||
{
|
||||
QPointF p;
|
||||
if (line.intersect(QLineF(rect.topLeft(), rect.topRight()), &p)
|
||||
if (line.intersect(QLineF(rect.topLeft(), rect.topRight()), p)
|
||||
== QLineF::BoundedIntersection)
|
||||
return p;
|
||||
if (line.intersect(QLineF(rect.topLeft(), rect.bottomLeft()), &p)
|
||||
return true;
|
||||
if (line.intersect(QLineF(rect.topLeft(), rect.bottomLeft()), p)
|
||||
== QLineF::BoundedIntersection)
|
||||
return p;
|
||||
if (line.intersect(QLineF(rect.bottomRight(), rect.bottomLeft()), &p)
|
||||
return true;
|
||||
if (line.intersect(QLineF(rect.bottomRight(), rect.bottomLeft()), p)
|
||||
== QLineF::BoundedIntersection)
|
||||
return p;
|
||||
if (line.intersect(QLineF(rect.bottomRight(), rect.topRight()), &p)
|
||||
return true;
|
||||
if (line.intersect(QLineF(rect.bottomRight(), rect.topRight()), p)
|
||||
== QLineF::BoundedIntersection)
|
||||
return p;
|
||||
return true;
|
||||
|
||||
return rect.center();
|
||||
return false;
|
||||
}
|
||||
|
||||
static QPainterPath subpath(const QList<QLineF> &lines, int start, int end,
|
||||
@ -63,6 +63,7 @@ static QList<QLineF> lineString(const QPainterPath &path,
|
||||
{
|
||||
QList<QLineF> lines;
|
||||
int start = 0, end = path.elementCount() - 1;
|
||||
QPointF p;
|
||||
|
||||
for (int i = 0; i < path.elementCount(); i++) {
|
||||
if (boundingRect.contains(path.elementAt(i))) {
|
||||
@ -79,16 +80,14 @@ static QList<QLineF> lineString(const QPainterPath &path,
|
||||
|
||||
if (start > 0) {
|
||||
QLineF l(path.elementAt(start-1), path.elementAt(start));
|
||||
QPointF p(intersection(l, boundingRect));
|
||||
if (p != boundingRect.center())
|
||||
if (intersection(l, boundingRect, &p))
|
||||
lines.append(QLineF(p, path.elementAt(start)));
|
||||
}
|
||||
for (int i = start + 1; i <= end; i++)
|
||||
lines.append(QLineF(path.elementAt(i-1), path.elementAt(i)));
|
||||
if (end < path.elementCount() - 1) {
|
||||
QLineF l(path.elementAt(end), path.elementAt(end+1));
|
||||
QPointF p(intersection(l, boundingRect));
|
||||
if (p != boundingRect.center())
|
||||
if (intersection(l, boundingRect, &p))
|
||||
lines.append(QLineF(path.elementAt(end), p));
|
||||
}
|
||||
|
||||
@ -134,9 +133,9 @@ static bool reverse(const QPainterPath &path)
|
||||
|
||||
TextPathItem::TextPathItem(const QString &text, const QPainterPath &path,
|
||||
const QFont &font, int maxAngle, const QRectF &tileRect)
|
||||
: TextItem(text)
|
||||
: TextItem(text, font)
|
||||
{
|
||||
int cw = avgCharWidth(text, font);
|
||||
qreal cw = avgCharWidth();
|
||||
int textWidth = text.size() * cw;
|
||||
if (textWidth > path.length())
|
||||
return;
|
||||
@ -146,7 +145,6 @@ TextPathItem::TextPathItem(const QString &text, const QPainterPath &path,
|
||||
return;
|
||||
|
||||
_path = reverse(tp) ? tp.toReversed() : tp;
|
||||
setFont(font);
|
||||
|
||||
QPainterPathStroker s;
|
||||
s.setWidth(font.pixelSize());
|
||||
@ -167,10 +165,35 @@ void TextPathItem::paint(QPainter *painter) const
|
||||
qreal percent = (1.0 - factor) / 2.0;
|
||||
|
||||
painter->setFont(font());
|
||||
painter->setPen(pen());
|
||||
|
||||
QTransform t = painter->transform();
|
||||
|
||||
if (halo().color().isValid() && halo().width() > 0) {
|
||||
painter->setPen(halo().color());
|
||||
for (int i = 0; i < text().size(); i++) {
|
||||
QPointF point = _path.pointAtPercent(percent);
|
||||
qreal angle = _path.angleAtPercent(percent);
|
||||
|
||||
painter->translate(point);
|
||||
painter->rotate(-angle);
|
||||
painter->drawText(QPoint(-1, fm.descent() - 1), text().at(i));
|
||||
painter->drawText(QPoint(1, fm.descent() + 1), text().at(i));
|
||||
painter->drawText(QPoint(-1, fm.descent() + 1), text().at(i));
|
||||
painter->drawText(QPoint(1, fm.descent() -1), text().at(i));
|
||||
painter->drawText(QPoint(0, fm.descent() - 1), text().at(i));
|
||||
painter->drawText(QPoint(0, fm.descent() + 1), text().at(i));
|
||||
painter->drawText(QPoint(-1, fm.descent()), text().at(i));
|
||||
painter->drawText(QPoint(1, fm.descent()), text().at(i));
|
||||
painter->setTransform(t);
|
||||
|
||||
int width = fm.charWidth(text(), i);
|
||||
percent += ((qreal)width / (qreal)textWidth) * factor;
|
||||
}
|
||||
|
||||
percent = (1.0 - factor) / 2.0;
|
||||
}
|
||||
|
||||
painter->setPen(pen());
|
||||
for (int i = 0; i < text().size(); i++) {
|
||||
QPointF point = _path.pointAtPercent(percent);
|
||||
qreal angle = _path.angleAtPercent(percent);
|
||||
|
@ -1,10 +1,7 @@
|
||||
#ifndef TEXTPATHITEM_H
|
||||
#define TEXTPATHITEM_H
|
||||
|
||||
#include <QFont>
|
||||
#include <QString>
|
||||
#include "textitem.h"
|
||||
#include "text.h"
|
||||
|
||||
class TextPathItem : public TextItem
|
||||
{
|
||||
|
@ -1,41 +1,23 @@
|
||||
#include <QPainter>
|
||||
#include <QtMath>
|
||||
#include <QStaticText>
|
||||
#include "config.h"
|
||||
#include "textpointitem.h"
|
||||
|
||||
|
||||
#define FLAGS (Qt::AlignCenter | Qt::TextWordWrap | Qt::TextDontClip)
|
||||
|
||||
QRectF TextPointItem::exactBoundingRect(const QString &str,
|
||||
const QFont &font, int maxWidth)
|
||||
QRectF TextPointItem::fuzzyBoundingRect() const
|
||||
{
|
||||
QFontMetrics fm(font);
|
||||
int limit = font.pixelSize() * maxWidth;
|
||||
// Italic fonts overflow the computed bounding rect, so reduce it
|
||||
// a little bit.
|
||||
if (font.italic())
|
||||
limit -= font.pixelSize() / 2.0;
|
||||
int fs = font().pixelSize();
|
||||
if (text().size() <= 3)
|
||||
return QRectF(0, 0, text().size() * fs, fs * 1.6);
|
||||
|
||||
QRect br = fm.boundingRect(QRect(0, 0, limit, 0), FLAGS, str);
|
||||
Q_ASSERT(br.isValid());
|
||||
// Expand the bounding rect back to the real content size
|
||||
if (font.italic())
|
||||
br.adjust(-font.pixelSize() / 4.0, 0, font.pixelSize() / 4.0, 0);
|
||||
|
||||
return br;
|
||||
}
|
||||
|
||||
QRectF TextPointItem::fuzzyBoundingRect(const QString &str,
|
||||
const QFont &font, int maxWidth)
|
||||
{
|
||||
int limit = font.pixelSize() * maxWidth;
|
||||
qreal cw = avgCharWidth(str, font);
|
||||
qreal lh = font.pixelSize() * 1.25;
|
||||
int limit = fs * _maxWidth;
|
||||
qreal cw = avgCharWidth();
|
||||
int width = 0, lines = 0;
|
||||
|
||||
if (font.italic())
|
||||
limit -= font.pixelSize() / 2.0;
|
||||
|
||||
QStringList l(str.split('\n'));
|
||||
QStringList l(text().split('\n'));
|
||||
for (int i = 0; i < l.size(); i++) {
|
||||
int lw = (int)(l.at(i).length() * cw);
|
||||
if (lw > limit) {
|
||||
@ -48,7 +30,7 @@ QRectF TextPointItem::fuzzyBoundingRect(const QString &str,
|
||||
if (wl + pl < limit) {
|
||||
pl += wl + cw;
|
||||
} else {
|
||||
if (wl > limit) {
|
||||
if (wl >= limit) {
|
||||
if (pl > 0)
|
||||
lines++;
|
||||
} else
|
||||
@ -65,27 +47,32 @@ QRectF TextPointItem::fuzzyBoundingRect(const QString &str,
|
||||
}
|
||||
}
|
||||
|
||||
return QRectF(0, 0, width, lines * lh);
|
||||
return QRectF(0, 0, width, lines * fs * 1.6);
|
||||
}
|
||||
|
||||
|
||||
QRectF TextPointItem::computeTextRect(BoundingRectFunction brf) const
|
||||
QRectF TextPointItem::moveTextRect(const QRectF &rect) const
|
||||
{
|
||||
#ifdef ENABLE_HIDPI
|
||||
QRectF iconRect = _icon.isNull() ? QRectF()
|
||||
: QRectF(QPointF(0, 0), QSizeF(_icon.size()) / _icon.devicePixelRatioF());
|
||||
QRectF textRect = brf(text(), font(), _maxWidth);
|
||||
#else // ENABLE_HIDPI
|
||||
QRectF iconRect = _icon.isNull() ? QRectF() : QRectF(QPointF(0, 0),
|
||||
QSizeF(_icon.size()));
|
||||
#endif // ENABLE_HIDPI
|
||||
QRectF textRect(rect);
|
||||
|
||||
switch (_anchor) {
|
||||
case Text::Center:
|
||||
textRect.moveCenter(_pos);
|
||||
break;
|
||||
case Text::Left:
|
||||
textRect.moveTopLeft(_pos - QPointF(-iconRect.width() / 2,
|
||||
textRect.height() / 2));
|
||||
textRect.moveTopLeft(_pos - QPointF(-iconRect.width() / 2
|
||||
- font().pixelSize()/4.0, textRect.height() / 2));
|
||||
break;
|
||||
case Text::Right:
|
||||
textRect.moveTopRight(_pos - QPointF(iconRect.width() / 2,
|
||||
textRect.height() / 2));
|
||||
textRect.moveTopRight(_pos - QPointF(iconRect.width() / 2
|
||||
+ font().pixelSize()/4.0, textRect.height() / 2));
|
||||
break;
|
||||
case Text::Bottom:
|
||||
textRect.moveTopLeft(_pos - QPointF(textRect.width() / 2,
|
||||
@ -102,15 +89,19 @@ QRectF TextPointItem::computeTextRect(BoundingRectFunction brf) const
|
||||
|
||||
TextPointItem::TextPointItem(const QString &text, const QPointF &pos,
|
||||
const QFont &font, int maxWidth, Text::Anchor anchor, const QImage &icon)
|
||||
: TextItem(text), _pos(pos), _icon(icon), _maxWidth(maxWidth),
|
||||
: TextItem(text, font), _pos(pos), _icon(icon), _maxWidth(maxWidth),
|
||||
_anchor(anchor)
|
||||
{
|
||||
setFont(font);
|
||||
_boundingRect = computeTextRect(fuzzyBoundingRect);
|
||||
_textRect = fuzzyBoundingRect();
|
||||
_boundingRect = moveTextRect(_textRect);
|
||||
|
||||
if (!_icon.isNull()) {
|
||||
#ifdef ENABLE_HIDPI
|
||||
QRectF iconRect(QPointF(0, 0), QSizeF(_icon.size())
|
||||
/ _icon.devicePixelRatioF());
|
||||
#else // ENABLE_HIDPI
|
||||
QRectF iconRect(QPointF(0, 0), QSizeF(_icon.size()));
|
||||
#endif // ENABLE_HIDPI
|
||||
iconRect.moveCenter(pos);
|
||||
_boundingRect |= iconRect;
|
||||
}
|
||||
@ -118,22 +109,59 @@ TextPointItem::TextPointItem(const QString &text, const QPointF &pos,
|
||||
_shape.addRect(_boundingRect);
|
||||
}
|
||||
|
||||
void TextPointItem::setPos(const QPointF &pos)
|
||||
{
|
||||
QPointF d(_boundingRect.left() - _pos.x(), _boundingRect.top() - _pos.y());
|
||||
_boundingRect.moveTopLeft(pos + d);
|
||||
_shape = QPainterPath();
|
||||
_shape.addRect(_boundingRect);
|
||||
_pos = pos;
|
||||
}
|
||||
|
||||
void TextPointItem::paint(QPainter *painter) const
|
||||
{
|
||||
//painter->setPen(Qt::red);
|
||||
//painter->drawRect(_boundingRect);
|
||||
|
||||
QRectF textRect;
|
||||
|
||||
if (!_icon.isNull()) {
|
||||
textRect = computeTextRect(exactBoundingRect);
|
||||
painter->drawImage(_pos - QPointF(_icon.width()
|
||||
/ _icon.devicePixelRatioF() / 2, _icon.height()
|
||||
/ _icon.devicePixelRatioF() / 2), _icon);
|
||||
} else
|
||||
textRect = computeTextRect(fuzzyBoundingRect);
|
||||
|
||||
painter->setFont(font());
|
||||
painter->setPen(pen());
|
||||
painter->drawText(textRect, FLAGS, text());
|
||||
|
||||
if (!_icon.isNull()) {
|
||||
textRect = moveTextRect(painter->boundingRect(_textRect, FLAGS, text()));
|
||||
#ifdef ENABLE_HIDPI
|
||||
painter->drawImage(_pos - QPointF(_icon.width()
|
||||
/ _icon.devicePixelRatioF() / 2, _icon.height()
|
||||
/ _icon.devicePixelRatioF() / 2), _icon);
|
||||
#else // ENABLE_HIDPI
|
||||
painter->drawImage(_pos - QPointF(_icon.width() / 2,
|
||||
_icon.height() / 2), _icon);
|
||||
#endif // ENABLE_HIDPI
|
||||
} else
|
||||
textRect = _boundingRect;
|
||||
|
||||
if (hasHalo()) {
|
||||
QStaticText st(text());
|
||||
st.setTextFormat(Qt::PlainText);
|
||||
st.setTextWidth(textRect.width());
|
||||
st.setTextOption(QTextOption(Qt::AlignHCenter));
|
||||
st.setPerformanceHint(QStaticText::AggressiveCaching);
|
||||
|
||||
painter->setPen(halo().color());
|
||||
painter->drawStaticText(textRect.topLeft() + QPointF(-1, -1), st);
|
||||
painter->drawStaticText(textRect.topLeft() + QPointF(+1, +1), st);
|
||||
painter->drawStaticText(textRect.topLeft() + QPointF(-1, +1), st);
|
||||
painter->drawStaticText(textRect.topLeft() + QPointF(+1, -1), st);
|
||||
painter->drawStaticText(textRect.topLeft() + QPointF(0, -1), st);
|
||||
painter->drawStaticText(textRect.topLeft() + QPointF(0, +1), st);
|
||||
painter->drawStaticText(textRect.topLeft() + QPointF(-1, 0), st);
|
||||
painter->drawStaticText(textRect.topLeft() + QPointF(+1, 0), st);
|
||||
painter->setPen(pen());
|
||||
painter->drawStaticText(textRect.topLeft(), st);
|
||||
} else
|
||||
painter->drawText(textRect, FLAGS, text());
|
||||
|
||||
//painter->setBrush(Qt::NoBrush);
|
||||
//painter->setPen(Qt::red);
|
||||
//painter->drawRect(_boundingRect);
|
||||
//painter->setPen(Qt::blue);
|
||||
//painter->drawRect(textRect);
|
||||
}
|
||||
|
@ -1,11 +1,9 @@
|
||||
#ifndef TEXTPOINTITEM_H
|
||||
#define TEXTPOINTITEM_H
|
||||
|
||||
#include <QPen>
|
||||
#include <QFont>
|
||||
#include <QString>
|
||||
#include "textitem.h"
|
||||
#include <QImage>
|
||||
#include "text.h"
|
||||
#include "textitem.h"
|
||||
|
||||
class TextPointItem : public TextItem
|
||||
{
|
||||
@ -17,19 +15,17 @@ public:
|
||||
QPainterPath shape() const {return _shape;}
|
||||
void paint(QPainter *painter) const;
|
||||
|
||||
void setPos(const QPointF &pos);
|
||||
|
||||
private:
|
||||
typedef QRectF (*BoundingRectFunction)(const QString &, const QFont &, int);
|
||||
|
||||
static QRectF exactBoundingRect(const QString &str, const QFont &font,
|
||||
int maxWidth);
|
||||
static QRectF fuzzyBoundingRect(const QString &str, const QFont &font,
|
||||
int maxWidth);
|
||||
|
||||
QRectF computeTextRect(BoundingRectFunction brf) const;
|
||||
QRectF fuzzyBoundingRect() const;
|
||||
QRectF moveTextRect(const QRectF &rect) const;
|
||||
bool hasHalo() const
|
||||
{return halo().color().isValid() && halo().width() > 0;}
|
||||
|
||||
QPointF _pos;
|
||||
QPainterPath _shape;
|
||||
QRectF _boundingRect;
|
||||
QRectF _textRect, _boundingRect;
|
||||
QImage _icon;
|
||||
int _maxWidth;
|
||||
Text::Anchor _anchor;
|
||||
|
Reference in New Issue
Block a user