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

Last change on this file since 15 was 15, checked in by Bayard Gildas, 7 years ago

sources reformatted with flair-format-dir script

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