forked from OlivierDelbeke/MapGraphics
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathPolygonObject.cpp
More file actions
362 lines (301 loc) · 10.2 KB
/
Copy pathPolygonObject.cpp
File metadata and controls
362 lines (301 loc) · 10.2 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
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
#include "PolygonObject.h"
#include "guts/Conversions.h"
#include "CircleObject.h"
#include <QtDebug>
#include <QKeyEvent>
PolygonObject::PolygonObject(QPolygonF geoPoly, QColor fillColor, QObject *parent) :
MapGraphicsObject(parent), _geoPoly(geoPoly), _fillColor(fillColor)
{
this->setFlag(MapGraphicsObject::ObjectIsMovable);
this->setFlag(MapGraphicsObject::ObjectIsSelectable,false);
this->setFlag(MapGraphicsObject::ObjectIsFocusable);
this->setGeoPoly(geoPoly);
}
PolygonObject::~PolygonObject()
{
qDebug() << this << "destroying";
foreach(MapGraphicsObject * circle, _editCircles)
this->destroyEditCircle(circle);
_editCircles.clear();
foreach(MapGraphicsObject * circle, _addVertexCircles)
this->destroyAddVertexCircle(circle);
_addVertexCircles.clear();
}
//pure-virtual from MapGraphicsObject
QRectF PolygonObject::boundingRect() const
{
QRectF latLonRect = _geoPoly.boundingRect();
QPointF latLonCenter = latLonRect.center();
Position latLonCenterPos(latLonCenter,0.0);
Position topLeftPos(latLonRect.topLeft(),0.0);
Position bottomRightPos(latLonRect.bottomRight(),0.0);
QPointF topLeftENU = Conversions::lla2enu(topLeftPos,latLonCenterPos).toPointF();
QPointF bottomRightENU = Conversions::lla2enu(bottomRightPos,latLonCenterPos).toPointF();
return QRectF(topLeftENU,bottomRightENU);
}
//virtual from MapGraphicsObject
bool PolygonObject::contains(const QPointF &geoPos) const
{
return _geoPoly.containsPoint(geoPos,
Qt::OddEvenFill);
}
//pure-virtual from MapGraphicsObject
void PolygonObject::paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget)
{
Q_UNUSED(option)
Q_UNUSED(widget)
painter->setRenderHint(QPainter::Antialiasing,true);
QPolygonF enuPoly;
Position latLonCenterPos(_geoPoly.boundingRect().center(),0);
foreach(QPointF latLon, _geoPoly)
{
Position latLonPos(latLon,0.0);
QPointF enu = Conversions::lla2enu(latLonPos,latLonCenterPos).toPointF();
enuPoly << enu;
}
painter->setBrush(_fillColor);
painter->drawPolygon(enuPoly);
//Populate edit and add-vertex handles if necessary.
//Is there a better place to do this? Most likely, yes.
if (_editCircles.isEmpty())
{
//Create objects to edit the polygon!
for (int i = 0; i < _geoPoly.size(); i++)
{
//Edit circles - to change the shape
CircleObject * circle = this->constructEditCircle();
circle->setPos(_geoPoly.at(i));
_editCircles.append(circle);
QPointF current = _geoPoly.at(i);
QPointF next = _geoPoly.at((i+1) % _geoPoly.size());
QPointF avg((current.x() + next.x())/2.0,
(current.y() + next.y())/2.0);
//Add vertex circles - to add new vertices
CircleObject * betweener = this->constructAddVertexCircle();
betweener->setPos(avg);
_addVertexCircles.append(betweener);
}
}
}
void PolygonObject::setPos(const QPointF & nPos)
{
/*
If the new position moved the object from the center of the bounding box made by the geo poly
then we need to move the geo poly
*/
if (nPos != _geoPoly.boundingRect().center())
{
const QPointF diff = nPos - this->pos();
//_geoPoly.translate(diff);
//We should also move our edit handles
foreach(MapGraphicsObject * circle, _editCircles)
circle->setPos(circle->pos() + diff);
//And our "add vertex" handles
this->fixAddVertexCirclePos();
}
MapGraphicsObject::setPos(nPos);
//If this isn't here, we get TEARING when we edit our polygons
this->posChanged();
}
QPolygonF PolygonObject::geoPoly() const
{
return _geoPoly;
}
void PolygonObject::setGeoPoly(const QPolygonF &newPoly)
{
if (newPoly == _geoPoly)
return;
_geoPoly = newPoly;
foreach(MapGraphicsObject * obj, _editCircles)
this->destroyEditCircle(obj);
foreach(MapGraphicsObject * obj, _addVertexCircles)
this->destroyAddVertexCircle(obj);
_editCircles.clear();
_addVertexCircles.clear();
this->setPos(newPoly.boundingRect().center());
this->polygonChanged(newPoly);
}
void PolygonObject::setFillColor(const QColor &color)
{
if (_fillColor == color)
return;
_fillColor = color;
this->redrawRequested();
}
//protected
//virtual from MapGraphicsObject
void PolygonObject::mousePressEvent(QGraphicsSceneMouseEvent *event)
{
/*
This method is a bit of a hack --- ideally contains() or some other method
we "reimplement" from PrivateQGraphicsObject should take care of this, but it
hasn't been working. This prevents the polygon from being grabbed when the user
click inside the bounding box but outside of the actual polygon
*/
const QPointF geoPos = event->scenePos();
//If the geo point is within our geo polygon, we might accept it
if (_geoPoly.containsPoint(geoPos,Qt::OddEvenFill))
MapGraphicsObject::mousePressEvent(event);
//Otherwise we ignore it
else
event->ignore();
}
//protected
//virtual from MapGraphicsObject
void PolygonObject::keyReleaseEvent(QKeyEvent *event)
{
if (event->matches(QKeySequence::Delete))
{
this->deleteLater();
event->accept();
}
else
event->ignore();
}
//private slot
void PolygonObject::handleEditCirclePosChanged()
{
QObject * sender = QObject::sender();
if (sender == 0)
return;
CircleObject * circle = qobject_cast<CircleObject *>(sender);
if (circle == 0)
return;
int index = _editCircles.indexOf(circle);
if (index == -1)
return;
QPointF newPos = circle->pos();
_geoPoly.replace(index,newPos);
this->setPos(_geoPoly.boundingRect().center());
//We need to update the positions of our "add vertex" controllers
this->fixAddVertexCirclePos();
//Emit a signal so everyone knows that the polygon changed
this->polygonChanged(this->geoPoly());
}
//private slot
void PolygonObject::handleAddVertexCircleSelected()
{
QObject * sender = QObject::sender();
if (sender == 0)
return;
CircleObject * circle = qobject_cast<CircleObject *>(sender);
if (circle == 0)
return;
//If the circle isn't selected, something is wrong
if (!circle->isSelected())
return;
//Now that we know the circle was selected, just deselect it. We don't need it selected actually
circle->setSelected(false);
//Get the position at which we should add a vertex
QPointF geoPos = circle->pos();
//The index at which we should insert the new vertex
int index = _addVertexCircles.indexOf(circle);
if (index == -1)
return;
index++;
//Put the vertex in the polygon
_geoPoly.insert(index,geoPos);
//Create a new "Edit Circle" and put it in the right spot
CircleObject * editCircle = this->constructEditCircle();
editCircle->setPos(geoPos);
_editCircles.insert(index,editCircle);
editCircle->setSelected(true);
//Create a new "Add vertex" circle and put it in the right spot
CircleObject * addVertexCircle = this->constructAddVertexCircle();
QPointF current = _geoPoly.at(index-1);
QPointF next = _geoPoly.at(index);
QPointF avg((current.x() + next.x())/2.0,
(current.y() + next.y())/2.0);
addVertexCircle->setPos(avg);
_addVertexCircles.insert(index,addVertexCircle);
this->fixAddVertexCirclePos();
//Emit a signal so everyone knows that the polygon changed
this->polygonChanged(this->geoPoly());
}
//private slot
void PolygonObject::handleEditCircleDestroyed()
{
QObject * sender = QObject::sender();
if (sender == 0)
{
qWarning() << "Can't process desyroyed edit circle. Sender is null";
return;
}
CircleObject * circle = (CircleObject *) sender;
int index = _editCircles.indexOf(circle);
if (index == -1)
{
qWarning() << "Can't process destroyed edit circle. Not contained in edit circle list";
return;
}
_geoPoly.remove(index);
_editCircles.removeAt(index);
_addVertexCircles.takeAt(index)->deleteLater();
this->fixAddVertexCirclePos();
this->redrawRequested();
this->setPos(_geoPoly.boundingRect().center());
}
//private
void PolygonObject::fixAddVertexCirclePos()
{
for (int i = 0; i < _geoPoly.size(); i++)
{
QPointF current = _geoPoly.at(i);
QPointF next = _geoPoly.at((i+1) % _geoPoly.size());
QPointF avg((current.x() + next.x())/2.0,
(current.y() + next.y())/2.0);
_addVertexCircles.at(i)->setPos(avg);
}
}
//private
CircleObject *PolygonObject::constructEditCircle()
{
CircleObject * toRet = new CircleObject(8);
connect(toRet,
SIGNAL(posChanged()),
this,
SLOT(handleEditCirclePosChanged()));
connect(toRet,
SIGNAL(destroyed()),
this,
SLOT(handleEditCircleDestroyed()));
this->newObjectGenerated(toRet);
return toRet;
}
//private
void PolygonObject::destroyEditCircle(MapGraphicsObject *obj)
{
disconnect(obj,
SIGNAL(posChanged()),
this,
SLOT(handleEditCirclePosChanged()));
disconnect(obj,
SIGNAL(destroyed()),
this,
SLOT(handleEditCircleDestroyed()));
obj->deleteLater();
}
//private
CircleObject *PolygonObject::constructAddVertexCircle()
{
CircleObject * toRet = new CircleObject(3,
true,
QColor(100,100,100,255));
toRet->setFlag(MapGraphicsObject::ObjectIsMovable,false);
connect(toRet,
SIGNAL(selectedChanged()),
this,
SLOT(handleAddVertexCircleSelected()));
this->newObjectGenerated(toRet);
toRet->setToolTip("Single-click (don't drag!) to add vertex.");
return toRet;
}
//private
void PolygonObject::destroyAddVertexCircle(MapGraphicsObject *obj)
{
disconnect(obj,
SIGNAL(selectedChanged()),
this,
SLOT(handleAddVertexCircleSelected()));
obj->deleteLater();
}