2016-08-30 21:26:28 +02:00
|
|
|
#include <QApplication>
|
2016-08-09 01:16:19 +02:00
|
|
|
#include <QPainter>
|
|
|
|
#include "ll.h"
|
2016-08-30 21:26:28 +02:00
|
|
|
#include "misc.h"
|
2016-08-09 01:16:19 +02:00
|
|
|
#include "waypoint.h"
|
|
|
|
#include "waypointitem.h"
|
2016-08-30 21:26:28 +02:00
|
|
|
#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
|
|
|
|
2016-08-30 21:26:28 +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());
|
2016-08-30 21:26:28 +02:00
|
|
|
_shape = s.createStroke(_path);
|
2016-09-14 22:37:12 +02:00
|
|
|
|
|
|
|
if (qMax(boundingRect().width(), boundingRect().height()) * scale() <= 768)
|
|
|
|
setCacheMode(QGraphicsItem::DeviceCoordinateCache);
|
|
|
|
else
|
|
|
|
setCacheMode(QGraphicsItem::NoCache);
|
2016-08-30 21:26:28 +02:00
|
|
|
}
|
|
|
|
|
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;
|
|
|
|
|
2016-08-10 22:16:39 +02:00
|
|
|
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);
|
|
|
|
}
|
|
|
|
|
2016-08-30 21:26:28 +02:00
|
|
|
_units = Metric;
|
2016-08-10 20:35:39 +02:00
|
|
|
_distance = route.distance();
|
|
|
|
|
2016-08-30 21:26:28 +02:00
|
|
|
setToolTip(toolTip());
|
|
|
|
setCursor(Qt::ArrowCursor);
|
2016-09-10 13:11:46 +02:00
|
|
|
setAcceptHoverEvents(true);
|
2016-08-30 21:26:28 +02:00
|
|
|
|
|
|
|
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);
|
|
|
|
|
|
|
|
/*
|
2016-09-14 22:37:12 +02:00
|
|
|
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);
|
2016-08-30 21:26:28 +02:00
|
|
|
|
|
|
|
updateShape();
|
2016-08-09 01:16:19 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
void RouteItem::setColor(const QColor &color)
|
|
|
|
{
|
|
|
|
_pen.setColor(color);
|
|
|
|
update();
|
|
|
|
}
|
|
|
|
|
2016-08-30 21:26:28 +02:00
|
|
|
void RouteItem::setUnits(enum Units units)
|
|
|
|
{
|
|
|
|
_units = units;
|
|
|
|
setToolTip(toolTip());
|
|
|
|
}
|
|
|
|
|
2016-08-10 20:35:39 +02:00
|
|
|
void RouteItem::moveMarker(qreal distance)
|
2016-08-09 01:16:19 +02:00
|
|
|
{
|
2016-08-10 20:35:39 +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);
|
|
|
|
}
|
2016-08-09 23:08:49 +02:00
|
|
|
|
|
|
|
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();
|
|
|
|
}
|