2016-08-02 20:46:22 +02:00
|
|
|
#include <QApplication>
|
2016-07-28 00:23:22 +02:00
|
|
|
#include <QCursor>
|
2016-08-09 01:16:19 +02:00
|
|
|
#include <QPainter>
|
2016-07-28 00:23:22 +02:00
|
|
|
#include "ll.h"
|
|
|
|
#include "misc.h"
|
2016-08-02 00:28:56 +02:00
|
|
|
#include "tooltip.h"
|
2016-07-28 00:23:22 +02:00
|
|
|
#include "trackitem.h"
|
|
|
|
|
|
|
|
|
2016-09-10 13:11:46 +02:00
|
|
|
#define TRACK_WIDTH 3
|
2016-09-12 02:27:30 +02:00
|
|
|
#define HOVER_WIDTH 4
|
2016-07-28 00:23:22 +02:00
|
|
|
|
|
|
|
QString TrackItem::toolTip()
|
|
|
|
{
|
2016-08-02 00:28:56 +02:00
|
|
|
ToolTip tt;
|
2016-07-28 00:23:22 +02:00
|
|
|
|
2016-08-02 20:46:22 +02:00
|
|
|
tt.insert(qApp->translate("TrackItem", "Distance"),
|
|
|
|
::distance(_distance, _units));
|
2016-08-02 00:28:56 +02:00
|
|
|
if (_time > 0)
|
2016-08-02 20:46:22 +02:00
|
|
|
tt.insert(qApp->translate("TrackItem", "Time"), ::timeSpan(_time));
|
2016-08-02 00:28:56 +02:00
|
|
|
if (!_date.isNull())
|
2016-08-02 20:46:22 +02:00
|
|
|
tt.insert(qApp->translate("TrackItem", "Date"),
|
|
|
|
_date.toString(Qt::SystemLocaleShortDate));
|
2016-08-02 00:28:56 +02:00
|
|
|
|
|
|
|
return tt.toString();
|
2016-07-28 00:23:22 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
void TrackItem::updateShape()
|
|
|
|
{
|
|
|
|
QPainterPathStroker s;
|
2016-09-10 13:11:46 +02:00
|
|
|
s.setWidth(HOVER_WIDTH * 1.0/scale());
|
2016-08-10 22:16:39 +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-07-28 00:23:22 +02:00
|
|
|
}
|
|
|
|
|
2016-08-02 00:28:56 +02:00
|
|
|
TrackItem::TrackItem(const Track &track, QGraphicsItem *parent)
|
2016-09-19 00:56:10 +02:00
|
|
|
: PathItem(parent)
|
2016-07-28 00:23:22 +02:00
|
|
|
{
|
2016-08-15 08:20:27 +02:00
|
|
|
const QVector<Trackpoint> &t = track.track();
|
2016-07-28 00:23:22 +02:00
|
|
|
Q_ASSERT(t.count() >= 2);
|
|
|
|
|
2016-08-15 08:20:27 +02:00
|
|
|
const QPointF &p = t.at(0).coordinates();
|
2016-08-09 01:16:19 +02:00
|
|
|
_path.moveTo(ll2mercator(QPointF(p.x(), -p.y())));
|
2016-07-28 00:23:22 +02:00
|
|
|
for (int i = 1; i < t.size(); i++) {
|
2016-08-15 08:20:27 +02:00
|
|
|
const QPointF &p = t.at(i).coordinates();
|
2016-08-09 01:16:19 +02:00
|
|
|
_path.lineTo(ll2mercator(QPointF(p.x(), -p.y())));
|
2016-07-28 00:23:22 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
_units = Metric;
|
|
|
|
_date = track.date();
|
|
|
|
_distance = track.distance();
|
|
|
|
_time = track.time();
|
|
|
|
|
|
|
|
setToolTip(toolTip());
|
|
|
|
setCursor(Qt::ArrowCursor);
|
2016-09-10 13:11:46 +02:00
|
|
|
setAcceptHoverEvents(true);
|
2016-07-28 00:23:22 +02:00
|
|
|
|
|
|
|
updateShape();
|
|
|
|
|
|
|
|
QBrush brush(Qt::SolidPattern);
|
2016-08-09 01:16:19 +02:00
|
|
|
_pen = QPen(brush, TRACK_WIDTH);
|
|
|
|
|
|
|
|
_marker = new MarkerItem(this);
|
|
|
|
_marker->setPos(_path.pointAtPercent(0));
|
|
|
|
}
|
|
|
|
|
|
|
|
void TrackItem::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());
|
|
|
|
*/
|
2016-07-28 00:23:22 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
void TrackItem::setScale(qreal scale)
|
|
|
|
{
|
2016-08-09 01:16:19 +02:00
|
|
|
prepareGeometryChange();
|
|
|
|
|
|
|
|
_pen.setWidthF(TRACK_WIDTH * 1.0/scale);
|
|
|
|
QGraphicsItem::setScale(scale);
|
|
|
|
_marker->setScale(1.0/scale);
|
2016-08-02 23:56:50 +02:00
|
|
|
|
2016-08-09 01:16:19 +02:00
|
|
|
updateShape();
|
2016-07-28 00:23:22 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
void TrackItem::setColor(const QColor &color)
|
|
|
|
{
|
2016-08-09 01:16:19 +02:00
|
|
|
_pen.setColor(color);
|
|
|
|
update();
|
2016-07-28 00:23:22 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
void TrackItem::setUnits(enum Units units)
|
|
|
|
{
|
|
|
|
_units = units;
|
|
|
|
setToolTip(toolTip());
|
|
|
|
}
|
2016-08-09 01:16:19 +02:00
|
|
|
|
2016-08-10 20:35:39 +02:00
|
|
|
void TrackItem::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-09-10 13:11:46 +02:00
|
|
|
|
|
|
|
void TrackItem::hoverEnterEvent(QGraphicsSceneHoverEvent *event)
|
|
|
|
{
|
|
|
|
Q_UNUSED(event);
|
|
|
|
|
|
|
|
_pen.setWidthF(HOVER_WIDTH * 1.0/scale());
|
2016-09-19 01:45:28 +02:00
|
|
|
setZValue(zValue() + 1.0);
|
2016-09-10 13:11:46 +02:00
|
|
|
update();
|
2016-09-22 20:59:13 +02:00
|
|
|
|
|
|
|
emit selected(true);
|
2016-09-10 13:11:46 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
void TrackItem::hoverLeaveEvent(QGraphicsSceneHoverEvent *event)
|
|
|
|
{
|
|
|
|
Q_UNUSED(event);
|
|
|
|
|
|
|
|
_pen.setWidthF(TRACK_WIDTH * 1.0/scale());
|
2016-09-19 01:45:28 +02:00
|
|
|
setZValue(zValue() - 1.0);
|
2016-09-10 13:11:46 +02:00
|
|
|
update();
|
2016-09-22 20:59:13 +02:00
|
|
|
|
|
|
|
emit selected(false);
|
2016-09-10 13:11:46 +02:00
|
|
|
}
|