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

137 lines
2.7 KiB
C++
Raw Normal View History

#include <QApplication>
#include <QCursor>
2016-08-09 01:16:19 +02:00
#include <QPainter>
#include "ll.h"
#include "misc.h"
2016-08-02 00:28:56 +02:00
#include "tooltip.h"
#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
QString TrackItem::toolTip()
{
2016-08-02 00:28:56 +02:00
ToolTip tt;
tt.insert(qApp->translate("TrackItem", "Distance"),
::distance(_distance, _units));
2016-08-02 00:28:56 +02:00
if (_time > 0)
tt.insert(qApp->translate("TrackItem", "Time"), ::timeSpan(_time));
2016-08-02 00:28:56 +02:00
if (!_date.isNull())
tt.insert(qApp->translate("TrackItem", "Date"),
_date.toString(Qt::SystemLocaleShortDate));
2016-08-02 00:28:56 +02:00
return tt.toString();
}
void TrackItem::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-02 00:28:56 +02:00
TrackItem::TrackItem(const Track &track, QGraphicsItem *parent)
2016-08-09 01:16:19 +02:00
: QGraphicsItem(parent)
{
2016-08-15 08:20:27 +02:00
const QVector<Trackpoint> &t = track.track();
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())));
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())));
}
_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);
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);
/*
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 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();
}
void TrackItem::setColor(const QColor &color)
{
2016-08-09 01:16:19 +02:00
_pen.setColor(color);
update();
}
void TrackItem::setUnits(enum Units units)
{
_units = units;
setToolTip(toolTip());
}
2016-08-09 01:16:19 +02:00
void TrackItem::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-09-10 13:11:46 +02:00
void TrackItem::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 TrackItem::hoverLeaveEvent(QGraphicsSceneHoverEvent *event)
{
Q_UNUSED(event);
_pen.setWidthF(TRACK_WIDTH * 1.0/scale());
setZValue(0);
update();
}