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

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

correction bug destruction map

File size: 10.7 KB
Line 
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
57using namespace QtMobility;
58
59MapWidget::MapWidget(Map *map, QWidget *parent) : QGraphicsView(parent) {
60  setCursor(Qt::ArrowCursor);
61  setHorizontalScrollBarPolicy(Qt::ScrollBarAlwaysOff);
62  setVerticalScrollBarPolicy(Qt::ScrollBarAlwaysOff);
63
64  geoMap = 0;
65  dragging = false;
66  pressed = false;
67  landmark_match = NULL;
68  is_uptodate = true;
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      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      }
120
121      menu->addSeparator();
122
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);
132
133      map->appendmenu(menu);
134      z = map->execmenu(this, menu, event->globalPos());
135
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          }
143
144          break;
145        }
146      }
147
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;
160        }
161      }
162
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      }
182    }
183
184    delete menu;
185  }
186}
187
188void MapWidget::initDrag(void) {
189  if (pressed && map->isCentered() && landmark_match == NULL) {
190    dragging = true;
191  }
192}
193
194void MapWidget::mouseMoveEvent(QMouseEvent *event) {
195  if (!geoMap)
196    return;
197
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;
210    }
211    if (landmark_match != NULL)
212      new_cursor = Qt::PointingHandCursor;
213  }
214
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;
225    }
226  }
227
228  if (new_cursor != cursor().shape())
229    setCursor(new_cursor);
230
231  QPoint v = event->pos() - dragStartPosition;
232
233  if (dragging) {
234    geoMap->pan(-v.x(), -v.y());
235    dragStartPosition = event->pos();
236
237  } else if (landmark_match != NULL) {
238    landmark_match->setCoordinate(
239        geoMap->screenPositionToCoordinate(event->pos()));
240
241  } else if (pressed && !map->isCentered() &&
242             v.manhattanLength() >= qApp->startDragDistance()) {
243    dragging = true;
244
245  } else {
246    dragStartPosition = event->pos();
247    emit positionChanged(geoMap->screenPositionToCoordinate(event->pos()));
248  }
249
250  event->accept();
251}
252
253void MapWidget::mouseReleaseEvent(QMouseEvent *event) {
254  pressed = false;
255  landmark_match = NULL;
256
257  if (dragging) {
258    QPoint v = event->pos() - dragStartPosition;
259    geoMap->pan(-v.x(), -v.y());
260    dragging = false;
261  }
262
263  event->accept();
264}
265
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  }
272
273  QGraphicsView::resizeEvent(event);
274}
275
276void MapWidget::wheelEvent(QWheelEvent *event) {
277  int steps = event->delta() / 120;
278  int zoom = qBound(geoMap->minimumZoomLevel(), geoMap->zoomLevel() + steps,
279                    geoMap->maximumZoomLevel());
280
281  if (zoom != geoMap->zoomLevel())
282    geoMap->setZoomLevel(zoom);
283  // if(!centered)
284  // geoMap->setCenter(geoMap->screenPositionToCoordinate(event->pos()));
285}
286
287void MapWidget::mouseDoubleClickEvent(QMouseEvent *event) {
288  if (!map->isCentered())
289    geoMap->setCenter(geoMap->screenPositionToCoordinate(event->pos()));
290}
291
292bool MapWidget::IsUptodate(void) { return is_uptodate; }
293
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();
299
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  }
308
309  is_uptodate = true;
310}
311
312void MapWidget::Reset(void) {
313  for (int i = 0; i < landmarks->count(); i++) {
314    delete landmarks->at(i);
315  }
316  landmarks->clear();
317
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  }
325
326  is_uptodate = true;
327}
328
329QList<Landmark *> *MapWidget::Landmarks(void) { return landmarks; }
330
331bool MapWidget::LandmarkToSend(int index) {
332  if (index >= landmarks_old->count())
333    return true;
334
335  if (landmarks->at(index)->coordinate() !=
336      landmarks_old->at(index)->coordinate())
337    return true;
338  else
339    return false;
340}
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  }
348}
349
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  }
357}
358
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);
369}
370
371void MapWidget::AddPoint(QString name) { points.append(name); }
Note: See TracBrowser for help on using the repository browser.