Added support for QT < 5.6 (and >= 5.4)

This commit is contained in:
Martin Tůma 2018-12-17 18:31:26 +01:00
parent 368c53307d
commit c1af3d82a5
6 changed files with 51 additions and 9 deletions

View File

@ -1,7 +1,8 @@
language: c++ language: c++
dist: xenial
os: os:
# - linux - linux
- osx - osx
env: env:

View File

@ -20,7 +20,8 @@ HEADERS += src/pbfhandler.h \
src/textpointitem.h \ src/textpointitem.h \
src/font.h \ src/font.h \
src/textitem.h \ src/textitem.h \
src/sprites.h src/sprites.h \
src/config.h
SOURCES += src/pbfplugin.cpp \ SOURCES += src/pbfplugin.cpp \
src/pbfhandler.cpp \ src/pbfhandler.cpp \
src/gzip.cpp \ src/gzip.cpp \

10
src/config.h Normal file
View 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

View File

@ -530,34 +530,43 @@ bool Style::load(const QString &fileName)
QDir styleDir = QFileInfo(fileName).absoluteDir(); QDir styleDir = QFileInfo(fileName).absoluteDir();
loadSprites(styleDir, "sprite.json", "sprite.png", _sprites); loadSprites(styleDir, "sprite.json", "sprite.png", _sprites);
#ifdef ENABLE_HIDPI
loadSprites(styleDir, "sprite@2x.json", "sprite@2x.png", _sprites2x); loadSprites(styleDir, "sprite@2x.json", "sprite@2x.png", _sprites2x);
#endif // ENABLE_HIDPI
return true; return true;
} }
const Sprites &Style::sprites(const QPointF &scale) const
{
#ifdef ENABLE_HIDPI
return (scale.x() > 1.0 || scale.y() > 1.0)
&& !_sprites2x.isNull() ? _sprites2x : _sprites;
#else // ENABLE_HIDPI
Q_UNUSED(scale);
return _sprites;
#endif // ENABLE_HIDPI
}
void Style::setupLayer(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);
const Sprites &sprites = (tile.scale().x() > 1.0 || tile.scale().y() > 1.0)
&& !_sprites2x.isNull() ? _sprites2x : _sprites;
if (sl.isSymbol()) if (sl.isSymbol())
sl.setTextProperties(tile); sl.setTextProperties(tile);
else if (sl.isPath()) else if (sl.isPath())
sl.setPathPainter(tile, sprites); sl.setPathPainter(tile, sprites(tile.scale()));
} }
void Style::drawFeature(Tile &tile, int layer, const QPainterPath &path, void Style::drawFeature(Tile &tile, int layer, const QPainterPath &path,
const QVariantHash &tags) const const QVariantHash &tags) const
{ {
const Layer &sl = _layers.at(layer); 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()) if (sl.isPath())
tile.painter().drawPath(path); tile.painter().drawPath(path);
else if (sl.isSymbol()) else if (sl.isSymbol())
sl.addSymbol(tile, path, tags, sprites); sl.addSymbol(tile, path, tags, sprites(tile.scale()));
} }
void Style::drawBackground(Tile &tile) const void Style::drawBackground(Tile &tile) const

View File

@ -11,6 +11,7 @@
#include <QPen> #include <QPen>
#include <QBrush> #include <QBrush>
#include <QFont> #include <QFont>
#include "config.h"
#include "text.h" #include "text.h"
#include "function.h" #include "function.h"
#include "sprites.h" #include "sprites.h"
@ -167,9 +168,14 @@ private:
Paint _paint; Paint _paint;
}; };
const Sprites &sprites(const QPointF &scale) const;
QVector<Layer> _layers; QVector<Layer> _layers;
QStringList _sourceLayers; QStringList _sourceLayers;
Sprites _sprites, _sprites2x; Sprites _sprites;
#ifdef ENABLE_HIDPI
Sprites _sprites2x;
#endif // QT >= 5.6
}; };
#endif // STYLE_H #endif // STYLE_H

View File

@ -1,5 +1,6 @@
#include <QPainter> #include <QPainter>
#include <QtMath> #include <QtMath>
#include "config.h"
#include "textpointitem.h" #include "textpointitem.h"
@ -66,8 +67,13 @@ QRectF TextPointItem::fuzzyBoundingRect() const
QRectF TextPointItem::computeTextRect(bool exact) const QRectF TextPointItem::computeTextRect(bool exact) const
{ {
#ifdef ENABLE_HIDPI
QRectF iconRect = _icon.isNull() ? QRectF() QRectF iconRect = _icon.isNull() ? QRectF()
: QRectF(QPointF(0, 0), QSizeF(_icon.size()) / _icon.devicePixelRatioF()); : QRectF(QPointF(0, 0), QSizeF(_icon.size()) / _icon.devicePixelRatioF());
#else // ENABLE_HIDPI
QRectF iconRect = _icon.isNull() ? QRectF() : QRectF(QPointF(0, 0),
QSizeF(_icon.size()));
#endif // ENABLE_HIDPI
QRectF textRect = exact ? exactBoundingRect() : fuzzyBoundingRect(); QRectF textRect = exact ? exactBoundingRect() : fuzzyBoundingRect();
switch (_anchor) { switch (_anchor) {
@ -103,8 +109,12 @@ TextPointItem::TextPointItem(const QString &text, const QPointF &pos,
_boundingRect = computeTextRect(false); _boundingRect = computeTextRect(false);
if (!_icon.isNull()) { if (!_icon.isNull()) {
#ifdef ENABLE_HIDPI
QRectF iconRect(QPointF(0, 0), QSizeF(_icon.size()) QRectF iconRect(QPointF(0, 0), QSizeF(_icon.size())
/ _icon.devicePixelRatioF()); / _icon.devicePixelRatioF());
#else // ENABLE_HIDPI
QRectF iconRect(QPointF(0, 0), QSizeF(_icon.size()));
#endif // ENABLE_HIDPI
iconRect.moveCenter(pos); iconRect.moveCenter(pos);
_boundingRect |= iconRect; _boundingRect |= iconRect;
} }
@ -121,9 +131,14 @@ void TextPointItem::paint(QPainter *painter) const
if (!_icon.isNull()) { if (!_icon.isNull()) {
textRect = computeTextRect(true); textRect = computeTextRect(true);
#ifdef ENABLE_HIDPI
painter->drawImage(_pos - QPointF(_icon.width() painter->drawImage(_pos - QPointF(_icon.width()
/ _icon.devicePixelRatioF() / 2, _icon.height() / _icon.devicePixelRatioF() / 2, _icon.height()
/ _icon.devicePixelRatioF() / 2), _icon); / _icon.devicePixelRatioF() / 2), _icon);
#else // ENABLE_HIDPI
painter->drawImage(_pos - QPointF(_icon.width() / 2,
_icon.height() / 2), _icon);
#endif // ENABLE_HIDPI
} else } else
textRect = _boundingRect; textRect = _boundingRect;