Changeset 15 in flair-src for trunk/tools/FlairGCS/src/mapwidget.cpp
- Timestamp:
- 04/08/16 15:40:57 (8 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/tools/FlairGCS/src/mapwidget.cpp
r10 r15 57 57 using namespace QtMobility; 58 58 59 MapWidget::MapWidget(Map* map,QWidget *parent) 60 : QGraphicsView(parent) { 61 setCursor(Qt::ArrowCursor); 62 setHorizontalScrollBarPolicy(Qt::ScrollBarAlwaysOff); 63 setVerticalScrollBarPolicy(Qt::ScrollBarAlwaysOff); 64 65 geoMap = 0; 59 MapWidget::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 79 QGraphicsScene *MapWidget::scene(void) const { return m_scene; } 80 81 void 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 88 void 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 183 void MapWidget::initDrag(void) { 184 if (pressed && map->isCentered() && landmark_match == NULL) { 185 dragging = true; 186 } 187 } 188 189 void 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 248 void 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()); 66 255 dragging = false; 67 pressed = false; 68 landmark_match=NULL; 69 is_uptodate=true; 70 this->map=map; 71 72 m_scene = new QGraphicsScene(this); 73 setScene(m_scene); 74 setMouseTracking(true); 75 76 landmarks=new QList<Landmark*>; 77 landmarks_old=new QList<Landmark*>; 78 } 79 80 QGraphicsScene *MapWidget::scene(void) const 81 { 82 return m_scene; 83 } 84 85 void MapWidget::setMap(QGraphicsGeoMap *geoMap) 86 { 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 93 void MapWidget::mousePressEvent(QMouseEvent *event) 94 { 95 if (event->button() == Qt::LeftButton) 96 { 97 pressed = true; 98 dragStartPosition = event->pos(); 99 QTimer::singleShot(qApp->startDragTime(), this, SLOT(initDrag())); 100 event->accept(); 101 } 102 103 if (event->button() == Qt::RightButton) 104 { 105 QMenu *menu = new QMenu("nom", this); 106 107 if(cursor().shape()!=Qt::PointingHandCursor) 108 { 109 QAction *b,*z;//*a, 110 QList<QAction*> go_to_actions; 111 QList<QAction*> centered_actions; 112 113 QMenu *centered_menu=menu->addMenu("centered"); 114 for(int i=0;i<points.count();i++) 115 { 116 QAction* action=centered_menu->addAction(points.at(i)); 117 centered_actions.append(action); 118 action->setCheckable(true); 119 if(map->centeredPoint()==i) 120 { 121 action->setChecked(true); 122 } 123 else 124 { 125 action->setChecked(false); 126 } 127 } 128 129 menu->addSeparator(); 130 131 b=menu->addAction("place checkpoint"); 132 QMenu *go_to_menu=menu->addMenu("go to"); 133 for(int i=0;i<landmarks->count();i++) 134 { 135 QAction* action=go_to_menu->addAction(QString("checkpoint %1").arg(i+1)); 136 go_to_actions.append(action); 137 } 138 if(landmarks->count()==0) go_to_menu->setEnabled(false); 139 140 map->appendmenu(menu); 141 z=map->execmenu(this,menu,event->globalPos()); 142 143 for(int i=0;i<centered_actions.count();i++) 144 { 145 if(z==centered_actions.at(i)) 146 { 147 if(centered_actions.at(i)->isChecked()) 148 { 149 map->setCenteredPoint(i); 150 } 151 else 152 { 153 map->setCenteredPoint(-1); 154 } 155 156 break; 157 } 158 } 159 160 if(z==b) 161 { 162 Landmark* landmark=new Landmark(geoMap,geoMap->screenPositionToCoordinate(event->pos()),QString("%1").arg(landmarks->count()+1)); 163 landmarks->append(landmark); 164 is_uptodate=false; 165 } 166 for(int i=0;i<go_to_actions.count();i++) 167 { 168 if(z==go_to_actions.at(i)) 169 { 170 map->setCenteredPoint(-1); 171 geoMap->setCenter(landmarks->at(i)->coordinate()); 172 break; 173 } 174 } 175 176 } 177 else 178 { 179 QAction *a,*z; 180 a=menu->addAction("delete"); 181 z=menu->exec(event->globalPos()); 182 183 if(z==a) 184 { 185 int i; 186 for(i=0;i<landmarks->count();i++) 187 { 188 if(landmarks->at(i)->contains(geoMap->screenPositionToCoordinate(event->pos()))) 189 { 190 delete landmarks->at(i); 191 landmarks->removeAt(i); 192 break; 193 } 194 } 195 for(int j=i;j<landmarks->count();j++) 196 { 197 landmarks->at(j)->setText(QString("%1").arg(j+1)); 198 } 199 } 200 } 201 202 delete menu; 203 } 204 } 205 206 void MapWidget::initDrag(void) 207 { 208 if (pressed && map->isCentered() && landmark_match==NULL) { 209 dragging = true; 210 } 211 } 212 213 void MapWidget::mouseMoveEvent(QMouseEvent *event) 214 { 215 if (!geoMap) 216 return; 217 218 Qt::CursorShape new_cursor; 219 if(dragging==true) 220 { 221 new_cursor=Qt::ClosedHandCursor; 222 } 223 else 224 { 225 if(map->isCentered()) 226 { 227 if(pressed==true) 228 { 229 new_cursor=Qt::ForbiddenCursor; 230 } 231 else 232 { 233 new_cursor=Qt::ArrowCursor; 234 } 235 } 236 else 237 { 238 new_cursor=Qt::OpenHandCursor; 239 } 240 if(landmark_match!=NULL) new_cursor=Qt::PointingHandCursor; 241 } 242 243 244 for(int i=0;i<landmarks->count();i++) 245 { 246 if(landmarks->at(i)->contains(geoMap->screenPositionToCoordinate(event->pos()))) 247 { 248 if(pressed && landmark_match==NULL) 249 { 250 landmark_match=landmarks->at(i); 251 landmark_match->setColor(Qt::red); 252 is_uptodate=false; 253 } 254 new_cursor=Qt::PointingHandCursor; 255 break; 256 } 257 } 258 259 if(new_cursor!=cursor().shape()) setCursor(new_cursor); 260 261 QPoint v = event->pos() - dragStartPosition; 262 263 if (dragging) { 264 geoMap->pan(-v.x(), -v.y()); 265 dragStartPosition = event->pos(); 266 267 } else if(landmark_match!=NULL){ 268 landmark_match->setCoordinate(geoMap->screenPositionToCoordinate(event->pos())); 269 270 } else if (pressed && !map->isCentered() && 271 v.manhattanLength() >= qApp->startDragDistance()) { 272 dragging = true; 273 274 } else { 275 dragStartPosition = event->pos(); 276 emit positionChanged(geoMap->screenPositionToCoordinate(event->pos())); 277 } 278 279 event->accept(); 280 } 281 282 void MapWidget::mouseReleaseEvent(QMouseEvent *event) 283 { 284 pressed = false; 285 landmark_match=NULL; 286 287 if (dragging) { 288 QPoint v = event->pos() - dragStartPosition; 289 geoMap->pan(-v.x(), -v.y()); 290 dragging = false; 291 } 292 293 event->accept(); 294 } 295 296 void MapWidget::resizeEvent(QResizeEvent *event) 297 { 298 if (geoMap) { 299 m_scene->setSceneRect(QRectF(0, 0, event->size().width(), event->size().height())); 300 geoMap->resize(event->size().width(), event->size().height()); 301 } 302 303 QGraphicsView::resizeEvent(event); 304 } 305 306 void MapWidget::wheelEvent(QWheelEvent *event) 307 { 308 int steps = event->delta() / 120; 309 int zoom = qBound(geoMap->minimumZoomLevel(), geoMap->zoomLevel() + steps, 310 geoMap->maximumZoomLevel()); 311 312 if (zoom != geoMap->zoomLevel()) geoMap->setZoomLevel(zoom); 313 //if(!centered) geoMap->setCenter(geoMap->screenPositionToCoordinate(event->pos())); 314 } 315 316 void MapWidget::mouseDoubleClickEvent(QMouseEvent *event) 317 { 318 if(!map->isCentered()) geoMap->setCenter(geoMap->screenPositionToCoordinate(event->pos())); 319 } 320 321 bool MapWidget::IsUptodate(void) 322 { 323 return is_uptodate; 324 } 325 326 void MapWidget::SetUptodate(void) 327 { 328 for(int i=0;i<landmarks_old->count();i++) 329 { 330 delete landmarks_old->at(i); 331 } 332 landmarks_old->clear(); 333 334 for(int i=0;i<landmarks->count();i++) 335 { 336 landmarks->at(i)->setColor(Qt::white); 337 Landmark* landmark=new Landmark(geoMap,landmarks->at(i)->coordinate(),QString("%1").arg(landmarks->count()+1)); 338 landmarks_old->append(landmark); 339 landmarks_old->at(i)->setVisible(false); 340 } 341 342 is_uptodate=true; 343 } 344 345 void MapWidget::Reset(void) 346 { 347 for(int i=0;i<landmarks->count();i++) 348 { 349 delete landmarks->at(i); 350 } 351 landmarks->clear(); 352 353 for(int i=0;i<landmarks_old->count();i++) 354 { 355 Landmark* landmark=new Landmark(geoMap,landmarks_old->at(i)->coordinate(),QString("%1").arg(landmarks->count()+1)); 356 landmarks->append(landmark); 357 landmarks->at(i)->setColor(Qt::white); 358 } 359 360 is_uptodate=true; 361 } 362 363 QList<Landmark*>* MapWidget::Landmarks(void) 364 { 365 return landmarks; 366 } 367 368 bool MapWidget::LandmarkToSend(int index) 369 { 370 if(index>=landmarks_old->count()) return true; 371 372 if(landmarks->at(index)->coordinate()!=landmarks_old->at(index)->coordinate()) 373 return true; 374 else 375 return false; 376 } 377 void MapWidget::RemoveLandmarks(void) 378 { 379 for(int i=0;i<landmarks->count();i++) 380 { 381 landmarks->at(i)->RemoveLandmark(); 382 } 383 for(int i=0;i<landmarks_old->count();i++) 384 { 385 landmarks_old->at(i)->RemoveLandmark(); 386 } 387 } 388 389 void MapWidget::AddLandmarks(QGraphicsGeoMap *geoMap) 390 { 391 for(int i=0;i<landmarks->count();i++) 392 { 393 landmarks->at(i)->AddLandmark(geoMap); 394 } 395 for(int i=0;i<landmarks_old->count();i++) 396 { 397 landmarks_old->at(i)->AddLandmark(geoMap); 398 } 399 } 400 401 void MapWidget::AddLandmark(const QGeoCoordinate &coordinate) 402 { 403 Landmark* landmark=new Landmark(geoMap,coordinate,QString("%1").arg(landmarks->count()+1)); 256 } 257 258 event->accept(); 259 } 260 261 void 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 271 void 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 282 void MapWidget::mouseDoubleClickEvent(QMouseEvent *event) { 283 if (!map->isCentered()) 284 geoMap->setCenter(geoMap->screenPositionToCoordinate(event->pos())); 285 } 286 287 bool MapWidget::IsUptodate(void) { return is_uptodate; } 288 289 void 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 307 void 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)); 404 317 landmarks->append(landmark); 405 landmark->setColor(Qt::white); 406 landmark=new Landmark(geoMap,coordinate,QString("%1").arg(landmarks_old->count()+1)); 407 landmarks_old->append(landmark); 408 landmark->setColor(Qt::white); 409 landmark->setVisible(false); 410 } 411 412 void MapWidget::AddPoint(QString name) 413 { 414 points.append(name); 415 } 318 landmarks->at(i)->setColor(Qt::white); 319 } 320 321 is_uptodate = true; 322 } 323 324 QList<Landmark *> *MapWidget::Landmarks(void) { return landmarks; } 325 326 bool 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 } 336 void 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 345 void 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 354 void 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 366 void MapWidget::AddPoint(QString name) { points.append(name); }
Note:
See TracChangeset
for help on using the changeset viewer.