source: flair-src/branches/mavlink/tools/FlairGCS/src/mapwidget.cpp@ 99

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

sources reformatted with flair-format-dir script

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
79QGraphicsScene *MapWidget::scene(void) const { return m_scene; }
80
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());
86}
87
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 }
95
96 if (event->button() == Qt::RightButton) {
97 QMenu *menu = new QMenu("nom", this);
98
99 if (cursor().shape() != Qt::PointingHandCursor) {
100 QAction *b, *z; //*a,
101 QList<QAction *> go_to_actions;
102 QList<QAction *> centered_actions;
103
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 }
115
116 menu->addSeparator();
117
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);
127
128 map->appendmenu(menu);
129 z = map->execmenu(this, menu, event->globalPos());
130
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 }
138
139 break;
140 }
141 }
142
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;
155 }
156 }
157
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 }
177 }
178
179 delete menu;
180 }
181}
182
183void MapWidget::initDrag(void) {
184 if (pressed && map->isCentered() && landmark_match == NULL) {
185 dragging = true;
186 }
187}
188
189void MapWidget::mouseMoveEvent(QMouseEvent *event) {
190 if (!geoMap)
191 return;
192
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;
205 }
206 if (landmark_match != NULL)
207 new_cursor = Qt::PointingHandCursor;
208 }
209
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;
220 }
221 }
222
223 if (new_cursor != cursor().shape())
224 setCursor(new_cursor);
225
226 QPoint v = event->pos() - dragStartPosition;
227
228 if (dragging) {
229 geoMap->pan(-v.x(), -v.y());
230 dragStartPosition = event->pos();
231
232 } else if (landmark_match != NULL) {
233 landmark_match->setCoordinate(
234 geoMap->screenPositionToCoordinate(event->pos()));
235
236 } else if (pressed && !map->isCentered() &&
237 v.manhattanLength() >= qApp->startDragDistance()) {
238 dragging = true;
239
240 } else {
241 dragStartPosition = event->pos();
242 emit positionChanged(geoMap->screenPositionToCoordinate(event->pos()));
243 }
244
245 event->accept();
246}
247
248void MapWidget::mouseReleaseEvent(QMouseEvent *event) {
249 pressed = false;
250 landmark_match = NULL;
251
252 if (dragging) {
253 QPoint v = event->pos() - dragStartPosition;
254 geoMap->pan(-v.x(), -v.y());
255 dragging = false;
256 }
257
258 event->accept();
259}
260
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 }
267
268 QGraphicsView::resizeEvent(event);
269}
270
271void MapWidget::wheelEvent(QWheelEvent *event) {
272 int steps = event->delta() / 120;
273 int zoom = qBound(geoMap->minimumZoomLevel(), geoMap->zoomLevel() + steps,
274 geoMap->maximumZoomLevel());
275
276 if (zoom != geoMap->zoomLevel())
277 geoMap->setZoomLevel(zoom);
278 // if(!centered)
279 // geoMap->setCenter(geoMap->screenPositionToCoordinate(event->pos()));
280}
281
282void MapWidget::mouseDoubleClickEvent(QMouseEvent *event) {
283 if (!map->isCentered())
284 geoMap->setCenter(geoMap->screenPositionToCoordinate(event->pos()));
285}
286
287bool MapWidget::IsUptodate(void) { return is_uptodate; }
288
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();
294
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 }
303
304 is_uptodate = true;
305}
306
307void MapWidget::Reset(void) {
308 for (int i = 0; i < landmarks->count(); i++) {
309 delete landmarks->at(i);
310 }
311 landmarks->clear();
312
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 }
320
321 is_uptodate = true;
322}
323
324QList<Landmark *> *MapWidget::Landmarks(void) { return landmarks; }
325
326bool MapWidget::LandmarkToSend(int index) {
327 if (index >= landmarks_old->count())
328 return true;
329
330 if (landmarks->at(index)->coordinate() !=
331 landmarks_old->at(index)->coordinate())
332 return true;
333 else
334 return false;
335}
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 }
343}
344
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 }
352}
353
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);
364}
365
366void MapWidget::AddPoint(QString name) { points.append(name); }
Note: See TracBrowser for help on using the repository browser.