forked from Serial-Studio/Serial-Studio
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathlinestring.cpp
More file actions
229 lines (198 loc) · 5.55 KB
/
linestring.cpp
File metadata and controls
229 lines (198 loc) · 5.55 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
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
/*
*
* This file is part of QMapControl,
* an open-source cross-platform map widget
*
* Copyright (C) 2007 - 2008 Kai Winter
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Lesser General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public License
* along with QMapControl. If not, see <http://www.gnu.org/licenses/>.
*
* Contact e-mail: kaiwinter@gmx.de
* Program URL : http://qmapcontrol.sourceforge.net/
*
*/
#include "linestring.h"
namespace qmapcontrol
{
LineString::LineString()
: Curve()
{
GeometryType = "LineString";
}
LineString::LineString(QList<Point *> const points, QString name, QPen *pen)
: Curve(name)
{
mypen = pen;
LineString();
setPoints(points);
}
LineString::~LineString()
{
removePoints();
}
void LineString::removePoints()
{
QListIterator<Point *> iter(childPoints);
while (iter.hasNext())
{
Point *pt = iter.next();
if (pt && pt->parentGeometry() == this)
{
delete pt;
pt = 0;
}
}
childPoints.clear();
}
void LineString::addPoint(Point *point)
{
point->setParentGeometry(this);
childPoints.append(point);
}
QList<Point *> LineString::points()
{
return childPoints;
}
void LineString::setPoints(QList<Point *> points)
{
removePoints();
for (int i = 0; i < points.size(); i++)
{
points.at(i)->setParentGeometry(this);
}
childPoints = points;
}
void LineString::draw(QPainter *painter, const MapAdapter *mapadapter,
const QRect &screensize, const QPoint offset)
{
if (!visible)
return;
QPolygon p = QPolygon();
QPointF c;
for (int i = 0; i < childPoints.size(); i++)
{
c = childPoints[i]->coordinate();
p.append(mapadapter->coordinateToDisplay(c));
}
if (mypen != 0)
{
painter->save();
painter->setPen(*mypen);
}
painter->drawPolyline(p);
if (mypen != 0)
{
painter->restore();
}
for (int i = 0; i < childPoints.size(); i++)
{
childPoints[i]->draw(painter, mapadapter, screensize, offset);
}
}
int LineString::numberOfPoints() const
{
return childPoints.count();
}
bool LineString::Touches(Point *geom, const MapAdapter *mapadapter)
{
touchedPoints.clear();
if (points().size() < 2)
{
// really shouldn't end up here since we always add atleast points to create a
// line
return false;
}
bool touches = false;
QPointF clickPt = mapadapter->coordinateToDisplay(geom->coordinate());
qreal halfwidth = 2; // use 2 pixels by default
if (mypen && mypen->width() > 0)
{
halfwidth = static_cast<qreal>(mypen->width()) / static_cast<qreal>(2);
}
QPointF pt1 = mapadapter->coordinateToDisplay(points().at(0)->coordinate());
qreal pt1x1 = pt1.x() - halfwidth;
qreal pt1x2 = pt1.x() + halfwidth;
qreal pt1y1 = pt1.y() - halfwidth;
qreal pt1y2 = pt1.y() + halfwidth;
for (int i = 1; i < childPoints.size(); ++i)
{
QPointF pt2 = mapadapter->coordinateToDisplay(childPoints.at(i)->coordinate());
qreal pt2x1 = pt2.x() - halfwidth;
qreal pt2x2 = pt2.x() + halfwidth;
qreal pt2y1 = pt2.y() - halfwidth;
qreal pt2y2 = pt2.y() + halfwidth;
// build lazy bounding box
qreal upperLeftX = qMin(pt1x1, qMin(pt1x2, qMin(pt2x1, pt2x2)));
qreal upperLeftY = qMin(pt1y1, qMin(pt1y2, qMin(pt2y1, pt2y2)));
qreal lowerRightX = qMax(pt1x1, qMax(pt1x2, qMax(pt2x1, pt2x2)));
qreal lowerRightY = qMax(pt1y1, qMax(pt1y2, qMax(pt2y1, pt2y2)));
QRectF bounds(QPointF(upperLeftX, upperLeftY), QPointF(lowerRightX, lowerRightY));
if (bounds.contains(clickPt))
{
touchedPoints.append(childPoints.at(i));
touches = true;
}
pt1x1 = pt2x1;
pt1x2 = pt2x2;
pt1y1 = pt2y1;
pt1y2 = pt2y2;
}
if (touches)
{
emit(geometryClicked(this, QPoint(0, 0)));
}
return touches;
}
bool LineString::Touches(Geometry * /*geom*/, const MapAdapter * /*mapadapter*/)
{
touchedPoints.clear();
return false;
}
QList<Geometry *> &LineString::clickedPoints()
{
return Geometry::clickedPoints();
}
bool LineString::hasPoints() const
{
return childPoints.size() > 0 ? true : false;
}
bool LineString::hasClickedPoints() const
{
return touchedPoints.size() > 0 ? true : false;
}
QRectF LineString::boundingBox()
{
qreal minlon = 180;
qreal maxlon = -180;
qreal minlat = 90;
qreal maxlat = -90;
for (int i = 0; i < childPoints.size(); ++i)
{
Point *tmp = childPoints.at(i);
if (tmp->longitude() < minlon)
minlon = tmp->longitude();
if (tmp->longitude() > maxlon)
maxlon = tmp->longitude();
if (tmp->latitude() < minlat)
minlat = tmp->latitude();
if (tmp->latitude() > maxlat)
maxlat = tmp->latitude();
}
QPointF min = QPointF(minlon, minlat);
QPointF max = QPointF(maxlon, maxlat);
QPointF dist = max - min;
QSizeF si = QSizeF(dist.x(), dist.y());
return QRectF(min, si);
}
}