source: flair-src/trunk/tools/FlairGCS/src/mapwidget.cpp@ 66

Last change on this file since 66 was 62, checked in by Sanahuja Guillaume, 8 years ago

correction bug destruction map

File size: 10.7 KB
RevLine 
[10]1// %flair:license{
[15]2// This file is part of the Flair framework distributed under the
3// CECILL-C License, Version 1.0.
[10]4// %flair:license}
[9]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
57using namespace QtMobility;
58
[15]59MapWidget::MapWidget(Map *map, QWidget *parent) : QGraphicsView(parent) {
60 setCursor(Qt::ArrowCursor);
61 setHorizontalScrollBarPolicy(Qt::ScrollBarAlwaysOff);
62 setVerticalScrollBarPolicy(Qt::ScrollBarAlwaysOff);
[9]63
[15]64 geoMap = 0;
65 dragging = false;
66 pressed = false;
67 landmark_match = NULL;
68 is_uptodate = true;
69 this->map = map;
[9]70
[15]71 m_scene = new QGraphicsScene(this);
72 setScene(m_scene);
73 setMouseTracking(true);
[9]74
[15]75 landmarks = new QList<Landmark *>;
76 landmarks_old = new QList<Landmark *>;
[9]77}
78
[62]79MapWidget::~MapWidget() {
80 m_scene->removeItem(geoMap);
81 delete m_scene;
82}
83
[15]84QGraphicsScene *MapWidget::scene(void) const { return m_scene; }
[9]85
[15]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());
[9]91}
92
[15]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 }
[9]100
[15]101 if (event->button() == Qt::RightButton) {
102 QMenu *menu = new QMenu("nom", this);
[9]103
[15]104 if (cursor().shape() != Qt::PointingHandCursor) {
105 QAction *b, *z; //*a,
106 QList<QAction *> go_to_actions;
107 QList<QAction *> centered_actions;
[9]108
[15]109 QMenu *centered_menu = menu->addMenu("centered");
110 for (int i = 0; i < points.count(); i++) {
111 QAction *action = centered_menu->addAction(points.at(i));
112 centered_actions.append(action);
113 action->setCheckable(true);
114 if (map->centeredPoint() == i) {
115 action->setChecked(true);
116 } else {
117 action->setChecked(false);
118 }
119 }
[9]120
[15]121 menu->addSeparator();
[9]122
[15]123 b = menu->addAction("place checkpoint");
124 QMenu *go_to_menu = menu->addMenu("go to");
125 for (int i = 0; i < landmarks->count(); i++) {
126 QAction *action =
127 go_to_menu->addAction(QString("checkpoint %1").arg(i + 1));
128 go_to_actions.append(action);
129 }
130 if (landmarks->count() == 0)
131 go_to_menu->setEnabled(false);
[9]132
[15]133 map->appendmenu(menu);
134 z = map->execmenu(this, menu, event->globalPos());
[9]135
[15]136 for (int i = 0; i < centered_actions.count(); i++) {
137 if (z == centered_actions.at(i)) {
138 if (centered_actions.at(i)->isChecked()) {
139 map->setCenteredPoint(i);
140 } else {
141 map->setCenteredPoint(-1);
142 }
[9]143
[15]144 break;
[9]145 }
[15]146 }
[9]147
[15]148 if (z == b) {
149 Landmark *landmark = new Landmark(
150 geoMap, geoMap->screenPositionToCoordinate(event->pos()),
151 QString("%1").arg(landmarks->count() + 1));
152 landmarks->append(landmark);
153 is_uptodate = false;
154 }
155 for (int i = 0; i < go_to_actions.count(); i++) {
156 if (z == go_to_actions.at(i)) {
157 map->setCenteredPoint(-1);
158 geoMap->setCenter(landmarks->at(i)->coordinate());
159 break;
[9]160 }
[15]161 }
[9]162
[15]163 } else {
164 QAction *a, *z;
165 a = menu->addAction("delete");
166 z = menu->exec(event->globalPos());
167
168 if (z == a) {
169 int i;
170 for (i = 0; i < landmarks->count(); i++) {
171 if (landmarks->at(i)->contains(
172 geoMap->screenPositionToCoordinate(event->pos()))) {
173 delete landmarks->at(i);
174 landmarks->removeAt(i);
175 break;
176 }
177 }
178 for (int j = i; j < landmarks->count(); j++) {
179 landmarks->at(j)->setText(QString("%1").arg(j + 1));
180 }
181 }
[9]182 }
[15]183
184 delete menu;
185 }
[9]186}
187
[15]188void MapWidget::initDrag(void) {
189 if (pressed && map->isCentered() && landmark_match == NULL) {
190 dragging = true;
191 }
[9]192}
193
[15]194void MapWidget::mouseMoveEvent(QMouseEvent *event) {
195 if (!geoMap)
196 return;
[9]197
[15]198 Qt::CursorShape new_cursor;
199 if (dragging == true) {
200 new_cursor = Qt::ClosedHandCursor;
201 } else {
202 if (map->isCentered()) {
203 if (pressed == true) {
204 new_cursor = Qt::ForbiddenCursor;
205 } else {
206 new_cursor = Qt::ArrowCursor;
207 }
208 } else {
209 new_cursor = Qt::OpenHandCursor;
[9]210 }
[15]211 if (landmark_match != NULL)
212 new_cursor = Qt::PointingHandCursor;
213 }
[9]214
[15]215 for (int i = 0; i < landmarks->count(); i++) {
216 if (landmarks->at(i)
217 ->contains(geoMap->screenPositionToCoordinate(event->pos()))) {
218 if (pressed && landmark_match == NULL) {
219 landmark_match = landmarks->at(i);
220 landmark_match->setColor(Qt::red);
221 is_uptodate = false;
222 }
223 new_cursor = Qt::PointingHandCursor;
224 break;
[9]225 }
[15]226 }
[9]227
[15]228 if (new_cursor != cursor().shape())
229 setCursor(new_cursor);
[9]230
[15]231 QPoint v = event->pos() - dragStartPosition;
[9]232
[15]233 if (dragging) {
234 geoMap->pan(-v.x(), -v.y());
235 dragStartPosition = event->pos();
[9]236
[15]237 } else if (landmark_match != NULL) {
238 landmark_match->setCoordinate(
239 geoMap->screenPositionToCoordinate(event->pos()));
[9]240
[15]241 } else if (pressed && !map->isCentered() &&
242 v.manhattanLength() >= qApp->startDragDistance()) {
243 dragging = true;
[9]244
[15]245 } else {
246 dragStartPosition = event->pos();
247 emit positionChanged(geoMap->screenPositionToCoordinate(event->pos()));
248 }
[9]249
[15]250 event->accept();
[9]251}
252
[15]253void MapWidget::mouseReleaseEvent(QMouseEvent *event) {
254 pressed = false;
255 landmark_match = NULL;
[9]256
[15]257 if (dragging) {
258 QPoint v = event->pos() - dragStartPosition;
259 geoMap->pan(-v.x(), -v.y());
260 dragging = false;
261 }
[9]262
[15]263 event->accept();
[9]264}
265
[15]266void MapWidget::resizeEvent(QResizeEvent *event) {
267 if (geoMap) {
268 m_scene->setSceneRect(
269 QRectF(0, 0, event->size().width(), event->size().height()));
270 geoMap->resize(event->size().width(), event->size().height());
271 }
[9]272
[15]273 QGraphicsView::resizeEvent(event);
[9]274}
275
[15]276void MapWidget::wheelEvent(QWheelEvent *event) {
277 int steps = event->delta() / 120;
278 int zoom = qBound(geoMap->minimumZoomLevel(), geoMap->zoomLevel() + steps,
279 geoMap->maximumZoomLevel());
[9]280
[15]281 if (zoom != geoMap->zoomLevel())
282 geoMap->setZoomLevel(zoom);
283 // if(!centered)
284 // geoMap->setCenter(geoMap->screenPositionToCoordinate(event->pos()));
[9]285}
286
[15]287void MapWidget::mouseDoubleClickEvent(QMouseEvent *event) {
288 if (!map->isCentered())
289 geoMap->setCenter(geoMap->screenPositionToCoordinate(event->pos()));
[9]290}
291
[15]292bool MapWidget::IsUptodate(void) { return is_uptodate; }
[9]293
[15]294void MapWidget::SetUptodate(void) {
295 for (int i = 0; i < landmarks_old->count(); i++) {
296 delete landmarks_old->at(i);
297 }
298 landmarks_old->clear();
[9]299
[15]300 for (int i = 0; i < landmarks->count(); i++) {
301 landmarks->at(i)->setColor(Qt::white);
302 Landmark *landmark =
303 new Landmark(geoMap, landmarks->at(i)->coordinate(),
304 QString("%1").arg(landmarks->count() + 1));
305 landmarks_old->append(landmark);
306 landmarks_old->at(i)->setVisible(false);
307 }
[9]308
[15]309 is_uptodate = true;
[9]310}
311
[15]312void MapWidget::Reset(void) {
313 for (int i = 0; i < landmarks->count(); i++) {
314 delete landmarks->at(i);
315 }
316 landmarks->clear();
[9]317
[15]318 for (int i = 0; i < landmarks_old->count(); i++) {
319 Landmark *landmark =
320 new Landmark(geoMap, landmarks_old->at(i)->coordinate(),
321 QString("%1").arg(landmarks->count() + 1));
322 landmarks->append(landmark);
323 landmarks->at(i)->setColor(Qt::white);
324 }
[9]325
[15]326 is_uptodate = true;
[9]327}
328
[15]329QList<Landmark *> *MapWidget::Landmarks(void) { return landmarks; }
[9]330
[15]331bool MapWidget::LandmarkToSend(int index) {
332 if (index >= landmarks_old->count())
333 return true;
[9]334
[15]335 if (landmarks->at(index)->coordinate() !=
336 landmarks_old->at(index)->coordinate())
337 return true;
338 else
339 return false;
[9]340}
[15]341void MapWidget::RemoveLandmarks(void) {
342 for (int i = 0; i < landmarks->count(); i++) {
343 landmarks->at(i)->RemoveLandmark();
344 }
345 for (int i = 0; i < landmarks_old->count(); i++) {
346 landmarks_old->at(i)->RemoveLandmark();
347 }
[9]348}
349
[15]350void MapWidget::AddLandmarks(QGraphicsGeoMap *geoMap) {
351 for (int i = 0; i < landmarks->count(); i++) {
352 landmarks->at(i)->AddLandmark(geoMap);
353 }
354 for (int i = 0; i < landmarks_old->count(); i++) {
355 landmarks_old->at(i)->AddLandmark(geoMap);
356 }
[9]357}
358
[15]359void MapWidget::AddLandmark(const QGeoCoordinate &coordinate) {
360 Landmark *landmark = new Landmark(geoMap, coordinate,
361 QString("%1").arg(landmarks->count() + 1));
362 landmarks->append(landmark);
363 landmark->setColor(Qt::white);
364 landmark = new Landmark(geoMap, coordinate,
365 QString("%1").arg(landmarks_old->count() + 1));
366 landmarks_old->append(landmark);
367 landmark->setColor(Qt::white);
368 landmark->setVisible(false);
[9]369}
370
[15]371void MapWidget::AddPoint(QString name) { points.append(name); }
Note: See TracBrowser for help on using the repository browser.