libosmscout  1.1.1
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>
27 #include <osmscout/util/GeoBox.h>
28 
30 
31 #include <osmscout/DBThread.h>
32 #include <osmscout/MapRenderer.h>
34 #include <osmscout/InputHandler.h>
35 #include <osmscout/OSMScoutQt.h>
36 #include <osmscout/OverlayObject.h>
38 
39 namespace osmscout {
40 
56 class OSMSCOUT_CLIENT_QT_API MapWidget : public QQuickPaintedItem
57 {
58  Q_OBJECT
59  Q_PROPERTY(QObject *view READ GetView WRITE SetMapView NOTIFY viewChanged)
60  Q_PROPERTY(QObject *vehiclePosition READ GetVehiclePosition WRITE SetVehiclePosition)
61  Q_PROPERTY(double lat READ GetLat NOTIFY viewChanged)
62  Q_PROPERTY(double lon READ GetLon NOTIFY viewChanged)
63  Q_PROPERTY(int zoomLevel READ GetMagLevel NOTIFY viewChanged)
64  Q_PROPERTY(QString zoomLevelName READ GetZoomLevelName NOTIFY viewChanged)
65  Q_PROPERTY(double pixelSize READ GetPixelSize NOTIFY viewChanged)
66  Q_PROPERTY(bool databaseLoaded READ isDatabaseLoaded NOTIFY databaseLoaded)
67  Q_PROPERTY(bool finished READ IsFinished NOTIFY finishedChanged)
68  Q_PROPERTY(bool showCurrentPosition READ getShowCurrentPosition WRITE setShowCurrentPosition)
69  Q_PROPERTY(bool lockToPosition READ isLockedToPosition WRITE setLockToPosition NOTIFY lockToPossitionChanged)
70  Q_PROPERTY(bool followVehicle READ isFollowVehicle WRITE setFollowVehicle NOTIFY followVehicleChanged)
71  Q_PROPERTY(QString stylesheetFilename READ GetStylesheetFilename NOTIFY stylesheetFilenameChanged)
72  Q_PROPERTY(QString renderingType READ GetRenderingType WRITE SetRenderingType NOTIFY renderingTypeChanged)
73 
79  Q_PROPERTY(bool preventMouseStealing READ isPreventMouseStealing WRITE setPreventMouseStealing)
80 
81  Q_PROPERTY(bool stylesheetHasErrors READ stylesheetHasErrors NOTIFY styleErrorsChanged)
82  Q_PROPERTY(int stylesheetErrorLine READ firstStylesheetErrorLine NOTIFY styleErrorsChanged)
83  Q_PROPERTY(int stylesheetErrorColumn READ firstStylesheetErrorColumn NOTIFY styleErrorsChanged)
84  Q_PROPERTY(QString stylesheetErrorDescription READ firstStylesheetErrorDescription NOTIFY styleErrorsChanged)
85 
86  Q_PROPERTY(QString vehicleStandardIconFile READ getVehicleStandardIconFile WRITE setVehicleStandardIconFile)
87  Q_PROPERTY(QString vehicleNoGpsSignalIconFile READ getVehicleNoGpsSignalIconFile WRITE setVehicleNoGpsSignalIconFile)
88  Q_PROPERTY(QString vehicleInTunnelIconFile READ getVehicleInTunnelIconFile WRITE setVehicleInTunnelIconFile)
89  Q_PROPERTY(double vehicleIconSize READ getVehicleIconSize WRITE setVehicleIconSize)
90 
91 private:
92  MapRenderer *renderer{nullptr};
93 
94  MapView *view{nullptr};
95 
96  InputHandler *inputHandler{nullptr};
97  TapRecognizer tapRecognizer;
98 
99  bool preventMouseStealing{false};
100 
101  bool finished{false};
102 
103  struct CurrentLocation {
104  QDateTime lastUpdate{QDateTime::currentDateTime()};
105  bool valid{false};
106  osmscout::GeoCoord coord;
107  bool horizontalAccuracyValid{false};
108  double horizontalAccuracy{0};
109  };
110  CurrentLocation currentPosition;
111  bool showCurrentPosition{false};
112 
114 
115  QMap<int, osmscout::GeoCoord> marks;
116 
117  // vehicle data
118  struct Vehicle {
119  VehiclePosition *position{nullptr};
120 
121  double iconSize{16}; // icon size [mm]
122 
124  bool follow{false};
125  QElapsedTimer lastGesture; // when there is some gesture, we will not follow vehicle for some short time
126 
127  QString standardIconFile{"vehicle.svg"}; // state == OnRoute | OffRoute
128  QString noGpsSignalIconFile{"vehicle_not_fixed.svg"}; // state == NoGpsSignal
129  QString inTunnelIconFile{"vehicle_tunnel.svg"}; // state == EstimateInTunnel
130 
131  QImage standardIcon;
132  QImage noGpsSignalIcon;
133  QImage inTunnelIcon;
134 
135  QImage getIcon()
136  {
137  if (position==nullptr){
138  return QImage();
139  }
140  switch(position->getState()){
141  case PositionAgent::PositionState::EstimateInTunnel:
142  return !inTunnelIcon.isNull() ? inTunnelIcon : standardIcon;
143  case PositionAgent::PositionState::NoGpsSignal:
144  return !noGpsSignalIcon.isNull() ? noGpsSignalIcon : standardIcon;
145  default:
146  return standardIcon;
147  }
148  }
149  };
150  Vehicle vehicle;
151 
152  float vehicleScaleFactor{1.0};
153 
154 signals:
155  void viewChanged();
156  void lockToPossitionChanged();
157  void followVehicleChanged();
158  void finishedChanged(bool finished);
159 
160  void mouseMove(const int screenX, const int screenY, const double lat, const double lon, const Qt::KeyboardModifiers modifiers);
161  void tap(const int screenX, const int screenY, const double lat, const double lon);
162  void doubleTap(const int screenX, const int screenY, const double lat, const double lon);
163  void longTap(const int screenX, const int screenY, const double lat, const double lon);
164  void tapLongTap(const int screenX, const int screenY, const double lat, const double lon);
165 
166  void stylesheetFilenameChanged();
167  void styleErrorsChanged();
168  void databaseLoaded(osmscout::GeoBox);
169  void renderingTypeChanged(QString type);
170 
171 public slots:
172  void changeView(const MapView &view);
173  void redraw();
174 
175  void recenter();
176 
177  void zoom(double zoomFactor);
178  void zoomIn(double zoomFactor);
179  void zoomOut(double zoomFactor);
180 
181  void zoom(double zoomFactor, const QPoint widgetPosition);
182  void zoomIn(double zoomFactor, const QPoint widgetPosition);
183  void zoomOut(double zoomFactor, const QPoint widgetPosition);
184 
185  void move(QVector2D vector);
186  void left();
187  void right();
188  void up();
189  void down();
190 
195  void rotateTo(double angle);
196  void rotateLeft();
197  void rotateRight();
198 
199  void toggleDaylight();
200  void reloadStyle();
201  void reloadTmpStyle();
202 
203  void showCoordinates(osmscout::GeoCoord coord, osmscout::Magnification magnification);
204  void showCoordinates(double lat, double lon);
205  void showCoordinatesInstantly(osmscout::GeoCoord coord, osmscout::Magnification magnification);
206  void showCoordinatesInstantly(double lat, double lon);
207  void showLocation(LocationEntry* location);
208 
209  void locationChanged(bool locationValid,
210  double lat, double lon,
211  bool horizontalAccuracyValid = false,
212  double horizontalAccuracy = 0,
213  const QDateTime &lastUpdate = QDateTime::currentDateTime());
214 
219  void addPositionMark(int id, double lat, double lon);
220  void removePositionMark(int id);
221 
234  void addOverlayObject(int id, QObject *o);
235  void removeOverlayObject(int id);
236  void removeAllOverlayObjects();
237 
238  OverlayWay *createOverlayWay(QString type="_route");
239  OverlayArea *createOverlayArea(QString type="_highlighted");
240  OverlayNode *createOverlayNode(QString type="_highlighted");
241 
242  bool toggleDebug();
243  bool toggleInfo();
244 
251  void setVehicleScaleFactor(float factor);
252 
253 private slots:
254 
255  virtual void onTap(const QPoint p);
256  virtual void onDoubleTap(const QPoint p);
257  virtual void onLongTap(const QPoint p);
258  virtual void onTapLongTap(const QPoint p);
259  virtual void onTapAndDrag(const QPoint p);
260 
261  void onMapDPIChange(double dpi);
262 
263  void onResize();
264 
265 private:
266  void setupInputHandler(InputHandler *newGesture);
267 
268  void loadVehicleIcons();
269 
274  osmscout::Magnification magnificationByDimension(const Distance &dimension);
275 
276 public:
277  MapWidget(QQuickItem* parent = nullptr);
278  ~MapWidget() override;
279 
280  inline MapView* GetView() const
281  {
282  return view; // We should be owner, parent is set http://doc.qt.io/qt-5/qqmlengine.html#objectOwnership
283  }
284 
285  inline void SetMapView(QObject *o)
286  {
287  MapView *updated = dynamic_cast<MapView*>(o);
288  if (updated == nullptr){
289  qWarning() << "Failed to cast " << o << " to MapView*.";
290  return;
291  }
292 
293  bool changed = *view != *updated;
294  if (changed){
295  setupInputHandler(new InputHandler(*updated));
296  changeView(*updated);
297  }
298  }
299 
301  {
302  return vehicle.position;
303  }
304 
305  void SetVehiclePosition(QObject *o);
306 
307  inline QString getVehicleStandardIconFile() const
308  {
309  return vehicle.standardIconFile;
310  }
311 
312  inline void setVehicleStandardIconFile(const QString &file)
313  {
314  vehicle.standardIconFile = file;
315  loadVehicleIcons();
316  redraw();
317  }
318 
319  inline QString getVehicleNoGpsSignalIconFile() const
320  {
321  return vehicle.noGpsSignalIconFile;
322  }
323 
324  inline void setVehicleNoGpsSignalIconFile(const QString &file)
325  {
326  vehicle.noGpsSignalIconFile = file;
327  loadVehicleIcons();
328  redraw();
329  }
330 
331  inline QString getVehicleInTunnelIconFile() const
332  {
333  return vehicle.inTunnelIconFile;
334  }
335 
336  inline void setVehicleInTunnelIconFile(const QString &file)
337  {
338  vehicle.inTunnelIconFile = file;
339  loadVehicleIcons();
340  redraw();
341  }
342 
343  inline double getVehicleIconSize() const{
344  return vehicle.iconSize;
345  }
346 
347  inline void setVehicleIconSize(double value)
348  {
349  vehicle.iconSize = value;
350  loadVehicleIcons();
351  redraw();
352  }
353 
354  inline double GetLat() const
355  {
356  return view->center.GetLat();
357  }
358 
359  inline double GetLon() const
360  {
361  return view->center.GetLon();
362  }
363 
364  inline osmscout::GeoCoord GetCenter() const
365  {
366  return view->center;
367  }
368 
369  QString GetStylesheetFilename() const;
370 
371  QString GetZoomLevelName() const;
372 
373  inline int GetMagLevel() const
374  {
375  return view->magnification.GetLevel();
376  }
377 
378  inline double GetPixelSize() const
379  {
380  return getProjection().GetPixelSize();
381  }
382 
383  inline bool IsFinished() const
384  {
385  return finished;
386  }
387 
388  inline bool getShowCurrentPosition() const
389  {
390  return showCurrentPosition;
391  };
392 
393  inline void setShowCurrentPosition(bool b)
394  {
395  showCurrentPosition = b;
396  redraw();
397  };
398 
399  inline bool isLockedToPosition() const
400  {
401  return inputHandler->isLockedToPosition();
402  };
403 
404  void setLockToPosition(bool);
405 
406  inline bool isFollowVehicle() const
407  {
408  return vehicle.follow;
409  }
410 
411  void setFollowVehicle(bool);
412 
414  {
415  osmscout::MercatorProjection projection;
416 
417  size_t w=width();
418  size_t h=height();
419  projection.Set(GetCenter(),
420  view->angle.AsRadians(),
421  view->magnification,
422  view->mapDpi,
423  // to avoid invalid projection when scene is not finished yet
424  w==0? 100:w,
425  h==0? 100:h);
426  return projection;
427  }
428 
429  void wheelEvent(QWheelEvent* event) override;
430  void touchEvent(QTouchEvent *event) override;
431 
432  void focusOutEvent(QFocusEvent *event) override;
433 
434  void translateToTouch(QMouseEvent* event, Qt::TouchPointStates states);
435 
436  void mousePressEvent(QMouseEvent* event) override;
437  void mouseMoveEvent(QMouseEvent* event) override;
438  void mouseReleaseEvent(QMouseEvent* event) override;
439  void hoverMoveEvent(QHoverEvent* event) override;
440 
441  void paint(QPainter *painter) override;
442 
443  bool stylesheetHasErrors() const;
444  int firstStylesheetErrorLine() const;
445  int firstStylesheetErrorColumn() const;
446  QString firstStylesheetErrorDescription() const;
447 
448  bool isDatabaseLoaded();
449  Q_INVOKABLE bool isInDatabaseBoundingBox(double lat, double lon);
450  Q_INVOKABLE QPointF screenPosition(double lat, double lon);
451 
452  QString GetRenderingType() const;
453  void SetRenderingType(QString type);
454 
456  {
457  return preventMouseStealing;
458  }
459 
461  {
462  preventMouseStealing = b;
463  }
464 
468  static QImage loadSVGIcon(const QString &directory, const QString fileName, double iconPixelSize);
469 };
470 
471 }
472 
473 #endif // OSMSCOUT_CLIENT_QT_MAPWIDGET_H
Definition: MapWidget.h:56
void setShowCurrentPosition(bool b)
Definition: MapWidget.h:393
void setPreventMouseStealing(bool b)
Definition: MapWidget.h:460
#define OSMSCOUT_CLIENT_QT_API
Definition: ClientQtImportExport.h:45
MapView * GetView() const
Definition: MapWidget.h:280
double GetPixelSize() const
Definition: MapWidget.h:378
Definition: OSMScoutQt.h:192
Definition: Projection.h:333
void setVehicleNoGpsSignalIconFile(const QString &file)
Definition: MapWidget.h:324
Vehicle
Definition: OSMScoutTypes.h:55
void setVehicleIconSize(double value)
Definition: MapWidget.h:347
void setVehicleStandardIconFile(const QString &file)
Definition: MapWidget.h:312
void setVehicleInTunnelIconFile(const QString &file)
Definition: MapWidget.h:336
double GetLat() const
Definition: MapWidget.h:354
osmscout::MercatorProjection getProjection() const
Definition: MapWidget.h:413
bool Set(const GeoCoord &coord, const Magnification &magnification, size_t width, size_t height)
Definition: Projection.h:374
Definition: Area.h:38
Definition: MapRenderer.h:72
Definition: InputHandler.h:248
double GetLon() const
Definition: MapWidget.h:359
QString getVehicleNoGpsSignalIconFile() const
Definition: MapWidget.h:319
bool getShowCurrentPosition() const
Definition: MapWidget.h:388
bool isPreventMouseStealing() const
Definition: MapWidget.h:455
VehiclePosition * GetVehiclePosition() const
Definition: MapWidget.h:300
QString getVehicleInTunnelIconFile() const
Definition: MapWidget.h:331
QString getVehicleStandardIconFile() const
Definition: MapWidget.h:307
osmscout::GeoCoord GetCenter() const
Definition: MapWidget.h:364
int GetMagLevel() const
Definition: MapWidget.h:373
Definition: VehiclePosition.h:40
void SetMapView(QObject *o)
Definition: MapWidget.h:285
bool isFollowVehicle() const
Definition: MapWidget.h:406
Definition: InputHandler.h:160
bool isLockedToPosition() const
Definition: MapWidget.h:399
double getVehicleIconSize() const
Definition: MapWidget.h:343
RenderingType
Definition: OSMScoutQt.h:191
bool IsFinished() const
Definition: MapWidget.h:383