1
0
mirror of https://github.com/tumic0/GPXSee.git synced 2025-03-15 03:17:44 +01:00
GPXSee/src/map/IMG/rastertile.cpp

454 lines
11 KiB
C++
Raw Normal View History

2020-04-26 01:17:54 +02:00
#include <QFont>
#include <QPainter>
#include <QCache>
#include "map/imgmap.h"
2021-04-10 15:27:40 +02:00
#include "map/textpathitem.h"
#include "map/textpointitem.h"
2020-04-26 01:17:54 +02:00
#include "bitmapline.h"
#include "style.h"
2021-01-27 21:18:06 +01:00
#include "lblfile.h"
2020-04-26 01:17:54 +02:00
#include "rastertile.h"
2021-04-10 15:27:40 +02:00
using namespace IMG;
2020-04-26 01:17:54 +02:00
#define AREA(rect) \
(rect.size().width() * rect.size().height())
2021-04-18 12:20:07 +02:00
static const QColor textColor(Qt::black);
static const QColor haloColor(Qt::white);
2020-04-26 01:17:54 +02:00
static const QColor shieldColor(Qt::white);
static const QColor shieldBgColor1("#dd3e3e");
static const QColor shieldBgColor2("#379947");
static const QColor shieldBgColor3("#4a7fc1");
static QFont pixelSizeFont(int pixelSize)
{
QFont f;
f.setPixelSize(pixelSize);
return f;
}
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);
static QFont extraSmall = pixelSizeFont(10);
switch (size) {
case Style::None:
return 0;
case Style::Large:
return &large;
case Style::Normal:
return &normal;
case Style::Small:
return &small;
case Style::ExtraSmall:
return &extraSmall;
default:
return font(defaultSize);
}
}
static QFont *poiFont(Style::FontSize size = Style::Normal)
{
static QFont poi = pixelSizeFont(10);
switch (size) {
case Style::None:
return 0;
default:
return &poi;
}
}
2021-04-10 15:27:40 +02:00
static const QColor *shieldBgColor(Shield::Type type)
2020-04-26 01:17:54 +02:00
{
switch (type) {
2021-04-10 15:27:40 +02:00
case Shield::USInterstate:
case Shield::Hbox:
2020-04-26 01:17:54 +02:00
return &shieldBgColor1;
2021-04-10 15:27:40 +02:00
case Shield::USShield:
case Shield::Box:
2020-04-26 01:17:54 +02:00
return &shieldBgColor2;
2021-04-10 15:27:40 +02:00
case Shield::USRound:
case Shield::Oval:
2020-04-26 01:17:54 +02:00
return &shieldBgColor3;
default:
return 0;
}
}
2021-04-10 15:27:40 +02:00
static int minShieldZoom(Shield::Type type)
2020-04-26 01:17:54 +02:00
{
switch (type) {
2021-04-10 15:27:40 +02:00
case Shield::USInterstate:
case Shield::Hbox:
2020-04-26 01:17:54 +02:00
return 17;
2021-04-10 15:27:40 +02:00
case Shield::USShield:
case Shield::Box:
2020-04-26 01:17:54 +02:00
return 19;
2021-04-10 15:27:40 +02:00
case Shield::USRound:
case Shield::Oval:
2020-04-26 01:17:54 +02:00
return 20;
default:
return 0;
}
}
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;
2021-04-11 11:37:43 +02:00
return area;
2020-04-26 01:17:54 +02:00
}
static QPointF centroid(const QVector<QPointF> &polygon)
{
qreal cx = 0, cy = 0;
qreal factor = 1.0 / (6.0 * area(polygon));
for (int i = 0; i < polygon.size(); i++) {
int j = (i + 1) % polygon.size();
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;
}
return QPointF(cx * factor, cy * factor);
}
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)));
}
void RasterTile::render()
{
QList<TextItem*> textItems;
ll2xy(_polygons);
ll2xy(_lines);
ll2xy(_points);
2020-04-26 01:17:54 +02:00
processPoints(textItems);
processPolygons(textItems);
processLines(textItems);
2021-04-13 20:30:27 +02:00
_pixmap.fill(Qt::transparent);
2020-04-26 01:17:54 +02:00
2021-04-13 20:30:27 +02:00
QPainter painter(&_pixmap);
2020-04-26 01:17:54 +02:00
painter.setRenderHint(QPainter::SmoothPixmapTransform);
painter.setRenderHint(QPainter::Antialiasing);
painter.translate(-_xy.x(), -_xy.y());
drawPolygons(&painter);
drawLines(&painter);
drawTextItems(&painter, textItems);
qDeleteAll(textItems);
2021-10-24 12:30:31 +02:00
//painter.setPen(Qt::red);
//painter.setRenderHint(QPainter::Antialiasing, false);
//painter.drawRect(QRect(_xy, _pixmap.size()));
2020-04-26 01:17:54 +02:00
}
void RasterTile::ll2xy(QList<MapData::Poly> &polys)
{
for (int i = 0; i < polys.size(); i++) {
MapData::Poly &poly = polys[i];
for (int j = 0; j < poly.points.size(); j++) {
QPointF &p = poly.points[j];
p = _map->ll2xy(Coordinates(p.x(), p.y()));
}
}
}
void RasterTile::ll2xy(QList<MapData::Point> &points)
{
for (int i = 0; i < points.size(); i++) {
QPointF p(_map->ll2xy(points.at(i).coordinates));
points[i].coordinates = Coordinates(p.x(), p.y());
}
}
2020-04-26 01:17:54 +02:00
void RasterTile::drawPolygons(QPainter *painter)
{
QCache<const LBLFile *, SubFile::Handle> hc(16);
2020-04-26 01:17:54 +02:00
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))
continue;
if (poly.raster.isValid()) {
RectC r(poly.raster.rect());
QPointF tl(_map->ll2xy(r.topLeft()));
QPointF br(_map->ll2xy(r.bottomRight()));
2021-06-04 21:04:09 +02:00
QSizeF size(QRectF(tl, br).size());
bool insert = false;
SubFile::Handle *hdl = hc.object(poly.raster.lbl());
if (!hdl) {
hdl = new SubFile::Handle(poly.raster.lbl());
insert = true;
}
QPixmap pm(poly.raster.lbl()->image(*hdl, poly.raster.id()));
if (insert)
hc.insert(poly.raster.lbl(), hdl);
2021-06-04 21:04:09 +02:00
qreal sx = size.width() / (qreal)pm.width();
qreal sy = size.height() / (qreal)pm.height();
2021-01-27 21:18:06 +01:00
painter->save();
painter->scale(sx, sy);
2021-03-19 08:42:20 +01:00
painter->drawPixmap(QPointF(tl.x() / sx, tl.y() / sy), pm);
painter->restore();
//painter->setPen(Qt::blue);
//painter->setBrush(Qt::NoBrush);
//painter->drawRect(QRectF(tl, br));
} else {
const Style::Polygon &style = _style->polygon(poly.type);
painter->setPen(style.pen());
painter->setBrush(style.brush());
painter->drawPolygon(poly.points);
}
2020-04-26 01:17:54 +02:00
}
}
}
void RasterTile::drawLines(QPainter *painter)
{
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);
if (style.background() == Qt::NoPen)
continue;
painter->setPen(style.background());
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);
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);
}
}
}
void RasterTile::drawTextItems(QPainter *painter,
const QList<TextItem*> &textItems)
{
for (int i = 0; i < textItems.size(); i++)
textItems.at(i)->paint(painter);
}
2020-10-17 14:26:59 +02:00
static void removeDuplicitLabel(QList<TextItem *> &labels, const QString &text,
const QRectF &tileRect)
{
2020-10-17 20:59:58 +02:00
for (int i = 0; i < labels.size(); i++) {
TextItem *item = labels.at(i);
2020-10-17 14:26:59 +02:00
if (tileRect.contains(item->boundingRect()) && *(item->text()) == text) {
2020-10-17 20:59:58 +02:00
labels.removeAt(i);
2020-10-17 14:30:06 +02:00
delete item;
2020-10-17 14:26:59 +02:00
return;
}
}
}
2020-04-26 01:17:54 +02:00
void RasterTile::processPolygons(QList<TextItem*> &textItems)
{
2021-04-13 20:30:27 +02:00
QRectF tileRect(_xy, _pixmap.size());
2020-10-17 14:26:59 +02:00
QSet<QString> set;
QList<TextItem *> labels;
2020-04-26 01:17:54 +02:00
for (int i = 0; i < _polygons.size(); i++) {
MapData::Poly &poly = _polygons[i];
2020-10-17 14:26:59 +02:00
bool exists = set.contains(poly.label.text());
2020-04-26 01:17:54 +02:00
if (poly.label.text().isEmpty())
continue;
if (_zoom <= 23 && (Style::isWaterArea(poly.type)
|| Style::isMilitaryArea(poly.type)
|| Style::isNatureReserve(poly.type))) {
const Style::Polygon &style = _style->polygon(poly.type);
TextPointItem *item = new TextPointItem(
2021-04-18 12:20:07 +02:00
centroid(poly.points).toPoint(), &poly.label.text(), poiFont(),
0, &style.brush().color(), &haloColor);
2020-04-26 01:17:54 +02:00
if (item->isValid() && !item->collides(textItems)
2020-10-22 01:16:23 +02:00
&& !item->collides(labels)
2020-10-17 14:26:59 +02:00
&& !(exists && tileRect.contains(item->boundingRect()))
&& rectNearPolygon(poly.points, item->boundingRect())) {
if (exists)
removeDuplicitLabel(labels, poly.label.text(), tileRect);
else
set.insert(poly.label.text());
labels.append(item);
} else
2020-04-26 01:17:54 +02:00
delete item;
}
}
2020-10-17 14:26:59 +02:00
textItems.append(labels);
2020-04-26 01:17:54 +02:00
}
void RasterTile::processLines(QList<TextItem*> &textItems)
{
2021-04-13 20:30:27 +02:00
QRect tileRect(_xy, _pixmap.size());
2020-04-26 01:17:54 +02:00
std::stable_sort(_lines.begin(), _lines.end());
2020-04-26 01:17:54 +02:00
if (_zoom >= 22)
processStreetNames(tileRect, textItems);
processShields(tileRect, textItems);
}
void RasterTile::processStreetNames(const QRect &tileRect,
QList<TextItem*> &textItems)
{
for (int i = 0; i < _lines.size(); i++) {
MapData::Poly &poly = _lines[i];
const Style::Line &style = _style->line(poly.type);
if (style.img().isNull() && style.foreground() == Qt::NoPen)
continue;
if (poly.label.text().isEmpty()
|| style.textFontSize() == Style::None)
continue;
const QFont *fnt = font(style.textFontSize(), Style::Small);
const QColor *color = style.textColor().isValid()
? &style.textColor() : 0;
TextPathItem *item = new TextPathItem(poly.points,
&poly.label.text(), tileRect, fnt, color);
if (item->isValid() && !item->collides(textItems))
textItems.append(item);
else
delete item;
}
}
void RasterTile::processShields(const QRect &tileRect,
QList<TextItem*> &textItems)
{
for (int type = FIRST_SHIELD; type <= LAST_SHIELD; type++) {
2021-04-10 15:27:40 +02:00
if (minShieldZoom(static_cast<Shield::Type>(type)) > _zoom)
2020-04-26 01:17:54 +02:00
continue;
2021-04-10 15:27:40 +02:00
QHash<Shield, QPolygonF> shields;
QHash<Shield, const Shield*> sp;
2020-04-26 01:17:54 +02:00
for (int i = 0; i < _lines.size(); i++) {
const MapData::Poly &poly = _lines.at(i);
2021-04-10 15:27:40 +02:00
const Shield &shield = poly.label.shield();
2020-04-26 01:17:54 +02:00
if (!shield.isValid() || shield.type() != type
|| !Style::isMajorRoad(poly.type))
continue;
QPolygonF &p = shields[shield];
for (int j = 0; j < poly.points.size(); j++)
p.append(poly.points.at(j));
sp.insert(shield, &shield);
}
2021-04-10 15:27:40 +02:00
for (QHash<Shield, QPolygonF>::const_iterator it = shields.constBegin();
it != shields.constEnd(); ++it) {
2020-04-26 01:17:54 +02:00
const QPolygonF &p = it.value();
QRectF rect(p.boundingRect() & tileRect);
2021-04-13 20:30:27 +02:00
if (AREA(rect) < AREA(QRect(0, 0, _pixmap.width()/4, _pixmap.width()/4)))
2020-04-26 01:17:54 +02:00
continue;
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);
}
QMap<qreal, int>::const_iterator jt = map.constBegin();
TextPointItem *item = new TextPointItem(
p.at(jt.value()).toPoint(), &(sp.value(it.key())->text()),
2021-04-18 12:20:07 +02:00
poiFont(), 0, &shieldColor, 0, shieldBgColor(it.key().type()));
2020-04-26 01:17:54 +02:00
bool valid = false;
while (true) {
if (!item->collides(textItems)
&& tileRect.contains(item->boundingRect().toRect())) {
valid = true;
break;
}
if (++jt == map.constEnd())
break;
item->setPos(p.at(jt.value()).toPoint());
}
if (valid)
textItems.append(item);
else
delete item;
}
}
}
void RasterTile::processPoints(QList<TextItem*> &textItems)
{
std::sort(_points.begin(), _points.end());
2020-04-26 01:17:54 +02:00
for (int i = 0; i < _points.size(); i++) {
MapData::Point &point = _points[i];
const Style::Point &style = _style->point(point.type);
2020-10-16 00:03:26 +02:00
bool poi = Style::isPOI(point.type);
2020-04-26 01:17:54 +02:00
const QString *label = point.label.text().isEmpty()
? 0 : &(point.label.text());
const QImage *img = style.img().isNull() ? 0 : &style.img();
2020-10-16 00:03:26 +02:00
const QFont *fnt = poi
? poiFont(_zoom > 25 ? Style::Normal : style.textFontSize())
: font(style.textFontSize());
2020-04-26 01:17:54 +02:00
const QColor *color = style.textColor().isValid()
2021-04-18 12:20:07 +02:00
? &style.textColor() : &textColor;
2020-04-26 01:17:54 +02:00
if ((!label || !fnt) && !img)
continue;
TextPointItem *item = new TextPointItem(QPoint(point.coordinates.lon(),
2021-04-18 12:20:07 +02:00
point.coordinates.lat()), label, fnt, img, color, &haloColor);
2020-04-26 01:17:54 +02:00
if (item->isValid() && !item->collides(textItems))
textItems.append(item);
else
delete item;
}
}