1
0
mirror of https://github.com/tumic0/GPXSee.git synced 2025-06-28 03:59:15 +02:00

Do not do any time consuming actions in the mapview redraw callback

On all vector maps (ENC, IMG and Mapsforge), do the data loading
asynchronous like the tile rendering.
This commit is contained in:
2023-05-19 19:33:22 +02:00
parent 4615709b99
commit a92d6efec6
17 changed files with 340 additions and 282 deletions

View File

@ -53,7 +53,9 @@ void MapData::polys(const RectC &rect, int bits, QList<Poly> *polygons,
max[0] = rect.right();
max[1] = rect.top();
_lock.lock();
_tileTree.Search(min, max, polyCb, &ctx);
_lock.unlock();
}
void MapData::points(const RectC &rect, int bits, QList<Point> *points)
@ -66,7 +68,9 @@ void MapData::points(const RectC &rect, int bits, QList<Point> *points)
max[0] = rect.right();
max[1] = rect.top();
_lock.lock();
_tileTree.Search(min, max, pointCb, &ctx);
_lock.unlock();
}
void MapData::load()

View File

@ -4,6 +4,7 @@
#include <QList>
#include <QPointF>
#include <QCache>
#include <QMutex>
#include <QDebug>
#include "common/rectc.h"
#include "common/rtree.h"
@ -97,11 +98,14 @@ private:
QList<Poly> lines;
};
typedef QCache<const SubDiv*, Polys> PolyCache;
typedef QCache<const SubDiv*, QList<Point> > PointCache;
struct PolyCTX
{
PolyCTX(const RectC &rect, const Zoom &zoom,
QList<MapData::Poly> *polygons, QList<MapData::Poly> *lines,
QCache<const SubDiv*, MapData::Polys> *polyCache)
PolyCache *polyCache)
: rect(rect), zoom(zoom), polygons(polygons), lines(lines),
polyCache(polyCache) {}
@ -109,20 +113,19 @@ private:
const Zoom &zoom;
QList<MapData::Poly> *polygons;
QList<MapData::Poly> *lines;
QCache<const SubDiv*, MapData::Polys> *polyCache;
PolyCache *polyCache;
};
struct PointCTX
{
PointCTX(const RectC &rect, const Zoom &zoom,
QList<MapData::Point> *points,
QCache<const SubDiv*, QList<MapData::Point> > *pointCache)
QList<MapData::Point> *points, PointCache *pointCache)
: rect(rect), zoom(zoom), points(points), pointCache(pointCache) {}
const RectC &rect;
const Zoom &zoom;
QList<MapData::Point> *points;
QCache<const SubDiv*, QList<MapData::Point> > *pointCache;
PointCache *pointCache;
};
const Zoom &zoom(int bits) const;
@ -130,8 +133,9 @@ private:
static bool polyCb(VectorTile *tile, void *context);
static bool pointCb(VectorTile *tile, void *context);
QCache<const SubDiv*, Polys> _polyCache;
QCache<const SubDiv*, QList<Point> > _pointCache;
PolyCache _polyCache;
PointCache _pointCache;
QMutex _lock;
friend class VectorTile;
};

View File

@ -5,12 +5,14 @@
#include "map/textpathitem.h"
#include "map/textpointitem.h"
#include "map/bitmapline.h"
#include "map/rectd.h"
#include "style.h"
#include "lblfile.h"
#include "rastertile.h"
using namespace IMG;
#define TEXT_EXTENT 160
#define ICON_PADDING 2
#define AREA(rect) \
@ -147,40 +149,6 @@ static bool rectNearPolygon(const QPolygonF &polygon, const QRectF &rect)
|| polygon.containsPoint(rect.bottomRight(), Qt::OddEvenFill)));
}
void RasterTile::render()
{
QList<TextItem*> textItems;
ll2xy(_polygons);
ll2xy(_lines);
ll2xy(_points);
processPoints(textItems);
processPolygons(textItems);
processLines(textItems);
_pixmap.setDevicePixelRatio(_ratio);
_pixmap.fill(Qt::transparent);
QPainter painter(&_pixmap);
painter.setRenderHint(QPainter::SmoothPixmapTransform);
painter.setRenderHint(QPainter::Antialiasing);
painter.translate(-_rect.x(), -_rect.y());
drawPolygons(&painter);
drawLines(&painter);
drawTextItems(&painter, textItems);
qDeleteAll(textItems);
_valid = true;
//painter.setPen(Qt::red);
//painter.setRenderHint(QPainter::Antialiasing, false);
//painter.drawRect(QRect(_xy, _pixmap.size()));
}
void RasterTile::ll2xy(QList<MapData::Poly> &polys)
{
for (int i = 0; i < polys.size(); i++) {
@ -200,14 +168,15 @@ void RasterTile::ll2xy(QList<MapData::Point> &points)
}
}
void RasterTile::drawPolygons(QPainter *painter)
void RasterTile::drawPolygons(QPainter *painter,
const QList<MapData::Poly> &polygons)
{
QCache<const LBLFile *, SubFile::Handle> hc(16);
for (int n = 0; n < _style->drawOrder().size(); n++) {
for (int i = 0; i < _polygons.size(); i++) {
const MapData::Poly &poly = _polygons.at(i);
if (poly.type != _style->drawOrder().at(n))
for (int n = 0; n < _data->style()->drawOrder().size(); n++) {
for (int i = 0; i < polygons.size(); i++) {
const MapData::Poly &poly = polygons.at(i);
if (poly.type != _data->style()->drawOrder().at(n))
continue;
if (poly.raster.isValid()) {
@ -237,7 +206,7 @@ void RasterTile::drawPolygons(QPainter *painter)
//painter->setBrush(Qt::NoBrush);
//painter->drawRect(QRectF(tl, br));
} else {
const Style::Polygon &style = _style->polygon(poly.type);
const Style::Polygon &style = _data->style()->polygon(poly.type);
painter->setPen(style.pen());
painter->setBrush(style.brush());
@ -247,13 +216,13 @@ void RasterTile::drawPolygons(QPainter *painter)
}
}
void RasterTile::drawLines(QPainter *painter)
void RasterTile::drawLines(QPainter *painter, const QList<MapData::Poly> &lines)
{
painter->setBrush(Qt::NoBrush);
for (int i = 0; i < _lines.size(); i++) {
const MapData::Poly &poly = _lines.at(i);
const Style::Line &style = _style->line(poly.type);
for (int i = 0; i < lines.size(); i++) {
const MapData::Poly &poly = lines.at(i);
const Style::Line &style = _data->style()->line(poly.type);
if (style.background() == Qt::NoPen)
continue;
@ -262,9 +231,9 @@ void RasterTile::drawLines(QPainter *painter)
painter->drawPolyline(poly.points);
}
for (int i = 0; i < _lines.size(); i++) {
const MapData::Poly &poly = _lines.at(i);
const Style::Line &style = _style->line(poly.type);
for (int i = 0; i < lines.size(); i++) {
const MapData::Poly &poly = lines.at(i);
const Style::Line &style = _data->style()->line(poly.type);
if (!style.img().isNull())
BitmapLine::draw(painter, poly.points, style.img());
@ -295,13 +264,14 @@ static void removeDuplicitLabel(QList<TextItem *> &labels, const QString &text,
}
}
void RasterTile::processPolygons(QList<TextItem*> &textItems)
void RasterTile::processPolygons(const QList<MapData::Poly> &polygons,
QList<TextItem*> &textItems)
{
QSet<QString> set;
QList<TextItem *> labels;
for (int i = 0; i < _polygons.size(); i++) {
const MapData::Poly &poly = _polygons.at(i);
for (int i = 0; i < polygons.size(); i++) {
const MapData::Poly &poly = polygons.at(i);
bool exists = set.contains(poly.label.text());
if (poly.label.text().isEmpty())
@ -310,7 +280,7 @@ void RasterTile::processPolygons(QList<TextItem*> &textItems)
if (_zoom <= 23 && (Style::isWaterArea(poly.type)
|| Style::isMilitaryArea(poly.type)
|| Style::isNatureReserve(poly.type))) {
const Style::Polygon &style = _style->polygon(poly.type);
const Style::Polygon &style = _data->style()->polygon(poly.type);
TextPointItem *item = new TextPointItem(
centroid(poly.points).toPoint(), &poly.label.text(), poiFont(),
0, &style.brush().color(), &haloColor);
@ -331,20 +301,22 @@ void RasterTile::processPolygons(QList<TextItem*> &textItems)
textItems.append(labels);
}
void RasterTile::processLines(QList<TextItem*> &textItems)
void RasterTile::processLines(QList<MapData::Poly> &lines,
QList<TextItem*> &textItems)
{
std::stable_sort(_lines.begin(), _lines.end());
std::stable_sort(lines.begin(), lines.end());
if (_zoom >= 22)
processStreetNames(textItems);
processShields(textItems);
processStreetNames(lines, textItems);
processShields(lines, textItems);
}
void RasterTile::processStreetNames(QList<TextItem*> &textItems)
void RasterTile::processStreetNames(const QList<MapData::Poly> &lines,
QList<TextItem*> &textItems)
{
for (int i = 0; i < _lines.size(); i++) {
const MapData::Poly &poly = _lines.at(i);
const Style::Line &style = _style->line(poly.type);
for (int i = 0; i < lines.size(); i++) {
const MapData::Poly &poly = lines.at(i);
const Style::Line &style = _data->style()->line(poly.type);
if (style.img().isNull() && style.foreground() == Qt::NoPen)
continue;
@ -366,7 +338,8 @@ void RasterTile::processStreetNames(QList<TextItem*> &textItems)
}
}
void RasterTile::processShields(QList<TextItem*> &textItems)
void RasterTile::processShields(const QList<MapData::Poly> &lines,
QList<TextItem*> &textItems)
{
for (int type = FIRST_SHIELD; type <= LAST_SHIELD; type++) {
if (minShieldZoom(static_cast<Shield::Type>(type)) > _zoom)
@ -375,8 +348,8 @@ void RasterTile::processShields(QList<TextItem*> &textItems)
QHash<Shield, QPolygonF> shields;
QHash<Shield, const Shield*> sp;
for (int i = 0; i < _lines.size(); i++) {
const MapData::Poly &poly = _lines.at(i);
for (int i = 0; i < lines.size(); i++) {
const MapData::Poly &poly = lines.at(i);
const Shield &shield = poly.label.shield();
if (!shield.isValid() || shield.type() != type
|| !Style::isMajorRoad(poly.type))
@ -429,13 +402,14 @@ void RasterTile::processShields(QList<TextItem*> &textItems)
}
}
void RasterTile::processPoints(QList<TextItem*> &textItems)
void RasterTile::processPoints(QList<MapData::Point> &points,
QList<TextItem*> &textItems)
{
std::sort(_points.begin(), _points.end());
std::sort(points.begin(), points.end());
for (int i = 0; i < _points.size(); i++) {
const MapData::Point &point = _points.at(i);
const Style::Point &style = _style->point(point.type);
for (int i = 0; i < points.size(); i++) {
const MapData::Point &point = points.at(i);
const Style::Point &style = _data->style()->point(point.type);
bool poi = Style::isPOI(point.type);
const QString *label = point.label.text().isEmpty()
@ -461,3 +435,60 @@ void RasterTile::processPoints(QList<TextItem*> &textItems)
delete item;
}
}
void RasterTile::fetchData(QList<MapData::Poly> &polygons,
QList<MapData::Poly> &lines, QList<MapData::Point> &points)
{
QPoint ttl(_rect.topLeft());
QRectF polyRect(ttl, QPointF(ttl.x() + _rect.width(), ttl.y()
+ _rect.height()));
RectD polyRectD(_transform.img2proj(polyRect.topLeft()),
_transform.img2proj(polyRect.bottomRight()));
_data->polys(polyRectD.toRectC(_proj, 20), _zoom,
&polygons, &lines);
QRectF pointRect(QPointF(ttl.x() - TEXT_EXTENT, ttl.y() - TEXT_EXTENT),
QPointF(ttl.x() + _rect.width() + TEXT_EXTENT, ttl.y() + _rect.height()
+ TEXT_EXTENT));
RectD pointRectD(_transform.img2proj(pointRect.topLeft()),
_transform.img2proj(pointRect.bottomRight()));
_data->points(pointRectD.toRectC(_proj, 20), _zoom, &points);
}
void RasterTile::render()
{
QList<MapData::Poly> polygons;
QList<MapData::Poly> lines;
QList<MapData::Point> points;
QList<TextItem*> textItems;
fetchData(polygons, lines, points);
ll2xy(polygons);
ll2xy(lines);
ll2xy(points);
processPoints(points, textItems);
processPolygons(polygons, textItems);
processLines(lines, textItems);
_pixmap.setDevicePixelRatio(_ratio);
_pixmap.fill(Qt::transparent);
QPainter painter(&_pixmap);
painter.setRenderHint(QPainter::SmoothPixmapTransform);
painter.setRenderHint(QPainter::Antialiasing);
painter.translate(-_rect.x(), -_rect.y());
drawPolygons(&painter, polygons);
drawLines(&painter, lines);
drawTextItems(&painter, textItems);
qDeleteAll(textItems);
_valid = true;
//painter.setPen(Qt::red);
//painter.setRenderHint(QPainter::Antialiasing, false);
//painter.drawRect(QRect(_xy, _pixmap.size()));
}

View File

@ -17,14 +17,11 @@ class Style;
class RasterTile
{
public:
RasterTile(const Projection &proj, const Transform &transform,
const Style *style, int zoom, const QRect &rect, qreal ratio,
const QString &key, const QList<MapData::Poly> &polygons,
const QList<MapData::Poly> &lines, QList<MapData::Point> &points)
: _proj(proj), _transform(transform), _style(style), _zoom(zoom),
_rect(rect), _ratio(ratio), _key(key),
_pixmap(rect.width() * ratio, rect.height() * ratio), _polygons(polygons),
_lines(lines), _points(points), _valid(false) {}
RasterTile(const Projection &proj, const Transform &transform, MapData *data,
int zoom, const QRect &rect, qreal ratio, const QString &key)
: _proj(proj), _transform(transform), _data(data), _zoom(zoom),
_rect(rect), _ratio(ratio), _key(key),
_pixmap(rect.width() * ratio, rect.height() * ratio), _valid(false) {}
const QString &key() const {return _key;}
QPoint xy() const {return _rect.topLeft();}
@ -34,32 +31,36 @@ public:
void render();
private:
void fetchData(QList<MapData::Poly> &polygons, QList<MapData::Poly> &lines,
QList<MapData::Point> &points);
QPointF ll2xy(const Coordinates &c) const
{return _transform.proj2img(_proj.ll2xy(c));}
void ll2xy(QList<MapData::Poly> &polys);
void ll2xy(QList<MapData::Point> &points);
void drawPolygons(QPainter *painter);
void drawLines(QPainter *painter);
void drawPolygons(QPainter *painter, const QList<MapData::Poly> &polygons);
void drawLines(QPainter *painter, const QList<MapData::Poly> &lines);
void drawTextItems(QPainter *painter, const QList<TextItem*> &textItems);
void processPolygons(QList<TextItem *> &textItems);
void processLines(QList<TextItem*> &textItems);
void processPoints(QList<TextItem*> &textItems);
void processShields(QList<TextItem*> &textItems);
void processStreetNames(QList<TextItem*> &textItems);
void processPolygons(const QList<MapData::Poly> &polygons,
QList<TextItem *> &textItems);
void processLines(QList<MapData::Poly> &lines,
QList<TextItem*> &textItems);
void processPoints(QList<MapData::Point> &points,
QList<TextItem*> &textItems);
void processShields(const QList<MapData::Poly> &lines,
QList<TextItem*> &textItems);
void processStreetNames(const QList<MapData::Poly> &lines,
QList<TextItem*> &textItems);
Projection _proj;
Transform _transform;
const Style *_style;
MapData *_data;
int _zoom;
QRect _rect;
qreal _ratio;
QString _key;
QPixmap _pixmap;
QList<MapData::Poly> _polygons;
QList<MapData::Poly> _lines;
QList<MapData::Point> _points;
bool _valid;
};

View File

@ -102,13 +102,16 @@ void VectorTile::clear()
void VectorTile::polys(const RectC &rect, const Zoom &zoom,
QList<MapData::Poly> *polygons, QList<MapData::Poly> *lines,
QCache<const SubDiv *, MapData::Polys> *polyCache)
MapData::PolyCache *polyCache)
{
SubFile::Handle *rgnHdl = 0, *lblHdl = 0, *netHdl = 0, *nodHdl = 0,
*nodHdl2 = 0;
if (_loaded < 0)
return;
//polyCache->lock.lock();
if (!_loaded) {
rgnHdl = new SubFile::Handle(_rgn);
lblHdl = new SubFile::Handle(_lbl);
@ -116,6 +119,7 @@ void VectorTile::polys(const RectC &rect, const Zoom &zoom,
nodHdl = new SubFile::Handle(_nod);
if (!load(*rgnHdl, *lblHdl, *netHdl, *nodHdl)) {
//polyCache->lock.unlock();
delete rgnHdl; delete lblHdl; delete netHdl; delete nodHdl;
return;
}
@ -166,6 +170,8 @@ void VectorTile::polys(const RectC &rect, const Zoom &zoom,
}
}
//polyCache->lock.unlock();
delete rgnHdl; delete lblHdl; delete netHdl; delete nodHdl; delete nodHdl2;
}

View File

@ -29,7 +29,7 @@ public:
void polys(const RectC &rect, const Zoom &zoom,
QList<MapData::Poly> *polygons, QList<MapData::Poly> *lines,
QCache<const SubDiv *, MapData::Polys> *polyCache);
MapData::PolyCache *polyCache);
void points(const RectC &rect, const Zoom &zoom,
QList<MapData::Point> *points, QCache<const SubDiv*,
QList<MapData::Point> > *pointCache);