2016-08-02 20:46:22 +02:00
|
|
|
#include <QApplication>
|
2015-10-05 01:43:48 +02:00
|
|
|
#include <QPainter>
|
2019-10-13 20:20:32 +02:00
|
|
|
#include <QGraphicsSceneMouseEvent>
|
|
|
|
#include <QLabel>
|
2018-11-02 20:01:19 +01:00
|
|
|
#include "font.h"
|
2019-10-13 20:20:32 +02:00
|
|
|
#include "popup.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
|
|
|
|
2020-05-20 21:00:36 +02:00
|
|
|
Units WaypointItem::_units = Metric;
|
|
|
|
CoordinatesFormat WaypointItem::_format = DecimalDegrees;
|
|
|
|
QTimeZone WaypointItem::_timeZone = QTimeZone::utc();
|
|
|
|
|
2021-08-04 08:57:42 +02:00
|
|
|
ToolTip WaypointItem::info() const
|
2016-07-28 00:23:22 +02:00
|
|
|
{
|
2016-08-02 00:28:56 +02:00
|
|
|
ToolTip tt;
|
2020-12-22 22:09:09 +01:00
|
|
|
QLocale l;
|
2016-08-02 00:28:56 +02:00
|
|
|
|
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"),
|
2019-10-15 23:59:15 +02:00
|
|
|
Format::coordinates(_waypoint.coordinates(), _format));
|
2020-03-25 23:08:26 +01:00
|
|
|
if (!std::isnan(_waypoint.elevations().first)) {
|
|
|
|
QString val = Format::elevation(_waypoint.elevations().first, _units);
|
|
|
|
if (!std::isnan(_waypoint.elevations().second))
|
|
|
|
val += " (" + Format::elevation(_waypoint.elevations().second,
|
|
|
|
_units) + ")";
|
|
|
|
tt.insert(qApp->translate("WaypointItem", "Elevation"), val);
|
|
|
|
}
|
2019-03-14 18:52:15 +01:00
|
|
|
if (_waypoint.timestamp().isValid())
|
2016-08-02 20:46:22 +02:00
|
|
|
tt.insert(qApp->translate("WaypointItem", "Date"),
|
2020-12-22 22:09:09 +01:00
|
|
|
l.toString(_waypoint.timestamp().toTimeZone(_timeZone),
|
|
|
|
QLocale::ShortFormat));
|
2019-03-14 18:52:15 +01:00
|
|
|
if (!_waypoint.description().isEmpty())
|
2016-08-02 20:46:22 +02:00
|
|
|
tt.insert(qApp->translate("WaypointItem", "Description"),
|
|
|
|
_waypoint.description());
|
2020-03-09 20:04:13 +01:00
|
|
|
if (!_waypoint.comment().isEmpty()
|
|
|
|
&& _waypoint.comment() != _waypoint.description())
|
|
|
|
tt.insert(qApp->translate("WaypointItem", "Comment"),
|
|
|
|
_waypoint.comment());
|
2021-10-10 08:38:38 +02:00
|
|
|
if (!_waypoint.symbol().isEmpty())
|
|
|
|
tt.insert(qApp->translate("WaypointItem", "Symbol"), _waypoint.symbol());
|
2021-05-12 21:58:46 +02:00
|
|
|
if (!_waypoint.address().isEmpty()) {
|
|
|
|
QString addr(_waypoint.address());
|
|
|
|
addr.replace('\n', "<br/>");
|
|
|
|
addr = "<address>" + addr + "</address>";
|
2019-11-08 21:00:59 +01:00
|
|
|
tt.insert(qApp->translate("WaypointItem", "Address"), addr);
|
|
|
|
}
|
2021-05-12 21:58:46 +02:00
|
|
|
if (!_waypoint.phone().isEmpty())
|
|
|
|
tt.insert(qApp->translate("WaypointItem", "Phone"), _waypoint.phone());
|
2019-10-22 22:27:12 +02:00
|
|
|
if (!_waypoint.links().isEmpty()) {
|
|
|
|
QString links;
|
|
|
|
for (int i = 0; i < _waypoint.links().size(); i++) {
|
|
|
|
const Link &link = _waypoint.links().at(i);
|
|
|
|
links.append(QString("<a href=\"%0\">%1</a>").arg(link.URL(),
|
|
|
|
link.text().isEmpty() ? link.URL() : link.text()));
|
|
|
|
if (i != _waypoint.links().size() - 1)
|
|
|
|
links.append("<br/>");
|
|
|
|
}
|
|
|
|
tt.insert(qApp->translate("WaypointItem", "Links"), links);
|
2019-10-14 20:07:05 +02:00
|
|
|
}
|
2021-10-10 08:38:38 +02:00
|
|
|
|
2019-11-15 22:10:55 +01:00
|
|
|
tt.setImages(_waypoint.images());
|
2016-08-02 00:28:56 +02:00
|
|
|
|
2021-08-04 08:57:42 +02:00
|
|
|
return tt;
|
2016-07-28 00:23:22 +02:00
|
|
|
}
|
|
|
|
|
2017-03-18 01:30:31 +01:00
|
|
|
WaypointItem::WaypointItem(const Waypoint &waypoint, Map *map,
|
2019-10-15 23:59:15 +02:00
|
|
|
QGraphicsItem *parent) : GraphicsItem(parent)
|
2015-10-05 01:43:48 +02:00
|
|
|
{
|
2017-03-18 01:30:31 +01:00
|
|
|
_waypoint = waypoint;
|
2023-02-10 00:16:16 +01:00
|
|
|
_showLabel = false;
|
2021-10-10 08:38:38 +02:00
|
|
|
_showIcon = false;
|
2022-09-25 02:15:24 +02:00
|
|
|
_size = 8;
|
|
|
|
_color = Qt::black;
|
2016-08-02 00:28:56 +02:00
|
|
|
|
2022-09-23 02:34:18 +02:00
|
|
|
_icon = (_waypoint.style().icon().isNull())
|
2021-10-10 08:38:38 +02:00
|
|
|
? Waypoint::symbolIcon(_waypoint.symbol())
|
2022-09-23 02:34:18 +02:00
|
|
|
: &_waypoint.style().icon();
|
2021-10-10 08:38:38 +02:00
|
|
|
|
2022-09-25 02:15:24 +02:00
|
|
|
_font.setPixelSize(FS(size()));
|
2018-05-19 20:30:29 +02:00
|
|
|
_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()));
|
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;
|
2022-09-25 02:15:24 +02:00
|
|
|
qreal pointSize = _font.bold() ? HS(size()) : size();
|
2022-09-23 02:34:18 +02:00
|
|
|
const QPixmap &icon = _waypoint.style().icon();
|
2016-09-10 13:11:46 +02:00
|
|
|
|
2022-09-01 00:25:48 +02:00
|
|
|
if (_showLabel && !_waypoint.name().isEmpty()) {
|
2018-05-19 20:30:29 +02:00
|
|
|
QFontMetrics fm(_font);
|
|
|
|
_labelBB = fm.tightBoundingRect(_waypoint.name());
|
2016-08-09 01:16:19 +02:00
|
|
|
|
2021-10-10 08:38:38 +02:00
|
|
|
if (_showIcon && _icon) {
|
2021-10-24 14:31:31 +02:00
|
|
|
if (_font.bold())
|
2022-09-23 02:34:18 +02:00
|
|
|
p.addRect(-_icon->width() * 0.625, icon.isNull()
|
2022-09-01 00:25:48 +02:00
|
|
|
? -_icon->height() * 1.25 : -_icon->height() * 0.625,
|
2021-10-24 14:31:31 +02:00
|
|
|
_icon->width() * 1.25, _icon->height() * 1.25);
|
|
|
|
else
|
2022-09-23 02:34:18 +02:00
|
|
|
p.addRect(-_icon->width()/2.0, icon.isNull()
|
2022-09-01 00:25:48 +02:00
|
|
|
? -_icon->height() : -_icon->height()/2, _icon->width(),
|
2021-10-24 14:31:31 +02:00
|
|
|
_icon->height());
|
2022-09-01 00:25:48 +02:00
|
|
|
|
2022-09-23 02:34:18 +02:00
|
|
|
if (icon.isNull())
|
2022-09-01 00:25:48 +02:00
|
|
|
p.addRect(0, 0, _labelBB.width(), _labelBB.height()
|
|
|
|
+ fm.descent());
|
|
|
|
else
|
|
|
|
p.addRect(_icon->width()/2, _icon->height()/2, _labelBB.width(),
|
|
|
|
_labelBB.height() + fm.descent());
|
2021-10-10 08:38:38 +02:00
|
|
|
} else {
|
|
|
|
p.addRect(-pointSize/2, -pointSize/2, pointSize, pointSize);
|
|
|
|
p.addRect(pointSize/2, pointSize/2, _labelBB.width(),
|
|
|
|
_labelBB.height() + fm.descent());
|
|
|
|
}
|
|
|
|
} else {
|
2021-10-24 14:31:31 +02:00
|
|
|
if (_showIcon && _icon) {
|
|
|
|
if (_font.bold())
|
2022-09-23 02:34:18 +02:00
|
|
|
p.addRect(-_icon->width() * 0.625, icon.isNull()
|
2022-09-01 00:25:48 +02:00
|
|
|
? -_icon->height() * 1.25 : -_icon->height() * 0.625,
|
2021-10-24 14:31:31 +02:00
|
|
|
_icon->width() * 1.25, _icon->height() * 1.25);
|
|
|
|
else
|
2022-09-23 02:34:18 +02:00
|
|
|
p.addRect(-_icon->width()/2, icon.isNull()
|
2022-09-01 00:25:48 +02:00
|
|
|
? -_icon->height() : -_icon->height()/2, _icon->width(),
|
2021-10-24 14:31:31 +02:00
|
|
|
_icon->height());
|
|
|
|
} else
|
2021-10-10 08:38:38 +02:00
|
|
|
p.addRect(-pointSize/2, -pointSize/2, pointSize, pointSize);
|
|
|
|
}
|
2016-09-11 13:42:22 +02:00
|
|
|
|
|
|
|
_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);
|
2022-09-25 02:15:24 +02:00
|
|
|
qreal pointSize = _font.bold() ? HS(size()) : size();
|
2022-09-23 02:34:18 +02:00
|
|
|
const QPixmap &icon = _waypoint.style().icon();
|
2017-09-10 12:42:49 +02:00
|
|
|
|
2022-09-25 02:15:24 +02:00
|
|
|
painter->setPen(color());
|
2016-09-10 13:11:46 +02:00
|
|
|
|
2022-09-01 00:25:48 +02:00
|
|
|
if (_showLabel && !_waypoint.name().isEmpty()) {
|
2018-05-19 20:30:29 +02:00
|
|
|
painter->setFont(_font);
|
2022-09-01 00:25:48 +02:00
|
|
|
if (_showIcon && _icon) {
|
2022-09-23 02:34:18 +02:00
|
|
|
if (icon.isNull())
|
2022-09-01 00:25:48 +02:00
|
|
|
painter->drawText(-qMax(_labelBB.x(), 0), _labelBB.height(),
|
|
|
|
_waypoint.name());
|
|
|
|
else
|
|
|
|
painter->drawText(_icon->width()/2 - qMax(_labelBB.x(), 0),
|
|
|
|
_icon->height()/2 + _labelBB.height(), _waypoint.name());
|
|
|
|
} else
|
2021-10-10 08:38:38 +02:00
|
|
|
painter->drawText(pointSize/2 - qMax(_labelBB.x(), 0), pointSize/2
|
|
|
|
+ _labelBB.height(), _waypoint.name());
|
2016-08-09 01:16:19 +02:00
|
|
|
}
|
|
|
|
|
2022-09-25 02:15:24 +02:00
|
|
|
painter->setBrush(QBrush(color(), Qt::SolidPattern));
|
2021-10-24 14:31:31 +02:00
|
|
|
if (_showIcon && _icon) {
|
|
|
|
if (_font.bold())
|
2022-09-23 02:34:18 +02:00
|
|
|
painter->drawPixmap(-_icon->width() * 0.625, icon.isNull()
|
2022-09-01 00:25:48 +02:00
|
|
|
? -_icon->height() * 1.25 : -_icon->height() * 0.625,
|
2021-10-24 14:31:31 +02:00
|
|
|
_icon->scaled(_icon->width() * 1.25, _icon->height() * 1.25,
|
|
|
|
Qt::IgnoreAspectRatio, Qt::SmoothTransformation));
|
|
|
|
else
|
2022-09-23 02:34:18 +02:00
|
|
|
painter->drawPixmap(-_icon->width()/2.0, icon.isNull()
|
2022-09-01 00:25:48 +02:00
|
|
|
? -_icon->height() : -_icon->height()/2, *_icon);
|
2021-10-24 14:31:31 +02:00
|
|
|
} else
|
2021-10-10 08:38:38 +02:00
|
|
|
painter->drawEllipse(-pointSize/2, -pointSize/2, pointSize, pointSize);
|
2015-10-05 01:43:48 +02:00
|
|
|
|
2021-10-24 14:31:31 +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
|
|
|
|
2022-09-25 02:15:24 +02:00
|
|
|
int WaypointItem::size() const
|
|
|
|
{
|
|
|
|
return (_useStyle && _waypoint.style().size() > 0)
|
|
|
|
? _waypoint.style().size() : _size;
|
|
|
|
}
|
|
|
|
|
2017-09-10 12:42:49 +02:00
|
|
|
void WaypointItem::setSize(int size)
|
|
|
|
{
|
2022-09-25 02:15:24 +02:00
|
|
|
_size = size;
|
|
|
|
updateSize();
|
|
|
|
}
|
2018-02-11 20:39:39 +01:00
|
|
|
|
2022-09-25 02:15:24 +02:00
|
|
|
void WaypointItem::updateSize()
|
|
|
|
{
|
2017-09-10 12:42:49 +02:00
|
|
|
prepareGeometryChange();
|
2022-09-25 02:15:24 +02:00
|
|
|
_font.setPixelSize(FS(size()));
|
2018-05-19 20:30:29 +02:00
|
|
|
updateCache();
|
2017-09-10 12:42:49 +02:00
|
|
|
}
|
|
|
|
|
2022-09-25 02:15:24 +02:00
|
|
|
const QColor &WaypointItem::color() const
|
2017-09-10 12:42:49 +02:00
|
|
|
{
|
2022-09-25 02:15:24 +02:00
|
|
|
return (_useStyle && _waypoint.style().color().isValid())
|
|
|
|
? _waypoint.style().color() : _color;
|
|
|
|
}
|
2018-02-11 20:39:39 +01:00
|
|
|
|
2022-09-25 02:15:24 +02:00
|
|
|
void WaypointItem::setColor(const QColor &color)
|
|
|
|
{
|
2017-09-10 12:42:49 +02:00
|
|
|
_color = color;
|
2022-09-25 02:15:24 +02:00
|
|
|
updateColor();
|
|
|
|
}
|
|
|
|
|
|
|
|
void WaypointItem::updateColor()
|
|
|
|
{
|
2017-09-10 12:42:49 +02:00
|
|
|
update();
|
|
|
|
}
|
|
|
|
|
2022-09-25 02:15:24 +02:00
|
|
|
void WaypointItem::updateStyle()
|
|
|
|
{
|
|
|
|
updateSize();
|
|
|
|
updateColor();
|
|
|
|
}
|
|
|
|
|
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
|
|
|
|
2021-10-10 08:38:38 +02:00
|
|
|
void WaypointItem::showIcon(bool show)
|
|
|
|
{
|
|
|
|
if (_showIcon == show)
|
|
|
|
return;
|
|
|
|
|
|
|
|
prepareGeometryChange();
|
|
|
|
_showIcon = show;
|
|
|
|
updateCache();
|
|
|
|
}
|
|
|
|
|
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
|
|
|
}
|
2019-10-13 20:20:32 +02:00
|
|
|
|
|
|
|
void WaypointItem::mousePressEvent(QGraphicsSceneMouseEvent *event)
|
|
|
|
{
|
2019-10-15 23:59:15 +02:00
|
|
|
Popup::show(event->screenPos(), info(), event->widget());
|
2019-10-14 20:07:05 +02:00
|
|
|
/* Do not propagate the event any further as lower stacked items (path
|
|
|
|
items) would replace the popup with their own popup */
|
2019-10-13 20:20:32 +02:00
|
|
|
}
|