1
0
mirror of https://github.com/tumic0/GPXSee.git synced 2025-01-18 11:52:08 +01:00

Added item hovering

This commit is contained in:
Martin Tůma 2016-09-10 13:11:46 +02:00
parent 8624b42e0b
commit 3074ba9957
6 changed files with 100 additions and 13 deletions

View File

@ -8,7 +8,8 @@
#include "routeitem.h" #include "routeitem.h"
#define ROUTE_WIDTH 3 #define ROUTE_WIDTH 3
#define HOVER_WIDTH 5
QString RouteItem::toolTip() QString RouteItem::toolTip()
{ {
@ -23,7 +24,7 @@ QString RouteItem::toolTip()
void RouteItem::updateShape() void RouteItem::updateShape()
{ {
QPainterPathStroker s; QPainterPathStroker s;
s.setWidth(ROUTE_WIDTH * 1.0/scale()); s.setWidth(HOVER_WIDTH * 1.0/scale());
_shape = s.createStroke(_path); _shape = s.createStroke(_path);
} }
@ -51,6 +52,7 @@ RouteItem::RouteItem(const Route &route, QGraphicsItem *parent)
setToolTip(toolTip()); setToolTip(toolTip());
setCursor(Qt::ArrowCursor); setCursor(Qt::ArrowCursor);
setAcceptHoverEvents(true);
updateShape(); updateShape();
@ -71,7 +73,9 @@ void RouteItem::paint(QPainter *painter, const QStyleOptionGraphicsItem *option,
painter->drawPath(_path); painter->drawPath(_path);
/* /*
painter->setPen(Qt::red); QPen p = QPen(Qt::red);
p.setWidthF(1.0/scale());
painter->setPen(p);
painter->drawRect(boundingRect()); painter->drawRect(boundingRect());
*/ */
} }
@ -130,3 +134,21 @@ void RouteItem::showWaypointLabels(bool show)
} }
} }
} }
void RouteItem::hoverEnterEvent(QGraphicsSceneHoverEvent *event)
{
Q_UNUSED(event);
_pen.setWidthF(HOVER_WIDTH * 1.0/scale());
setZValue(1.0);
update();
}
void RouteItem::hoverLeaveEvent(QGraphicsSceneHoverEvent *event)
{
Q_UNUSED(event);
_pen.setWidthF(ROUTE_WIDTH * 1.0/scale());
setZValue(0);
update();
}

View File

@ -30,6 +30,9 @@ public:
void showWaypointLabels(bool show); void showWaypointLabels(bool show);
private: private:
void hoverEnterEvent(QGraphicsSceneHoverEvent *event);
void hoverLeaveEvent(QGraphicsSceneHoverEvent *event);
void updateShape(); void updateShape();
QString toolTip(); QString toolTip();

View File

@ -7,7 +7,8 @@
#include "trackitem.h" #include "trackitem.h"
#define TRACK_WIDTH 3 #define TRACK_WIDTH 3
#define HOVER_WIDTH 5
QString TrackItem::toolTip() QString TrackItem::toolTip()
{ {
@ -27,7 +28,7 @@ QString TrackItem::toolTip()
void TrackItem::updateShape() void TrackItem::updateShape()
{ {
QPainterPathStroker s; QPainterPathStroker s;
s.setWidth(TRACK_WIDTH * 1.0/scale()); s.setWidth(HOVER_WIDTH * 1.0/scale());
_shape = s.createStroke(_path); _shape = s.createStroke(_path);
} }
@ -51,6 +52,7 @@ TrackItem::TrackItem(const Track &track, QGraphicsItem *parent)
setToolTip(toolTip()); setToolTip(toolTip());
setCursor(Qt::ArrowCursor); setCursor(Qt::ArrowCursor);
setAcceptHoverEvents(true);
updateShape(); updateShape();
@ -71,7 +73,10 @@ void TrackItem::paint(QPainter *painter, const QStyleOptionGraphicsItem *option,
painter->drawPath(_path); painter->drawPath(_path);
/* /*
painter->setPen(Qt::red); QPen p = QPen(Qt::red);
p.setWidthF(1.0/scale());
painter->setPen(p);
painter->setBrush(Qt::NoBrush);
painter->drawRect(boundingRect()); painter->drawRect(boundingRect());
*/ */
} }
@ -108,3 +113,21 @@ void TrackItem::moveMarker(qreal distance)
_marker->setPos(_path.pointAtPercent(distance / _distance)); _marker->setPos(_path.pointAtPercent(distance / _distance));
} }
} }
void TrackItem::hoverEnterEvent(QGraphicsSceneHoverEvent *event)
{
Q_UNUSED(event);
_pen.setWidthF(HOVER_WIDTH * 1.0/scale());
setZValue(1.0);
update();
}
void TrackItem::hoverLeaveEvent(QGraphicsSceneHoverEvent *event)
{
Q_UNUSED(event);
_pen.setWidthF(TRACK_WIDTH * 1.0/scale());
setZValue(0);
update();
}

View File

@ -28,6 +28,9 @@ public:
void moveMarker(qreal distance); void moveMarker(qreal distance);
private: private:
void hoverEnterEvent(QGraphicsSceneHoverEvent *event);
void hoverLeaveEvent(QGraphicsSceneHoverEvent *event);
void updateShape(); void updateShape();
QString toolTip(); QString toolTip();

View File

@ -7,7 +7,8 @@
#include "waypointitem.h" #include "waypointitem.h"
#define POINT_SIZE 8 #define POINT_SIZE 8
#define HOVER_SIZE 10
QString WaypointItem::toolTip() QString WaypointItem::toolTip()
{ {
@ -35,6 +36,7 @@ WaypointItem::WaypointItem(const Waypoint &waypoint, QGraphicsItem *parent)
{ {
_units = Metric; _units = Metric;
_showLabel = true; _showLabel = true;
_hover = false;
_waypoint = waypoint; _waypoint = waypoint;
_coordinates = ll2mercator(QPointF(waypoint.coordinates().x(), _coordinates = ll2mercator(QPointF(waypoint.coordinates().x(),
@ -45,22 +47,27 @@ WaypointItem::WaypointItem(const Waypoint &waypoint, QGraphicsItem *parent)
setPos(_coordinates); setPos(_coordinates);
setToolTip(toolTip()); setToolTip(toolTip());
setCursor(Qt::ArrowCursor); setCursor(Qt::ArrowCursor);
setAcceptHoverEvents(true);
} }
void WaypointItem::updateBoundingRect() void WaypointItem::updateBoundingRect()
{ {
qreal pointSize = _hover ? HOVER_SIZE : POINT_SIZE;
if (_showLabel) { if (_showLabel) {
QFont font; QFont font;
font.setPixelSize(FONT_SIZE); font.setPixelSize(FONT_SIZE);
font.setFamily(FONT_FAMILY); font.setFamily(FONT_FAMILY);
if (_hover)
font.setBold(true);
QFontMetrics fm(font); QFontMetrics fm(font);
QRect ts = fm.tightBoundingRect(_waypoint.name()); QRect ts = fm.tightBoundingRect(_waypoint.name());
_boundingRect = QRectF(-POINT_SIZE/2, -POINT_SIZE/2, ts.width() _boundingRect = QRectF(-pointSize/2, -pointSize/2, ts.width()
+ POINT_SIZE, ts.height() + fm.descent() + POINT_SIZE); + pointSize, ts.height() + fm.descent() + pointSize);
} else } else
_boundingRect = QRectF(-POINT_SIZE/2, -POINT_SIZE/2, POINT_SIZE, _boundingRect = QRectF(-pointSize/2, -pointSize/2, pointSize,
POINT_SIZE); pointSize);
} }
void WaypointItem::paint(QPainter *painter, void WaypointItem::paint(QPainter *painter,
@ -69,20 +76,24 @@ void WaypointItem::paint(QPainter *painter,
Q_UNUSED(option); Q_UNUSED(option);
Q_UNUSED(widget); Q_UNUSED(widget);
qreal pointSize = _hover ? HOVER_SIZE : POINT_SIZE;
if (_showLabel) { if (_showLabel) {
QFont font; QFont font;
font.setPixelSize(FONT_SIZE); font.setPixelSize(FONT_SIZE);
font.setFamily(FONT_FAMILY); font.setFamily(FONT_FAMILY);
if (_hover)
font.setBold(true);
QFontMetrics fm(font); QFontMetrics fm(font);
QRect ts = fm.tightBoundingRect(_waypoint.name()); QRect ts = fm.tightBoundingRect(_waypoint.name());
painter->setFont(font); painter->setFont(font);
painter->drawText(POINT_SIZE/2 - qMax(ts.x(), 0), POINT_SIZE/2 painter->drawText(pointSize/2 - qMax(ts.x(), 0), pointSize/2
+ ts.height(), _waypoint.name()); + ts.height(), _waypoint.name());
} }
painter->setBrush(Qt::SolidPattern); painter->setBrush(Qt::SolidPattern);
painter->drawEllipse(-POINT_SIZE/2, -POINT_SIZE/2, POINT_SIZE, POINT_SIZE); painter->drawEllipse(-pointSize/2, -pointSize/2, pointSize, pointSize);
/* /*
painter->setPen(Qt::red); painter->setPen(Qt::red);
@ -109,3 +120,23 @@ void WaypointItem::showLabel(bool show)
updateBoundingRect(); updateBoundingRect();
setToolTip(toolTip()); setToolTip(toolTip());
} }
void WaypointItem::hoverEnterEvent(QGraphicsSceneHoverEvent *event)
{
Q_UNUSED(event);
prepareGeometryChange();
_hover = true;
updateBoundingRect();
setZValue(1.0);
}
void WaypointItem::hoverLeaveEvent(QGraphicsSceneHoverEvent *event)
{
Q_UNUSED(event);
prepareGeometryChange();
_hover = false;
updateBoundingRect();
setZValue(0);
}

View File

@ -22,6 +22,9 @@ public:
QWidget *widget); QWidget *widget);
private: private:
void hoverEnterEvent(QGraphicsSceneHoverEvent *event);
void hoverLeaveEvent(QGraphicsSceneHoverEvent *event);
void updateBoundingRect(); void updateBoundingRect();
QString toolTip(); QString toolTip();
@ -29,6 +32,8 @@ private:
QPointF _coordinates; QPointF _coordinates;
Waypoint _waypoint; Waypoint _waypoint;
Units _units; Units _units;
bool _hover;
bool _showLabel; bool _showLabel;
}; };