[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"
|
---|
[67] | 56 | #include "ConnectionLayout.h"
|
---|
[9] | 57 |
|
---|
| 58 | using namespace QtMobility;
|
---|
| 59 |
|
---|
[15] | 60 | MapWidget::MapWidget(Map *map, QWidget *parent) : QGraphicsView(parent) {
|
---|
| 61 | setCursor(Qt::ArrowCursor);
|
---|
| 62 | setHorizontalScrollBarPolicy(Qt::ScrollBarAlwaysOff);
|
---|
| 63 | setVerticalScrollBarPolicy(Qt::ScrollBarAlwaysOff);
|
---|
[9] | 64 |
|
---|
[15] | 65 | geoMap = 0;
|
---|
| 66 | dragging = false;
|
---|
| 67 | pressed = false;
|
---|
| 68 | landmark_match = NULL;
|
---|
| 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] | 79 | MapWidget::~MapWidget() {
|
---|
| 80 | m_scene->removeItem(geoMap);
|
---|
| 81 | delete m_scene;
|
---|
| 82 | }
|
---|
| 83 |
|
---|
[15] | 84 | QGraphicsScene *MapWidget::scene(void) const { return m_scene; }
|
---|
[9] | 85 |
|
---|
[15] | 86 | void 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] | 93 | void 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 |
|
---|
[67] | 109 | //add centered for every point
|
---|
[15] | 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 | }
|
---|
[9] | 121 |
|
---|
[15] | 122 | menu->addSeparator();
|
---|
[9] | 123 |
|
---|
[15] | 124 | b = menu->addAction("place checkpoint");
|
---|
[67] | 125 |
|
---|
| 126 | //add go to for every landmark
|
---|
[15] | 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);
|
---|
[9] | 135 |
|
---|
[15] | 136 | map->appendmenu(menu);
|
---|
| 137 | z = map->execmenu(this, menu, event->globalPos());
|
---|
[9] | 138 |
|
---|
[67] | 139 | //center to the desired point
|
---|
[15] | 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 | }
|
---|
[9] | 147 |
|
---|
[15] | 148 | break;
|
---|
[9] | 149 | }
|
---|
[15] | 150 | }
|
---|
[9] | 151 |
|
---|
[67] | 152 | //add a landmark
|
---|
[15] | 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;
|
---|
[9] | 164 | }
|
---|
[15] | 165 | }
|
---|
[9] | 166 |
|
---|
[67] | 167 | //delete landmark
|
---|
[15] | 168 | } else {
|
---|
| 169 | QAction *a, *z;
|
---|
| 170 | a = menu->addAction("delete");
|
---|
[67] | 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 | }
|
---|
[15] | 180 | z = menu->exec(event->globalPos());
|
---|
| 181 |
|
---|
| 182 | if (z == a) {
|
---|
[67] | 183 | //remove from landmarks_old
|
---|
| 184 | for (int i = 0; i < landmarks_old->count(); i++) {
|
---|
| 185 | if (landmarks_old->at(i)->contains(
|
---|
[15] | 186 | geoMap->screenPositionToCoordinate(event->pos()))) {
|
---|
[67] | 187 | landmarks_old->removeAt(i);
|
---|
[15] | 188 | break;
|
---|
| 189 | }
|
---|
| 190 | }
|
---|
[67] | 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++) {
|
---|
[15] | 198 | landmarks->at(j)->setText(QString("%1").arg(j + 1));
|
---|
| 199 | }
|
---|
[67] | 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));
|
---|
[15] | 208 | }
|
---|
[9] | 209 | }
|
---|
[15] | 210 |
|
---|
| 211 | delete menu;
|
---|
| 212 | }
|
---|
[9] | 213 | }
|
---|
| 214 |
|
---|
[15] | 215 | void MapWidget::initDrag(void) {
|
---|
| 216 | if (pressed && map->isCentered() && landmark_match == NULL) {
|
---|
| 217 | dragging = true;
|
---|
| 218 | }
|
---|
[9] | 219 | }
|
---|
| 220 |
|
---|
[15] | 221 | void MapWidget::mouseMoveEvent(QMouseEvent *event) {
|
---|
| 222 | if (!geoMap)
|
---|
| 223 | return;
|
---|
[9] | 224 |
|
---|
[15] | 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;
|
---|
[9] | 237 | }
|
---|
[15] | 238 | if (landmark_match != NULL)
|
---|
| 239 | new_cursor = Qt::PointingHandCursor;
|
---|
| 240 | }
|
---|
[9] | 241 |
|
---|
[15] | 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;
|
---|
[9] | 251 | }
|
---|
[15] | 252 | }
|
---|
[9] | 253 |
|
---|
[15] | 254 | if (new_cursor != cursor().shape())
|
---|
| 255 | setCursor(new_cursor);
|
---|
[9] | 256 |
|
---|
[15] | 257 | QPoint v = event->pos() - dragStartPosition;
|
---|
[9] | 258 |
|
---|
[15] | 259 | if (dragging) {
|
---|
| 260 | geoMap->pan(-v.x(), -v.y());
|
---|
| 261 | dragStartPosition = event->pos();
|
---|
[9] | 262 |
|
---|
[15] | 263 | } else if (landmark_match != NULL) {
|
---|
| 264 | landmark_match->setCoordinate(
|
---|
| 265 | geoMap->screenPositionToCoordinate(event->pos()));
|
---|
[9] | 266 |
|
---|
[15] | 267 | } else if (pressed && !map->isCentered() &&
|
---|
| 268 | v.manhattanLength() >= qApp->startDragDistance()) {
|
---|
| 269 | dragging = true;
|
---|
[9] | 270 |
|
---|
[15] | 271 | } else {
|
---|
| 272 | dragStartPosition = event->pos();
|
---|
| 273 | emit positionChanged(geoMap->screenPositionToCoordinate(event->pos()));
|
---|
| 274 | }
|
---|
[9] | 275 |
|
---|
[15] | 276 | event->accept();
|
---|
[9] | 277 | }
|
---|
| 278 |
|
---|
[15] | 279 | void MapWidget::mouseReleaseEvent(QMouseEvent *event) {
|
---|
| 280 | pressed = false;
|
---|
| 281 | landmark_match = NULL;
|
---|
[9] | 282 |
|
---|
[15] | 283 | if (dragging) {
|
---|
| 284 | QPoint v = event->pos() - dragStartPosition;
|
---|
| 285 | geoMap->pan(-v.x(), -v.y());
|
---|
| 286 | dragging = false;
|
---|
| 287 | }
|
---|
[9] | 288 |
|
---|
[15] | 289 | event->accept();
|
---|
[9] | 290 | }
|
---|
| 291 |
|
---|
[15] | 292 | void 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 | }
|
---|
[9] | 298 |
|
---|
[15] | 299 | QGraphicsView::resizeEvent(event);
|
---|
[9] | 300 | }
|
---|
| 301 |
|
---|
[15] | 302 | void MapWidget::wheelEvent(QWheelEvent *event) {
|
---|
| 303 | int steps = event->delta() / 120;
|
---|
| 304 | int zoom = qBound(geoMap->minimumZoomLevel(), geoMap->zoomLevel() + steps,
|
---|
| 305 | geoMap->maximumZoomLevel());
|
---|
[9] | 306 |
|
---|
[15] | 307 | if (zoom != geoMap->zoomLevel())
|
---|
| 308 | geoMap->setZoomLevel(zoom);
|
---|
| 309 | // if(!centered)
|
---|
| 310 | // geoMap->setCenter(geoMap->screenPositionToCoordinate(event->pos()));
|
---|
[9] | 311 | }
|
---|
| 312 |
|
---|
[15] | 313 | void MapWidget::mouseDoubleClickEvent(QMouseEvent *event) {
|
---|
| 314 | if (!map->isCentered())
|
---|
| 315 | geoMap->setCenter(geoMap->screenPositionToCoordinate(event->pos()));
|
---|
[9] | 316 | }
|
---|
| 317 |
|
---|
[67] | 318 | bool 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 | }
|
---|
[9] | 324 |
|
---|
[15] | 325 | void MapWidget::SetUptodate(void) {
|
---|
| 326 | for (int i = 0; i < landmarks_old->count(); i++) {
|
---|
| 327 | delete landmarks_old->at(i);
|
---|
| 328 | }
|
---|
| 329 | landmarks_old->clear();
|
---|
[9] | 330 |
|
---|
[15] | 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 | }
|
---|
[9] | 339 | }
|
---|
| 340 |
|
---|
[15] | 341 | void MapWidget::Reset(void) {
|
---|
| 342 | for (int i = 0; i < landmarks->count(); i++) {
|
---|
| 343 | delete landmarks->at(i);
|
---|
| 344 | }
|
---|
| 345 | landmarks->clear();
|
---|
[9] | 346 |
|
---|
[15] | 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 | }
|
---|
[9] | 354 | }
|
---|
| 355 |
|
---|
[15] | 356 | QList<Landmark *> *MapWidget::Landmarks(void) { return landmarks; }
|
---|
[9] | 357 |
|
---|
[15] | 358 | bool MapWidget::LandmarkToSend(int index) {
|
---|
| 359 | if (index >= landmarks_old->count())
|
---|
| 360 | return true;
|
---|
[9] | 361 |
|
---|
[15] | 362 | if (landmarks->at(index)->coordinate() !=
|
---|
| 363 | landmarks_old->at(index)->coordinate())
|
---|
| 364 | return true;
|
---|
| 365 | else
|
---|
| 366 | return false;
|
---|
[9] | 367 | }
|
---|
[15] | 368 | void 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 | }
|
---|
[9] | 375 | }
|
---|
| 376 |
|
---|
[15] | 377 | void 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 | }
|
---|
[9] | 384 | }
|
---|
| 385 |
|
---|
[15] | 386 | void 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);
|
---|
[9] | 396 | }
|
---|
| 397 |
|
---|
[15] | 398 | void MapWidget::AddPoint(QString name) { points.append(name); }
|
---|