1
0
mirror of https://github.com/tumic0/GPXSee.git synced 2025-01-18 03:42:09 +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

@ -9,6 +9,7 @@
#define ROUTE_WIDTH 3
#define HOVER_WIDTH 5
QString RouteItem::toolTip()
{
@ -23,7 +24,7 @@ QString RouteItem::toolTip()
void RouteItem::updateShape()
{
QPainterPathStroker s;
s.setWidth(ROUTE_WIDTH * 1.0/scale());
s.setWidth(HOVER_WIDTH * 1.0/scale());
_shape = s.createStroke(_path);
}
@ -51,6 +52,7 @@ RouteItem::RouteItem(const Route &route, QGraphicsItem *parent)
setToolTip(toolTip());
setCursor(Qt::ArrowCursor);
setAcceptHoverEvents(true);
updateShape();
@ -71,7 +73,9 @@ void RouteItem::paint(QPainter *painter, const QStyleOptionGraphicsItem *option,
painter->drawPath(_path);
/*
painter->setPen(Qt::red);
QPen p = QPen(Qt::red);
p.setWidthF(1.0/scale());
painter->setPen(p);
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);
private:
void hoverEnterEvent(QGraphicsSceneHoverEvent *event);
void hoverLeaveEvent(QGraphicsSceneHoverEvent *event);
void updateShape();
QString toolTip();

View File

@ -8,6 +8,7 @@
#define TRACK_WIDTH 3
#define HOVER_WIDTH 5
QString TrackItem::toolTip()
{
@ -27,7 +28,7 @@ QString TrackItem::toolTip()
void TrackItem::updateShape()
{
QPainterPathStroker s;
s.setWidth(TRACK_WIDTH * 1.0/scale());
s.setWidth(HOVER_WIDTH * 1.0/scale());
_shape = s.createStroke(_path);
}
@ -51,6 +52,7 @@ TrackItem::TrackItem(const Track &track, QGraphicsItem *parent)
setToolTip(toolTip());
setCursor(Qt::ArrowCursor);
setAcceptHoverEvents(true);
updateShape();
@ -71,7 +73,10 @@ void TrackItem::paint(QPainter *painter, const QStyleOptionGraphicsItem *option,
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());
*/
}
@ -108,3 +113,21 @@ void TrackItem::moveMarker(qreal 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);
private:
void hoverEnterEvent(QGraphicsSceneHoverEvent *event);
void hoverLeaveEvent(QGraphicsSceneHoverEvent *event);
void updateShape();
QString toolTip();

View File

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