1
0
mirror of https://github.com/tumic0/GPXSee.git synced 2024-10-07 07:13:21 +02:00
GPXSee/src/routeitem.cpp

159 lines
3.2 KiB
C++
Raw Normal View History

#include <QApplication>
2016-08-09 01:16:19 +02:00
#include <QPainter>
#include "ll.h"
#include "misc.h"
2016-08-09 01:16:19 +02:00
#include "waypoint.h"
#include "waypointitem.h"
#include "tooltip.h"
2016-08-09 01:16:19 +02:00
#include "routeitem.h"
2016-09-10 13:11:46 +02:00
#define ROUTE_WIDTH 3
2016-09-12 02:27:30 +02:00
#define HOVER_WIDTH 4
2016-08-09 01:16:19 +02:00
QString RouteItem::toolTip()
{
ToolTip tt;
tt.insert(qApp->translate("RouteItem", "Distance"),
::distance(_distance, _units));
return tt.toString();
}
void RouteItem::updateShape()
{
QPainterPathStroker s;
2016-09-10 13:11:46 +02:00
s.setWidth(HOVER_WIDTH * 1.0/scale());
_shape = s.createStroke(_path);
if (qMax(boundingRect().width(), boundingRect().height()) * scale() <= 768)
setCacheMode(QGraphicsItem::DeviceCoordinateCache);
else
setCacheMode(QGraphicsItem::NoCache);
}
2016-08-09 01:16:19 +02:00
RouteItem::RouteItem(const Route &route, QGraphicsItem *parent)
2016-09-19 00:56:10 +02:00
: PathItem(parent)
2016-08-09 01:16:19 +02:00
{
WaypointItem *wi;
QVector<Waypoint> r = route.route();
2016-08-09 01:16:19 +02:00
Q_ASSERT(r.count() >= 2);
wi = new WaypointItem(r.at(0));
wi->setParentItem(this);
const QPointF &p = r.at(0).coordinates();
_path.moveTo(ll2mercator(QPointF(p.x(), -p.y())));
for (int i = 1; i < r.size(); i++) {
const QPointF &p = r.at(i).coordinates();
_path.lineTo(ll2mercator(QPointF(p.x(), -p.y())));
wi = new WaypointItem(r.at(i));
wi->setParentItem(this);
}
_units = Metric;
_distance = route.distance();
setToolTip(toolTip());
setCursor(Qt::ArrowCursor);
2016-09-10 13:11:46 +02:00
setAcceptHoverEvents(true);
updateShape();
2016-08-09 01:16:19 +02:00
QBrush brush(Qt::SolidPattern);
_pen = QPen(brush, ROUTE_WIDTH, Qt::DotLine);
_marker = new MarkerItem(this);
_marker->setPos(_path.pointAtPercent(0));
}
void RouteItem::paint(QPainter *painter, const QStyleOptionGraphicsItem *option,
QWidget *widget)
{
Q_UNUSED(option);
Q_UNUSED(widget);
painter->setPen(_pen);
painter->drawPath(_path);
/*
QPen p = QPen(QBrush(Qt::red), 0);
2016-09-10 13:11:46 +02:00
painter->setPen(p);
2016-08-09 01:16:19 +02:00
painter->drawRect(boundingRect());
*/
}
void RouteItem::setScale(qreal scale)
{
prepareGeometryChange();
_pen.setWidthF(ROUTE_WIDTH * 1.0/scale);
QGraphicsItem::setScale(scale);
QList<QGraphicsItem *> childs = childItems();
for (int i = 0; i < childs.count(); i++)
childs.at(i)->setScale(1.0/scale);
updateShape();
2016-08-09 01:16:19 +02:00
}
void RouteItem::setColor(const QColor &color)
{
_pen.setColor(color);
update();
}
void RouteItem::setUnits(enum Units units)
{
_units = units;
setToolTip(toolTip());
}
void RouteItem::moveMarker(qreal distance)
2016-08-09 01:16:19 +02:00
{
if (distance > _distance)
_marker->setVisible(false);
else {
_marker->setVisible(true);
_marker->setPos(_path.pointAtPercent(distance / _distance));
}
2016-08-09 01:16:19 +02:00
}
2016-08-09 10:47:49 +02:00
void RouteItem::showWaypoints(bool show)
{
QList<QGraphicsItem *> childs = childItems();
for (int i = 0; i < childs.count(); i++)
if (childs.at(i) != _marker)
childs.at(i)->setVisible(show);
}
void RouteItem::showWaypointLabels(bool show)
{
QList<QGraphicsItem *> childs = childItems();
for (int i = 0; i < childs.count(); i++) {
if (childs.at(i) != _marker) {
WaypointItem *wi = static_cast<WaypointItem*>(childs.at(i));
wi->showLabel(show);
}
}
}
2016-09-10 13:11:46 +02:00
void RouteItem::hoverEnterEvent(QGraphicsSceneHoverEvent *event)
{
Q_UNUSED(event);
_pen.setWidthF(HOVER_WIDTH * 1.0/scale());
2016-09-11 13:43:33 +02:00
setZValue(3.0);
2016-09-10 13:11:46 +02:00
update();
}
void RouteItem::hoverLeaveEvent(QGraphicsSceneHoverEvent *event)
{
Q_UNUSED(event);
_pen.setWidthF(ROUTE_WIDTH * 1.0/scale());
setZValue(0);
update();
}