You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

432 lines
13 KiB

7 years ago
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
  1. /*
  2. *
  3. * This file is part of QMapControl,
  4. * an open-source cross-platform map widget
  5. *
  6. * Copyright (C) 2007 - 2008 Kai Winter
  7. *
  8. * This program is free software: you can redistribute it and/or modify
  9. * it under the terms of the GNU Lesser General Public License as published by
  10. * the Free Software Foundation, either version 3 of the License, or
  11. * (at your option) any later version.
  12. *
  13. * This program is distributed in the hope that it will be useful,
  14. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  15. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  16. * GNU Lesser General Public License for more details.
  17. *
  18. * You should have received a copy of the GNU Lesser General Public License
  19. * along with QMapControl. If not, see <http://www.gnu.org/licenses/>.
  20. *
  21. * Contact e-mail: kaiwinter@gmx.de
  22. * Program URL : http://qmapcontrol.sourceforge.net/
  23. *
  24. */
  25. #ifndef MAPCONTROL_H
  26. #define MAPCONTROL_H
  27. #include "qmapcontrol_global.h"
  28. #include "layermanager.h"
  29. #include "layer.h"
  30. #include "mapadapter.h"
  31. #include "geometry.h"
  32. //#include "imagemanager.h"
  33. #include <QtCore/QObject>
  34. #include <QtWidgets/QWidget>
  35. #include <QtWidgets/QFrame>
  36. #include <QtCore/QDir>
  37. class ImageManager;
  38. class LayerManager;
  39. //! The control element of the widget and also the widget itself
  40. /*!
  41. * This is the main widget.
  42. * To this control layers can be added.
  43. * A MapControl has to be instantiated with a QSize which sets the size the widget takes in a layout.
  44. * The given size is also the size which is assured to be filled with map images.
  45. *
  46. * @author Kai Winter <kaiwinter@gmx.de>
  47. */
  48. class MapControl : public QFrame {
  49. Q_OBJECT
  50. public:
  51. //! Declares what actions mouse movements have on the map
  52. enum MouseMode
  53. {
  54. Panning, /*!< The map is moved */
  55. Dragging, /*!< A rectangular can be drawn */
  56. None /*!< Mouse move events have no efect to the map */
  57. };
  58. //! The QWidget constructor of MapControl for use with QtDesigner
  59. /*!
  60. * @param qParent QWidget parent
  61. * @param windowFlags QWidget Window flags
  62. */
  63. MapControl (QWidget * parent = 0, Qt::WindowFlags windowFlags = 0);
  64. //! The constructor of MapControl
  65. /*!
  66. * The MapControl is the widget which displays the maps.
  67. * The size describes the area which gets filled with map data
  68. * When you give no MouseMode, the mouse moves the map.
  69. * You can change the MouseMode at runtime, e.g. to Dragging, which lets the user drag a rectangular box.
  70. * After the dragging a signal with the size of the box is emitted.
  71. * The MouseMode ´None´ can be used, to completely define the control of the map yourself.
  72. * @param size the size which the widget should fill with map data
  73. * @param mousemode the way mouse events are handled
  74. * @param showScale true if the scale should be displayed
  75. * @param showCrossHairs true if crosshairs should be shown at the centre of the map
  76. * @param parent QWidget parent
  77. * @param windowFlags QWidget Window flags
  78. */
  79. MapControl ( QSize size, MouseMode mousemode = Panning, bool showScale = false, bool showCrosshairs = true, QWidget * parent = 0, Qt::WindowFlags windowFlags = 0);
  80. ~MapControl();
  81. //! adds a layer
  82. /*!
  83. * If multiple layers are added, they are painted in the added order.
  84. * @param layer the layer which should be added
  85. */
  86. void addLayer ( Layer* layer );
  87. //! removes a layer
  88. /*!
  89. * Removes a layer and redraws existing layers
  90. * @param layer the layer which should be removed
  91. */
  92. void removeLayer ( Layer* layer );
  93. //! returns the layer with the given name
  94. /*!
  95. * @param layername name of the wanted layer
  96. * @return the layer with the given name
  97. */
  98. Layer* layer ( const QString& layername ) const;
  99. //! returns the names of all layers
  100. /*!
  101. * @return returns a QList with the names of all layers
  102. */
  103. QList<QString> layers() const;
  104. //! returns the number of existing layers
  105. /*!
  106. * @return returns the number of existing layers
  107. */
  108. int numberOfLayers() const;
  109. //! returns the coordinate of the center of the map
  110. /*!
  111. * @return returns the coordinate of the middle of the screen
  112. */
  113. QPointF currentCoordinate() const;
  114. //! returns the current zoom level
  115. /*!
  116. * @return returns the current zoom level
  117. */
  118. int currentZoom() const;
  119. //! update screen
  120. void updateView() const;
  121. //! enable mouse wheel events
  122. /*!
  123. * @parm enable mouse event
  124. */
  125. void enableMouseWheelEvents( bool enabled = true );
  126. //! returns mouse wheel allowed
  127. /*!
  128. * @return mouse wheel events enabled
  129. */
  130. bool mouseWheelEventsEnabled();
  131. //! sets the middle of the map to the given coordinate
  132. /*!
  133. * @param coordinate the coordinate which the view´s middle should be set to
  134. */
  135. void setView ( const QPointF& coordinate ) const;
  136. //! sets the view, so all coordinates are visible
  137. /*!
  138. * @param coordinates the Coorinates which should be visible
  139. */
  140. void setView ( const QList<QPointF> coordinates ) const;
  141. //! sets the view and zooms in, so all coordinates are visible
  142. /*!
  143. * The code of setting the view to multiple coordinates is "brute force" and pretty slow.
  144. * Have to be reworked.
  145. * @param coordinates the Coorinates which should be visible
  146. */
  147. void setViewAndZoomIn ( const QList<QPointF> coordinates ) const;
  148. //! sets the view to the given Point
  149. /*!
  150. *
  151. * @param point the geometric point the view should be set to
  152. */
  153. void setView ( const mapPoint* point ) const;
  154. //! Keeps the center of the map on the Geometry, even when it moves
  155. /*!
  156. * To stop the following the method stopFollowing() have to be called
  157. * @param geometry the Geometry which should stay centered.
  158. */
  159. void followGeometry ( const Geometry* geometry ) const;
  160. //TODO:
  161. // void followGeometry(const QList<Geometry*>) const;
  162. //! Stops the following of a Geometry
  163. /*!
  164. * if the view is set to follow a Geometry this method stops the trace.
  165. * See followGeometry().
  166. * @param geometry the Geometry which should not followed anymore
  167. */
  168. void stopFollowing ( const Geometry* geometry ) const;
  169. //! Smoothly moves the center of the view to the given Coordinate
  170. /*!
  171. * @param coordinate the Coordinate which the center of the view should moved to
  172. */
  173. void moveTo ( QPointF coordinate );
  174. //! sets the Mouse Mode of the MapControl
  175. /*!
  176. * There are three MouseModes declard by an enum.
  177. * The MouesMode Dragging draws an rectangular in the map while the MouseButton is pressed.
  178. * When the Button is released a boxDragged() signal is emitted.
  179. *
  180. * The second MouseMode (the default) is Panning, which allows to drag the map around.
  181. * @param mousemode the MouseMode
  182. */
  183. void setMouseMode ( MouseMode mousemode );
  184. //! returns the current MouseMode
  185. /*!
  186. * For a explanation for the MouseModes see setMouseMode()
  187. * @return the current MouseMode
  188. */
  189. MapControl::MouseMode mouseMode();
  190. //! Enable persistent caching of map tiles
  191. /*!
  192. * Call this method to allow the QMapControl widget to save map tiles
  193. * persistent (also over application restarts).
  194. * Tiles are stored in the subdirectory "QMapControl.cache" within the
  195. * user's home directory. This can be changed by giving a path.
  196. * @param path the path to the cache directory
  197. * @param tileExpiry how long to keep in cache before requesting a new image. 0 or -1 to disable and keep forever
  198. */
  199. void enablePersistentCache ( const QDir& path= QDir::homePath() + "/QMapControl.cache", const int qDiskSizeMB = 250 );
  200. //! Sets the proxy for HTTP connections
  201. /*!
  202. * This method sets the proxy for HTTP connections.
  203. * This is not provided by the current Qtopia version!
  204. * @param host the proxy´s hostname or ip
  205. * @param port the proxy´s port
  206. * @param username the proxy´s username
  207. * @param password the proxy´s password
  208. */
  209. void setProxy(QString host, int port, const QString username = QString(), const QString password = QString());
  210. //! Displays the scale within the widget
  211. /*!
  212. *
  213. * @param show true if the scale should be displayed
  214. */
  215. void showScale ( bool visible );
  216. void showCrosshairs ( bool visible );
  217. //! Set whether to enable a view bounding box
  218. /*!
  219. *
  220. * @param usebounds enable/disable use of bounding box
  221. */
  222. void setUseBoundingBox( bool usebounds );
  223. //! Check if bounding box is being used
  224. /*!
  225. *
  226. * @return if bounding box is being used
  227. */
  228. bool isBoundingBoxEnabled();
  229. //! Set constraints for bounding box
  230. /*!
  231. *
  232. * @param rect specified bounds for view to stay within
  233. */
  234. void setBoundingBox( QRectF &rect );
  235. //! Get current bounding box
  236. /*!
  237. *
  238. * @return bounding box
  239. */
  240. QRectF getBoundingBox();
  241. //! Get viewport rect
  242. /*!
  243. *
  244. * @return view port rect
  245. */
  246. QRectF getViewport();
  247. //! returns if a geometry is visible on viewport
  248. /*!
  249. *
  250. * @param geometry the Geometry object to check
  251. * @return true if geometry is visible
  252. */
  253. bool isGeometryVisible( Geometry * geometry);
  254. //! returns loading images queue size
  255. /*!
  256. *
  257. * @return int pending loading images (queue size)
  258. */
  259. int loadingQueueSize();
  260. //! returns the imagemanager pointer
  261. /*!
  262. *
  263. * @return imagemanager pointer
  264. */
  265. ImageManager* getImageManager();
  266. private:
  267. void __init();
  268. LayerManager* m_layermanager;
  269. ImageManager* m_imagemanager;
  270. QPoint screen_middle; // middle of the widget (half size)
  271. QPoint pre_click_px; // used for scrolling (MouseMode Panning)
  272. QPoint current_mouse_pos; // used for scrolling and dragging (MouseMode Panning/Dragging)
  273. QSize size; // size of the widget
  274. bool mouse_wheel_events;
  275. bool mousepressed;
  276. MouseMode mymousemode;
  277. bool scaleVisible;
  278. bool crosshairsVisible;
  279. bool m_loadingFlag;
  280. QMutex moveMutex; // used for method moveTo()
  281. QPointF target; // used for method moveTo()
  282. int steps; // used for method moveTo()
  283. QPixmap *m_doubleBuffer;
  284. QPointF clickToWorldCoordinate ( QPoint click );
  285. //Q_DISABLE_COPY( MapControl )
  286. protected:
  287. void paintEvent ( QPaintEvent* evnt );
  288. void mousePressEvent ( QMouseEvent* evnt );
  289. void mouseReleaseEvent ( QMouseEvent* evnt );
  290. void mouseMoveEvent ( QMouseEvent* evnt );
  291. void wheelEvent( QWheelEvent* evnt );
  292. signals:
  293. // void mouseEvent(const QMouseEvent* evnt);
  294. //! Emitted AFTER a MouseEvent occured
  295. /*!
  296. * This signals allows to receive click events within the MapWidget together with the world coordinate.
  297. * It is emitted on MousePressEvents and MouseReleaseEvents.
  298. * The kind of the event can be obtained by checking the events type.
  299. * @param evnt the QMouseEvent that occured
  300. * @param coordinate the corresponding world coordinate
  301. */
  302. void mouseEventCoordinate ( const QMouseEvent* evnt, const QPointF coordinate );
  303. //! Emitted, after a Rectangular is dragged.
  304. /*!
  305. * It is possible to select a rectangular area in the map, if the MouseMode is set to Dragging.
  306. * The coordinates are in world coordinates
  307. * @param QRectF the dragged Rect
  308. */
  309. void boxDragged ( const QRectF );
  310. //! This signal is emitted, when a Geometry is clicked
  311. /*!
  312. * @param geometry The clicked Geometry object
  313. * @param coord_px The coordinate in pixel coordinates
  314. */
  315. void geometryClicked ( Geometry* geometry, QPoint coord_px );
  316. //! This signal is emitted, after the view have changed
  317. /*!
  318. * @param coordinate The current coordinate
  319. * @param zoom The current zoom
  320. */
  321. void viewChanged ( const QPointF &coordinate, int zoom ) const;
  322. public slots:
  323. //! zooms in one step
  324. void zoomIn();
  325. //! zooms out one step
  326. void zoomOut();
  327. //! sets the given zoomlevel
  328. /*!
  329. * @param zoomlevel the zoomlevel
  330. */
  331. void setZoom ( int zoomlevel );
  332. //! scrolls the view to the left
  333. void scrollLeft ( int pixel=10 );
  334. //! scrolls the view to the right
  335. void scrollRight ( int pixel=10 );
  336. //! scrolls the view up
  337. void scrollUp ( int pixel=10 );
  338. //! scrolls the view down
  339. void scrollDown ( int pixel=10 );
  340. //! scrolls the view by the given point
  341. void scroll ( const QPoint scroll );
  342. //! updates the map for the given rect
  343. /*!
  344. * @param rect the area which should be repainted
  345. */
  346. void updateRequest ( QRect rect );
  347. //! updates the hole map by creating a new offscreen image
  348. /*!
  349. *
  350. */
  351. void updateRequestNew();
  352. //! Resizes the map to the given size
  353. /*!
  354. * @param newSize The new size
  355. */
  356. void resize(const QSize newSize);
  357. public slots:
  358. void tick();
  359. void loadingFinished();
  360. void positionChanged ( Geometry* geom );
  361. };
  362. #endif