1
0
mirror of https://github.com/tumic0/GPXSee.git synced 2025-01-18 03:42:09 +01:00

Added item shape for better collision detection/hover handling

This commit is contained in:
Martin Tůma 2016-09-11 13:42:22 +02:00
parent 3074ba9957
commit 1827787fc2
2 changed files with 18 additions and 14 deletions

View File

@ -42,7 +42,7 @@ WaypointItem::WaypointItem(const Waypoint &waypoint, QGraphicsItem *parent)
_coordinates = ll2mercator(QPointF(waypoint.coordinates().x(), _coordinates = ll2mercator(QPointF(waypoint.coordinates().x(),
-waypoint.coordinates().y())); -waypoint.coordinates().y()));
updateBoundingRect(); updateShape();
setPos(_coordinates); setPos(_coordinates);
setToolTip(toolTip()); setToolTip(toolTip());
@ -50,8 +50,9 @@ WaypointItem::WaypointItem(const Waypoint &waypoint, QGraphicsItem *parent)
setAcceptHoverEvents(true); setAcceptHoverEvents(true);
} }
void WaypointItem::updateBoundingRect() void WaypointItem::updateShape()
{ {
QPainterPath p;
qreal pointSize = _hover ? HOVER_SIZE : POINT_SIZE; qreal pointSize = _hover ? HOVER_SIZE : POINT_SIZE;
if (_showLabel) { if (_showLabel) {
@ -63,11 +64,13 @@ void WaypointItem::updateBoundingRect()
QFontMetrics fm(font); QFontMetrics fm(font);
QRect ts = fm.tightBoundingRect(_waypoint.name()); QRect ts = fm.tightBoundingRect(_waypoint.name());
_boundingRect = QRectF(-pointSize/2, -pointSize/2, ts.width() p.addRect(-pointSize/2, -pointSize/2, pointSize, pointSize);
+ pointSize, ts.height() + fm.descent() + pointSize); p.addRect(pointSize/2, pointSize/2,
ts.width(), ts.height() + fm.descent());
} else } else
_boundingRect = QRectF(-pointSize/2, -pointSize/2, pointSize, p.addRect(-pointSize/2, -pointSize/2, pointSize, pointSize);
pointSize);
_shape = p;
} }
void WaypointItem::paint(QPainter *painter, void WaypointItem::paint(QPainter *painter,
@ -98,7 +101,7 @@ void WaypointItem::paint(QPainter *painter,
/* /*
painter->setPen(Qt::red); painter->setPen(Qt::red);
painter->setBrush(Qt::NoBrush); painter->setBrush(Qt::NoBrush);
painter->drawRect(boundingRect()); painter->drawPath(_shape);
*/ */
} }
@ -117,7 +120,7 @@ void WaypointItem::showLabel(bool show)
{ {
prepareGeometryChange(); prepareGeometryChange();
_showLabel = show; _showLabel = show;
updateBoundingRect(); updateShape();
setToolTip(toolTip()); setToolTip(toolTip());
} }
@ -127,8 +130,8 @@ void WaypointItem::hoverEnterEvent(QGraphicsSceneHoverEvent *event)
prepareGeometryChange(); prepareGeometryChange();
_hover = true; _hover = true;
updateBoundingRect(); updateShape();
setZValue(1.0); setZValue(3.0);
} }
void WaypointItem::hoverLeaveEvent(QGraphicsSceneHoverEvent *event) void WaypointItem::hoverLeaveEvent(QGraphicsSceneHoverEvent *event)
@ -137,6 +140,6 @@ void WaypointItem::hoverLeaveEvent(QGraphicsSceneHoverEvent *event)
prepareGeometryChange(); prepareGeometryChange();
_hover = false; _hover = false;
updateBoundingRect(); updateShape();
setZValue(0); setZValue(0);
} }

View File

@ -17,7 +17,8 @@ public:
void setScale(qreal scale); void setScale(qreal scale);
void showLabel(bool show); void showLabel(bool show);
QRectF boundingRect() const {return _boundingRect;} QPainterPath shape() const {return _shape;}
QRectF boundingRect() const {return _shape.boundingRect();}
void paint(QPainter *painter, const QStyleOptionGraphicsItem *option, void paint(QPainter *painter, const QStyleOptionGraphicsItem *option,
QWidget *widget); QWidget *widget);
@ -25,10 +26,10 @@ private:
void hoverEnterEvent(QGraphicsSceneHoverEvent *event); void hoverEnterEvent(QGraphicsSceneHoverEvent *event);
void hoverLeaveEvent(QGraphicsSceneHoverEvent *event); void hoverLeaveEvent(QGraphicsSceneHoverEvent *event);
void updateBoundingRect(); void updateShape();
QString toolTip(); QString toolTip();
QRectF _boundingRect; QPainterPath _shape;
QPointF _coordinates; QPointF _coordinates;
Waypoint _waypoint; Waypoint _waypoint;
Units _units; Units _units;