| 1 | /****************************************************************************
|
---|
| 2 | **
|
---|
| 3 | ** Copyright (C) 2011 Nokia Corporation and/or its subsidiary(-ies).
|
---|
| 4 | ** All rights reserved.
|
---|
| 5 | ** Contact: Nokia Corporation (qt-info@nokia.com)
|
---|
| 6 | **
|
---|
| 7 | ** This file is part of the documentation of Qt. It was originally
|
---|
| 8 | ** published as part of Qt Quarterly.
|
---|
| 9 | **
|
---|
| 10 | ** $QT_BEGIN_LICENSE:LGPL$
|
---|
| 11 | ** Commercial Usage
|
---|
| 12 | ** Licensees holding valid Qt Commercial licenses may use this file in
|
---|
| 13 | ** accordance with the Qt Commercial License Agreement provided with the
|
---|
| 14 | ** Software or, alternatively, in accordance with the terms contained in
|
---|
| 15 | ** a written agreement between you and Nokia.
|
---|
| 16 | **
|
---|
| 17 | ** GNU Lesser General Public License Usage
|
---|
| 18 | ** Alternatively, this file may be used under the terms of the GNU Lesser
|
---|
| 19 | ** General Public License version 2.1 as published by the Free Software
|
---|
| 20 | ** Foundation and appearing in the file LICENSE.LGPL included in the
|
---|
| 21 | ** packaging of this file. Please review the following information to
|
---|
| 22 | ** ensure the GNU Lesser General Public License version 2.1 requirements
|
---|
| 23 | ** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
|
---|
| 24 | **
|
---|
| 25 | ** In addition, as a special exception, Nokia gives you certain additional
|
---|
| 26 | ** rights. These rights are described in the Nokia Qt LGPL Exception
|
---|
| 27 | ** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
|
---|
| 28 | **
|
---|
| 29 | ** GNU General Public License Usage
|
---|
| 30 | ** Alternatively, this file may be used under the terms of the GNU
|
---|
| 31 | ** General Public License version 3.0 as published by the Free Software
|
---|
| 32 | ** Foundation and appearing in the file LICENSE.GPL included in the
|
---|
| 33 | ** packaging of this file. Please review the following information to
|
---|
| 34 | ** ensure the GNU General Public License version 3.0 requirements will be
|
---|
| 35 | ** met: http://www.gnu.org/copyleft/gpl.html.
|
---|
| 36 | **
|
---|
| 37 | ** If you have questions regarding the use of this file, please contact
|
---|
| 38 | ** Nokia at qt-info@nokia.com.
|
---|
| 39 | ** $QT_END_LICENSE$
|
---|
| 40 | **
|
---|
| 41 | ****************************************************************************/
|
---|
| 42 |
|
---|
| 43 | #include <QApplication>
|
---|
| 44 | #include <QGeoCoordinate>
|
---|
| 45 | #include <QGraphicsScene>
|
---|
| 46 |
|
---|
| 47 | #include <QTimer>
|
---|
| 48 | #include <QMenu>
|
---|
| 49 | #include "mapwidget.h"
|
---|
| 50 | #include "Landmark.h"
|
---|
| 51 | #include "Map.h"
|
---|
| 52 |
|
---|
| 53 | using namespace QtMobility;
|
---|
| 54 |
|
---|
| 55 | MapWidget::MapWidget(Map* map,QWidget *parent)
|
---|
| 56 | : QGraphicsView(parent) {
|
---|
| 57 | setCursor(Qt::ArrowCursor);
|
---|
| 58 | setHorizontalScrollBarPolicy(Qt::ScrollBarAlwaysOff);
|
---|
| 59 | setVerticalScrollBarPolicy(Qt::ScrollBarAlwaysOff);
|
---|
| 60 |
|
---|
| 61 | geoMap = 0;
|
---|
| 62 | dragging = false;
|
---|
| 63 | pressed = false;
|
---|
| 64 | landmark_match=NULL;
|
---|
| 65 | is_uptodate=true;
|
---|
| 66 | this->map=map;
|
---|
| 67 |
|
---|
| 68 | m_scene = new QGraphicsScene(this);
|
---|
| 69 | setScene(m_scene);
|
---|
| 70 | setMouseTracking(true);
|
---|
| 71 |
|
---|
| 72 | landmarks=new QList<Landmark*>;
|
---|
| 73 | landmarks_old=new QList<Landmark*>;
|
---|
| 74 | }
|
---|
| 75 |
|
---|
| 76 | QGraphicsScene *MapWidget::scene(void) const
|
---|
| 77 | {
|
---|
| 78 | return m_scene;
|
---|
| 79 | }
|
---|
| 80 |
|
---|
| 81 | void MapWidget::setMap(QGraphicsGeoMap *geoMap)
|
---|
| 82 | {
|
---|
| 83 | m_scene->clear();
|
---|
| 84 | m_scene->addItem(geoMap);
|
---|
| 85 | this->geoMap = geoMap;
|
---|
| 86 | geoMap->resize(m_scene->sceneRect().width(), m_scene->sceneRect().height());
|
---|
| 87 | }
|
---|
| 88 |
|
---|
| 89 | void MapWidget::mousePressEvent(QMouseEvent *event)
|
---|
| 90 | {
|
---|
| 91 | if (event->button() == Qt::LeftButton)
|
---|
| 92 | {
|
---|
| 93 | pressed = true;
|
---|
| 94 | dragStartPosition = event->pos();
|
---|
| 95 | QTimer::singleShot(qApp->startDragTime(), this, SLOT(initDrag()));
|
---|
| 96 | event->accept();
|
---|
| 97 | }
|
---|
| 98 |
|
---|
| 99 | if (event->button() == Qt::RightButton)
|
---|
| 100 | {
|
---|
| 101 | QMenu *menu = new QMenu("nom", this);
|
---|
| 102 |
|
---|
| 103 | if(cursor().shape()!=Qt::PointingHandCursor)
|
---|
| 104 | {
|
---|
| 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 | {
|
---|
| 112 | QAction* action=centered_menu->addAction(points.at(i));
|
---|
| 113 | centered_actions.append(action);
|
---|
| 114 | action->setCheckable(true);
|
---|
| 115 | if(map->centeredPoint()==i)
|
---|
| 116 | {
|
---|
| 117 | action->setChecked(true);
|
---|
| 118 | }
|
---|
| 119 | else
|
---|
| 120 | {
|
---|
| 121 | action->setChecked(false);
|
---|
| 122 | }
|
---|
| 123 | }
|
---|
| 124 |
|
---|
| 125 | menu->addSeparator();
|
---|
| 126 |
|
---|
| 127 | b=menu->addAction("place checkpoint");
|
---|
| 128 | QMenu *go_to_menu=menu->addMenu("go to");
|
---|
| 129 | for(int i=0;i<landmarks->count();i++)
|
---|
| 130 | {
|
---|
| 131 | QAction* action=go_to_menu->addAction(QString("checkpoint %1").arg(i+1));
|
---|
| 132 | go_to_actions.append(action);
|
---|
| 133 | }
|
---|
| 134 | if(landmarks->count()==0) go_to_menu->setEnabled(false);
|
---|
| 135 |
|
---|
| 136 | map->appendmenu(menu);
|
---|
| 137 | z=map->execmenu(this,menu,event->globalPos());
|
---|
| 138 |
|
---|
| 139 | for(int i=0;i<centered_actions.count();i++)
|
---|
| 140 | {
|
---|
| 141 | if(z==centered_actions.at(i))
|
---|
| 142 | {
|
---|
| 143 | if(centered_actions.at(i)->isChecked())
|
---|
| 144 | {
|
---|
| 145 | map->setCenteredPoint(i);
|
---|
| 146 | }
|
---|
| 147 | else
|
---|
| 148 | {
|
---|
| 149 | map->setCenteredPoint(-1);
|
---|
| 150 | }
|
---|
| 151 |
|
---|
| 152 | break;
|
---|
| 153 | }
|
---|
| 154 | }
|
---|
| 155 |
|
---|
| 156 | if(z==b)
|
---|
| 157 | {
|
---|
| 158 | Landmark* landmark=new Landmark(geoMap,geoMap->screenPositionToCoordinate(event->pos()),QString("%1").arg(landmarks->count()+1));
|
---|
| 159 | landmarks->append(landmark);
|
---|
| 160 | is_uptodate=false;
|
---|
| 161 | }
|
---|
| 162 | for(int i=0;i<go_to_actions.count();i++)
|
---|
| 163 | {
|
---|
| 164 | if(z==go_to_actions.at(i))
|
---|
| 165 | {
|
---|
| 166 | map->setCenteredPoint(-1);
|
---|
| 167 | geoMap->setCenter(landmarks->at(i)->coordinate());
|
---|
| 168 | break;
|
---|
| 169 | }
|
---|
| 170 | }
|
---|
| 171 |
|
---|
| 172 | }
|
---|
| 173 | else
|
---|
| 174 | {
|
---|
| 175 | QAction *a,*z;
|
---|
| 176 | a=menu->addAction("delete");
|
---|
| 177 | z=menu->exec(event->globalPos());
|
---|
| 178 |
|
---|
| 179 | if(z==a)
|
---|
| 180 | {
|
---|
| 181 | int i;
|
---|
| 182 | for(i=0;i<landmarks->count();i++)
|
---|
| 183 | {
|
---|
| 184 | if(landmarks->at(i)->contains(geoMap->screenPositionToCoordinate(event->pos())))
|
---|
| 185 | {
|
---|
| 186 | delete landmarks->at(i);
|
---|
| 187 | landmarks->removeAt(i);
|
---|
| 188 | break;
|
---|
| 189 | }
|
---|
| 190 | }
|
---|
| 191 | for(int j=i;j<landmarks->count();j++)
|
---|
| 192 | {
|
---|
| 193 | landmarks->at(j)->setText(QString("%1").arg(j+1));
|
---|
| 194 | }
|
---|
| 195 | }
|
---|
| 196 | }
|
---|
| 197 |
|
---|
| 198 | delete menu;
|
---|
| 199 | }
|
---|
| 200 | }
|
---|
| 201 |
|
---|
| 202 | void MapWidget::initDrag(void)
|
---|
| 203 | {
|
---|
| 204 | if (pressed && map->isCentered() && landmark_match==NULL) {
|
---|
| 205 | dragging = true;
|
---|
| 206 | }
|
---|
| 207 | }
|
---|
| 208 |
|
---|
| 209 | void MapWidget::mouseMoveEvent(QMouseEvent *event)
|
---|
| 210 | {
|
---|
| 211 | if (!geoMap)
|
---|
| 212 | return;
|
---|
| 213 |
|
---|
| 214 | Qt::CursorShape new_cursor;
|
---|
| 215 | if(dragging==true)
|
---|
| 216 | {
|
---|
| 217 | new_cursor=Qt::ClosedHandCursor;
|
---|
| 218 | }
|
---|
| 219 | else
|
---|
| 220 | {
|
---|
| 221 | if(map->isCentered())
|
---|
| 222 | {
|
---|
| 223 | if(pressed==true)
|
---|
| 224 | {
|
---|
| 225 | new_cursor=Qt::ForbiddenCursor;
|
---|
| 226 | }
|
---|
| 227 | else
|
---|
| 228 | {
|
---|
| 229 | new_cursor=Qt::ArrowCursor;
|
---|
| 230 | }
|
---|
| 231 | }
|
---|
| 232 | else
|
---|
| 233 | {
|
---|
| 234 | new_cursor=Qt::OpenHandCursor;
|
---|
| 235 | }
|
---|
| 236 | if(landmark_match!=NULL) new_cursor=Qt::PointingHandCursor;
|
---|
| 237 | }
|
---|
| 238 |
|
---|
| 239 |
|
---|
| 240 | for(int i=0;i<landmarks->count();i++)
|
---|
| 241 | {
|
---|
| 242 | if(landmarks->at(i)->contains(geoMap->screenPositionToCoordinate(event->pos())))
|
---|
| 243 | {
|
---|
| 244 | if(pressed && landmark_match==NULL)
|
---|
| 245 | {
|
---|
| 246 | landmark_match=landmarks->at(i);
|
---|
| 247 | landmark_match->setColor(Qt::red);
|
---|
| 248 | is_uptodate=false;
|
---|
| 249 | }
|
---|
| 250 | new_cursor=Qt::PointingHandCursor;
|
---|
| 251 | break;
|
---|
| 252 | }
|
---|
| 253 | }
|
---|
| 254 |
|
---|
| 255 | if(new_cursor!=cursor().shape()) setCursor(new_cursor);
|
---|
| 256 |
|
---|
| 257 | QPoint v = event->pos() - dragStartPosition;
|
---|
| 258 |
|
---|
| 259 | if (dragging) {
|
---|
| 260 | geoMap->pan(-v.x(), -v.y());
|
---|
| 261 | dragStartPosition = event->pos();
|
---|
| 262 |
|
---|
| 263 | } else if(landmark_match!=NULL){
|
---|
| 264 | landmark_match->setCoordinate(geoMap->screenPositionToCoordinate(event->pos()));
|
---|
| 265 |
|
---|
| 266 | } else if (pressed && !map->isCentered() &&
|
---|
| 267 | v.manhattanLength() >= qApp->startDragDistance()) {
|
---|
| 268 | dragging = true;
|
---|
| 269 |
|
---|
| 270 | } else {
|
---|
| 271 | dragStartPosition = event->pos();
|
---|
| 272 | emit positionChanged(geoMap->screenPositionToCoordinate(event->pos()));
|
---|
| 273 | }
|
---|
| 274 |
|
---|
| 275 | event->accept();
|
---|
| 276 | }
|
---|
| 277 |
|
---|
| 278 | void MapWidget::mouseReleaseEvent(QMouseEvent *event)
|
---|
| 279 | {
|
---|
| 280 | pressed = false;
|
---|
| 281 | landmark_match=NULL;
|
---|
| 282 |
|
---|
| 283 | if (dragging) {
|
---|
| 284 | QPoint v = event->pos() - dragStartPosition;
|
---|
| 285 | geoMap->pan(-v.x(), -v.y());
|
---|
| 286 | dragging = false;
|
---|
| 287 | }
|
---|
| 288 |
|
---|
| 289 | event->accept();
|
---|
| 290 | }
|
---|
| 291 |
|
---|
| 292 | void MapWidget::resizeEvent(QResizeEvent *event)
|
---|
| 293 | {
|
---|
| 294 | if (geoMap) {
|
---|
| 295 | m_scene->setSceneRect(QRectF(0, 0, event->size().width(), event->size().height()));
|
---|
| 296 | geoMap->resize(event->size().width(), event->size().height());
|
---|
| 297 | }
|
---|
| 298 |
|
---|
| 299 | QGraphicsView::resizeEvent(event);
|
---|
| 300 | }
|
---|
| 301 |
|
---|
| 302 | void MapWidget::wheelEvent(QWheelEvent *event)
|
---|
| 303 | {
|
---|
| 304 | int steps = event->delta() / 120;
|
---|
| 305 | int zoom = qBound(geoMap->minimumZoomLevel(), geoMap->zoomLevel() + steps,
|
---|
| 306 | geoMap->maximumZoomLevel());
|
---|
| 307 |
|
---|
| 308 | if (zoom != geoMap->zoomLevel()) geoMap->setZoomLevel(zoom);
|
---|
| 309 | //if(!centered) geoMap->setCenter(geoMap->screenPositionToCoordinate(event->pos()));
|
---|
| 310 | }
|
---|
| 311 |
|
---|
| 312 | void MapWidget::mouseDoubleClickEvent(QMouseEvent *event)
|
---|
| 313 | {
|
---|
| 314 | if(!map->isCentered()) geoMap->setCenter(geoMap->screenPositionToCoordinate(event->pos()));
|
---|
| 315 | }
|
---|
| 316 |
|
---|
| 317 | bool MapWidget::IsUptodate(void)
|
---|
| 318 | {
|
---|
| 319 | return is_uptodate;
|
---|
| 320 | }
|
---|
| 321 |
|
---|
| 322 | void MapWidget::SetUptodate(void)
|
---|
| 323 | {
|
---|
| 324 | for(int i=0;i<landmarks_old->count();i++)
|
---|
| 325 | {
|
---|
| 326 | delete landmarks_old->at(i);
|
---|
| 327 | }
|
---|
| 328 | landmarks_old->clear();
|
---|
| 329 |
|
---|
| 330 | for(int i=0;i<landmarks->count();i++)
|
---|
| 331 | {
|
---|
| 332 | landmarks->at(i)->setColor(Qt::white);
|
---|
| 333 | Landmark* landmark=new Landmark(geoMap,landmarks->at(i)->coordinate(),QString("%1").arg(landmarks->count()+1));
|
---|
| 334 | landmarks_old->append(landmark);
|
---|
| 335 | landmarks_old->at(i)->setVisible(false);
|
---|
| 336 | }
|
---|
| 337 |
|
---|
| 338 | is_uptodate=true;
|
---|
| 339 | }
|
---|
| 340 |
|
---|
| 341 | void MapWidget::Reset(void)
|
---|
| 342 | {
|
---|
| 343 | for(int i=0;i<landmarks->count();i++)
|
---|
| 344 | {
|
---|
| 345 | delete landmarks->at(i);
|
---|
| 346 | }
|
---|
| 347 | landmarks->clear();
|
---|
| 348 |
|
---|
| 349 | for(int i=0;i<landmarks_old->count();i++)
|
---|
| 350 | {
|
---|
| 351 | Landmark* landmark=new Landmark(geoMap,landmarks_old->at(i)->coordinate(),QString("%1").arg(landmarks->count()+1));
|
---|
| 352 | landmarks->append(landmark);
|
---|
| 353 | landmarks->at(i)->setColor(Qt::white);
|
---|
| 354 | }
|
---|
| 355 |
|
---|
| 356 | is_uptodate=true;
|
---|
| 357 | }
|
---|
| 358 |
|
---|
| 359 | QList<Landmark*>* MapWidget::Landmarks(void)
|
---|
| 360 | {
|
---|
| 361 | return landmarks;
|
---|
| 362 | }
|
---|
| 363 |
|
---|
| 364 | bool MapWidget::LandmarkToSend(int index)
|
---|
| 365 | {
|
---|
| 366 | if(index>=landmarks_old->count()) return true;
|
---|
| 367 |
|
---|
| 368 | if(landmarks->at(index)->coordinate()!=landmarks_old->at(index)->coordinate())
|
---|
| 369 | return true;
|
---|
| 370 | else
|
---|
| 371 | return false;
|
---|
| 372 | }
|
---|
| 373 | void MapWidget::RemoveLandmarks(void)
|
---|
| 374 | {
|
---|
| 375 | for(int i=0;i<landmarks->count();i++)
|
---|
| 376 | {
|
---|
| 377 | landmarks->at(i)->RemoveLandmark();
|
---|
| 378 | }
|
---|
| 379 | for(int i=0;i<landmarks_old->count();i++)
|
---|
| 380 | {
|
---|
| 381 | landmarks_old->at(i)->RemoveLandmark();
|
---|
| 382 | }
|
---|
| 383 | }
|
---|
| 384 |
|
---|
| 385 | void MapWidget::AddLandmarks(QGraphicsGeoMap *geoMap)
|
---|
| 386 | {
|
---|
| 387 | for(int i=0;i<landmarks->count();i++)
|
---|
| 388 | {
|
---|
| 389 | landmarks->at(i)->AddLandmark(geoMap);
|
---|
| 390 | }
|
---|
| 391 | for(int i=0;i<landmarks_old->count();i++)
|
---|
| 392 | {
|
---|
| 393 | landmarks_old->at(i)->AddLandmark(geoMap);
|
---|
| 394 | }
|
---|
| 395 | }
|
---|
| 396 |
|
---|
| 397 | void MapWidget::AddLandmark(const QGeoCoordinate &coordinate)
|
---|
| 398 | {
|
---|
| 399 | Landmark* landmark=new Landmark(geoMap,coordinate,QString("%1").arg(landmarks->count()+1));
|
---|
| 400 | landmarks->append(landmark);
|
---|
| 401 | landmark->setColor(Qt::white);
|
---|
| 402 | landmark=new Landmark(geoMap,coordinate,QString("%1").arg(landmarks_old->count()+1));
|
---|
| 403 | landmarks_old->append(landmark);
|
---|
| 404 | landmark->setColor(Qt::white);
|
---|
| 405 | landmark->setVisible(false);
|
---|
| 406 | }
|
---|
| 407 |
|
---|
| 408 | void MapWidget::AddPoint(QString name)
|
---|
| 409 | {
|
---|
| 410 | points.append(name);
|
---|
| 411 | }
|
---|