1
0
mirror of https://github.com/tumic0/GPXSee.git synced 2024-11-30 22:51:16 +01:00

Precache font related info

This commit is contained in:
Martin Tůma 2018-05-19 20:30:29 +02:00
parent 7198d610c5
commit 273a918ed9
2 changed files with 25 additions and 36 deletions

View File

@ -36,11 +36,13 @@ WaypointItem::WaypointItem(const Waypoint &waypoint, Map *map,
{ {
_waypoint = waypoint; _waypoint = waypoint;
_showLabel = true; _showLabel = true;
_hover = false;
_size = 8; _size = 8;
_color = Qt::black; _color = Qt::black;
updateShape(); _font.setPixelSize(FS(_size));
_font.setFamily(FONT_FAMILY);
updateCache();
setPos(map->ll2xy(waypoint.coordinates())); setPos(map->ll2xy(waypoint.coordinates()));
setToolTip(toolTip(Metric, DecimalDegrees)); setToolTip(toolTip(Metric, DecimalDegrees));
@ -48,23 +50,18 @@ WaypointItem::WaypointItem(const Waypoint &waypoint, Map *map,
setAcceptHoverEvents(true); setAcceptHoverEvents(true);
} }
void WaypointItem::updateShape() void WaypointItem::updateCache()
{ {
QPainterPath p; QPainterPath p;
qreal pointSize = _hover ? HS(_size) : _size; qreal pointSize = _font.bold() ? HS(_size) : _size;
if (_showLabel) { if (_showLabel) {
QFont font; QFontMetrics fm(_font);
font.setPixelSize(FS(_size)); _labelBB = fm.tightBoundingRect(_waypoint.name());
font.setFamily(FONT_FAMILY);
if (_hover)
font.setBold(true);
QFontMetrics fm(font);
QRect ts = fm.tightBoundingRect(_waypoint.name());
p.addRect(-pointSize/2, -pointSize/2, pointSize, pointSize); p.addRect(-pointSize/2, -pointSize/2, pointSize, pointSize);
p.addRect(pointSize/2, pointSize/2, p.addRect(pointSize/2, pointSize/2, _labelBB.width(), _labelBB.height()
ts.width(), ts.height() + fm.descent()); + fm.descent());
} else } else
p.addRect(-pointSize/2, -pointSize/2, pointSize, pointSize); p.addRect(-pointSize/2, -pointSize/2, pointSize, pointSize);
@ -76,23 +73,14 @@ void WaypointItem::paint(QPainter *painter,
{ {
Q_UNUSED(option); Q_UNUSED(option);
Q_UNUSED(widget); Q_UNUSED(widget);
qreal pointSize = _font.bold() ? HS(_size) : _size;
qreal pointSize = _hover ? HS(_size) : _size;
painter->setPen(_color); painter->setPen(_color);
if (_showLabel) { if (_showLabel) {
QFont font; painter->setFont(_font);
font.setPixelSize(FS(_size)); painter->drawText(pointSize/2 - qMax(_labelBB.x(), 0), pointSize/2
font.setFamily(FONT_FAMILY); + _labelBB.height(), _waypoint.name());
if (_hover)
font.setBold(true);
QFontMetrics fm(font);
QRect ts = fm.tightBoundingRect(_waypoint.name());
painter->setFont(font);
painter->drawText(pointSize/2 - qMax(ts.x(), 0), pointSize/2
+ ts.height(), _waypoint.name());
} }
painter->setBrush(QBrush(_color, Qt::SolidPattern)); painter->setBrush(QBrush(_color, Qt::SolidPattern));
@ -112,7 +100,8 @@ void WaypointItem::setSize(int size)
prepareGeometryChange(); prepareGeometryChange();
_size = size; _size = size;
updateShape(); _font.setPixelSize(FS(_size));
updateCache();
} }
void WaypointItem::setColor(const QColor &color) void WaypointItem::setColor(const QColor &color)
@ -136,7 +125,7 @@ void WaypointItem::showLabel(bool show)
prepareGeometryChange(); prepareGeometryChange();
_showLabel = show; _showLabel = show;
updateShape(); updateCache();
} }
void WaypointItem::hoverEnterEvent(QGraphicsSceneHoverEvent *event) void WaypointItem::hoverEnterEvent(QGraphicsSceneHoverEvent *event)
@ -144,8 +133,8 @@ void WaypointItem::hoverEnterEvent(QGraphicsSceneHoverEvent *event)
Q_UNUSED(event); Q_UNUSED(event);
prepareGeometryChange(); prepareGeometryChange();
_hover = true; _font.setBold(true);
updateShape(); updateCache();
setZValue(zValue() + 1.0); setZValue(zValue() + 1.0);
} }
@ -154,7 +143,7 @@ void WaypointItem::hoverLeaveEvent(QGraphicsSceneHoverEvent *event)
Q_UNUSED(event); Q_UNUSED(event);
prepareGeometryChange(); prepareGeometryChange();
_hover = false; _font.setBold(false);
updateShape(); updateCache();
setZValue(zValue() - 1.0); setZValue(zValue() - 1.0);
} }

View File

@ -31,16 +31,16 @@ private:
void hoverEnterEvent(QGraphicsSceneHoverEvent *event); void hoverEnterEvent(QGraphicsSceneHoverEvent *event);
void hoverLeaveEvent(QGraphicsSceneHoverEvent *event); void hoverLeaveEvent(QGraphicsSceneHoverEvent *event);
void updateShape(); void updateCache();
QString toolTip(Units units, CoordinatesFormat format); QString toolTip(Units units, CoordinatesFormat format);
QPainterPath _shape;
Waypoint _waypoint; Waypoint _waypoint;
QPainterPath _shape;
QColor _color; QColor _color;
int _size; int _size;
bool _hover;
bool _showLabel; bool _showLabel;
QFont _font;
QRect _labelBB;
}; };
#endif // WAYPOINTITEM_H #endif // WAYPOINTITEM_H