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

145 lines
3.2 KiB
C++
Raw Normal View History

#include <QApplication>
2015-10-05 01:43:48 +02:00
#include <QPainter>
2015-10-17 01:33:02 +02:00
#include "config.h"
#include "misc.h"
2016-08-02 00:28:56 +02:00
#include "tooltip.h"
2016-02-19 21:34:55 +01:00
#include "waypointitem.h"
2015-10-05 01:43:48 +02:00
2016-09-10 13:11:46 +02:00
#define POINT_SIZE 8
#define HOVER_SIZE 10
2015-10-05 01:43:48 +02:00
2016-08-02 00:28:56 +02:00
QString WaypointItem::toolTip()
{
2016-08-02 00:28:56 +02:00
ToolTip tt;
2016-08-09 01:16:19 +02:00
if (!_waypoint.name().isEmpty() && !_showLabel)
tt.insert(qApp->translate("WaypointItem", "Name"), _waypoint.name());
tt.insert(qApp->translate("WaypointItem", "Coordinates"),
::coordinates(_waypoint.coordinates()));
if (!std::isnan(_waypoint.elevation()))
tt.insert(qApp->translate("WaypointItem", "Elevation"),
::elevation(_waypoint.elevation(), _units));
if (!_waypoint.timestamp().isNull())
tt.insert(qApp->translate("WaypointItem", "Date"),
_waypoint.timestamp().toString(Qt::SystemLocaleShortDate));
if (!_waypoint.description().isNull())
tt.insert(qApp->translate("WaypointItem", "Description"),
_waypoint.description());
2016-08-02 00:28:56 +02:00
return tt.toString();
}
WaypointItem::WaypointItem(const Waypoint &waypoint, QGraphicsItem *parent)
2015-10-17 01:33:02 +02:00
: QGraphicsItem(parent)
2015-10-05 01:43:48 +02:00
{
2016-08-02 00:28:56 +02:00
_units = Metric;
2016-08-09 01:16:19 +02:00
_showLabel = true;
2016-09-10 13:11:46 +02:00
_hover = false;
2016-08-02 00:28:56 +02:00
_waypoint = waypoint;
QPointF p = waypoint.coordinates().toMercator();
_coordinates = QPointF(p.x(), -p.y());
updateShape();
2016-07-25 19:32:36 +02:00
setPos(_coordinates);
2016-08-02 00:28:56 +02:00
setToolTip(toolTip());
setCursor(Qt::ArrowCursor);
2016-09-10 13:11:46 +02:00
setAcceptHoverEvents(true);
2015-10-05 01:43:48 +02:00
}
void WaypointItem::updateShape()
2015-10-05 01:43:48 +02:00
{
QPainterPath p;
2016-09-10 13:11:46 +02:00
qreal pointSize = _hover ? HOVER_SIZE : POINT_SIZE;
2016-08-09 01:16:19 +02:00
if (_showLabel) {
QFont font;
font.setPixelSize(FONT_SIZE);
font.setFamily(FONT_FAMILY);
2016-09-10 13:11:46 +02:00
if (_hover)
font.setBold(true);
2016-08-09 01:16:19 +02:00
QFontMetrics fm(font);
QRect ts = fm.tightBoundingRect(_waypoint.name());
p.addRect(-pointSize/2, -pointSize/2, pointSize, pointSize);
p.addRect(pointSize/2, pointSize/2,
ts.width(), ts.height() + fm.descent());
2016-08-09 01:16:19 +02:00
} else
p.addRect(-pointSize/2, -pointSize/2, pointSize, pointSize);
_shape = p;
2015-10-05 01:43:48 +02:00
}
2016-07-25 19:32:36 +02:00
void WaypointItem::paint(QPainter *painter,
const QStyleOptionGraphicsItem *option, QWidget *widget)
2015-10-05 01:43:48 +02:00
{
Q_UNUSED(option);
Q_UNUSED(widget);
2016-08-09 01:16:19 +02:00
2016-09-10 13:11:46 +02:00
qreal pointSize = _hover ? HOVER_SIZE : POINT_SIZE;
2016-08-09 01:16:19 +02:00
if (_showLabel) {
QFont font;
font.setPixelSize(FONT_SIZE);
font.setFamily(FONT_FAMILY);
2016-09-10 13:11:46 +02:00
if (_hover)
font.setBold(true);
2016-08-09 01:16:19 +02:00
QFontMetrics fm(font);
QRect ts = fm.tightBoundingRect(_waypoint.name());
painter->setFont(font);
2016-09-10 13:11:46 +02:00
painter->drawText(pointSize/2 - qMax(ts.x(), 0), pointSize/2
2016-08-09 01:16:19 +02:00
+ ts.height(), _waypoint.name());
}
2015-10-05 01:43:48 +02:00
painter->setBrush(Qt::SolidPattern);
2016-09-10 13:11:46 +02:00
painter->drawEllipse(-pointSize/2, -pointSize/2, pointSize, pointSize);
2015-10-05 01:43:48 +02:00
/*
painter->setPen(Qt::red);
painter->setBrush(Qt::NoBrush);
painter->drawPath(_shape);
2015-10-05 01:43:48 +02:00
*/
}
2016-08-02 00:28:56 +02:00
void WaypointItem::setScale(qreal scale)
{
setPos(_coordinates * scale);
}
void WaypointItem::setUnits(enum Units units)
{
_units = units;
setToolTip(toolTip());
}
2016-08-09 01:16:19 +02:00
void WaypointItem::showLabel(bool show)
{
prepareGeometryChange();
_showLabel = show;
updateShape();
2016-08-09 01:16:19 +02:00
setToolTip(toolTip());
}
2016-09-10 13:11:46 +02:00
void WaypointItem::hoverEnterEvent(QGraphicsSceneHoverEvent *event)
{
Q_UNUSED(event);
prepareGeometryChange();
_hover = true;
updateShape();
2016-09-19 01:45:28 +02:00
setZValue(zValue() + 1.0);
2016-09-10 13:11:46 +02:00
}
void WaypointItem::hoverLeaveEvent(QGraphicsSceneHoverEvent *event)
{
Q_UNUSED(event);
prepareGeometryChange();
_hover = false;
updateShape();
2016-09-19 01:45:28 +02:00
setZValue(zValue() - 1.0);
2016-09-10 13:11:46 +02:00
}