2016-02-11 20:58:52 +01:00
|
|
|
#include <QGraphicsView>
|
|
|
|
#include <QGraphicsScene>
|
|
|
|
#include <QPainterPath>
|
|
|
|
#include <QWheelEvent>
|
2016-07-28 00:23:22 +02:00
|
|
|
#include "ll.h"
|
2016-02-12 10:23:14 +01:00
|
|
|
#include "poi.h"
|
|
|
|
#include "gpx.h"
|
2016-02-12 10:09:17 +01:00
|
|
|
#include "map.h"
|
2016-07-28 00:23:22 +02:00
|
|
|
#include "trackitem.h"
|
2016-02-19 21:34:55 +01:00
|
|
|
#include "waypointitem.h"
|
2016-02-11 20:58:52 +01:00
|
|
|
#include "markeritem.h"
|
|
|
|
#include "scaleitem.h"
|
|
|
|
#include "trackview.h"
|
|
|
|
|
|
|
|
|
|
|
|
#define MARGIN 10.0
|
|
|
|
#define SCALE_OFFSET 7
|
|
|
|
|
|
|
|
TrackView::TrackView(QWidget *parent)
|
|
|
|
: QGraphicsView(parent)
|
|
|
|
{
|
|
|
|
_scene = new QGraphicsScene(this);
|
|
|
|
setScene(_scene);
|
|
|
|
setCacheMode(QGraphicsView::CacheBackground);
|
|
|
|
setDragMode(QGraphicsView::ScrollHandDrag);
|
|
|
|
setViewportUpdateMode(QGraphicsView::FullViewportUpdate);
|
|
|
|
setVerticalScrollBarPolicy(Qt::ScrollBarAlwaysOff);
|
|
|
|
setHorizontalScrollBarPolicy(Qt::ScrollBarAlwaysOff);
|
2016-02-28 10:58:25 +01:00
|
|
|
setRenderHints(QPainter::Antialiasing);
|
2016-02-11 20:58:52 +01:00
|
|
|
|
|
|
|
_mapScale = new ScaleItem();
|
|
|
|
_mapScale->setZValue(2.0);
|
|
|
|
|
2016-03-15 01:20:24 +01:00
|
|
|
_zoom = ZOOM_MAX;
|
|
|
|
_scale = mapScale(_zoom);
|
2016-02-11 20:58:52 +01:00
|
|
|
_map = 0;
|
2016-03-21 22:37:55 +01:00
|
|
|
_maxPath = 0;
|
|
|
|
_maxDistance = 0;
|
2016-05-19 01:10:40 +02:00
|
|
|
|
|
|
|
_plot = false;
|
2016-07-25 19:32:36 +02:00
|
|
|
_units = Metric;
|
2016-02-11 20:58:52 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
TrackView::~TrackView()
|
|
|
|
{
|
2016-05-12 09:03:05 +02:00
|
|
|
if (_mapScale->scene() != _scene)
|
|
|
|
delete _mapScale;
|
2016-02-11 20:58:52 +01:00
|
|
|
}
|
|
|
|
|
2016-07-28 00:23:22 +02:00
|
|
|
void TrackView::addTrack(const Track &track)
|
2016-02-11 20:58:52 +01:00
|
|
|
{
|
2016-07-28 00:23:22 +02:00
|
|
|
if (track.isNull()) {
|
2016-03-23 20:49:40 +01:00
|
|
|
_palette.color();
|
2016-03-03 09:15:56 +01:00
|
|
|
return;
|
2016-03-23 20:49:40 +01:00
|
|
|
}
|
2016-02-11 20:58:52 +01:00
|
|
|
|
2016-07-28 00:23:22 +02:00
|
|
|
TrackItem *pi = new TrackItem(track);
|
2016-03-17 20:56:40 +01:00
|
|
|
_paths.append(pi);
|
2016-03-15 01:20:24 +01:00
|
|
|
_zoom = qMin(_zoom, scale2zoom(trackScale()));
|
|
|
|
_scale = mapScale(_zoom);
|
2016-03-03 09:15:56 +01:00
|
|
|
pi->setScale(1.0/_scale);
|
2016-07-28 00:23:22 +02:00
|
|
|
pi->setColor(_palette.color());
|
2016-03-03 09:15:56 +01:00
|
|
|
_scene->addItem(pi);
|
2016-02-11 20:58:52 +01:00
|
|
|
|
2016-07-28 00:23:22 +02:00
|
|
|
MarkerItem *mi = new MarkerItem(pi);
|
2016-03-03 09:15:56 +01:00
|
|
|
_markers.append(mi);
|
|
|
|
mi->setPos(pi->path().pointAtPercent(0));
|
|
|
|
mi->setScale(_scale);
|
2016-07-28 00:23:22 +02:00
|
|
|
|
|
|
|
_maxPath = qMax(pi->path().length(), _maxPath);
|
|
|
|
_maxDistance = qMax(track.distance(), _maxDistance);
|
2016-03-03 09:15:56 +01:00
|
|
|
}
|
2016-02-11 20:58:52 +01:00
|
|
|
|
2016-03-15 01:20:24 +01:00
|
|
|
void TrackView::addWaypoints(const QList<Waypoint> &waypoints)
|
|
|
|
{
|
|
|
|
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
|
|
|
|
2016-07-28 00:23:22 +02:00
|
|
|
WaypointItem *wi = new WaypointItem(w);
|
2016-08-02 00:28:56 +02:00
|
|
|
wi->setScale(1.0/_scale);
|
2016-03-15 01:20:24 +01:00
|
|
|
wi->setZValue(1);
|
|
|
|
_scene->addItem(wi);
|
|
|
|
|
2016-07-28 00:23:22 +02:00
|
|
|
_waypoints.append(wi);
|
2016-03-15 01:20:24 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
_zoom = qMin(_zoom, scale2zoom(waypointScale()));
|
|
|
|
_scale = mapScale(_zoom);
|
|
|
|
}
|
|
|
|
|
2016-03-03 09:15:56 +01:00
|
|
|
void TrackView::loadGPX(const GPX &gpx)
|
|
|
|
{
|
2016-03-15 01:20:24 +01:00
|
|
|
int zoom = _zoom;
|
2016-02-11 20:58:52 +01:00
|
|
|
|
2016-07-28 00:23:22 +02:00
|
|
|
for (int i = 0; i < gpx.trackCount(); i++)
|
|
|
|
addTrack(gpx.track(i));
|
2016-03-15 01:20:24 +01:00
|
|
|
addWaypoints(gpx.waypoints());
|
|
|
|
|
2016-07-28 00:23:22 +02:00
|
|
|
if (_paths.empty() && _waypoints.empty())
|
2016-03-03 09:15:56 +01:00
|
|
|
return;
|
2016-03-15 01:20:24 +01:00
|
|
|
|
2016-03-17 20:56:40 +01:00
|
|
|
if ((_paths.size() > 1 && _zoom < zoom)
|
2016-07-28 00:23:22 +02:00
|
|
|
|| (_waypoints.size() && _zoom < zoom))
|
2016-03-03 09:15:56 +01:00
|
|
|
rescale(_scale);
|
2016-02-11 20:58:52 +01:00
|
|
|
|
2016-03-15 01:20:24 +01:00
|
|
|
QRectF br = trackBoundingRect() | waypointBoundingRect();
|
2016-03-03 09:15:56 +01:00
|
|
|
QRectF ba = br.adjusted(-TILE_SIZE, -TILE_SIZE, TILE_SIZE, TILE_SIZE);
|
|
|
|
_scene->setSceneRect(ba);
|
|
|
|
centerOn(ba.center());
|
2016-02-11 20:58:52 +01:00
|
|
|
|
2016-03-03 09:15:56 +01:00
|
|
|
_mapScale->setLatitude(-(br.center().ry() * _scale));
|
|
|
|
_mapScale->setZoom(_zoom);
|
|
|
|
if (_mapScale->scene() != _scene)
|
|
|
|
_scene->addItem(_mapScale);
|
2016-02-11 20:58:52 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
QRectF TrackView::trackBoundingRect() const
|
|
|
|
{
|
2016-03-17 20:56:40 +01:00
|
|
|
if (_paths.empty())
|
2016-03-03 09:15:56 +01:00
|
|
|
return QRectF();
|
|
|
|
|
2016-03-17 20:56:40 +01:00
|
|
|
QRectF br = _paths.at(0)->sceneBoundingRect();
|
|
|
|
for (int i = 1; i < _paths.size(); i++)
|
|
|
|
br |= _paths.at(i)->sceneBoundingRect();
|
2016-02-11 20:58:52 +01:00
|
|
|
|
2016-03-17 20:56:40 +01:00
|
|
|
return br;
|
2016-02-11 20:58:52 +01:00
|
|
|
}
|
|
|
|
|
2016-03-15 01:20:24 +01:00
|
|
|
QRectF TrackView::waypointBoundingRect() const
|
|
|
|
{
|
|
|
|
qreal bottom, top, left, right;
|
|
|
|
|
2016-07-28 00:23:22 +02:00
|
|
|
if (_waypoints.empty())
|
2016-03-15 01:20:24 +01:00
|
|
|
return QRectF();
|
|
|
|
|
2016-07-28 00:23:22 +02:00
|
|
|
const QPointF &p = _waypoints.at(0)->pos();
|
2016-03-17 20:56:40 +01:00
|
|
|
bottom = p.y();
|
|
|
|
top = p.y();
|
|
|
|
left = p.x();
|
|
|
|
right = p.x();
|
|
|
|
|
2016-07-28 00:23:22 +02:00
|
|
|
for (int i = 1; i < _waypoints.size(); i++) {
|
|
|
|
const QPointF &p = _waypoints.at(i)->pos();
|
2016-03-17 20:56:40 +01:00
|
|
|
bottom = qMax(bottom, p.y());
|
|
|
|
top = qMin(top, p.y());
|
|
|
|
right = qMax(right, p.x());
|
|
|
|
left = qMin(left, p.x());
|
2016-03-15 01:20:24 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
return QRectF(QPointF(left, top), QPointF(right, bottom));
|
|
|
|
}
|
|
|
|
|
2016-02-11 20:58:52 +01:00
|
|
|
qreal TrackView::trackScale() const
|
|
|
|
{
|
2016-03-17 20:56:40 +01:00
|
|
|
if (_paths.empty())
|
2016-03-15 01:20:24 +01:00
|
|
|
return mapScale(ZOOM_MAX);
|
|
|
|
|
2016-03-17 20:56:40 +01:00
|
|
|
QRectF br = _paths.at(0)->path().boundingRect();
|
2016-02-11 20:58:52 +01:00
|
|
|
|
2016-03-17 20:56:40 +01:00
|
|
|
for (int i = 1; i < _paths.size(); i++)
|
|
|
|
br |= _paths.at(i)->path().boundingRect();
|
2016-02-11 20:58:52 +01:00
|
|
|
|
|
|
|
QPointF sc(br.width() / (viewport()->width() - MARGIN/2),
|
|
|
|
br.height() / (viewport()->height() - MARGIN/2));
|
|
|
|
|
|
|
|
return qMax(sc.x(), sc.y());
|
|
|
|
}
|
|
|
|
|
2016-03-15 01:20:24 +01:00
|
|
|
qreal TrackView::waypointScale() const
|
2016-02-11 20:58:52 +01:00
|
|
|
{
|
2016-03-15 01:20:24 +01:00
|
|
|
qreal bottom, top, left, right;
|
|
|
|
|
2016-07-28 00:23:22 +02:00
|
|
|
if (_waypoints.size() < 2)
|
2016-03-15 01:20:24 +01:00
|
|
|
return mapScale(ZOOM_MAX);
|
|
|
|
|
2016-07-28 00:23:22 +02:00
|
|
|
const QPointF &p = _waypoints.at(0)->coordinates();
|
2016-03-17 20:56:40 +01:00
|
|
|
bottom = p.y();
|
|
|
|
top = p.y();
|
|
|
|
left = p.x();
|
|
|
|
right = p.x();
|
|
|
|
|
2016-07-28 00:23:22 +02:00
|
|
|
for (int i = 1; i < _waypoints.size(); i++) {
|
|
|
|
const QPointF &p = _waypoints.at(i)->coordinates();
|
2016-03-17 20:56:40 +01:00
|
|
|
bottom = qMax(bottom, p.y());
|
|
|
|
top = qMin(top, p.y());
|
|
|
|
right = qMax(right, p.x());
|
|
|
|
left = qMin(left, p.x());
|
2016-03-15 01:20:24 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
QRectF br(QPointF(left, top), QPointF(right, bottom));
|
|
|
|
QPointF sc(br.width() / (viewport()->width() - MARGIN/2),
|
|
|
|
br.height() / (viewport()->height() - MARGIN/2));
|
|
|
|
|
|
|
|
return qMax(sc.x(), sc.y());
|
|
|
|
}
|
|
|
|
|
|
|
|
qreal TrackView::mapScale(int zoom) const
|
|
|
|
{
|
|
|
|
return ((360.0/(qreal)(1<<zoom))/(qreal)TILE_SIZE);
|
2016-02-11 20:58:52 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
void TrackView::rescale(qreal scale)
|
|
|
|
{
|
2016-03-17 20:56:40 +01:00
|
|
|
for (int i = 0; i < _paths.size(); i++) {
|
2016-02-11 20:58:52 +01:00
|
|
|
_markers.at(i)->setScale(scale);
|
2016-03-17 20:56:40 +01:00
|
|
|
_paths.at(i)->setScale(1.0/scale);
|
2016-02-11 20:58:52 +01:00
|
|
|
}
|
|
|
|
|
2016-07-28 00:23:22 +02:00
|
|
|
for (int i = 0; i < _waypoints.size(); i++)
|
2016-08-02 00:28:56 +02:00
|
|
|
_waypoints.at(i)->setScale(1.0/scale);
|
2016-03-15 01:20:24 +01:00
|
|
|
|
2016-02-19 21:42:54 +01:00
|
|
|
QHash<Waypoint, WaypointItem*>::const_iterator it, jt;
|
2016-02-11 20:58:52 +01:00
|
|
|
for (it = _pois.constBegin(); it != _pois.constEnd(); it++) {
|
2016-08-02 00:28:56 +02:00
|
|
|
it.value()->setScale(1.0/scale);
|
2016-02-11 20:58:52 +01:00
|
|
|
it.value()->show();
|
|
|
|
}
|
|
|
|
|
|
|
|
for (it = _pois.constBegin(); it != _pois.constEnd(); it++) {
|
|
|
|
for (jt = _pois.constBegin(); jt != _pois.constEnd(); jt++) {
|
|
|
|
if (it != jt && it.value()->isVisible() && jt.value()->isVisible()
|
|
|
|
&& it.value()->collidesWithItem(jt.value()))
|
|
|
|
jt.value()->hide();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
_scale = scale;
|
|
|
|
}
|
|
|
|
|
2016-03-17 20:56:40 +01:00
|
|
|
void TrackView::addPOI(const QVector<Waypoint> &waypoints)
|
2016-02-11 20:58:52 +01:00
|
|
|
{
|
2016-03-17 20:56:40 +01:00
|
|
|
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))
|
2016-03-17 20:56:40 +01:00
|
|
|
continue;
|
2016-02-11 20:58:52 +01:00
|
|
|
|
2016-07-28 00:23:22 +02:00
|
|
|
WaypointItem *pi = new WaypointItem(w);
|
2016-08-02 00:28:56 +02:00
|
|
|
pi->setScale(1.0/_scale);
|
2016-03-17 20:56:40 +01:00
|
|
|
pi->setZValue(1);
|
|
|
|
_scene->addItem(pi);
|
2016-02-11 20:58:52 +01:00
|
|
|
|
2016-03-19 09:06:43 +01:00
|
|
|
_pois.insert(w, pi);
|
2016-03-17 20:56:40 +01:00
|
|
|
}
|
|
|
|
}
|
2016-02-11 20:58:52 +01:00
|
|
|
|
2016-03-17 20:56:40 +01:00
|
|
|
void TrackView::loadPOI(const POI &poi)
|
|
|
|
{
|
|
|
|
QHash<Waypoint, WaypointItem*>::const_iterator it,jt;
|
2016-03-13 17:44:25 +01:00
|
|
|
|
2016-07-28 00:23:22 +02:00
|
|
|
if (!_paths.size() && !_waypoints.size())
|
2016-03-17 20:56:40 +01:00
|
|
|
return;
|
2016-02-11 20:58:52 +01:00
|
|
|
|
2016-07-28 00:23:22 +02:00
|
|
|
for (int i = 0; i < _paths.size(); i++)
|
|
|
|
addPOI(poi.points(_paths.at(i)->path()));
|
2016-03-17 20:56:40 +01:00
|
|
|
addPOI(poi.points(_waypoints));
|
2016-02-11 20:58:52 +01:00
|
|
|
|
|
|
|
for (it = _pois.constBegin(); it != _pois.constEnd(); it++) {
|
|
|
|
for (jt = _pois.constBegin(); jt != _pois.constEnd(); jt++) {
|
|
|
|
if (it != jt && it.value()->isVisible() && jt.value()->isVisible()
|
|
|
|
&& it.value()->collidesWithItem(jt.value()))
|
|
|
|
jt.value()->hide();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void TrackView::setMap(Map *map)
|
|
|
|
{
|
|
|
|
_map = map;
|
|
|
|
if (_map)
|
2016-05-19 01:10:40 +02:00
|
|
|
connect(_map, SIGNAL(loaded()), this, SLOT(redraw()),
|
|
|
|
Qt::UniqueConnection);
|
2016-02-11 20:58:52 +01:00
|
|
|
resetCachedContent();
|
|
|
|
}
|
|
|
|
|
|
|
|
void TrackView::setUnits(enum Units units)
|
|
|
|
{
|
2016-07-25 19:32:36 +02:00
|
|
|
_units = units;
|
|
|
|
|
2016-02-11 20:58:52 +01:00
|
|
|
_mapScale->setUnits(units);
|
2016-07-25 19:32:36 +02:00
|
|
|
|
2016-07-28 00:23:22 +02:00
|
|
|
for (int i = 0; i < _paths.count(); i++)
|
|
|
|
_paths[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-02-11 20:58:52 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
void TrackView::redraw()
|
|
|
|
{
|
|
|
|
resetCachedContent();
|
|
|
|
}
|
|
|
|
|
2016-04-05 09:10:19 +02:00
|
|
|
void TrackView::rescale()
|
|
|
|
{
|
|
|
|
_zoom = qMin(scale2zoom(trackScale()), scale2zoom(waypointScale()));
|
|
|
|
rescale(mapScale(_zoom));
|
|
|
|
_mapScale->setZoom(_zoom);
|
|
|
|
}
|
|
|
|
|
2016-07-21 22:39:53 +02:00
|
|
|
void TrackView::zoom(int z, const QPointF &pos)
|
2016-02-11 20:58:52 +01:00
|
|
|
{
|
2016-07-28 00:23:22 +02:00
|
|
|
if (_paths.isEmpty() && _waypoints.isEmpty())
|
2016-02-11 20:58:52 +01:00
|
|
|
return;
|
|
|
|
|
|
|
|
qreal scale = _scale;
|
2016-07-21 22:39:53 +02:00
|
|
|
_zoom = z;
|
2016-02-11 20:58:52 +01:00
|
|
|
|
2016-03-15 01:20:24 +01:00
|
|
|
rescale(mapScale(_zoom));
|
|
|
|
QRectF br = trackBoundingRect() | waypointBoundingRect();
|
2016-02-11 20:58:52 +01:00
|
|
|
QRectF ba = br.adjusted(-TILE_SIZE, -TILE_SIZE, TILE_SIZE, TILE_SIZE);
|
|
|
|
_scene->setSceneRect(ba);
|
|
|
|
|
|
|
|
if (br.width() < viewport()->size().width()
|
|
|
|
&& br.height() < viewport()->size().height())
|
|
|
|
centerOn(br.center());
|
|
|
|
else
|
|
|
|
centerOn(pos * scale/_scale);
|
|
|
|
|
|
|
|
_mapScale->setZoom(_zoom);
|
|
|
|
|
|
|
|
resetCachedContent();
|
|
|
|
}
|
|
|
|
|
2016-07-21 22:39:53 +02:00
|
|
|
void TrackView::wheelEvent(QWheelEvent *event)
|
|
|
|
{
|
2016-07-28 00:23:22 +02:00
|
|
|
if (_paths.isEmpty() && _waypoints.isEmpty())
|
2016-07-21 22:39:53 +02:00
|
|
|
return;
|
|
|
|
|
|
|
|
QPointF pos = mapToScene(event->pos());
|
|
|
|
int z = (event->delta() > 0) ?
|
|
|
|
qMin(_zoom + 1, ZOOM_MAX) : qMax(_zoom - 1, ZOOM_MIN);
|
|
|
|
|
|
|
|
zoom(z, pos);
|
|
|
|
}
|
|
|
|
|
|
|
|
void TrackView::keyPressEvent(QKeyEvent *event)
|
|
|
|
{
|
|
|
|
int z = -1;
|
|
|
|
|
|
|
|
if (event->matches(QKeySequence::ZoomIn))
|
|
|
|
z = qMin(_zoom + 1, ZOOM_MAX);
|
|
|
|
if (event->matches(QKeySequence::ZoomOut))
|
|
|
|
z = qMax(_zoom - 1, ZOOM_MIN);
|
|
|
|
|
|
|
|
if (z >= 0)
|
|
|
|
zoom(z, mapToScene(QRect(QPoint(), size()).center()));
|
|
|
|
else
|
|
|
|
QWidget::keyPressEvent(event);
|
|
|
|
}
|
|
|
|
|
2016-02-11 20:58:52 +01:00
|
|
|
void TrackView::plot(QPainter *painter, const QRectF &target)
|
|
|
|
{
|
2016-05-19 01:10:40 +02:00
|
|
|
QRect orig, adj;
|
2016-02-11 20:58:52 +01:00
|
|
|
qreal ratio, diff;
|
|
|
|
|
2016-05-20 22:44:03 +02:00
|
|
|
|
2016-05-15 13:19:07 +02:00
|
|
|
orig = viewport()->rect();
|
2016-02-11 20:58:52 +01:00
|
|
|
|
2016-05-27 22:45:58 +02:00
|
|
|
if (orig.height() * (target.width() / target.height()) - orig.width() < 0) {
|
2016-02-11 20:58:52 +01:00
|
|
|
ratio = target.height()/target.width();
|
2016-05-27 22:45:58 +02:00
|
|
|
diff = (orig.width() * ratio) - orig.height();
|
2016-02-11 20:58:52 +01:00
|
|
|
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-02-11 20:58:52 +01:00
|
|
|
}
|
|
|
|
|
2016-05-20 22:44:03 +02:00
|
|
|
setUpdatesEnabled(false);
|
2016-05-19 01:10:40 +02:00
|
|
|
_plot = 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())))));
|
|
|
|
|
2016-05-19 01:10:40 +02:00
|
|
|
render(painter, target, adj);
|
2016-05-20 22:44:03 +02:00
|
|
|
|
|
|
|
_mapScale->setPos(pos);
|
|
|
|
|
2016-05-19 01:10:40 +02:00
|
|
|
_plot = false;
|
2016-05-20 22:44:03 +02:00
|
|
|
setUpdatesEnabled(true);
|
2016-02-11 20:58:52 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
void TrackView::clearPOI()
|
|
|
|
{
|
2016-02-19 21:42:54 +01:00
|
|
|
QHash<Waypoint, WaypointItem*>::const_iterator it;
|
2016-02-11 20:58:52 +01:00
|
|
|
|
|
|
|
for (it = _pois.constBegin(); it != _pois.constEnd(); it++) {
|
|
|
|
_scene->removeItem(it.value());
|
|
|
|
delete it.value();
|
|
|
|
}
|
|
|
|
|
|
|
|
_pois.clear();
|
|
|
|
}
|
|
|
|
|
|
|
|
void TrackView::clear()
|
|
|
|
{
|
|
|
|
if (_mapScale->scene() == _scene)
|
|
|
|
_scene->removeItem(_mapScale);
|
|
|
|
|
|
|
|
_pois.clear();
|
2016-03-17 20:56:40 +01:00
|
|
|
_paths.clear();
|
2016-07-28 00:23:22 +02:00
|
|
|
_waypoints.clear();
|
2016-02-11 20:58:52 +01:00
|
|
|
_markers.clear();
|
|
|
|
_scene->clear();
|
2016-03-02 09:34:39 +01:00
|
|
|
_palette.reset();
|
2016-02-11 20:58:52 +01:00
|
|
|
|
2016-03-21 22:37:55 +01:00
|
|
|
_maxPath = 0;
|
|
|
|
_maxDistance = 0;
|
2016-03-15 01:20:24 +01:00
|
|
|
_zoom = ZOOM_MAX;
|
|
|
|
_scale = mapScale(_zoom);
|
2016-02-11 20:58:52 +01:00
|
|
|
|
2016-03-03 09:15:56 +01:00
|
|
|
_scene->setSceneRect(QRectF());
|
2016-02-11 20:58:52 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
void TrackView::movePositionMarker(qreal val)
|
|
|
|
{
|
2016-03-21 22:37:55 +01:00
|
|
|
qreal mp = val / _maxDistance;
|
|
|
|
|
2016-03-17 20:56:40 +01:00
|
|
|
for (int i = 0; i < _paths.size(); i++) {
|
2016-03-21 22:37:55 +01:00
|
|
|
qreal f = _maxPath / _paths.at(i)->path().length();
|
2016-03-30 20:50:51 +02:00
|
|
|
if (mp * f < 0 || mp * f > 1.0)
|
2016-03-21 22:37:55 +01:00
|
|
|
_markers.at(i)->setVisible(false);
|
|
|
|
else {
|
|
|
|
QPointF pos = _paths.at(i)->path().pointAtPercent(mp * f);
|
|
|
|
_markers.at(i)->setPos(pos);
|
|
|
|
_markers.at(i)->setVisible(true);
|
|
|
|
}
|
2016-02-11 20:58:52 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void TrackView::drawBackground(QPainter *painter, const QRectF &rect)
|
|
|
|
{
|
2016-07-28 00:23:22 +02:00
|
|
|
if ((_paths.isEmpty() && _waypoints.isEmpty()) || !_map) {
|
2016-02-11 20:58:52 +01:00
|
|
|
painter->fillRect(rect, Qt::white);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
QRectF rr(rect.topLeft() * _scale, rect.size());
|
|
|
|
QPoint tile = mercator2tile(QPointF(rr.topLeft().x(), -rr.topLeft().y()),
|
|
|
|
_zoom);
|
|
|
|
QPointF tm = tile2mercator(tile, _zoom);
|
2016-05-15 13:19:07 +02:00
|
|
|
QPoint tl = mapToScene(mapFromScene(QPointF(tm.x() / _scale,
|
|
|
|
-tm.y() / _scale))).toPoint();
|
2016-02-11 20:58:52 +01:00
|
|
|
|
|
|
|
QList<Tile> tiles;
|
|
|
|
for (int i = 0; i <= rr.size().width() / TILE_SIZE + 1; i++) {
|
|
|
|
for (int j = 0; j <= rr.size().height() / TILE_SIZE + 1; j++) {
|
|
|
|
tiles.append(Tile(QPoint(tile.x() + i, tile.y() + j), _zoom));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-05-19 01:10:40 +02:00
|
|
|
_map->loadTiles(tiles, _plot);
|
2016-02-11 20:58:52 +01:00
|
|
|
|
|
|
|
for (int i = 0; i < tiles.count(); i++) {
|
|
|
|
Tile &t = tiles[i];
|
2016-05-19 01:10:40 +02:00
|
|
|
QPoint tp(tl.x() + (t.xy().x() - tile.x()) * TILE_SIZE,
|
|
|
|
tl.y() + (t.xy().y() - tile.y()) * TILE_SIZE);
|
2016-02-11 20:58:52 +01:00
|
|
|
painter->drawPixmap(tp, t.pixmap());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void TrackView::resizeEvent(QResizeEvent *e)
|
|
|
|
{
|
2016-07-28 00:23:22 +02:00
|
|
|
if (_paths.isEmpty() && _waypoints.isEmpty())
|
2016-02-11 20:58:52 +01:00
|
|
|
return;
|
|
|
|
|
2016-04-05 09:10:19 +02:00
|
|
|
rescale();
|
|
|
|
|
2016-03-15 01:20:24 +01:00
|
|
|
QRectF br = trackBoundingRect() | waypointBoundingRect();
|
2016-02-11 20:58:52 +01:00
|
|
|
QRectF ba = br.adjusted(-TILE_SIZE, -TILE_SIZE, TILE_SIZE, TILE_SIZE);
|
|
|
|
|
|
|
|
if (ba.width() < e->size().width()) {
|
|
|
|
qreal diff = e->size().width() - ba.width();
|
|
|
|
ba.adjust(-diff/2, 0, diff/2, 0);
|
|
|
|
}
|
|
|
|
if (ba.height() < e->size().height()) {
|
|
|
|
qreal diff = e->size().height() - ba.height();
|
|
|
|
ba.adjust(0, -diff/2, 0, diff/2);
|
|
|
|
}
|
|
|
|
|
|
|
|
_scene->setSceneRect(ba);
|
|
|
|
|
|
|
|
centerOn(br.center());
|
|
|
|
resetCachedContent();
|
|
|
|
}
|
|
|
|
|
|
|
|
void TrackView::paintEvent(QPaintEvent *e)
|
|
|
|
{
|
2016-05-20 22:44:03 +02:00
|
|
|
QPointF scenePos = mapToScene(rect().bottomRight() + QPoint(
|
|
|
|
-(SCALE_OFFSET + _mapScale->boundingRect().width()),
|
2016-02-11 20:58:52 +01:00
|
|
|
-(SCALE_OFFSET + _mapScale->boundingRect().height())));
|
2016-05-19 01:10:40 +02:00
|
|
|
if (_mapScale->pos() != scenePos && !_plot)
|
2016-02-11 20:58:52 +01:00
|
|
|
_mapScale->setPos(scenePos);
|
|
|
|
|
|
|
|
QGraphicsView::paintEvent(e);
|
|
|
|
}
|