libosmscout 1.1.1
Loading...
Searching...
No Matches
MapWidget.h
Go to the documentation of this file.
1#ifndef OSMSCOUT_CLIENT_QT_MAPWIDGET_H
2#define OSMSCOUT_CLIENT_QT_MAPWIDGET_H
3
4/*
5 OSMScout - a Qt backend for libosmscout and libosmscout-map
6 Copyright (C) 2010 Tim Teulings
7 Copyright (C) 2016 Lukáš Karas
8
9 This library is free software; you can redistribute it and/or
10 modify it under the terms of the GNU Lesser General Public
11 License as published by the Free Software Foundation; either
12 version 2.1 of the License, or (at your option) any later version.
13
14 This library is distributed in the hope that it will be useful,
15 but WITHOUT ANY WARRANTY; without even the implied warranty of
16 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
17 Lesser General Public License for more details.
18
19 You should have received a copy of the GNU Lesser General Public
20 License along with this library; if not, write to the Free Software
21 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
22 */
23
24#include <QQuickPaintedItem>
25
26#include <osmscout/GeoCoord.h>
28#include <osmscoutclient/DBThread.h>
29
31
40
41namespace osmscout {
42
48
58class OSMSCOUT_CLIENT_QT_API MapWidget : public QQuickPaintedItem
59{
60 Q_OBJECT
61 Q_PROPERTY(QObject *view READ GetView WRITE SetMapView NOTIFY viewChanged)
63 Q_PROPERTY(double lat READ GetLat NOTIFY latChanged)
64 Q_PROPERTY(double lon READ GetLon NOTIFY lonChanged)
65 Q_PROPERTY(double angle READ GetAngle NOTIFY angleChanged)
66 Q_PROPERTY(int zoomLevel READ GetMagLevel NOTIFY magLevelChanged)
67 Q_PROPERTY(QString zoomLevelName READ GetZoomLevelName NOTIFY magLevelChanged)
68 Q_PROPERTY(double pixelSize READ GetPixelSize NOTIFY pixelSizeChanged)
69 Q_PROPERTY(bool databaseLoaded READ isDatabaseLoaded NOTIFY databaseLoaded)
70 Q_PROPERTY(bool finished READ IsFinished NOTIFY finishedChanged)
77
84
89
94
96
97private:
98 MapRenderer *renderer{nullptr};
99
100 MapView *view{nullptr};
101
102 InputHandler *inputHandler{nullptr};
103 TapRecognizer tapRecognizer;
104
105 IconLookup *iconLookup{nullptr};
106 IconAnimation iconAnimation;
107
108 bool preventMouseStealing{false};
109
110 bool finished{false};
111
112 struct CurrentLocation {
113 QDateTime lastUpdate{QDateTime::currentDateTime()};
114 bool valid{false};
115 osmscout::GeoCoord coord;
116 bool horizontalAccuracyValid{false};
117 double horizontalAccuracy{0};
118 };
119 CurrentLocation currentPosition;
120 bool showCurrentPosition{false};
121
122 RenderingType renderingType{RenderingType::PlaneRendering};
123
124 QMap<int, osmscout::GeoCoord> marks;
125
126 // vehicle data
127 struct Vehicle {
128 VehiclePosition *position{nullptr};
129
130 double iconSize{16}; // icon size [mm]
131
133 bool follow{false};
134 bool autoRotateMap{true};
135 QElapsedTimer lastGesture; // when there is some gesture, we will not follow vehicle for some short time
136
137 QString standardIconFile{"vehicle.svg"}; // state == OnRoute | OffRoute
138 QString noGpsSignalIconFile{"vehicle_not_fixed.svg"}; // state == NoGpsSignal
139 QString inTunnelIconFile{"vehicle_tunnel.svg"}; // state == EstimateInTunnel
140
141 QImage standardIcon;
142 QImage noGpsSignalIcon;
143 QImage inTunnelIcon;
144
145 QImage getIcon()
146 {
147 if (position==nullptr){
148 return QImage();
149 }
150 switch(position->getState()){
151 case PositionAgent::PositionState::EstimateInTunnel:
152 return !inTunnelIcon.isNull() ? inTunnelIcon : standardIcon;
153 case PositionAgent::PositionState::NoGpsSignal:
154 return !noGpsSignalIcon.isNull() ? noGpsSignalIcon : standardIcon;
155 default:
156 return standardIcon;
157 }
158 }
159 };
160 Vehicle vehicle;
161
162 float vehicleScaleFactor{1.0};
163
164signals:
175
176 void mouseMove(const int screenX, const int screenY, const double lat, const double lon, const Qt::KeyboardModifiers modifiers);
177 void tap(const int screenX, const int screenY, const double lat, const double lon);
178 void doubleTap(const int screenX, const int screenY, const double lat, const double lon);
179 void longTap(const int screenX, const int screenY, const double lat, const double lon);
180 void tapLongTap(const int screenX, const int screenY, const double lat, const double lon);
181
182 void iconTapped(QPoint screenCoord, double lat, double lon, QString databasePath,
183 QString objectType, quint64 objectId, int poiId, QString type,
184 QString name, QString altName, QString ref, QString operatorName, QString phone, QString website,
185 QString openingHours);
186
189 void databaseLoaded(osmscout::GeoBox);
190 void renderingTypeChanged(QString type);
191
192public slots:
193 void changeView(const MapView &view);
194 void redraw();
195
196 void recenter();
197
198 void zoom(double zoomFactor);
199 void zoomIn(double zoomFactor);
200 void zoomOut(double zoomFactor);
201
202 void zoom(double zoomFactor, const QPoint widgetPosition);
203 void zoomIn(double zoomFactor, const QPoint widgetPosition);
204 void zoomOut(double zoomFactor, const QPoint widgetPosition);
205
206 void move(QVector2D vector);
207 void left();
208 void right();
209 void up();
210 void down();
211
216 void rotateTo(double angle);
219
223
224 void showCoordinates(osmscout::GeoCoord coord, osmscout::Magnification magnification);
225 void showCoordinates(double lat, double lon);
226 void showCoordinatesInstantly(osmscout::GeoCoord coord, osmscout::Magnification magnification);
227 void showCoordinatesInstantly(double lat, double lon);
229
230 void locationChanged(bool locationValid,
231 double lat, double lon,
232 bool horizontalAccuracyValid = false,
233 double horizontalAccuracy = 0,
234 const QDateTime &lastUpdate = QDateTime::currentDateTime());
235
240 void addPositionMark(int id, double lat, double lon);
241 void removePositionMark(int id);
242
255 void addOverlayObject(int id, QObject *o);
258
259 OverlayWay *createOverlayWay(QString type="_route");
260 OverlayArea *createOverlayArea(QString type="_highlighted");
261 OverlayNode *createOverlayNode(QString type="_highlighted");
262
264
267
274 void setVehicleScaleFactor(float factor);
275
276 void onIconFound(QPoint lookupCoord, MapIcon icon);
277
278private:
279 Slot<double> mapDpiSlot{ std::bind(&MapWidget::onMapDPIChange, this, std::placeholders::_1) };
280
281 Slot<> stylesheetFilenameChangedSlot{
282 [this](){
283 emit stylesheetFilenameChanged();
284 }
285 };
286
287 Slot<> styleErrorsChangedSlot{
288 [this](){
289 emit styleErrorsChanged();
290 }
291 };
292
293 Slot<osmscout::GeoBox> databaseLoadedSlot{
294 [this](const osmscout::GeoBox &geoBox){
295 emit databaseLoaded(geoBox);
296 }
297 };
298
299private slots:
300
301 virtual void onTap(const QPoint p);
302 virtual void onDoubleTap(const QPoint p);
303 virtual void onLongTap(const QPoint p);
304 virtual void onTapLongTap(const QPoint p);
305 virtual void onTapAndDrag(const QPoint p);
306
307 void onMapDPIChange(double dpi);
308
309 void onResize();
310
311private:
312 void setupInputHandler(InputHandler *newGesture);
313
314 void loadVehicleIcons();
315
320 osmscout::Magnification magnificationByDimension(const Distance &dimension);
321
322public:
323 MapWidget(QQuickItem* parent = nullptr);
324 ~MapWidget() override;
325
326 inline MapView* GetView() const
327 {
328 return view; // We should be owner, parent is set http://doc.qt.io/qt-5/qqmlengine.html#objectOwnership
329 }
330
331 inline void SetMapView(QObject *o)
332 {
333 MapView *updated = dynamic_cast<MapView*>(o);
334 if (updated == nullptr){
335 qWarning() << "Failed to cast " << o << " to MapView*.";
336 return;
337 }
338
339 bool changed = *view != *updated;
340 if (changed){
341 setupInputHandler(new InputHandler(*updated));
342 changeView(*updated);
343 }
344 }
345
346 MapViewStruct GetViewStruct() const;
347
349 {
350 return vehicle.position;
351 }
352
353 void SetVehiclePosition(QObject *o);
354
355 inline QString getVehicleStandardIconFile() const
356 {
357 return vehicle.standardIconFile;
358 }
359
360 inline void setVehicleStandardIconFile(const QString &file)
361 {
362 vehicle.standardIconFile = file;
363 loadVehicleIcons();
364 redraw();
365 }
366
367 inline QString getVehicleNoGpsSignalIconFile() const
368 {
369 return vehicle.noGpsSignalIconFile;
370 }
371
372 inline void setVehicleNoGpsSignalIconFile(const QString &file)
373 {
374 vehicle.noGpsSignalIconFile = file;
375 loadVehicleIcons();
376 redraw();
377 }
378
379 inline QString getVehicleInTunnelIconFile() const
380 {
381 return vehicle.inTunnelIconFile;
382 }
383
384 inline void setVehicleInTunnelIconFile(const QString &file)
385 {
386 vehicle.inTunnelIconFile = file;
387 loadVehicleIcons();
388 redraw();
389 }
390
391 inline double getVehicleIconSize() const{
392 return vehicle.iconSize;
393 }
394
395 inline void setVehicleIconSize(double value)
396 {
397 vehicle.iconSize = value;
398 loadVehicleIcons();
399 redraw();
400 }
401
403 {
404 return iconLookup!=nullptr;
405 }
406
408
409 inline double GetLat() const
410 {
411 return view->center.GetLat();
412 }
413
414 inline double GetLon() const
415 {
416 return view->center.GetLon();
417 }
418
419 inline double GetAngle() const
420 {
421 return view->angle.AsRadians();
422 }
423
424 inline osmscout::GeoCoord GetCenter() const
425 {
426 return view->center;
427 }
428
429 QString GetStylesheetFilename() const;
430
431 QString GetZoomLevelName() const;
432
433 inline int GetMagLevel() const
434 {
435 return view->magnification.GetLevel();
436 }
437
438 inline double GetPixelSize() const
439 {
440 return getProjection().GetPixelSize();
441 }
442
443 inline bool IsFinished() const
444 {
445 return finished;
446 }
447
448 inline bool getShowCurrentPosition() const
449 {
450 return showCurrentPosition;
451 };
452
453 inline void setShowCurrentPosition(bool b)
454 {
456 redraw();
457 };
458
459 inline bool isLockedToPosition() const
460 {
461 return inputHandler->isLockedToPosition();
462 };
463
465
466 inline bool isFollowVehicle() const
467 {
468 return vehicle.follow;
469 }
470
472
473 inline bool isVehicleAutoRotateMap() const
474 {
475 return vehicle.autoRotateMap;
476 }
477
479
481 {
483
484 size_t w=width();
485 size_t h=height();
486 projection.Set(GetCenter(),
487 view->angle.AsRadians(),
488 view->magnification,
489 view->mapDpi,
490 // to avoid invalid projection when scene is not finished yet
491 w==0? 100:w,
492 h==0? 100:h);
493 return projection;
494 }
495
496 void wheelEvent(QWheelEvent* event) override;
497 void touchEvent(QTouchEvent *event) override;
498
499 void focusOutEvent(QFocusEvent *event) override;
500
501 void translateToTouch(QMouseEvent* event, Qt::TouchPointStates states);
502
503 void mousePressEvent(QMouseEvent* event) override;
504 void mouseMoveEvent(QMouseEvent* event) override;
505 void mouseReleaseEvent(QMouseEvent* event) override;
506 void hoverMoveEvent(QHoverEvent* event) override;
507
508 void paint(QPainter *painter) override;
509
514
516 Q_INVOKABLE bool isInDatabaseBoundingBox(double lat, double lon);
517 Q_INVOKABLE QPointF screenPosition(double lat, double lon);
518
519 QString GetRenderingType() const;
520 void SetRenderingType(QString type);
521
523 {
525 }
526
528 {
530 }
531
535 static QImage loadSVGIcon(const QString &directory, const QString fileName, double iconPixelSize);
536};
537
538}
539
540#endif // OSMSCOUT_CLIENT_QT_MAPWIDGET_H
#define OSMSCOUT_CLIENT_QT_API
Definition ClientQtImportExport.h:45
Definition InputHandler.h:265
Definition LocationEntry.h:47
Definition MapRenderer.h:85
Definition InputHandler.h:161
void showCoordinatesInstantly(double lat, double lon)
int stylesheetErrorLine
Definition MapWidget.h:86
void setPreventMouseStealing(bool b)
Definition MapWidget.h:527
void doubleTap(const int screenX, const int screenY, const double lat, const double lon)
void zoomIn(double zoomFactor, const QPoint widgetPosition)
QString zoomLevelName
Definition MapWidget.h:67
QString GetRenderingType() const
void iconTapped(QPoint screenCoord, double lat, double lon, QString databasePath, QString objectType, quint64 objectId, int poiId, QString type, QString name, QString altName, QString ref, QString operatorName, QString phone, QString website, QString openingHours)
void longTap(const int screenX, const int screenY, const double lat, const double lon)
double lat
Definition MapWidget.h:63
bool finished
Definition MapWidget.h:70
void setInteractiveIcons(bool b)
QObject * vehiclePosition
Definition MapWidget.h:62
void showLocation(LocationEntry *location)
Q_INVOKABLE QPointF screenPosition(double lat, double lon)
bool interactiveIcons
Definition MapWidget.h:95
void zoom(double zoomFactor)
void translateToTouch(QMouseEvent *event, Qt::TouchPointStates states)
void setVehicleInTunnelIconFile(const QString &file)
Definition MapWidget.h:384
void tap(const int screenX, const int screenY, const double lat, const double lon)
QString vehicleInTunnelIconFile
Definition MapWidget.h:92
void hoverMoveEvent(QHoverEvent *event) override
void removeAllOverlayObjects()
bool databaseLoaded
Definition MapWidget.h:69
void zoom(double zoomFactor, const QPoint widgetPosition)
void showCoordinates(double lat, double lon)
void stylesheetFilenameChanged()
void zoomOut(double zoomFactor, const QPoint widgetPosition)
void tapLongTap(const int screenX, const int screenY, const double lat, const double lon)
QString stylesheetErrorDescription
Definition MapWidget.h:88
bool getShowCurrentPosition() const
Definition MapWidget.h:448
void removeOverlayObject(int id)
bool IsFinished() const
Definition MapWidget.h:443
bool showCurrentPosition
Definition MapWidget.h:71
void setVehicleIconSize(double value)
Definition MapWidget.h:395
void setVehicleStandardIconFile(const QString &file)
Definition MapWidget.h:360
double getVehicleIconSize() const
Definition MapWidget.h:391
void setVehicleAutoRotateMap(bool)
QString GetZoomLevelName() const
void zoomIn(double zoomFactor)
MapView * GetView() const
Definition MapWidget.h:326
osmscout::MercatorProjection getProjection() const
Definition MapWidget.h:480
bool stylesheetHasErrors
Definition MapWidget.h:85
void removePositionMark(int id)
~MapWidget() override
void touchEvent(QTouchEvent *event) override
void mouseReleaseEvent(QMouseEvent *event) override
void locationChanged(bool locationValid, double lat, double lon, bool horizontalAccuracyValid=false, double horizontalAccuracy=0, const QDateTime &lastUpdate=QDateTime::currentDateTime())
void setFollowVehicle(bool)
double GetLon() const
Definition MapWidget.h:414
MapWidget(QQuickItem *parent=nullptr)
double lon
Definition MapWidget.h:64
void addOverlayObject(int id, QObject *o)
void changeView(const MapView &view)
QString GetStylesheetFilename() const
void zoomOut(double zoomFactor)
bool isVehicleAutoRotateMap() const
Definition MapWidget.h:473
osmscout::GeoCoord GetCenter() const
Definition MapWidget.h:424
double vehicleIconSize
Definition MapWidget.h:93
void move(QVector2D vector)
int firstStylesheetErrorColumn() const
int firstStylesheetErrorLine() const
void addPositionMark(int id, double lat, double lon)
void setVehicleScaleFactor(float factor)
bool hasInteractiveIcons() const
Definition MapWidget.h:402
QString getVehicleInTunnelIconFile() const
Definition MapWidget.h:379
void showCoordinates(osmscout::GeoCoord coord, osmscout::Magnification magnification)
OverlayArea * createOverlayArea(QString type="_highlighted")
QString vehicleStandardIconFile
Definition MapWidget.h:90
void wheelEvent(QWheelEvent *event) override
void lockToPositionChanged()
void mousePressEvent(QMouseEvent *event) override
void SetMapView(QObject *o)
Definition MapWidget.h:331
bool isPreventMouseStealing() const
Definition MapWidget.h:522
void finishedChanged(bool finished)
QString renderingType
Definition MapWidget.h:76
void setLockToPosition(bool)
void paint(QPainter *painter) override
OverlayWay * createOverlayWay(QString type="_route")
int stylesheetErrorColumn
Definition MapWidget.h:87
QString stylesheetFilename
Definition MapWidget.h:75
void setVehicleNoGpsSignalIconFile(const QString &file)
Definition MapWidget.h:372
void renderingTypeChanged(QString type)
double angle
Definition MapWidget.h:65
double GetPixelSize() const
Definition MapWidget.h:438
bool lockToPosition
Definition MapWidget.h:72
QString vehicleNoGpsSignalIconFile
Definition MapWidget.h:91
MapViewStruct GetViewStruct() const
void focusOutEvent(QFocusEvent *event) override
int zoomLevel
Definition MapWidget.h:66
void SetRenderingType(QString type)
QObject * view
Definition MapWidget.h:61
void databaseLoaded(osmscout::GeoBox)
bool followVehicle
Definition MapWidget.h:73
void setShowCurrentPosition(bool b)
Definition MapWidget.h:453
bool isLockedToPosition() const
Definition MapWidget.h:459
void showCoordinatesInstantly(osmscout::GeoCoord coord, osmscout::Magnification magnification)
bool stylesheetHasErrors() const
void SetVehiclePosition(QObject *o)
void onIconFound(QPoint lookupCoord, MapIcon icon)
bool isFollowVehicle() const
Definition MapWidget.h:466
int GetMagLevel() const
Definition MapWidget.h:433
double GetAngle() const
Definition MapWidget.h:419
double pixelSize
Definition MapWidget.h:68
OverlayNode * createOverlayNode(QString type="_highlighted")
Q_INVOKABLE bool isInDatabaseBoundingBox(double lat, double lon)
void rotateTo(double angle)
static QImage loadSVGIcon(const QString &directory, const QString fileName, double iconPixelSize)
QString getVehicleNoGpsSignalIconFile() const
Definition MapWidget.h:367
bool preventMouseStealing
Definition MapWidget.h:83
void vehicleAutoRotateMapChanged()
VehiclePosition * GetVehiclePosition() const
Definition MapWidget.h:348
QString getVehicleStandardIconFile() const
Definition MapWidget.h:355
bool vehicleAutoRotateMap
Definition MapWidget.h:74
double GetLat() const
Definition MapWidget.h:409
void mouseMoveEvent(QMouseEvent *event) override
QString firstStylesheetErrorDescription() const
void mouseMove(const int screenX, const int screenY, const double lat, const double lon, const Qt::KeyboardModifiers modifiers)
Definition MercatorProjection.h:40
bool Set(const GeoCoord &coord, const Magnification &magnification, size_t width, size_t height)
Definition MercatorProjection.h:86
Definition OverlayObject.h:169
Definition OverlayObject.h:211
Definition OverlayObject.h:190
Definition Signal.h:98
Definition VehiclePosition.h:41
RenderingType
Definition OSMScoutQt.h:200
Definition Area.h:39
Vehicle
Definition OSMScoutTypes.h:55
Definition IconLookup.h:43