1
0
mirror of https://github.com/tumic0/GPXSee.git synced 2025-01-18 19:52:09 +01:00

propper hidpi icon scaling

This commit is contained in:
Martin Tůma 2021-04-18 18:10:23 +02:00
parent 663859ef1c
commit 3c8ac118ae
5 changed files with 47 additions and 39 deletions

View File

@ -8,9 +8,9 @@
using namespace Mapsforge; using namespace Mapsforge;
static const Style& style() static const Style& style(qreal ratio)
{ {
static Style s(ProgramPaths::renderthemeFile()); static Style s(ProgramPaths::renderthemeFile(), ratio);
return s; return s;
} }
@ -88,7 +88,7 @@ static const QColor *haloColor(const Style::TextRender *ti)
void RasterTile::processPointLabels(QList<TextItem*> &textItems) void RasterTile::processPointLabels(QList<TextItem*> &textItems)
{ {
const Style &s = style(); const Style &s = style(_ratio);
QList<const Style::TextRender*> labels(s.pointLabels(_zoom)); QList<const Style::TextRender*> labels(s.pointLabels(_zoom));
QList<const Style::Symbol*> symbols(s.pointSymbols(_zoom)); QList<const Style::Symbol*> symbols(s.pointSymbols(_zoom));
@ -136,7 +136,7 @@ void RasterTile::processPointLabels(QList<TextItem*> &textItems)
void RasterTile::processAreaLabels(QList<TextItem*> &textItems) void RasterTile::processAreaLabels(QList<TextItem*> &textItems)
{ {
const Style &s = style(); const Style &s = style(_ratio);
QList<const Style::TextRender*> labels(s.areaLabels(_zoom)); QList<const Style::TextRender*> labels(s.areaLabels(_zoom));
QList<const Style::Symbol*> symbols(s.areaSymbols(_zoom)); QList<const Style::Symbol*> symbols(s.areaSymbols(_zoom));
@ -189,7 +189,7 @@ void RasterTile::processAreaLabels(QList<TextItem*> &textItems)
void RasterTile::processLineLabels(QList<TextItem*> &textItems) void RasterTile::processLineLabels(QList<TextItem*> &textItems)
{ {
const Style &s = style(); const Style &s = style(_ratio);
QList<const Style::TextRender*> instructions(s.pathLabels(_zoom)); QList<const Style::TextRender*> instructions(s.pathLabels(_zoom));
QSet<QString> set; QSet<QString> set;
@ -248,7 +248,7 @@ QVector<RasterTile::PathInstruction> RasterTile::pathInstructions()
{ {
QCache<Key, QVector<const Style::PathRender *> > cache(1024); QCache<Key, QVector<const Style::PathRender *> > cache(1024);
QVector<PathInstruction> instructions; QVector<PathInstruction> instructions;
const Style &s = style(); const Style &s = style(_ratio);
QVector<const Style::PathRender*> *ri; QVector<const Style::PathRender*> *ri;
for (int i = 0 ; i < _paths.size(); i++) { for (int i = 0 ; i < _paths.size(); i++) {
@ -322,11 +322,11 @@ void RasterTile::render()
QList<TextItem*> textItems; QList<TextItem*> textItems;
_pixmap.setDevicePixelRatio(_ratio);
_pixmap.fill(Qt::transparent); _pixmap.fill(Qt::transparent);
QPainter painter(&_pixmap); QPainter painter(&_pixmap);
painter.setRenderHint(QPainter::Antialiasing); painter.setRenderHint(QPainter::Antialiasing);
painter.scale(_ratio, _ratio);
painter.translate(-_rect.x(), -_rect.y()); painter.translate(-_rect.x(), -_rect.y());
drawPaths(&painter); drawPaths(&painter);

View File

@ -16,7 +16,7 @@ static QString resourcePath(const QString &src, const QString &dir)
return dir + "/" + url.toLocalFile(); return dir + "/" + url.toLocalFile();
} }
static QImage image(const QString &path, int width, int height) static QImage image(const QString &path, int width, int height, qreal ratio)
{ {
QImageReader ir(path, "svg"); QImageReader ir(path, "svg");
@ -29,8 +29,10 @@ static QImage image(const QString &path, int width, int height)
} else if (!height) } else if (!height)
height = ir.size().width() / (ir.size().width() / (double)width); height = ir.size().width() / (ir.size().width() / (double)width);
ir.setScaledSize(QSize(width, height)); ir.setScaledSize(QSize(width * ratio, height * ratio));
return ir.read(); QImage img(ir.read());
img.setDevicePixelRatio(ratio);
return img;
} else } else
return QImage(path); return QImage(path);
} }
@ -77,7 +79,8 @@ bool Style::Rule::match(int zoom, bool closed,
return true; return true;
} }
void Style::area(QXmlStreamReader &reader, const QString &dir, const Rule &rule) void Style::area(QXmlStreamReader &reader, const QString &dir, qreal ratio,
const Rule &rule)
{ {
PathRender ri(rule, _paths.size()); PathRender ri(rule, _paths.size());
const QXmlStreamAttributes &attr = reader.attributes(); const QXmlStreamAttributes &attr = reader.attributes();
@ -99,7 +102,7 @@ void Style::area(QXmlStreamReader &reader, const QString &dir, const Rule &rule)
width = attr.value("symbol-width").toInt(); width = attr.value("symbol-width").toInt();
if (!file.isNull()) if (!file.isNull())
ri._fillImage = image(file, width, height); ri._fillImage = image(file, width, height, ratio);
_paths.append(ri); _paths.append(ri);
@ -184,7 +187,7 @@ void Style::text(QXmlStreamReader &reader, const Rule &rule,
reader.skipCurrentElement(); reader.skipCurrentElement();
} }
void Style::symbol(QXmlStreamReader &reader, const QString &dir, void Style::symbol(QXmlStreamReader &reader, const QString &dir, qreal ratio,
const Rule &rule) const Rule &rule)
{ {
Symbol ri(rule); Symbol ri(rule);
@ -200,14 +203,14 @@ void Style::symbol(QXmlStreamReader &reader, const QString &dir,
width = attr.value("symbol-width").toInt(); width = attr.value("symbol-width").toInt();
if (!file.isNull()) if (!file.isNull())
ri._img = image(file, width, height); ri._img = image(file, width, height, ratio);
_symbols.append(ri); _symbols.append(ri);
reader.skipCurrentElement(); reader.skipCurrentElement();
} }
void Style::rule(QXmlStreamReader &reader, const QString &dir, void Style::rule(QXmlStreamReader &reader, const QString &dir, qreal ratio,
const QSet<QString> &cats, const Rule &parent) const QSet<QString> &cats, const Rule &parent)
{ {
Rule r(parent); Rule r(parent);
@ -242,9 +245,9 @@ void Style::rule(QXmlStreamReader &reader, const QString &dir,
while (reader.readNextStartElement()) { while (reader.readNextStartElement()) {
if (reader.name() == QLatin1String("rule")) if (reader.name() == QLatin1String("rule"))
rule(reader, dir, cats, r); rule(reader, dir, ratio, cats, r);
else if (reader.name() == QLatin1String("area")) else if (reader.name() == QLatin1String("area"))
area(reader, dir, r); area(reader, dir, ratio, r);
else if (reader.name() == QLatin1String("line")) else if (reader.name() == QLatin1String("line"))
line(reader, r); line(reader, r);
else if (reader.name() == QLatin1String("pathText")) { else if (reader.name() == QLatin1String("pathText")) {
@ -260,7 +263,7 @@ void Style::rule(QXmlStreamReader &reader, const QString &dir,
text(reader, r, list); text(reader, r, list);
} }
else if (reader.name() == QLatin1String("symbol")) else if (reader.name() == QLatin1String("symbol"))
symbol(reader, dir, r); symbol(reader, dir, ratio, r);
else else
reader.skipCurrentElement(); reader.skipCurrentElement();
} }
@ -298,14 +301,15 @@ void Style::stylemenu(QXmlStreamReader &reader, QSet<QString> &cats)
} }
} }
void Style::rendertheme(QXmlStreamReader &reader, const QString &dir) void Style::rendertheme(QXmlStreamReader &reader, const QString &dir,
qreal ratio)
{ {
Rule r; Rule r;
QSet<QString> cats; QSet<QString> cats;
while (reader.readNextStartElement()) { while (reader.readNextStartElement()) {
if (reader.name() == QLatin1String("rule")) if (reader.name() == QLatin1String("rule"))
rule(reader, dir, cats, r); rule(reader, dir, ratio, cats, r);
else if (reader.name() == QLatin1String("stylemenu")) else if (reader.name() == QLatin1String("stylemenu"))
stylemenu(reader, cats); stylemenu(reader, cats);
else else
@ -313,7 +317,7 @@ void Style::rendertheme(QXmlStreamReader &reader, const QString &dir)
} }
} }
bool Style::loadXml(const QString &path) bool Style::loadXml(const QString &path, qreal ratio)
{ {
QFile file(path); QFile file(path);
if (!file.open(QFile::ReadOnly)) if (!file.open(QFile::ReadOnly))
@ -323,7 +327,7 @@ bool Style::loadXml(const QString &path)
if (reader.readNextStartElement()) { if (reader.readNextStartElement()) {
if (reader.name() == QLatin1String("rendertheme")) if (reader.name() == QLatin1String("rendertheme"))
rendertheme(reader, fi.absolutePath()); rendertheme(reader, fi.absolutePath(), ratio);
else else
reader.raiseError("Not a Mapsforge style file"); reader.raiseError("Not a Mapsforge style file");
} }
@ -335,10 +339,10 @@ bool Style::loadXml(const QString &path)
return !reader.error(); return !reader.error();
} }
Style::Style(const QString &path) Style::Style(const QString &path, qreal ratio)
{ {
if (!QFileInfo::exists(path) || !loadXml(path)) if (!QFileInfo::exists(path) || !loadXml(path, ratio))
loadXml(":/mapsforge/default.xml"); loadXml(":/mapsforge/default.xml", ratio);
} }
QVector<const Style::PathRender *> Style::paths(int zoom, bool closed, QVector<const Style::PathRender *> Style::paths(int zoom, bool closed,

View File

@ -218,7 +218,7 @@ public:
QImage _img; QImage _img;
}; };
Style(const QString &path); Style(const QString &path, qreal ratio);
QVector<const PathRender *> paths(int zoom, bool closed, QVector<const PathRender *> paths(int zoom, bool closed,
const QVector<MapData::Tag> &tags) const; const QVector<MapData::Tag> &tags) const;
@ -233,18 +233,20 @@ private:
QList<TextRender> _pathLabels, _pointLabels, _areaLabels; QList<TextRender> _pathLabels, _pointLabels, _areaLabels;
QList<Symbol> _symbols; QList<Symbol> _symbols;
bool loadXml(const QString &path); bool loadXml(const QString &path, qreal ratio);
void rendertheme(QXmlStreamReader &reader, const QString &dir); void rendertheme(QXmlStreamReader &reader, const QString &dir, qreal ratio);
void layer(QXmlStreamReader &reader, QSet<QString> &cats); void layer(QXmlStreamReader &reader, QSet<QString> &cats);
void stylemenu(QXmlStreamReader &reader, QSet<QString> &cats); void stylemenu(QXmlStreamReader &reader, QSet<QString> &cats);
void cat(QXmlStreamReader &reader, QSet<QString> &cats); void cat(QXmlStreamReader &reader, QSet<QString> &cats);
void rule(QXmlStreamReader &reader, const QString &dir, void rule(QXmlStreamReader &reader, const QString &dir, qreal ratio,
const QSet<QString> &cats, const Rule &parent); const QSet<QString> &cats, const Rule &parent);
void area(QXmlStreamReader &reader, const QString &dir, const Rule &rule); void area(QXmlStreamReader &reader, const QString &dir, qreal ratio,
const Rule &rule);
void line(QXmlStreamReader &reader, const Rule &rule); void line(QXmlStreamReader &reader, const Rule &rule);
void text(QXmlStreamReader &reader, const Rule &rule, void text(QXmlStreamReader &reader, const Rule &rule,
QList<QList<TextRender> *> &lists); QList<QList<TextRender> *> &lists);
void symbol(QXmlStreamReader &reader, const QString &dir, const Rule &rule); void symbol(QXmlStreamReader &reader, const QString &dir, qreal ratio,
const Rule &rule);
}; };
} }

View File

@ -148,10 +148,9 @@ void MapsforgeMap::draw(QPainter *painter, const QRectF &rect, Flags flags)
if (isRunning(key)) if (isRunning(key))
continue; continue;
if (QPixmapCache::find(key, &pm)) { if (QPixmapCache::find(key, &pm))
pm.setDevicePixelRatio(_tileRatio);
painter->drawPixmap(ttl, pm); painter->drawPixmap(ttl, pm);
} else { else {
QList<MapData::Path> paths; QList<MapData::Path> paths;
QList<MapData::Point> points; QList<MapData::Point> points;

View File

@ -41,10 +41,11 @@ void TextPointItem::setPos(const QPoint &point, bool padding)
QRect iconRect; QRect iconRect;
if (_img) { if (_img) {
int xOffset = padding ? _img->width() : _img->width() / 2; QSize s(_img->size() / _img->devicePixelRatioF());
int xOffset = padding ? s.width() : s.width() / 2;
iconRect = QRect(QPoint(point.x() - xOffset, point.y() iconRect = QRect(QPoint(point.x() - xOffset, point.y()
- _img->height()/2), _img->size()); - s.height()/2), s);
_textRect.moveTopLeft(QPoint(point.x() + _img->width()/2, point.y() _textRect.moveTopLeft(QPoint(point.x() + s.width()/2, point.y()
- _textRect.height()/2)); - _textRect.height()/2));
} else } else
_textRect.moveCenter(point); _textRect.moveCenter(point);
@ -56,9 +57,11 @@ void TextPointItem::setPos(const QPoint &point, bool padding)
void TextPointItem::paint(QPainter *painter) const void TextPointItem::paint(QPainter *painter) const
{ {
if (_img) if (_img) {
QSize s(_img->size() / _img->devicePixelRatioF());
painter->drawImage(QPoint(_rect.left(), _rect.center().y() painter->drawImage(QPoint(_rect.left(), _rect.center().y()
- _img->height()/2), *_img); - s.height()/2), *_img);
}
if (_text) { if (_text) {
if (_bgColor) { if (_bgColor) {