1
0
mirror of https://github.com/tumic0/GPXSee.git synced 2024-10-06 23:03:22 +02:00
GPXSee/src/pathview.cpp

643 lines
14 KiB
C++
Raw Normal View History

#include <QGraphicsView>
#include <QGraphicsScene>
#include <QWheelEvent>
#include <QSysInfo>
2016-12-20 00:11:30 +01:00
#include "opengl.h"
#include "misc.h"
2016-02-12 10:23:14 +01:00
#include "poi.h"
2016-10-23 11:09:20 +02:00
#include "data.h"
2016-02-12 10:09:17 +01:00
#include "map.h"
#include "emptymap.h"
#include "trackitem.h"
2016-08-09 01:16:19 +02:00
#include "routeitem.h"
2016-02-19 21:34:55 +01:00
#include "waypointitem.h"
#include "scaleitem.h"
2016-09-26 21:01:58 +02:00
#include "pathview.h"
#define MARGIN 10.0
#define SCALE_OFFSET 7
2016-11-11 17:58:18 +01:00
static void unite(QRectF &rect, const QPointF &p)
{
if (p.x() < rect.left())
rect.setLeft(p.x());
if (p.x() > rect.right())
rect.setRight(p.x());
if (p.y() > rect.bottom())
rect.setBottom(p.y());
if (p.y() < rect.top())
rect.setTop(p.y());
}
PathView::PathView(Map *map, POI *poi, QWidget *parent)
: QGraphicsView(parent)
{
Q_ASSERT(map != 0);
Q_ASSERT(poi != 0);
_scene = new QGraphicsScene(this);
setScene(_scene);
setCacheMode(QGraphicsView::CacheBackground);
setDragMode(QGraphicsView::ScrollHandDrag);
setViewportUpdateMode(QGraphicsView::FullViewportUpdate);
setVerticalScrollBarPolicy(Qt::ScrollBarAlwaysOff);
setHorizontalScrollBarPolicy(Qt::ScrollBarAlwaysOff);
2016-12-06 01:48:26 +01:00
setRenderHint(QPainter::Antialiasing, true);
2016-10-04 10:16:46 +02:00
setAcceptDrops(false);
_mapScale = new ScaleItem();
_mapScale->setZValue(2.0);
_map = map;
_poi = poi;
connect(_map, SIGNAL(loaded()), this, SLOT(redraw()));
connect(_poi, SIGNAL(pointsChanged()), this, SLOT(updatePOI()));
2016-07-25 19:32:36 +02:00
_units = Metric;
2016-08-09 01:16:19 +02:00
_showMap = true;
2016-08-09 01:16:19 +02:00
_showTracks = true;
_showRoutes = true;
_showWaypoints = true;
_showWaypointLabels = true;
2016-10-08 14:53:10 +02:00
_showPOI = true;
2016-08-09 01:16:19 +02:00
_showPOILabels = true;
_overlapPOIs = true;
2016-08-09 10:47:49 +02:00
_showRouteWaypoints = true;
2016-12-06 01:48:26 +01:00
_trackWidth = 3;
_routeWidth = 3;
_trackStyle = Qt::SolidLine;
_routeStyle = Qt::DashLine;
2016-08-09 01:16:19 +02:00
_plot = false;
2017-01-20 01:17:22 +01:00
setSceneRect(_map->bounds());
_res = _map->resolution(_scene->sceneRect().center());
}
2016-09-26 21:01:58 +02:00
PathView::~PathView()
{
2016-05-12 09:03:05 +02:00
if (_mapScale->scene() != _scene)
delete _mapScale;
}
2016-09-26 21:01:58 +02:00
PathItem *PathView::addTrack(const Track &track)
{
if (track.isNull()) {
2016-12-06 01:48:26 +01:00
_palette.nextColor();
2016-09-19 00:56:10 +02:00
return 0;
2016-03-23 20:49:40 +01:00
}
TrackItem *ti = new TrackItem(track, _map);
2016-08-09 01:16:19 +02:00
_tracks.append(ti);
2016-11-11 17:58:18 +01:00
_tr |= ti->path().boundingRect();
2016-12-06 01:48:26 +01:00
ti->setColor(_palette.nextColor());
ti->setWidth(_trackWidth);
ti->setStyle(_trackStyle);
2016-08-09 01:16:19 +02:00
ti->setVisible(_showTracks);
_scene->addItem(ti);
2016-09-19 00:56:10 +02:00
addPOI(_poi->points(ti->path()));
2016-10-08 14:53:10 +02:00
2016-09-19 00:56:10 +02:00
return ti;
2016-03-03 09:15:56 +01:00
}
2016-09-26 21:01:58 +02:00
PathItem *PathView::addRoute(const Route &route)
2016-08-09 01:16:19 +02:00
{
if (route.isNull()) {
2016-12-06 01:48:26 +01:00
_palette.nextColor();
2016-09-19 00:56:10 +02:00
return 0;
2016-08-09 01:16:19 +02:00
}
RouteItem *ri = new RouteItem(route, _map);
2016-08-09 01:16:19 +02:00
_routes.append(ri);
2016-11-11 17:58:18 +01:00
_rr |= ri->path().boundingRect();
2016-12-06 01:48:26 +01:00
ri->setColor(_palette.nextColor());
ri->setWidth(_routeWidth);
ri->setStyle(_routeStyle);
2016-08-09 01:16:19 +02:00
ri->setVisible(_showRoutes);
2016-08-09 10:47:49 +02:00
ri->showWaypoints(_showRouteWaypoints);
ri->showWaypointLabels(_showWaypointLabels);
2016-08-09 01:16:19 +02:00
_scene->addItem(ri);
2016-09-19 00:56:10 +02:00
addPOI(_poi->points(ri->path()));
2016-10-08 14:53:10 +02:00
2016-09-19 00:56:10 +02:00
return ri;
2016-08-09 01:16:19 +02:00
}
2016-09-26 21:01:58 +02:00
void PathView::addWaypoints(const QList<Waypoint> &waypoints)
2016-03-15 01:20:24 +01:00
{
for (int i = 0; i < waypoints.count(); i++) {
2016-03-19 09:06:43 +01:00
const Waypoint &w = waypoints.at(i);
2016-03-15 01:20:24 +01:00
WaypointItem *wi = new WaypointItem(w, _map);
_waypoints.append(wi);
Coordinates c = wi->waypoint().coordinates();
updateWaypointsBoundingRect(QPointF(c.lon(), c.lat()));
2016-03-15 01:20:24 +01:00
wi->setZValue(1);
2016-08-09 01:16:19 +02:00
wi->showLabel(_showWaypointLabels);
wi->setVisible(_showWaypoints);
2016-03-15 01:20:24 +01:00
_scene->addItem(wi);
}
addPOI(_poi->points(waypoints));
2016-03-15 01:20:24 +01:00
}
2016-10-23 11:09:20 +02:00
QList<PathItem *> PathView::loadData(const Data &data)
2016-03-03 09:15:56 +01:00
{
2016-09-19 00:56:10 +02:00
QList<PathItem *> paths;
qreal scale = _map->zoom();
2016-10-23 11:09:20 +02:00
for (int i = 0; i < data.tracks().count(); i++)
paths.append(addTrack(*(data.tracks().at(i))));
for (int i = 0; i < data.routes().count(); i++)
paths.append(addRoute(*(data.routes().at(i))));
addWaypoints(data.waypoints());
2016-03-15 01:20:24 +01:00
2016-08-09 01:16:19 +02:00
if (_tracks.empty() && _routes.empty() && _waypoints.empty())
2016-09-19 00:56:10 +02:00
return paths;
2016-03-15 01:20:24 +01:00
if (mapScale() != scale)
rescale();
2017-01-20 01:17:22 +01:00
else
2016-10-08 14:53:10 +02:00
updatePOIVisibility();
QPointF center = contentCenter();
2017-01-16 09:54:12 +01:00
centerOn(center);
_res = _map->resolution(center);
2017-01-16 09:54:12 +01:00
_mapScale->setResolution(_res);
2016-03-03 09:15:56 +01:00
if (_mapScale->scene() != _scene)
_scene->addItem(_mapScale);
2016-09-19 00:56:10 +02:00
return paths;
}
void PathView::updateWaypointsBoundingRect(const QPointF &wp)
{
if (_wr.isNull()) {
if (_wp.isNull())
_wp = wp;
else {
2017-01-16 09:54:12 +01:00
_wr = QRectF(_wp, wp).normalized();
_wp = QPointF();
}
} else
unite(_wr, wp);
}
qreal PathView::mapScale() const
{
2016-11-11 17:58:18 +01:00
QRectF br = _tr | _rr | _wr;
if (!br.isNull() && !_wp.isNull())
unite(br, _wp);
2016-03-15 01:20:24 +01:00
return _map->zoomFit(viewport()->size() - QSize(MARGIN/2, MARGIN/2), br);
2016-03-15 01:20:24 +01:00
}
QPointF PathView::contentCenter() const
{
QRectF br = _tr | _rr | _wr;
if (!br.isNull() && !_wp.isNull())
unite(br, _wp);
if (br.isNull())
return _map->ll2xy(_wp);
else
return _map->ll2xy(br.center());
}
2016-10-08 14:53:10 +02:00
void PathView::updatePOIVisibility()
{
QHash<Waypoint, WaypointItem*>::const_iterator it, jt;
2016-10-08 14:53:10 +02:00
if (!_showPOI)
return;
for (it = _pois.constBegin(); it != _pois.constEnd(); it++)
it.value()->show();
if (!_overlapPOIs) {
for (it = _pois.constBegin(); it != _pois.constEnd(); it++) {
for (jt = _pois.constBegin(); jt != _pois.constEnd(); jt++) {
if (it.value()->isVisible() && jt.value()->isVisible()
&& it != jt && it.value()->collidesWithItem(jt.value()))
jt.value()->hide();
}
}
}
}
void PathView::rescale()
{
setSceneRect(_map->bounds());
2017-01-16 09:54:12 +01:00
2016-08-09 01:16:19 +02:00
for (int i = 0; i < _tracks.size(); i++)
_tracks.at(i)->setMap(_map);
2016-08-09 01:16:19 +02:00
for (int i = 0; i < _routes.size(); i++)
_routes.at(i)->setMap(_map);
for (int i = 0; i < _waypoints.size(); i++)
_waypoints.at(i)->setMap(_map);
2016-03-15 01:20:24 +01:00
QHash<Waypoint, WaypointItem*>::const_iterator it;
2016-10-08 14:53:10 +02:00
for (it = _pois.constBegin(); it != _pois.constEnd(); it++)
it.value()->setMap(_map);
2016-10-08 14:53:10 +02:00
updatePOIVisibility();
}
2016-12-06 01:48:26 +01:00
void PathView::setPalette(const Palette &palette)
{
_palette = palette;
_palette.reset();
for (int i = 0; i < _tracks.count(); i++)
_tracks.at(i)->setColor(_palette.nextColor());
for (int i = 0; i < _routes.count(); i++)
_routes.at(i)->setColor(_palette.nextColor());
}
void PathView::setMap(Map *map)
{
_map->unload();
disconnect(_map, SIGNAL(loaded()), this, SLOT(redraw()));
_map = map;
_map->load();
connect(_map, SIGNAL(loaded()), this, SLOT(redraw()));
mapScale();
setSceneRect(_map->bounds());
for (int i = 0; i < _tracks.size(); i++)
_tracks.at(i)->setMap(map);
for (int i = 0; i < _routes.size(); i++)
_routes.at(i)->setMap(map);
for (int i = 0; i < _waypoints.size(); i++)
_waypoints.at(i)->setMap(map);
QPointF center = contentCenter();
centerOn(center);
_res = _map->resolution(center);
_mapScale->setResolution(_res);
resetCachedContent();
}
2016-10-08 14:53:10 +02:00
void PathView::setPOI(POI *poi)
{
disconnect(_poi, SIGNAL(pointsChanged()), this, SLOT(updatePOI()));
connect(poi, SIGNAL(pointsChanged()), this, SLOT(updatePOI()));
2016-10-09 23:46:30 +02:00
2016-10-08 14:53:10 +02:00
_poi = poi;
updatePOI();
2016-10-08 14:53:10 +02:00
}
2016-10-09 23:46:30 +02:00
void PathView::updatePOI()
2016-10-08 14:53:10 +02:00
{
2016-10-09 23:46:30 +02:00
QHash<Waypoint, WaypointItem*>::const_iterator it;
for (it = _pois.constBegin(); it != _pois.constEnd(); it++) {
_scene->removeItem(it.value());
delete it.value();
}
_pois.clear();
2016-10-08 14:53:10 +02:00
for (int i = 0; i < _tracks.size(); i++)
addPOI(_poi->points(_tracks.at(i)->path()));
2016-10-08 14:53:10 +02:00
for (int i = 0; i < _routes.size(); i++)
addPOI(_poi->points(_routes.at(i)->path()));
2016-10-08 14:53:10 +02:00
addPOI(_poi->points(_waypoints));
updatePOIVisibility();
}
2016-09-26 21:01:58 +02:00
void PathView::addPOI(const QVector<Waypoint> &waypoints)
{
for (int i = 0; i < waypoints.size(); i++) {
2016-03-19 09:06:43 +01:00
const Waypoint &w = waypoints.at(i);
if (_pois.contains(w))
continue;
WaypointItem *pi = new WaypointItem(w, _map);
pi->setZValue(1);
2016-08-09 01:16:19 +02:00
pi->showLabel(_showPOILabels);
2016-10-08 14:53:10 +02:00
pi->setVisible(_showPOI);
_scene->addItem(pi);
2016-03-19 09:06:43 +01:00
_pois.insert(w, pi);
}
}
2016-09-26 21:01:58 +02:00
void PathView::setUnits(enum Units units)
{
2016-07-25 19:32:36 +02:00
_units = units;
_mapScale->setUnits(units);
2016-07-25 19:32:36 +02:00
2016-08-09 01:16:19 +02:00
for (int i = 0; i < _tracks.count(); i++)
_tracks[i]->setUnits(units);
for (int i = 0; i < _routes.count(); i++)
_routes[i]->setUnits(units);
2016-08-02 00:28:56 +02:00
for (int i = 0; i < _waypoints.size(); i++)
_waypoints.at(i)->setUnits(units);
QHash<Waypoint, WaypointItem*>::const_iterator it;
for (it = _pois.constBegin(); it != _pois.constEnd(); it++)
it.value()->setUnits(units);
}
2016-09-26 21:01:58 +02:00
void PathView::redraw()
{
resetCachedContent();
}
void PathView::zoom(const QPoint &pos, const Coordinates &c)
{
QPoint offset = pos - viewport()->rect().center();
rescale();
QPointF center = _map->ll2xy(c) - offset;
2017-01-16 09:54:12 +01:00
centerOn(center);
_res = _map->resolution(center);
2017-01-16 09:54:12 +01:00
_mapScale->setResolution(_res);
resetCachedContent();
}
2016-09-26 21:01:58 +02:00
void PathView::wheelEvent(QWheelEvent *event)
{
qreal os, ns;
os = _map->zoom();
Coordinates c = _map->xy2ll(mapToScene(event->pos()));
ns = (event->delta() > 0) ? _map->zoomIn() : _map->zoomOut();
if (ns != os)
zoom(event->pos(), c);
}
void PathView::mouseDoubleClickEvent(QMouseEvent *event)
{
qreal os, ns;
if (event->button() != Qt::LeftButton && event->button() != Qt::RightButton)
return;
os = _map->zoom();
Coordinates c = _map->xy2ll(mapToScene(event->pos()));
ns = (event->button() == Qt::LeftButton) ? _map->zoomIn() : _map->zoomOut();
if (ns != os)
zoom(event->pos(), c);
}
2016-09-26 21:01:58 +02:00
void PathView::keyPressEvent(QKeyEvent *event)
{
qreal os, ns;
os = _map->zoom();
QPoint pos = QRect(QPoint(), viewport()->size()).center();
Coordinates c = _map->xy2ll(mapToScene(pos));
if (event->matches(QKeySequence::ZoomIn))
ns = _map->zoomIn();
else if (event->matches(QKeySequence::ZoomOut))
ns = _map->zoomOut();
else {
QWidget::keyPressEvent(event);
return;
}
if (ns != os)
zoom(pos, c);
}
2016-09-26 21:01:58 +02:00
void PathView::plot(QPainter *painter, const QRectF &target)
{
QRect orig, adj;
qreal ratio, diff;
2016-05-20 22:44:03 +02:00
2016-05-15 13:19:07 +02:00
orig = viewport()->rect();
2016-05-27 22:45:58 +02:00
if (orig.height() * (target.width() / target.height()) - orig.width() < 0) {
ratio = target.height()/target.width();
2016-05-27 22:45:58 +02:00
diff = (orig.width() * ratio) - orig.height();
adj = orig.adjusted(0, -diff/2, 0, diff/2);
2016-05-27 22:45:58 +02:00
} else {
ratio = target.width() / target.height();
diff = (orig.height() * ratio) - orig.width();
adj = orig.adjusted(-diff/2, 0, diff/2, 0);
}
2016-05-20 22:44:03 +02:00
setUpdatesEnabled(false);
_plot = true;
_map->setBlockingMode(true);
2016-05-20 22:44:03 +02:00
QPointF pos = _mapScale->pos();
_mapScale->setPos(mapToScene(QPoint(adj.bottomRight() + QPoint(
-(SCALE_OFFSET + _mapScale->boundingRect().width()),
-(SCALE_OFFSET + _mapScale->boundingRect().height())))));
render(painter, target, adj);
2016-05-20 22:44:03 +02:00
_mapScale->setPos(pos);
_map->setBlockingMode(false);
_plot = false;
2016-05-20 22:44:03 +02:00
setUpdatesEnabled(true);
}
2016-09-26 21:01:58 +02:00
void PathView::clear()
{
if (_mapScale->scene() == _scene)
_scene->removeItem(_mapScale);
_pois.clear();
2016-08-09 01:16:19 +02:00
_tracks.clear();
_routes.clear();
_waypoints.clear();
_scene->clear();
2016-03-02 09:34:39 +01:00
_palette.reset();
2016-11-11 17:58:18 +01:00
_tr = QRectF(); _rr = QRectF(); _wr = QRectF();
_wp = QPointF();
setSceneRect(_map->bounds());
_res = _map->resolution(_scene->sceneRect().center());
}
2016-09-26 21:01:58 +02:00
void PathView::showTracks(bool show)
2016-08-09 01:16:19 +02:00
{
_showTracks = show;
for (int i = 0; i < _tracks.count(); i++)
_tracks.at(i)->setVisible(show);
}
2016-09-26 21:01:58 +02:00
void PathView::showRoutes(bool show)
2016-08-09 01:16:19 +02:00
{
_showRoutes = show;
for (int i = 0; i < _routes.count(); i++)
_routes.at(i)->setVisible(show);
}
2016-09-26 21:01:58 +02:00
void PathView::showWaypoints(bool show)
2016-08-09 01:16:19 +02:00
{
_showWaypoints = show;
for (int i = 0; i < _waypoints.count(); i++)
_waypoints.at(i)->setVisible(show);
}
2016-09-26 21:01:58 +02:00
void PathView::showWaypointLabels(bool show)
2016-08-09 01:16:19 +02:00
{
_showWaypointLabels = show;
for (int i = 0; i < _waypoints.size(); i++)
_waypoints.at(i)->showLabel(show);
for (int i = 0; i < _routes.size(); i++)
_routes.at(i)->showWaypointLabels(show);
2016-08-09 01:16:19 +02:00
}
2016-09-26 21:01:58 +02:00
void PathView::showRouteWaypoints(bool show)
2016-08-09 10:47:49 +02:00
{
_showRouteWaypoints = show;
for (int i = 0; i < _routes.size(); i++)
_routes.at(i)->showWaypoints(show);
}
void PathView::showMap(bool show)
{
_showMap = show;
resetCachedContent();
}
2016-10-08 14:53:10 +02:00
void PathView::showPOI(bool show)
{
_showPOI = show;
QHash<Waypoint, WaypointItem*>::const_iterator it;
for (it = _pois.constBegin(); it != _pois.constEnd(); it++)
it.value()->setVisible(show);
updatePOIVisibility();
}
2016-09-26 21:01:58 +02:00
void PathView::showPOILabels(bool show)
2016-08-09 01:16:19 +02:00
{
_showPOILabels = show;
QHash<Waypoint, WaypointItem*>::const_iterator it;
for (it = _pois.constBegin(); it != _pois.constEnd(); it++)
it.value()->showLabel(show);
2016-10-08 14:53:10 +02:00
updatePOIVisibility();
2016-08-09 01:16:19 +02:00
}
2016-09-26 21:01:58 +02:00
void PathView::setPOIOverlap(bool overlap)
{
2016-08-09 01:16:19 +02:00
_overlapPOIs = overlap;
2016-10-08 14:53:10 +02:00
updatePOIVisibility();
}
2016-12-06 01:48:26 +01:00
void PathView::setTrackWidth(int width)
{
_trackWidth = width;
for (int i = 0; i < _tracks.count(); i++)
_tracks.at(i)->setWidth(width);
}
void PathView::setRouteWidth(int width)
{
_routeWidth = width;
for (int i = 0; i < _routes.count(); i++)
_routes.at(i)->setWidth(width);
}
void PathView::setTrackStyle(Qt::PenStyle style)
{
_trackStyle = style;
for (int i = 0; i < _tracks.count(); i++)
_tracks.at(i)->setStyle(style);
}
void PathView::setRouteStyle(Qt::PenStyle style)
{
_routeStyle = style;
for (int i = 0; i < _routes.count(); i++)
_routes.at(i)->setStyle(style);
}
2016-09-26 21:01:58 +02:00
void PathView::drawBackground(QPainter *painter, const QRectF &rect)
{
if (_showMap)
_map->draw(painter, rect);
else
painter->fillRect(rect, Qt::white);
}
void PathView::resizeEvent(QResizeEvent *event)
{
2017-01-16 09:54:12 +01:00
Q_UNUSED(event);
qreal scale = _map->zoom();
if (mapScale() != scale)
rescale();
QPointF center = contentCenter();
2017-01-16 09:54:12 +01:00
centerOn(center);
_res = _map->resolution(center);
2017-01-16 09:54:12 +01:00
_mapScale->setResolution(_res);
resetCachedContent();
}
void PathView::paintEvent(QPaintEvent *event)
{
2016-05-20 22:44:03 +02:00
QPointF scenePos = mapToScene(rect().bottomRight() + QPoint(
-(SCALE_OFFSET + _mapScale->boundingRect().width()),
-(SCALE_OFFSET + _mapScale->boundingRect().height())));
if (_mapScale->pos() != scenePos && !_plot)
_mapScale->setPos(scenePos);
QGraphicsView::paintEvent(event);
}
2016-12-06 01:48:26 +01:00
2017-01-16 09:54:12 +01:00
void PathView::scrollContentsBy(int dx, int dy)
{
QGraphicsView::scrollContentsBy(dx, dy);
QPointF center = mapToScene(viewport()->rect().center());
qreal res = _map->resolution(center);
2017-01-16 09:54:12 +01:00
if (qMax(res, _res) / qMin(res, _res) > 1.1) {
_mapScale->setResolution(res);
_res = res;
}
}
2016-12-06 01:48:26 +01:00
void PathView::useOpenGL(bool use)
{
2017-02-12 17:38:20 +01:00
if (use)
2017-01-02 23:01:50 +01:00
setViewport(new OPENGL_WIDGET);
2017-02-12 17:38:20 +01:00
else
2016-12-06 01:48:26 +01:00
setViewport(new QWidget);
}