1 #include <QFontMetrics>
4 #include "coordinatesitem.h"
7 CoordinatesItem::CoordinatesItem(QGraphicsItem
*parent
) : QGraphicsItem(parent
)
9 _format
= DecimalDegrees
;
14 _drawBackground
= false;
15 _font
.setPixelSize(FONT_SIZE
);
16 _font
.setFamily(FONT_FAMILY
);
19 setAcceptHoverEvents(true);
24 void CoordinatesItem::paint(QPainter
*painter
,
25 const QStyleOptionGraphicsItem
*option
, QWidget
*widget
)
33 if (_drawBackground
) {
34 painter
->setPen(Qt::NoPen
);
37 painter
->setBrush(QBrush(bc
));
38 painter
->drawRect(_boundingRect
);
39 painter
->setBrush(Qt::NoBrush
);
42 QFontMetrics
fm(_font
);
43 painter
->setFont(_font
);
44 painter
->setPen(QPen(_color
));
45 painter
->drawText(0, -fm
.descent(), text());
47 //painter->setPen(Qt::red);
48 //painter->drawRect(boundingRect());
51 void CoordinatesItem::setCoordinates(const Coordinates
&c
, qreal elevation
)
53 prepareGeometryChange();
62 void CoordinatesItem::setFormat(CoordinatesFormat format
)
64 prepareGeometryChange();
70 void CoordinatesItem::setUnits(Units units
)
72 prepareGeometryChange();
78 void CoordinatesItem::setDigitalZoom(qreal zoom
)
81 setScale(pow(2, -_digitalZoom
));
84 QString
CoordinatesItem::text() const
86 return (std::isnan(_ele
))
87 ? Format::coordinates(_c
, _format
)
88 : Format::coordinates(_c
, _format
) + ", " + Format::elevation(_ele
, _units
);
91 void CoordinatesItem::updateBoundingRect()
93 QFontMetrics
fm(_font
);
95 QRectF
br(fm
.tightBoundingRect(text()));
98 r1
.moveTop(-fm
.ascent());
99 r2
.moveBottom(-fm
.descent());
101 _boundingRect
= r1
| r2
;
104 void CoordinatesItem::setColor(const QColor
&color
)
110 void CoordinatesItem::setBackgroundColor(const QColor
&color
)
116 void CoordinatesItem::drawBackground(bool draw
)
118 _drawBackground
= draw
;