forked from OlivierDelbeke/MapGraphics
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathLineObject.cpp
More file actions
130 lines (108 loc) · 3.48 KB
/
Copy pathLineObject.cpp
File metadata and controls
130 lines (108 loc) · 3.48 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
#include "LineObject.h"
#include "guts/Conversions.h"
#include <QtGlobal>
LineObject::LineObject(const Position &endA,
const Position &endB,
qreal thickness,
bool thicknessIsZoomInvariant,
MapGraphicsObject *parent) :
MapGraphicsObject(CoordinateMode::LocalMeters, parent),
_a(endA), _b(endB)
{
_thickness = qBound<qreal>(0.0, thickness, 5.0);
_thicknessIsZoomInvariant = thicknessIsZoomInvariant;
_color = QColor(0, 0, 0, 200);
this->updatePositionFromEndPoints();
}
LineObject::~LineObject()
{
}
//pure-virtual from MapGraphicsObject
QRectF LineObject::boundingRect() const
{
const qreal avgLat = (_a.latitude() + _b.latitude()) / 2.0;
const qreal lonPerMeter = Conversions::degreesLonPerMeter(avgLat);
const qreal latPerMeter = Conversions::degreesLatPerMeter(avgLat);
const qreal widthLon = qAbs<qreal>(_a.longitude() - _b.longitude());
const qreal heightlat = qAbs<qreal>(_a.latitude() - _b.latitude());
const qreal widthMeters = qMax<qreal>(widthLon / lonPerMeter, 5.0);
const qreal heightMeters = qMax<qreal>(heightlat / latPerMeter, 5.0);
const QRectF toRet(-1.0 * widthMeters,
-1.0 * heightMeters,
2.0 * widthMeters,
2.0 * heightMeters);
return toRet;
}
//pure-virtual from MapGraphicsObject
void LineObject::paint(QPainter *painter,
const QStyleOptionGraphicsItem *option,
QWidget *widget)
{
Q_UNUSED(option);
Q_UNUSED(widget);
painter->setRenderHint(QPainter::Antialiasing, true);
QPen pen = painter->pen();
pen.setWidthF(_thickness);
pen.setCosmetic(_thicknessIsZoomInvariant);
pen.setColor(_color);
painter->setPen(pen);
const qreal avgLat = (_a.latitude() + _b.latitude()) / 2.0;
const qreal lonPerMeter = Conversions::degreesLonPerMeter(avgLat);
const qreal latPerMeter = Conversions::degreesLatPerMeter(avgLat);
const QPointF center = this->pos();
const QPointF offsetA = _a.lonLat() - center;
const QPointF offsetB = _b.lonLat() - center;
const QPointF metersA(offsetA.x() / lonPerMeter,
offsetA.y() / latPerMeter);
const QPointF metersB(offsetB.x() / lonPerMeter,
offsetB.y() / latPerMeter);
painter->drawLine(metersA, metersB);
}
qreal LineObject::thickness() const
{
return _thickness;
}
void LineObject::setThickness(qreal nThick)
{
_thickness = qBound<qreal>(0.0, nThick, 5.0);
this->redrawRequested();
}
//public slot
void LineObject::setEndPointA(const Position &pos)
{
_a = pos;
this->updatePositionFromEndPoints();
this->redrawRequested();
}
//public slot
void LineObject::setEndPointB(const Position &pos)
{
_b = pos;
this->updatePositionFromEndPoints();
this->redrawRequested();
}
//public slot
void LineObject::setEndPoints(const Position &a,
const Position &b)
{
_a = a;
_b = b;
this->updatePositionFromEndPoints();
this->redrawRequested();
}
//private slot
void LineObject::updatePositionFromEndPoints()
{
const qreal avgLon = (_a.longitude() + _b.longitude()) / 2.0;
const qreal avgLat = (_a.latitude() + _b.latitude()) / 2.0;
this->setPos(QPointF(avgLon, avgLat));
}
QColor LineObject::color() const
{
return _color;
}
void LineObject::setColor(const QColor &color)
{
_color = color;
this->redrawRequested();
}