-
Notifications
You must be signed in to change notification settings - Fork 17
/
maprectitem.cpp
291 lines (263 loc) · 9.24 KB
/
maprectitem.cpp
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
#include "maprectitem.h"
#include "graphicsmap.h"
#include <QGraphicsSceneMouseEvent>
#include <QGraphicsSceneHoverEvent>
#include <QPen>
QSet<MapRectItem*> MapRectItem::m_items;
MapRectItem::MapRectItem():
m_editable(false),
m_center(0,0),
m_size(1e3, 1e3)
{
// keep the outline width of 1-pixel when item scales
auto pen = this->pen();
pen.setWidth(1);
pen.setCosmetic(true);
this->setPen(pen);
//
m_firstCtrl.setRect(-4, -4, 8, 8);
m_secondCtrl.setRect(-4, -4, 8, 8);
m_rectCtrl.setParentItem(this);
m_firstCtrl.setParentItem(this);
m_secondCtrl.setParentItem(this);
//
pen.setColor(Qt::lightGray);
pen.setStyle(Qt::DashLine);
m_rectCtrl.setPen(pen);
m_firstCtrl.setAcceptHoverEvents(true);
m_firstCtrl.setPen(QPen(Qt::gray));
m_firstCtrl.setBrush(Qt::lightGray);
m_firstCtrl.setCursor(Qt::DragMoveCursor);
m_firstCtrl.setFlag(QGraphicsItem::ItemIgnoresTransformations);
m_firstCtrl.setFlag(QGraphicsItem::ItemIsMovable);
m_firstCtrl.setCursor(Qt::DragMoveCursor);
m_secondCtrl.setAcceptHoverEvents(true);
m_secondCtrl.setPen(QPen(Qt::gray));
m_secondCtrl.setBrush(Qt::lightGray);
m_secondCtrl.setCursor(Qt::DragMoveCursor);
m_secondCtrl.setFlag(QGraphicsItem::ItemIgnoresTransformations);
m_secondCtrl.setFlag(QGraphicsItem::ItemIsMovable);
m_secondCtrl.setCursor(Qt::DragMoveCursor);
//
m_items.insert(this);
updateEditable();
}
MapRectItem::~MapRectItem()
{
m_items.remove(this);
}
void MapRectItem::setEditable(const bool &editable)
{
if(m_editable == editable)
return;
m_editable = editable;
emit editableChanged(editable);
updateEditable();
}
bool MapRectItem::isEditable() const
{
return m_editable;
}
void MapRectItem::toggleEditable()
{
setEditable(!m_editable);
}
void MapRectItem::setCenter(const QGeoCoordinate ¢er)
{
if(center == m_center)
return;
m_center = center;
// We should to compute topleft and botoom right coordinate to keep previous size unchanged
auto leftCoord = m_center.atDistanceAndAzimuth(m_size.width()/2, -90);
auto rightCoord = m_center.atDistanceAndAzimuth(m_size.height()/2, 90);
auto topCoord = m_center.atDistanceAndAzimuth(m_size.height()/2, 0);
auto bottomCoord = m_center.atDistanceAndAzimuth(m_size.height()/2, 180);
m_topLeftCoord = {topCoord.latitude(), leftCoord.longitude()};
m_bottomRightCoord = {bottomCoord.latitude(), rightCoord.longitude()};
updateRect();
//
emit centerChanged(center);
}
void MapRectItem::setSize(const QSizeF &size)
{
if(m_size == size)
return;
m_size = size;
// We should to compute topleft and botoom right coordinate from new size
auto leftCoord = m_center.atDistanceAndAzimuth(m_size.width()/2, -90);
auto rightCoord = m_center.atDistanceAndAzimuth(m_size.height()/2, 90);
auto topCoord = m_center.atDistanceAndAzimuth(m_size.height()/2, 0);
auto bottomCoord = m_center.atDistanceAndAzimuth(m_size.height()/2, 180);
m_topLeftCoord = {topCoord.latitude(), leftCoord.longitude()};
m_bottomRightCoord = {bottomCoord.latitude(), rightCoord.longitude()};
updateRect();
//
emit sizeChanged(size);
}
void MapRectItem::setRect(const QGeoCoordinate &first, const QGeoCoordinate &second)
{
// compute the left right top bottom, which help up to compute the center and the width&height
double left, right, top, bottom;
if(first.longitude() <= second.longitude()) {
left = first.longitude();
right = second.longitude();
} else {
left = second.longitude();
right = first.longitude();
}
if(first.latitude() <= second.latitude()) {
bottom = first.latitude();
top = second.latitude();
} else {
bottom = second.latitude();
top = first.latitude();
}
// ignore setter requeset if shape and position has no differece
const QGeoCoordinate tlCoord(top, left);
const QGeoCoordinate brCoord(bottom, right);
if(tlCoord == m_topLeftCoord && brCoord == m_bottomRightCoord)
return;
m_topLeftCoord = tlCoord;
m_bottomRightCoord = brCoord;
// compute new center and size
m_center = {(top+right)/2, (left+right)/2};
auto width = QGeoCoordinate(m_center.latitude(), left).distanceTo(QGeoCoordinate(m_center.latitude(), right));
auto height = QGeoCoordinate(bottom, m_center.longitude()).distanceTo(QGeoCoordinate(top, m_center.longitude()));
m_size = {width, height};
// rebuild rect shape
updateRect();
//
emit centerChanged(m_center);
emit sizeChanged(m_size);
}
const QGeoCoordinate &MapRectItem::center() const
{
return m_center;
}
const QSizeF &MapRectItem::size() const
{
return m_size;
}
QVector<QGeoCoordinate> MapRectItem::points() const
{
QVector<QGeoCoordinate> points;
auto leftCoord = m_center.atDistanceAndAzimuth(m_size.width()/2, -90);
auto rightCoord = m_center.atDistanceAndAzimuth(m_size.height()/2, 90);
auto topCoord = m_center.atDistanceAndAzimuth(m_size.height()/2, 0);
auto bottomCoord = m_center.atDistanceAndAzimuth(m_size.height()/2, 180);
points.append({topCoord.latitude(), leftCoord.longitude(), 0});
points.append({topCoord.latitude(), rightCoord.longitude(), 0});
points.append({bottomCoord.latitude(), rightCoord.longitude(), 0});
points.append({bottomCoord.latitude(), leftCoord.longitude(), 0});
return points;
}
const QSet<MapRectItem *> &MapRectItem::items()
{
return m_items;
}
bool MapRectItem::sceneEventFilter(QGraphicsItem *watched, QEvent *event)
{
if(!m_editable)
return false;
auto ctrlPoint = watched == &m_firstCtrl ? &m_firstCtrl : &m_secondCtrl;
switch (event->type()) {
case QEvent::GraphicsSceneMouseMove:
case QEvent::GraphicsSceneMouseRelease:
{
// We should to compute center and size
// and then update rect item rect
auto firstCtrl = watched == &m_firstCtrl ? &m_firstCtrl : &m_secondCtrl;
auto secondCtrl = firstCtrl == &m_firstCtrl ? &m_secondCtrl : &m_firstCtrl;
// compute center
auto centerPoint = (firstCtrl->pos() + secondCtrl->pos()) / 2;
m_center = GraphicsMap::toCoordinate(centerPoint);
// compute left right top bottom
auto first = firstCtrl->pos();
auto second = secondCtrl->pos();
double left, right, top, bottom;
if(first.x() <= second.x()) {
left = first.x();
right = second.x();
} else {
left = second.x();
right = first.x();
}
if(first.y() <= second.y()) {
bottom = first.y();
top = second.y();
} else {
bottom = second.y();
top = first.y();
}
QPointF leftPoint(left, centerPoint.y());
QPointF rightPoint(right, centerPoint.y());
QPointF topPoint(centerPoint.x(), top);
QPointF bottomPoint(centerPoint.x(), bottom);
m_size.setWidth(GraphicsMap::toCoordinate(leftPoint).distanceTo(GraphicsMap::toCoordinate(rightPoint)));
m_size.setHeight(GraphicsMap::toCoordinate(topPoint).distanceTo(GraphicsMap::toCoordinate(bottomPoint)));
auto topLeftPoint = QPointF(left, top);
auto bottomRightPoint = QPointF(right, bottom);
m_rectCtrl.setRect({topLeftPoint, bottomRightPoint});
QGraphicsRectItem::setRect({topLeftPoint, bottomRightPoint});
//
emit centerChanged(m_center);
emit sizeChanged(m_size);
// compute
break;
}
case QEvent::GraphicsSceneHoverEnter:
{
ctrlPoint->setBrush(Qt::white);
ctrlPoint->setScale(1.2);
break;
}
case QEvent::GraphicsSceneHoverLeave:
{
ctrlPoint->setBrush(Qt::lightGray);
ctrlPoint->setScale(1);
break;
}
default:
break;
}
return false;
}
QVariant MapRectItem::itemChange(QGraphicsItem::GraphicsItemChange change, const QVariant &value)
{
if(change != ItemSceneHasChanged)
return QGraphicsRectItem::itemChange(change, value);
m_firstCtrl.installSceneEventFilter(this);
m_secondCtrl.installSceneEventFilter(this);
return QGraphicsRectItem::itemChange(change, value);
}
void MapRectItem::mouseDoubleClickEvent(QGraphicsSceneMouseEvent *event)
{
QGraphicsRectItem::mouseDoubleClickEvent(event);
emit doubleClicked();
}
/*!
* \brief MapRectItem::updateRect
* \details 更新矩形实际是根据左上和右下两个点进行重建,所以在其他Setter函数必须提前将两个计算好
*/
void MapRectItem::updateRect()
{
// compute topleft point and bottomright point in scene
auto topLeftPoint = GraphicsMap::toScene(m_topLeftCoord);
auto bottomRightPoint = GraphicsMap::toScene(m_bottomRightCoord);
// update rect outlook
QGraphicsRectItem::setRect({topLeftPoint, bottomRightPoint});
// update rect's contorl points
m_firstCtrl.setPos(topLeftPoint);
m_secondCtrl.setPos(bottomRightPoint);
m_rectCtrl.setRect({topLeftPoint, bottomRightPoint});
}
void MapRectItem::updateEditable()
{
auto pen = this->pen();
pen.setWidth(0);
pen.setColor(m_editable ? Qt::white : Qt::lightGray);
setPen(pen);
m_rectCtrl.setVisible(m_editable);
m_firstCtrl.setVisible(m_editable);
m_secondCtrl.setVisible(m_editable);
}