source: flair-src/branches/sanscv/tools/FlairGCS/src/mapwidget.cpp@ 412

Last change on this file since 412 was 324, checked in by Sanahuja Guillaume, 5 years ago

removing opencv dependency

File size: 11.9 KB
RevLine 
[324]1// %flair:license{
2// This file is part of the Flair framework distributed under the
3// CECILL-C License, Version 1.0.
4// %flair:license}
5/****************************************************************************
6**
7** Copyright (C) 2011 Nokia Corporation and/or its subsidiary(-ies).
8** All rights reserved.
9** Contact: Nokia Corporation (qt-info@nokia.com)
10**
11** This file is part of the documentation of Qt. It was originally
12** published as part of Qt Quarterly.
13**
14** $QT_BEGIN_LICENSE:LGPL$
15** Commercial Usage
16** Licensees holding valid Qt Commercial licenses may use this file in
17** accordance with the Qt Commercial License Agreement provided with the
18** Software or, alternatively, in accordance with the terms contained in
19** a written agreement between you and Nokia.
20**
21** GNU Lesser General Public License Usage
22** Alternatively, this file may be used under the terms of the GNU Lesser
23** General Public License version 2.1 as published by the Free Software
24** Foundation and appearing in the file LICENSE.LGPL included in the
25** packaging of this file. Please review the following information to
26** ensure the GNU Lesser General Public License version 2.1 requirements
27** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
28**
29** In addition, as a special exception, Nokia gives you certain additional
30** rights. These rights are described in the Nokia Qt LGPL Exception
31** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
32**
33** GNU General Public License Usage
34** Alternatively, this file may be used under the terms of the GNU
35** General Public License version 3.0 as published by the Free Software
36** Foundation and appearing in the file LICENSE.GPL included in the
37** packaging of this file. Please review the following information to
38** ensure the GNU General Public License version 3.0 requirements will be
39** met: http://www.gnu.org/copyleft/gpl.html.
40**
41** If you have questions regarding the use of this file, please contact
42** Nokia at qt-info@nokia.com.
43** $QT_END_LICENSE$
44**
45****************************************************************************/
46
47#include <QApplication>
48#include <QGeoCoordinate>
49#include <QGraphicsScene>
50
51#include <QTimer>
52#include <QMenu>
53#include "mapwidget.h"
54#include "Landmark.h"
55#include "Map.h"
56#include "ConnectionLayout.h"
57
58using namespace QtMobility;
59
60MapWidget::MapWidget(Map *map, QWidget *parent) : QGraphicsView(parent) {
61 setCursor(Qt::ArrowCursor);
62 setHorizontalScrollBarPolicy(Qt::ScrollBarAlwaysOff);
63 setVerticalScrollBarPolicy(Qt::ScrollBarAlwaysOff);
64
65 geoMap = 0;
66 dragging = false;
67 pressed = false;
68 landmark_match = NULL;
69 this->map = map;
70
71 m_scene = new QGraphicsScene(this);
72 setScene(m_scene);
73 setMouseTracking(true);
74
75 landmarks = new QList<Landmark *>;
76 landmarks_old = new QList<Landmark *>;
77}
78
79MapWidget::~MapWidget() {
80 m_scene->removeItem(geoMap);
81 delete m_scene;
82}
83
84QGraphicsScene *MapWidget::scene(void) const { return m_scene; }
85
86void MapWidget::setMap(QGraphicsGeoMap *geoMap) {
87 m_scene->clear();
88 m_scene->addItem(geoMap);
89 this->geoMap = geoMap;
90 geoMap->resize(m_scene->sceneRect().width(), m_scene->sceneRect().height());
91}
92
93void MapWidget::mousePressEvent(QMouseEvent *event) {
94 if (event->button() == Qt::LeftButton) {
95 pressed = true;
96 dragStartPosition = event->pos();
97 QTimer::singleShot(qApp->startDragTime(), this, SLOT(initDrag()));
98 event->accept();
99 }
100
101 if (event->button() == Qt::RightButton) {
102 QMenu *menu = new QMenu("nom", this);
103
104 if (cursor().shape() != Qt::PointingHandCursor) {
105 QAction *b, *z; //*a,
106 QList<QAction *> go_to_actions;
107 QList<QAction *> centered_actions;
108
109 //add centered for every point
110 QMenu *centered_menu = menu->addMenu("centered");
111 for (int i = 0; i < points.count(); i++) {
112 QAction *action = centered_menu->addAction(points.at(i));
113 centered_actions.append(action);
114 action->setCheckable(true);
115 if (map->centeredPoint() == i) {
116 action->setChecked(true);
117 } else {
118 action->setChecked(false);
119 }
120 }
121
122 menu->addSeparator();
123
124 b = menu->addAction("place checkpoint");
125
126 //add go to for every landmark
127 QMenu *go_to_menu = menu->addMenu("go to");
128 for (int i = 0; i < landmarks->count(); i++) {
129 QAction *action =
130 go_to_menu->addAction(QString("checkpoint %1").arg(i + 1));
131 go_to_actions.append(action);
132 }
133 if (landmarks->count() == 0)
134 go_to_menu->setEnabled(false);
135
136 map->appendmenu(menu);
137 z = map->execmenu(this, menu, event->globalPos());
138
139 //center to the desired point
140 for (int i = 0; i < centered_actions.count(); i++) {
141 if (z == centered_actions.at(i)) {
142 if (centered_actions.at(i)->isChecked()) {
143 map->setCenteredPoint(i);
144 } else {
145 map->setCenteredPoint(-1);
146 }
147
148 break;
149 }
150 }
151
152 //add a landmark
153 if (z == b) {
154 Landmark *landmark = new Landmark(
155 geoMap, geoMap->screenPositionToCoordinate(event->pos()),
156 QString("%1").arg(landmarks->count() + 1));
157 landmarks->append(landmark);
158 }
159 for (int i = 0; i < go_to_actions.count(); i++) {
160 if (z == go_to_actions.at(i)) {
161 map->setCenteredPoint(-1);
162 geoMap->setCenter(landmarks->at(i)->coordinate());
163 break;
164 }
165 }
166
167 //delete landmark
168 } else {
169 QAction *a, *z;
170 a = menu->addAction("delete");
171 //get selected landmark id
172 int landmark_index;
173 for (landmark_index = 0; landmark_index < landmarks->count(); landmark_index++) {
174 if (landmarks->at(landmark_index)->contains(
175 geoMap->screenPositionToCoordinate(event->pos()))) {
176 a->setEnabled(landmarks->at(landmark_index)->IsUptodate());
177 break;
178 }
179 }
180 z = menu->exec(event->globalPos());
181
182 if (z == a) {
183 //remove from landmarks_old
184 for (int i = 0; i < landmarks_old->count(); i++) {
185 if (landmarks_old->at(i)->contains(
186 geoMap->screenPositionToCoordinate(event->pos()))) {
187 landmarks_old->removeAt(i);
188 break;
189 }
190 }
191
192 //remove from landmarks
193 delete landmarks->at(landmark_index);
194 landmarks->removeAt(landmark_index);
195
196 //reorder the remaining ones
197 for (int j = landmark_index; j < landmarks->count(); j++) {
198 landmarks->at(j)->setText(QString("%1").arg(j + 1));
199 }
200
201 //send delete
202 map->RemoveAllAttributes();
203 map->SetAttribute("lat" + QString::number(landmark_index),"delete");
204 map->SetAttribute("long" + QString::number(landmark_index),"delete");
205 map->connectionLayout()->XmlToSend(map->XmlDoc());
206 map->RemoveAttribute("lat" + QString::number(landmark_index));
207 map->RemoveAttribute("long" + QString::number(landmark_index));
208 }
209 }
210
211 delete menu;
212 }
213}
214
215void MapWidget::initDrag(void) {
216 if (pressed && map->isCentered() && landmark_match == NULL) {
217 dragging = true;
218 }
219}
220
221void MapWidget::mouseMoveEvent(QMouseEvent *event) {
222 if (!geoMap)
223 return;
224
225 Qt::CursorShape new_cursor;
226 if (dragging == true) {
227 new_cursor = Qt::ClosedHandCursor;
228 } else {
229 if (map->isCentered()) {
230 if (pressed == true) {
231 new_cursor = Qt::ForbiddenCursor;
232 } else {
233 new_cursor = Qt::ArrowCursor;
234 }
235 } else {
236 new_cursor = Qt::OpenHandCursor;
237 }
238 if (landmark_match != NULL)
239 new_cursor = Qt::PointingHandCursor;
240 }
241
242 for (int i = 0; i < landmarks->count(); i++) {
243 if (landmarks->at(i)
244 ->contains(geoMap->screenPositionToCoordinate(event->pos()))) {
245 if (pressed && landmark_match == NULL) {
246 landmark_match = landmarks->at(i);
247 landmark_match->setColor(Qt::red);
248 }
249 new_cursor = Qt::PointingHandCursor;
250 break;
251 }
252 }
253
254 if (new_cursor != cursor().shape())
255 setCursor(new_cursor);
256
257 QPoint v = event->pos() - dragStartPosition;
258
259 if (dragging) {
260 geoMap->pan(-v.x(), -v.y());
261 dragStartPosition = event->pos();
262
263 } else if (landmark_match != NULL) {
264 landmark_match->setCoordinate(
265 geoMap->screenPositionToCoordinate(event->pos()));
266
267 } else if (pressed && !map->isCentered() &&
268 v.manhattanLength() >= qApp->startDragDistance()) {
269 dragging = true;
270
271 } else {
272 dragStartPosition = event->pos();
273 emit positionChanged(geoMap->screenPositionToCoordinate(event->pos()));
274 }
275
276 event->accept();
277}
278
279void MapWidget::mouseReleaseEvent(QMouseEvent *event) {
280 pressed = false;
281 landmark_match = NULL;
282
283 if (dragging) {
284 QPoint v = event->pos() - dragStartPosition;
285 geoMap->pan(-v.x(), -v.y());
286 dragging = false;
287 }
288
289 event->accept();
290}
291
292void MapWidget::resizeEvent(QResizeEvent *event) {
293 if (geoMap) {
294 m_scene->setSceneRect(
295 QRectF(0, 0, event->size().width(), event->size().height()));
296 geoMap->resize(event->size().width(), event->size().height());
297 }
298
299 QGraphicsView::resizeEvent(event);
300}
301
302void MapWidget::wheelEvent(QWheelEvent *event) {
303 int steps = event->delta() / 120;
304 int zoom = qBound(geoMap->minimumZoomLevel(), geoMap->zoomLevel() + steps,
305 geoMap->maximumZoomLevel());
306
307 if (zoom != geoMap->zoomLevel())
308 geoMap->setZoomLevel(zoom);
309 // if(!centered)
310 // geoMap->setCenter(geoMap->screenPositionToCoordinate(event->pos()));
311}
312
313void MapWidget::mouseDoubleClickEvent(QMouseEvent *event) {
314 if (!map->isCentered())
315 geoMap->setCenter(geoMap->screenPositionToCoordinate(event->pos()));
316}
317
318bool MapWidget::IsUptodate(void) {
319 for (int i = 0; i < landmarks->count(); i++) {
320 if(!landmarks->at(i)->IsUptodate()) return false;
321 }
322 return true;
323}
324
325void MapWidget::SetUptodate(void) {
326 for (int i = 0; i < landmarks_old->count(); i++) {
327 delete landmarks_old->at(i);
328 }
329 landmarks_old->clear();
330
331 for (int i = 0; i < landmarks->count(); i++) {
332 landmarks->at(i)->setColor(Qt::white);
333 Landmark *landmark =
334 new Landmark(geoMap, landmarks->at(i)->coordinate(),
335 QString("%1").arg(landmarks->count() + 1));
336 landmarks_old->append(landmark);
337 landmarks_old->at(i)->setVisible(false);
338 }
339}
340
341void MapWidget::Reset(void) {
342 for (int i = 0; i < landmarks->count(); i++) {
343 delete landmarks->at(i);
344 }
345 landmarks->clear();
346
347 for (int i = 0; i < landmarks_old->count(); i++) {
348 Landmark *landmark =
349 new Landmark(geoMap, landmarks_old->at(i)->coordinate(),
350 QString("%1").arg(landmarks->count() + 1));
351 landmarks->append(landmark);
352 landmarks->at(i)->setColor(Qt::white);
353 }
354}
355
356QList<Landmark *> *MapWidget::Landmarks(void) { return landmarks; }
357
358bool MapWidget::LandmarkToSend(int index) {
359 if (index >= landmarks_old->count())
360 return true;
361
362 if (landmarks->at(index)->coordinate() !=
363 landmarks_old->at(index)->coordinate())
364 return true;
365 else
366 return false;
367}
368void MapWidget::RemoveLandmarks(void) {
369 for (int i = 0; i < landmarks->count(); i++) {
370 landmarks->at(i)->RemoveLandmark();
371 }
372 for (int i = 0; i < landmarks_old->count(); i++) {
373 landmarks_old->at(i)->RemoveLandmark();
374 }
375}
376
377void MapWidget::AddLandmarks(QGraphicsGeoMap *geoMap) {
378 for (int i = 0; i < landmarks->count(); i++) {
379 landmarks->at(i)->AddLandmark(geoMap);
380 }
381 for (int i = 0; i < landmarks_old->count(); i++) {
382 landmarks_old->at(i)->AddLandmark(geoMap);
383 }
384}
385
386void MapWidget::AddLandmark(const QGeoCoordinate &coordinate) {
387 Landmark *landmark = new Landmark(geoMap, coordinate,
388 QString("%1").arg(landmarks->count() + 1));
389 landmarks->append(landmark);
390 landmark->setColor(Qt::white);
391 landmark = new Landmark(geoMap, coordinate,
392 QString("%1").arg(landmarks_old->count() + 1));
393 landmarks_old->append(landmark);
394 landmark->setColor(Qt::white);
395 landmark->setVisible(false);
396}
397
398void MapWidget::AddPoint(QString name) { points.append(name); }
Note: See TracBrowser for help on using the repository browser.