2019-05-10 18:56:19 +02:00
|
|
|
#include <QFile>
|
|
|
|
#include <QPainter>
|
2020-01-19 13:23:20 +01:00
|
|
|
#include <QFont>
|
2019-05-10 18:56:19 +02:00
|
|
|
#include <QPixmapCache>
|
|
|
|
#if QT_VERSION < QT_VERSION_CHECK(5, 0, 0)
|
|
|
|
#include <QtCore>
|
|
|
|
#else // QT_VERSION < 5
|
|
|
|
#include <QtConcurrent>
|
|
|
|
#endif // QT_VERSION < 5
|
|
|
|
#include "common/rectc.h"
|
|
|
|
#include "common/wgs84.h"
|
2020-01-19 17:05:26 +01:00
|
|
|
#include "common/range.h"
|
2019-05-10 18:56:19 +02:00
|
|
|
#include "IMG/textpathitem.h"
|
|
|
|
#include "IMG/textpointitem.h"
|
|
|
|
#include "IMG/bitmapline.h"
|
2020-02-09 23:24:48 +01:00
|
|
|
#include "IMG/style.h"
|
|
|
|
#include "IMG/img.h"
|
|
|
|
#include "IMG/gmap.h"
|
2019-05-10 18:56:19 +02:00
|
|
|
#include "pcs.h"
|
|
|
|
#include "rectd.h"
|
|
|
|
#include "imgmap.h"
|
|
|
|
|
|
|
|
|
2019-10-05 12:41:42 +02:00
|
|
|
#define TILE_SIZE 384
|
|
|
|
#define TEXT_EXTENT 160
|
2019-05-10 18:56:19 +02:00
|
|
|
|
2019-07-13 08:36:04 +02:00
|
|
|
#define AREA(rect) \
|
|
|
|
(rect.size().width() * rect.size().height())
|
|
|
|
|
2019-05-10 18:56:19 +02:00
|
|
|
class RasterTile
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
RasterTile() : _map(0) {}
|
|
|
|
RasterTile(IMGMap *map, const QPoint &xy, const QString &key)
|
|
|
|
: _map(map), _xy(xy), _key(key),
|
|
|
|
_img(TILE_SIZE, TILE_SIZE, QImage::Format_ARGB32_Premultiplied) {}
|
|
|
|
|
|
|
|
const QString &key() const {return _key;}
|
|
|
|
const QPoint &xy() const {return _xy;}
|
|
|
|
QImage &img() {return _img;}
|
2020-02-09 23:24:48 +01:00
|
|
|
QList<MapData::Poly> &polygons() {return _polygons;}
|
|
|
|
QList<MapData::Poly> &lines() {return _lines;}
|
|
|
|
QList<MapData::Point> &points() {return _points;}
|
2019-05-10 18:56:19 +02:00
|
|
|
|
2019-07-13 08:36:04 +02:00
|
|
|
void render()
|
2019-05-10 18:56:19 +02:00
|
|
|
{
|
2019-06-07 20:33:08 +02:00
|
|
|
QList<TextItem*> textItems;
|
|
|
|
|
2020-01-22 21:01:05 +01:00
|
|
|
QRect tileRect(_xy, QSize(TILE_SIZE, TILE_SIZE));
|
2020-01-24 19:44:19 +01:00
|
|
|
|
2019-06-07 20:33:08 +02:00
|
|
|
_map->processPoints(_points, textItems);
|
2020-01-24 19:44:19 +01:00
|
|
|
_map->processPolygons(_polygons, textItems);
|
2020-01-22 21:01:05 +01:00
|
|
|
_map->processLines(_lines, tileRect, textItems);
|
2019-06-07 20:33:08 +02:00
|
|
|
|
2019-05-10 18:56:19 +02:00
|
|
|
_img.fill(Qt::transparent);
|
|
|
|
|
|
|
|
QPainter painter(&_img);
|
|
|
|
painter.setRenderHint(QPainter::SmoothPixmapTransform);
|
|
|
|
painter.setRenderHint(QPainter::Antialiasing);
|
|
|
|
painter.translate(-_xy.x(), -_xy.y());
|
|
|
|
|
|
|
|
_map->drawPolygons(&painter, _polygons);
|
2019-06-07 20:33:08 +02:00
|
|
|
_map->drawLines(&painter, _lines);
|
|
|
|
_map->drawTextItems(&painter, textItems);
|
2019-07-13 08:36:04 +02:00
|
|
|
//painter.setPen(Qt::red);
|
|
|
|
//painter.drawRect(QRect(_xy, QSize(TILE_SIZE, TILE_SIZE)));
|
2019-06-07 20:33:08 +02:00
|
|
|
|
|
|
|
qDeleteAll(textItems);
|
2019-05-10 18:56:19 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
|
|
|
IMGMap *_map;
|
|
|
|
QPoint _xy;
|
|
|
|
QString _key;
|
|
|
|
QImage _img;
|
2020-02-09 23:24:48 +01:00
|
|
|
QList<MapData::Poly> _polygons;
|
|
|
|
QList<MapData::Poly> _lines;
|
|
|
|
QList<MapData::Point> _points;
|
2019-05-10 18:56:19 +02:00
|
|
|
};
|
|
|
|
|
2019-06-30 20:39:22 +02:00
|
|
|
|
2019-08-01 08:36:58 +02:00
|
|
|
static const Range zooms(12, 28);
|
2019-07-10 21:49:46 +02:00
|
|
|
|
2019-08-01 08:36:58 +02:00
|
|
|
static const QColor shieldColor(Qt::white);
|
|
|
|
static const QColor shieldBgColor1("#dd3e3e");
|
|
|
|
static const QColor shieldBgColor2("#379947");
|
|
|
|
static const QColor shieldBgColor3("#4a7fc1");
|
2019-06-30 20:39:22 +02:00
|
|
|
|
|
|
|
static QString convertUnits(const QString &str)
|
2019-05-10 18:56:19 +02:00
|
|
|
{
|
|
|
|
bool ok;
|
|
|
|
int number = str.toInt(&ok);
|
2019-06-30 20:39:22 +02:00
|
|
|
return ok ? QString::number(qRound(number * 0.3048)) : str;
|
2019-05-10 18:56:19 +02:00
|
|
|
}
|
|
|
|
|
2019-06-07 09:37:10 +02:00
|
|
|
static int minPOIZoom(Style::POIClass cl)
|
|
|
|
{
|
|
|
|
switch (cl) {
|
|
|
|
case Style::Food:
|
|
|
|
case Style::Shopping:
|
|
|
|
case Style::Services:
|
|
|
|
return 27;
|
|
|
|
case Style::Accommodation:
|
|
|
|
case Style::Recreation:
|
|
|
|
return 25;
|
|
|
|
case Style::ManmadePlaces:
|
|
|
|
case Style::NaturePlaces:
|
|
|
|
case Style::Transport:
|
|
|
|
case Style::Community:
|
|
|
|
case Style::Elementary:
|
|
|
|
return 23;
|
|
|
|
default:
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
}
|
2019-05-10 18:56:19 +02:00
|
|
|
|
2019-07-08 21:19:25 +02:00
|
|
|
static QFont pixelSizeFont(int pixelSize)
|
2019-06-29 10:27:12 +02:00
|
|
|
{
|
|
|
|
QFont f;
|
|
|
|
f.setPixelSize(pixelSize);
|
|
|
|
return f;
|
|
|
|
}
|
|
|
|
|
2019-07-08 21:19:25 +02:00
|
|
|
static QFont *font(Style::FontSize size, Style::FontSize defaultSize
|
|
|
|
= Style::Normal)
|
|
|
|
{
|
|
|
|
/* The fonts must be initialized on first usage (after the QGuiApplication
|
|
|
|
instance is created) */
|
|
|
|
static QFont large = pixelSizeFont(16);
|
|
|
|
static QFont normal = pixelSizeFont(14);
|
|
|
|
static QFont small = pixelSizeFont(12);
|
|
|
|
|
|
|
|
switch (size) {
|
|
|
|
case Style::None:
|
|
|
|
return 0;
|
|
|
|
case Style::Large:
|
|
|
|
return &large;
|
|
|
|
case Style::Normal:
|
|
|
|
return &normal;
|
|
|
|
case Style::Small:
|
|
|
|
return &small;
|
|
|
|
default:
|
|
|
|
return font(defaultSize);
|
|
|
|
}
|
2019-06-28 07:43:16 +02:00
|
|
|
}
|
|
|
|
|
2019-07-08 21:19:25 +02:00
|
|
|
static QFont *poiFont(Style::FontSize size = Style::Normal)
|
|
|
|
{
|
|
|
|
static QFont poi = pixelSizeFont(10);
|
|
|
|
|
|
|
|
switch (size) {
|
|
|
|
case Style::None:
|
|
|
|
return 0;
|
|
|
|
default:
|
|
|
|
return &poi;
|
|
|
|
}
|
|
|
|
}
|
2019-06-28 07:43:16 +02:00
|
|
|
|
2019-06-30 20:39:22 +02:00
|
|
|
static const QColor *shieldBgColor(Label::Shield::Type type)
|
|
|
|
{
|
|
|
|
switch (type) {
|
|
|
|
case Label::Shield::USInterstate:
|
|
|
|
case Label::Shield::Hbox:
|
|
|
|
return &shieldBgColor1;
|
|
|
|
case Label::Shield::USShield:
|
|
|
|
case Label::Shield::Box:
|
|
|
|
return &shieldBgColor2;
|
|
|
|
case Label::Shield::USRound:
|
|
|
|
case Label::Shield::Oval:
|
|
|
|
return &shieldBgColor3;
|
|
|
|
default:
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static int minShieldZoom(Label::Shield::Type type)
|
|
|
|
{
|
|
|
|
switch (type) {
|
|
|
|
case Label::Shield::USInterstate:
|
|
|
|
case Label::Shield::Hbox:
|
2020-01-24 19:44:19 +01:00
|
|
|
return 17;
|
2019-06-30 20:39:22 +02:00
|
|
|
case Label::Shield::USShield:
|
|
|
|
case Label::Shield::Box:
|
|
|
|
return 19;
|
|
|
|
case Label::Shield::USRound:
|
|
|
|
case Label::Shield::Oval:
|
|
|
|
return 20;
|
|
|
|
default:
|
2019-07-13 14:42:44 +02:00
|
|
|
return 0;
|
2019-06-30 20:39:22 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-01-22 21:01:05 +01:00
|
|
|
static qreal area(const QVector<QPointF> &polygon)
|
|
|
|
{
|
|
|
|
qreal area = 0;
|
|
|
|
|
|
|
|
for (int i = 0; i < polygon.size(); i++) {
|
|
|
|
int j = (i + 1) % polygon.size();
|
|
|
|
area += polygon.at(i).x() * polygon.at(j).y();
|
|
|
|
area -= polygon.at(i).y() * polygon.at(j).x();
|
|
|
|
}
|
|
|
|
area /= 2.0;
|
|
|
|
|
|
|
|
return area;
|
|
|
|
}
|
|
|
|
|
2020-02-04 23:03:50 +01:00
|
|
|
static QPointF centroid(const QVector<QPointF> &polygon)
|
2020-01-22 21:01:05 +01:00
|
|
|
{
|
2020-02-04 23:01:47 +01:00
|
|
|
qreal cx = 0, cy = 0;
|
|
|
|
qreal factor = 1.0 / (6.0 * area(polygon));
|
2020-01-22 21:01:05 +01:00
|
|
|
|
|
|
|
for (int i = 0; i < polygon.size(); i++) {
|
|
|
|
int j = (i + 1) % polygon.size();
|
2020-02-04 23:01:47 +01:00
|
|
|
qreal f = (polygon.at(i).x() * polygon.at(j).y() - polygon.at(j).x()
|
|
|
|
* polygon.at(i).y());
|
|
|
|
cx += (polygon.at(i).x() + polygon.at(j).x()) * f;
|
|
|
|
cy += (polygon.at(i).y() + polygon.at(j).y()) * f;
|
2020-01-22 21:01:05 +01:00
|
|
|
}
|
|
|
|
|
2020-02-04 23:01:47 +01:00
|
|
|
return QPointF(cx * factor, cy * factor);
|
2020-01-22 21:01:05 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
static bool rectNearPolygon(const QPolygonF &polygon, const QRectF &rect)
|
|
|
|
{
|
|
|
|
return (polygon.boundingRect().contains(rect)
|
|
|
|
&& (polygon.containsPoint(rect.topLeft(), Qt::OddEvenFill)
|
|
|
|
|| polygon.containsPoint(rect.topRight(), Qt::OddEvenFill)
|
|
|
|
|| polygon.containsPoint(rect.bottomLeft(), Qt::OddEvenFill)
|
|
|
|
|| polygon.containsPoint(rect.bottomRight(), Qt::OddEvenFill)));
|
|
|
|
}
|
2019-06-28 07:43:16 +02:00
|
|
|
|
2020-02-09 23:24:48 +01:00
|
|
|
|
2019-05-10 18:56:19 +02:00
|
|
|
IMGMap::IMGMap(const QString &fileName, QObject *parent)
|
2020-02-09 23:24:48 +01:00
|
|
|
: Map(parent), _projection(PCS::pcs(3857)), _valid(false)
|
2019-05-10 18:56:19 +02:00
|
|
|
{
|
2020-02-09 23:24:48 +01:00
|
|
|
if (GMAP::isGMAP(fileName))
|
|
|
|
_data = new GMAP(fileName);
|
|
|
|
else
|
|
|
|
_data = new IMG(fileName);
|
|
|
|
|
|
|
|
if (!_data->isValid()) {
|
|
|
|
_errorString = _data->errorString();
|
2019-05-10 18:56:19 +02:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2019-07-10 21:49:46 +02:00
|
|
|
_zoom = zooms.min();
|
2019-05-10 18:56:19 +02:00
|
|
|
updateTransform();
|
|
|
|
|
|
|
|
_valid = true;
|
|
|
|
}
|
|
|
|
|
2019-07-04 18:18:03 +02:00
|
|
|
void IMGMap::load()
|
|
|
|
{
|
2020-02-09 23:24:48 +01:00
|
|
|
_data->load();
|
2019-07-04 18:18:03 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
void IMGMap::unload()
|
|
|
|
{
|
2020-02-09 23:24:48 +01:00
|
|
|
_data->clear();
|
2019-07-04 18:18:03 +02:00
|
|
|
}
|
|
|
|
|
2019-05-10 18:56:19 +02:00
|
|
|
QRectF IMGMap::bounds()
|
|
|
|
{
|
2020-02-09 23:24:48 +01:00
|
|
|
RectD prect(_data->bounds(), _projection);
|
2019-05-10 18:56:19 +02:00
|
|
|
return QRectF(_transform.proj2img(prect.topLeft()),
|
|
|
|
_transform.proj2img(prect.bottomRight()));
|
|
|
|
}
|
|
|
|
|
|
|
|
int IMGMap::zoomFit(const QSize &size, const RectC &rect)
|
|
|
|
{
|
|
|
|
if (rect.isValid()) {
|
2019-05-29 18:27:11 +02:00
|
|
|
RectD pr(rect, _projection, 10);
|
2019-05-10 18:56:19 +02:00
|
|
|
|
2019-07-10 21:49:46 +02:00
|
|
|
_zoom = zooms.min();
|
|
|
|
for (int i = zooms.min() + 1; i <= zooms.max(); i++) {
|
2019-05-29 18:27:11 +02:00
|
|
|
Transform t(transform(i));
|
|
|
|
QRectF r(t.proj2img(pr.topLeft()), t.proj2img(pr.bottomRight()));
|
|
|
|
if (size.width() < r.width() || size.height() < r.height())
|
2019-05-10 18:56:19 +02:00
|
|
|
break;
|
|
|
|
_zoom = i;
|
|
|
|
}
|
|
|
|
} else
|
2019-07-10 21:49:46 +02:00
|
|
|
_zoom = zooms.max();
|
2019-05-10 18:56:19 +02:00
|
|
|
|
|
|
|
updateTransform();
|
|
|
|
|
|
|
|
return _zoom;
|
|
|
|
}
|
|
|
|
|
|
|
|
int IMGMap::zoomIn()
|
|
|
|
{
|
2019-07-10 21:49:46 +02:00
|
|
|
_zoom = qMin(_zoom + 1, zooms.max());
|
2019-05-10 18:56:19 +02:00
|
|
|
updateTransform();
|
|
|
|
return _zoom;
|
|
|
|
}
|
|
|
|
|
|
|
|
int IMGMap::zoomOut()
|
|
|
|
{
|
2019-07-10 21:49:46 +02:00
|
|
|
_zoom = qMax(_zoom - 1, zooms.min());
|
2019-05-10 18:56:19 +02:00
|
|
|
updateTransform();
|
|
|
|
return _zoom;
|
|
|
|
}
|
|
|
|
|
2019-05-21 17:44:47 +02:00
|
|
|
void IMGMap::setZoom(int zoom)
|
|
|
|
{
|
|
|
|
_zoom = zoom;
|
|
|
|
updateTransform();
|
|
|
|
}
|
|
|
|
|
2019-05-29 18:27:11 +02:00
|
|
|
Transform IMGMap::transform(int zoom) const
|
2019-05-10 18:56:19 +02:00
|
|
|
{
|
2019-08-01 08:36:58 +02:00
|
|
|
double scale = _projection.isGeographic()
|
|
|
|
? 360.0 / (1<<zoom) : (2.0 * M_PI * WGS84_RADIUS) / (1<<zoom);
|
2020-02-09 23:24:48 +01:00
|
|
|
PointD topLeft(_projection.ll2xy(_data->bounds().topLeft()));
|
2019-05-29 18:27:11 +02:00
|
|
|
return Transform(ReferencePoint(PointD(0, 0), topLeft),
|
2019-05-10 18:56:19 +02:00
|
|
|
PointD(scale, scale));
|
|
|
|
}
|
|
|
|
|
2019-05-29 18:27:11 +02:00
|
|
|
void IMGMap::updateTransform()
|
|
|
|
{
|
|
|
|
_transform = transform(_zoom);
|
|
|
|
}
|
|
|
|
|
2019-05-10 18:56:19 +02:00
|
|
|
QPointF IMGMap::ll2xy(const Coordinates &c)
|
|
|
|
{
|
|
|
|
return _transform.proj2img(_projection.ll2xy(c));
|
|
|
|
}
|
|
|
|
|
|
|
|
Coordinates IMGMap::xy2ll(const QPointF &p)
|
|
|
|
{
|
|
|
|
return _projection.xy2ll(_transform.img2proj(p));
|
|
|
|
}
|
|
|
|
|
2020-02-09 23:24:48 +01:00
|
|
|
void IMGMap::drawPolygons(QPainter *painter, const QList<MapData::Poly> &polygons)
|
2019-05-10 18:56:19 +02:00
|
|
|
{
|
2020-02-09 23:24:48 +01:00
|
|
|
for (int n = 0; n < _data->style()->drawOrder().size(); n++) {
|
2019-05-10 18:56:19 +02:00
|
|
|
for (int i = 0; i < polygons.size(); i++) {
|
2020-02-09 23:24:48 +01:00
|
|
|
const MapData::Poly &poly = polygons.at(i);
|
|
|
|
if (poly.type != _data->style()->drawOrder().at(n))
|
2019-05-10 18:56:19 +02:00
|
|
|
continue;
|
2020-02-09 23:24:48 +01:00
|
|
|
const Style::Polygon &style = _data->style()->polygon(poly.type);
|
2019-05-10 18:56:19 +02:00
|
|
|
|
|
|
|
painter->setPen(style.pen());
|
|
|
|
painter->setBrush(style.brush());
|
|
|
|
painter->drawPolygon(poly.points);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-02-09 23:24:48 +01:00
|
|
|
void IMGMap::drawLines(QPainter *painter, const QList<MapData::Poly> &lines)
|
2019-05-10 18:56:19 +02:00
|
|
|
{
|
|
|
|
painter->setBrush(Qt::NoBrush);
|
|
|
|
|
|
|
|
for (int i = 0; i < lines.size(); i++) {
|
2020-02-09 23:24:48 +01:00
|
|
|
const MapData::Poly &poly = lines.at(i);
|
|
|
|
const Style::Line &style = _data->style()->line(poly.type);
|
2019-05-10 18:56:19 +02:00
|
|
|
|
|
|
|
if (style.background() == Qt::NoPen)
|
|
|
|
continue;
|
|
|
|
|
|
|
|
painter->setPen(style.background());
|
|
|
|
painter->drawPolyline(poly.points);
|
|
|
|
}
|
|
|
|
|
|
|
|
for (int i = 0; i < lines.size(); i++) {
|
2020-02-09 23:24:48 +01:00
|
|
|
const MapData::Poly &poly = lines.at(i);
|
|
|
|
const Style::Line &style = _data->style()->line(poly.type);
|
2019-05-10 18:56:19 +02:00
|
|
|
|
|
|
|
if (!style.img().isNull())
|
|
|
|
BitmapLine::draw(painter, poly.points, style.img());
|
|
|
|
else if (style.foreground() != Qt::NoPen) {
|
|
|
|
painter->setPen(style.foreground());
|
|
|
|
painter->drawPolyline(poly.points);
|
|
|
|
}
|
|
|
|
}
|
2019-06-07 20:33:08 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
void IMGMap::drawTextItems(QPainter *painter, const QList<TextItem*> &textItems)
|
|
|
|
{
|
|
|
|
for (int i = 0; i < textItems.size(); i++)
|
|
|
|
textItems.at(i)->paint(painter);
|
|
|
|
}
|
|
|
|
|
2020-02-09 23:24:48 +01:00
|
|
|
void IMGMap::processPolygons(QList<MapData::Poly> &polygons,
|
2020-01-22 21:01:05 +01:00
|
|
|
QList<TextItem*> &textItems)
|
2019-06-07 20:33:08 +02:00
|
|
|
{
|
|
|
|
for (int i = 0; i < polygons.size(); i++) {
|
2020-02-09 23:24:48 +01:00
|
|
|
MapData::Poly &poly = polygons[i];
|
2019-06-07 20:33:08 +02:00
|
|
|
for (int j = 0; j < poly.points.size(); j++) {
|
|
|
|
QPointF &p = poly.points[j];
|
|
|
|
p = ll2xy(Coordinates(p.x(), p.y()));
|
|
|
|
}
|
2020-01-22 21:01:05 +01:00
|
|
|
|
|
|
|
if (poly.label.text().isEmpty())
|
|
|
|
continue;
|
|
|
|
|
2020-01-24 19:44:19 +01:00
|
|
|
if (_zoom <= 23 && (Style::isWaterArea(poly.type)
|
|
|
|
|| Style::isMilitaryArea(poly.type)
|
|
|
|
|| Style::isNatureReserve(poly.type))) {
|
2020-02-09 23:24:48 +01:00
|
|
|
const Style::Polygon &style = _data->style()->polygon(poly.type);
|
2020-01-22 21:01:05 +01:00
|
|
|
TextPointItem *item = new TextPointItem(
|
|
|
|
centroid(poly.points).toPoint(), &poly.label.text(),
|
2020-01-24 19:44:19 +01:00
|
|
|
poiFont(), 0, &style.brush().color());
|
2020-01-22 21:01:05 +01:00
|
|
|
if (item->isValid() && !item->collides(textItems)
|
|
|
|
&& rectNearPolygon(poly.points, item->boundingRect()))
|
|
|
|
textItems.append(item);
|
|
|
|
else
|
|
|
|
delete item;
|
|
|
|
}
|
2019-06-07 20:33:08 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-02-09 23:24:48 +01:00
|
|
|
void IMGMap::processLines(QList<MapData::Poly> &lines, const QRect &tileRect,
|
2019-06-07 20:33:08 +02:00
|
|
|
QList<TextItem*> &textItems)
|
|
|
|
{
|
|
|
|
qStableSort(lines);
|
|
|
|
|
|
|
|
for (int i = 0; i < lines.size(); i++) {
|
2020-02-09 23:24:48 +01:00
|
|
|
MapData::Poly &poly = lines[i];
|
2019-06-07 20:33:08 +02:00
|
|
|
for (int j = 0; j < poly.points.size(); j++) {
|
|
|
|
QPointF &p = poly.points[j];
|
|
|
|
p = ll2xy(Coordinates(p.x(), p.y()));
|
|
|
|
}
|
|
|
|
}
|
2019-05-10 18:56:19 +02:00
|
|
|
|
2019-07-13 14:42:44 +02:00
|
|
|
if (_zoom >= 22)
|
|
|
|
processStreetNames(lines, tileRect, textItems);
|
2019-07-13 08:36:04 +02:00
|
|
|
processShields(lines, tileRect, textItems);
|
|
|
|
}
|
|
|
|
|
2020-02-09 23:24:48 +01:00
|
|
|
void IMGMap::processStreetNames(QList<MapData::Poly> &lines,
|
|
|
|
const QRect &tileRect, QList<TextItem*> &textItems)
|
2019-07-13 08:36:04 +02:00
|
|
|
{
|
2019-07-13 14:42:44 +02:00
|
|
|
for (int i = 0; i < lines.size(); i++) {
|
2020-02-09 23:24:48 +01:00
|
|
|
MapData::Poly &poly = lines[i];
|
|
|
|
const Style::Line &style = _data->style()->line(poly.type);
|
2019-05-10 18:56:19 +02:00
|
|
|
|
2019-07-13 14:42:44 +02:00
|
|
|
if (style.img().isNull() && style.foreground() == Qt::NoPen)
|
|
|
|
continue;
|
|
|
|
if (poly.label.text().isEmpty()
|
|
|
|
|| style.textFontSize() == Style::None)
|
|
|
|
continue;
|
2019-05-10 18:56:19 +02:00
|
|
|
|
2019-07-13 14:42:44 +02:00
|
|
|
if (Style::isContourLine(poly.type))
|
|
|
|
poly.label.setText(convertUnits(poly.label.text()));
|
2019-05-10 18:56:19 +02:00
|
|
|
|
2019-07-13 14:42:44 +02:00
|
|
|
const QFont *fnt = font(style.textFontSize(), Style::Small);
|
|
|
|
const QColor *color = style.textColor().isValid()
|
|
|
|
? &style.textColor() : 0;
|
2019-05-10 18:56:19 +02:00
|
|
|
|
2019-07-13 14:42:44 +02:00
|
|
|
TextPathItem *item = new TextPathItem(poly.points,
|
|
|
|
&poly.label.text(), tileRect, fnt, color);
|
|
|
|
if (item->isValid() && !item->collides(textItems))
|
|
|
|
textItems.append(item);
|
|
|
|
else
|
|
|
|
delete item;
|
2019-06-30 20:39:22 +02:00
|
|
|
}
|
2019-07-13 08:36:04 +02:00
|
|
|
}
|
2019-06-30 20:39:22 +02:00
|
|
|
|
2020-02-09 23:24:48 +01:00
|
|
|
void IMGMap::processShields(QList<MapData::Poly> &lines, const QRect &tileRect,
|
2019-07-13 08:36:04 +02:00
|
|
|
QList<TextItem*> &textItems)
|
|
|
|
{
|
2019-07-13 14:42:44 +02:00
|
|
|
for (int type = FIRST_SHIELD; type <= LAST_SHIELD; type++) {
|
2019-07-10 21:37:03 +02:00
|
|
|
if (minShieldZoom(static_cast<Label::Shield::Type>(type)) > _zoom)
|
2019-06-30 20:39:22 +02:00
|
|
|
continue;
|
2019-05-10 18:56:19 +02:00
|
|
|
|
2019-07-13 08:36:04 +02:00
|
|
|
QHash<Label::Shield, QPolygonF> shields;
|
|
|
|
QHash<Label::Shield, const Label::Shield*> sp;
|
2019-06-30 20:39:22 +02:00
|
|
|
|
|
|
|
for (int i = 0; i < lines.size(); i++) {
|
2020-02-09 23:24:48 +01:00
|
|
|
const MapData::Poly &poly = lines.at(i);
|
2019-06-30 20:39:22 +02:00
|
|
|
const Label::Shield &shield = poly.label.shield();
|
2019-07-13 08:36:04 +02:00
|
|
|
if (!shield.isValid() || shield.type() != type
|
|
|
|
|| !Style::isMajorRoad(poly.type))
|
2019-06-30 20:39:22 +02:00
|
|
|
continue;
|
|
|
|
|
2019-07-13 08:36:04 +02:00
|
|
|
QPolygonF &p = shields[shield];
|
|
|
|
for (int j = 0; j < poly.points.size(); j++)
|
|
|
|
p.append(poly.points.at(j));
|
2019-06-30 20:39:22 +02:00
|
|
|
|
2019-07-13 08:36:04 +02:00
|
|
|
sp.insert(shield, &shield);
|
|
|
|
}
|
2019-06-30 20:39:22 +02:00
|
|
|
|
2019-07-13 08:36:04 +02:00
|
|
|
for (QHash<Label::Shield, QPolygonF>::const_iterator it
|
|
|
|
= shields.constBegin(); it != shields.constEnd(); ++it) {
|
|
|
|
const QPolygonF &p = it.value();
|
|
|
|
QRectF rect(p.boundingRect() & tileRect);
|
2019-10-05 12:41:42 +02:00
|
|
|
if (qSqrt(AREA(rect)) < TILE_SIZE/8)
|
2019-07-13 08:36:04 +02:00
|
|
|
continue;
|
2019-06-30 20:39:22 +02:00
|
|
|
|
2019-07-13 08:36:04 +02:00
|
|
|
QMap<qreal, int> map;
|
|
|
|
QPointF center = rect.center();
|
|
|
|
for (int j = 0; j < p.size(); j++) {
|
|
|
|
QLineF l(p.at(j), center);
|
|
|
|
map.insert(l.length(), j);
|
|
|
|
}
|
2019-06-30 20:39:22 +02:00
|
|
|
|
2019-07-13 15:14:29 +02:00
|
|
|
QMap<qreal, int>::const_iterator jt = map.constBegin();
|
|
|
|
|
2019-07-13 08:36:04 +02:00
|
|
|
TextPointItem *item = new TextPointItem(
|
2019-07-13 15:14:29 +02:00
|
|
|
p.at(jt.value()).toPoint(), &(sp.value(it.key())->text()),
|
2019-07-13 08:36:04 +02:00
|
|
|
poiFont(), 0, &shieldColor, shieldBgColor(it.key().type()));
|
|
|
|
|
|
|
|
bool valid = false;
|
|
|
|
while (true) {
|
|
|
|
if (!item->collides(textItems)
|
|
|
|
&& tileRect.contains(item->boundingRect().toRect())) {
|
|
|
|
valid = true;
|
|
|
|
break;
|
2019-06-30 20:39:22 +02:00
|
|
|
}
|
2019-07-13 08:36:04 +02:00
|
|
|
if (++jt == map.constEnd())
|
|
|
|
break;
|
|
|
|
item->setPos(p.at(jt.value()).toPoint());
|
2019-06-30 20:39:22 +02:00
|
|
|
}
|
2019-07-13 08:36:04 +02:00
|
|
|
|
|
|
|
if (valid)
|
|
|
|
textItems.append(item);
|
|
|
|
else
|
|
|
|
delete item;
|
2019-06-30 20:39:22 +02:00
|
|
|
}
|
2019-05-10 18:56:19 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-02-09 23:24:48 +01:00
|
|
|
void IMGMap::processPoints(QList<MapData::Point> &points,
|
2019-06-07 20:33:08 +02:00
|
|
|
QList<TextItem*> &textItems)
|
2019-05-10 18:56:19 +02:00
|
|
|
{
|
|
|
|
qSort(points);
|
|
|
|
|
|
|
|
for (int i = 0; i < points.size(); i++) {
|
2020-02-09 23:24:48 +01:00
|
|
|
MapData::Point &point = points[i];
|
|
|
|
const Style::Point &style = _data->style()->point(point.type);
|
2019-05-10 18:56:19 +02:00
|
|
|
|
2020-01-24 19:44:19 +01:00
|
|
|
if (point.poi && _zoom < minPOIZoom(Style::poiClass(point.type)))
|
2019-05-10 18:56:19 +02:00
|
|
|
continue;
|
|
|
|
|
2019-06-30 20:39:22 +02:00
|
|
|
const QString *label = point.label.text().isEmpty()
|
|
|
|
? 0 : &(point.label.text());
|
2019-05-10 18:56:19 +02:00
|
|
|
const QImage *img = style.img().isNull() ? 0 : &style.img();
|
2019-07-08 21:19:25 +02:00
|
|
|
const QFont *fnt = point.poi
|
|
|
|
? poiFont(style.textFontSize()) : font(style.textFontSize());
|
2019-05-10 18:56:19 +02:00
|
|
|
const QColor *color = style.textColor().isValid()
|
|
|
|
? &style.textColor() : 0;
|
|
|
|
|
2019-07-08 21:19:25 +02:00
|
|
|
if ((!label || !fnt) && !img)
|
2019-05-10 18:56:19 +02:00
|
|
|
continue;
|
|
|
|
|
2019-05-12 15:43:46 +02:00
|
|
|
if (Style::isSpot(point.type))
|
2019-06-30 20:39:22 +02:00
|
|
|
point.label.setText(convertUnits(point.label.text()));
|
|
|
|
if (Style::isSummit(point.type) && !point.label.text().isEmpty()) {
|
|
|
|
QStringList list = point.label.text().split(" ");
|
|
|
|
list.last() = convertUnits(list.last());
|
2019-06-07 20:33:08 +02:00
|
|
|
point.label = list.join(" ");
|
|
|
|
}
|
2019-05-12 15:43:46 +02:00
|
|
|
|
2019-06-07 20:33:08 +02:00
|
|
|
TextPointItem *item = new TextPointItem(
|
2019-07-08 21:19:25 +02:00
|
|
|
ll2xy(point.coordinates).toPoint(), label, fnt, img, color);
|
2019-06-07 20:33:08 +02:00
|
|
|
if (item->isValid() && !item->collides(textItems))
|
|
|
|
textItems.append(item);
|
|
|
|
else
|
|
|
|
delete item;
|
2019-05-10 18:56:19 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static void render(RasterTile &tile)
|
|
|
|
{
|
2019-07-13 08:36:04 +02:00
|
|
|
tile.render();
|
2019-05-10 18:56:19 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
void IMGMap::draw(QPainter *painter, const QRectF &rect, Flags flags)
|
|
|
|
{
|
|
|
|
Q_UNUSED(flags);
|
|
|
|
|
|
|
|
QPointF tl(floor(rect.left() / TILE_SIZE)
|
|
|
|
* TILE_SIZE, floor(rect.top() / TILE_SIZE) * TILE_SIZE);
|
|
|
|
QSizeF s(rect.right() - tl.x(), rect.bottom() - tl.y());
|
|
|
|
int width = ceil(s.width() / TILE_SIZE);
|
|
|
|
int height = ceil(s.height() / TILE_SIZE);
|
|
|
|
|
|
|
|
QList<RasterTile> tiles;
|
|
|
|
|
|
|
|
for (int i = 0; i < width; i++) {
|
|
|
|
for (int j = 0; j < height; j++) {
|
|
|
|
QPixmap pm;
|
|
|
|
QPoint ttl(tl.x() + i * TILE_SIZE, tl.y() + j * TILE_SIZE);
|
2020-02-09 23:24:48 +01:00
|
|
|
QString key = _data->fileName() + "-" + QString::number(_zoom) + "_"
|
2019-05-10 18:56:19 +02:00
|
|
|
+ QString::number(ttl.x()) + "_" + QString::number(ttl.y());
|
|
|
|
if (QPixmapCache::find(key, pm))
|
|
|
|
painter->drawPixmap(ttl, pm);
|
|
|
|
else {
|
|
|
|
tiles.append(RasterTile(this, ttl, key));
|
|
|
|
RasterTile &tile = tiles.last();
|
|
|
|
|
|
|
|
RectD polyRect(_transform.img2proj(ttl), _transform.img2proj(
|
|
|
|
QPointF(ttl.x() + TILE_SIZE, ttl.y() + TILE_SIZE)));
|
2020-02-09 23:24:48 +01:00
|
|
|
_data->polys(polyRect.toRectC(_projection, 4), _zoom,
|
2020-02-07 22:10:06 +01:00
|
|
|
&(tile.polygons()), &(tile.lines()));
|
|
|
|
|
2019-05-10 18:56:19 +02:00
|
|
|
RectD pointRect(_transform.img2proj(QPointF(ttl.x() - TEXT_EXTENT,
|
|
|
|
ttl.y() - TEXT_EXTENT)), _transform.img2proj(QPointF(ttl.x()
|
|
|
|
+ TILE_SIZE + TEXT_EXTENT, ttl.y() + TILE_SIZE + TEXT_EXTENT)));
|
2020-02-09 23:24:48 +01:00
|
|
|
_data->points(pointRect.toRectC(_projection, 4), _zoom,
|
2020-02-07 22:10:06 +01:00
|
|
|
&(tile.points()));
|
2019-05-10 18:56:19 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
QFuture<void> future = QtConcurrent::map(tiles, render);
|
|
|
|
future.waitForFinished();
|
|
|
|
|
|
|
|
for (int i = 0; i < tiles.size(); i++) {
|
|
|
|
RasterTile &mt = tiles[i];
|
|
|
|
QPixmap pm(QPixmap::fromImage(mt.img()));
|
|
|
|
if (pm.isNull())
|
|
|
|
continue;
|
|
|
|
|
|
|
|
QPixmapCache::insert(mt.key(), pm);
|
|
|
|
|
|
|
|
painter->drawPixmap(mt.xy(), pm);
|
|
|
|
}
|
|
|
|
}
|
2019-05-14 23:01:24 +02:00
|
|
|
|
|
|
|
void IMGMap::setProjection(const Projection &projection)
|
|
|
|
{
|
|
|
|
_projection = projection;
|
|
|
|
updateTransform();
|
|
|
|
QPixmapCache::clear();
|
|
|
|
}
|