2016-08-02 20:46:22 +02:00
|
|
|
#include <QApplication>
|
2015-10-05 01:43:48 +02:00
|
|
|
#include <QPainter>
|
2015-10-17 01:33:02 +02:00
|
|
|
#include "config.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
|
|
|
|
|
|
|
|
2017-09-10 12:42:49 +02:00
|
|
|
#define HS(size) \
|
|
|
|
((int)((qreal)size * 1.2))
|
|
|
|
#define FS(size) \
|
|
|
|
((int)((qreal)size * 1.41))
|
2015-10-05 01:43:48 +02:00
|
|
|
|
2018-02-11 20:49:26 +01:00
|
|
|
QString WaypointItem::toolTip(Units units, CoordinatesFormat format)
|
2016-07-28 00:23:22 +02:00
|
|
|
{
|
2016-08-02 00:28:56 +02:00
|
|
|
ToolTip tt;
|
|
|
|
|
2017-03-18 01:30:31 +01:00
|
|
|
if (!_waypoint.name().isEmpty())
|
2016-08-09 01:16:19 +02:00
|
|
|
tt.insert(qApp->translate("WaypointItem", "Name"), _waypoint.name());
|
2016-08-02 20:46:22 +02:00
|
|
|
tt.insert(qApp->translate("WaypointItem", "Coordinates"),
|
2018-02-11 20:49:26 +01:00
|
|
|
Format::coordinates(_waypoint.coordinates(), format));
|
2016-08-02 20:46:22 +02:00
|
|
|
if (!std::isnan(_waypoint.elevation()))
|
|
|
|
tt.insert(qApp->translate("WaypointItem", "Elevation"),
|
2017-03-18 01:30:31 +01:00
|
|
|
Format::elevation(_waypoint.elevation(), units));
|
2016-08-02 20:46:22 +02:00
|
|
|
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();
|
2016-07-28 00:23:22 +02:00
|
|
|
}
|
|
|
|
|
2017-03-18 01:30:31 +01:00
|
|
|
WaypointItem::WaypointItem(const Waypoint &waypoint, Map *map,
|
|
|
|
QGraphicsItem *parent) : QGraphicsItem(parent)
|
2015-10-05 01:43:48 +02:00
|
|
|
{
|
2017-03-18 01:30:31 +01:00
|
|
|
_waypoint = waypoint;
|
2016-08-09 01:16:19 +02:00
|
|
|
_showLabel = true;
|
2017-09-10 12:42:49 +02:00
|
|
|
_size = 8;
|
|
|
|
_color = Qt::black;
|
2016-08-02 00:28:56 +02:00
|
|
|
|
2018-05-19 20:30:29 +02:00
|
|
|
_font.setPixelSize(FS(_size));
|
|
|
|
_font.setFamily(FONT_FAMILY);
|
|
|
|
|
|
|
|
updateCache();
|
2016-07-25 19:32:36 +02:00
|
|
|
|
2017-03-18 01:30:31 +01:00
|
|
|
setPos(map->ll2xy(waypoint.coordinates()));
|
2018-02-11 20:39:39 +01:00
|
|
|
setToolTip(toolTip(Metric, DecimalDegrees));
|
2016-07-28 00:23:22 +02:00
|
|
|
setCursor(Qt::ArrowCursor);
|
2016-09-10 13:11:46 +02:00
|
|
|
setAcceptHoverEvents(true);
|
2015-10-05 01:43:48 +02:00
|
|
|
}
|
|
|
|
|
2018-05-19 20:30:29 +02:00
|
|
|
void WaypointItem::updateCache()
|
2015-10-05 01:43:48 +02:00
|
|
|
{
|
2016-09-11 13:42:22 +02:00
|
|
|
QPainterPath p;
|
2018-05-19 20:30:29 +02:00
|
|
|
qreal pointSize = _font.bold() ? HS(_size) : _size;
|
2016-09-10 13:11:46 +02:00
|
|
|
|
2016-08-09 01:16:19 +02:00
|
|
|
if (_showLabel) {
|
2018-05-19 20:30:29 +02:00
|
|
|
QFontMetrics fm(_font);
|
|
|
|
_labelBB = fm.tightBoundingRect(_waypoint.name());
|
2016-08-09 01:16:19 +02:00
|
|
|
|
2016-09-11 13:42:22 +02:00
|
|
|
p.addRect(-pointSize/2, -pointSize/2, pointSize, pointSize);
|
2018-05-19 20:30:29 +02:00
|
|
|
p.addRect(pointSize/2, pointSize/2, _labelBB.width(), _labelBB.height()
|
|
|
|
+ fm.descent());
|
2016-08-09 01:16:19 +02:00
|
|
|
} else
|
2016-09-11 13:42:22 +02:00
|
|
|
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);
|
2018-05-19 20:30:29 +02:00
|
|
|
qreal pointSize = _font.bold() ? HS(_size) : _size;
|
2017-09-10 12:42:49 +02:00
|
|
|
|
|
|
|
painter->setPen(_color);
|
2016-09-10 13:11:46 +02:00
|
|
|
|
2016-08-09 01:16:19 +02:00
|
|
|
if (_showLabel) {
|
2018-05-19 20:30:29 +02:00
|
|
|
painter->setFont(_font);
|
|
|
|
painter->drawText(pointSize/2 - qMax(_labelBB.x(), 0), pointSize/2
|
|
|
|
+ _labelBB.height(), _waypoint.name());
|
2016-08-09 01:16:19 +02:00
|
|
|
}
|
|
|
|
|
2017-09-10 12:42:49 +02:00
|
|
|
painter->setBrush(QBrush(_color, 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);
|
2016-09-11 13:42:22 +02:00
|
|
|
painter->drawPath(_shape);
|
2015-10-05 01:43:48 +02:00
|
|
|
*/
|
|
|
|
}
|
2016-08-02 00:28:56 +02:00
|
|
|
|
2017-09-10 12:42:49 +02:00
|
|
|
void WaypointItem::setSize(int size)
|
|
|
|
{
|
2018-02-11 20:39:39 +01:00
|
|
|
if (_size == size)
|
|
|
|
return;
|
|
|
|
|
2017-09-10 12:42:49 +02:00
|
|
|
prepareGeometryChange();
|
|
|
|
_size = size;
|
2018-05-19 20:30:29 +02:00
|
|
|
_font.setPixelSize(FS(_size));
|
|
|
|
updateCache();
|
2017-09-10 12:42:49 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
void WaypointItem::setColor(const QColor &color)
|
|
|
|
{
|
2018-02-11 20:39:39 +01:00
|
|
|
if (_color == color)
|
|
|
|
return;
|
|
|
|
|
2017-09-10 12:42:49 +02:00
|
|
|
_color = color;
|
|
|
|
update();
|
|
|
|
}
|
|
|
|
|
2018-02-11 20:49:26 +01:00
|
|
|
void WaypointItem::setToolTipFormat(Units units, CoordinatesFormat format)
|
2016-08-02 00:28:56 +02:00
|
|
|
{
|
2018-02-11 20:49:26 +01:00
|
|
|
setToolTip(toolTip(units, format));
|
2016-08-02 00:28:56 +02:00
|
|
|
}
|
2016-08-09 01:16:19 +02:00
|
|
|
|
|
|
|
void WaypointItem::showLabel(bool show)
|
|
|
|
{
|
2018-02-11 20:39:39 +01:00
|
|
|
if (_showLabel == show)
|
|
|
|
return;
|
|
|
|
|
2016-08-09 01:16:19 +02:00
|
|
|
prepareGeometryChange();
|
|
|
|
_showLabel = show;
|
2018-05-19 20:30:29 +02:00
|
|
|
updateCache();
|
2016-08-09 01:16:19 +02:00
|
|
|
}
|
2016-09-10 13:11:46 +02:00
|
|
|
|
|
|
|
void WaypointItem::hoverEnterEvent(QGraphicsSceneHoverEvent *event)
|
|
|
|
{
|
|
|
|
Q_UNUSED(event);
|
|
|
|
|
|
|
|
prepareGeometryChange();
|
2018-05-19 20:30:29 +02:00
|
|
|
_font.setBold(true);
|
|
|
|
updateCache();
|
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();
|
2018-05-19 20:30:29 +02:00
|
|
|
_font.setBold(false);
|
|
|
|
updateCache();
|
2016-09-19 01:45:28 +02:00
|
|
|
setZValue(zValue() - 1.0);
|
2016-09-10 13:11:46 +02:00
|
|
|
}
|