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.

387 lines
11 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. #include "layer.h"
  26. Layer::Layer()
  27. : visible(true),
  28. mylayertype(MapLayer),
  29. mapAdapter(0),
  30. takeevents(true),
  31. myoffscreenViewport(QRect(0,0,0,0)),
  32. m_ImageManager(0)
  33. {
  34. }
  35. Layer::Layer(QString layername, MapAdapter* mapadapter, enum LayerType layertype, bool takeevents)
  36. : visible(true),
  37. mylayername(layername),
  38. mylayertype(layertype),
  39. mapAdapter(mapadapter),
  40. takeevents(takeevents),
  41. myoffscreenViewport(QRect(0,0,0,0)),
  42. m_ImageManager(0)
  43. {
  44. }
  45. Layer::~Layer()
  46. {
  47. if( mapAdapter )
  48. {
  49. mapAdapter->deleteLater();
  50. mapAdapter = 0;
  51. }
  52. }
  53. void Layer::setSize(QSize size)
  54. {
  55. this->size = size;
  56. screenmiddle = QPoint(size.width()/2, size.height()/2);
  57. emit(updateRequest());
  58. }
  59. QString Layer::layername() const
  60. {
  61. return mylayername;
  62. }
  63. MapAdapter* Layer::mapadapter()
  64. {
  65. return mapAdapter;
  66. }
  67. void Layer::setVisible(bool visible)
  68. {
  69. this->visible = visible;
  70. emit(updateRequest());
  71. }
  72. QList<Geometry*>& Layer::getGeometries()
  73. {
  74. return geometries;
  75. }
  76. bool Layer::containsGeometry( Geometry* geometry )
  77. {
  78. return geometry && geometries.contains( geometry );
  79. }
  80. void Layer::sendGeometryToFront(Geometry *geometry)
  81. {
  82. if ( !geometry || !geometries.contains( geometry ) )
  83. {
  84. return;
  85. }
  86. geometries.removeAll(geometry);
  87. geometries.prepend(geometry);
  88. emit(updateRequest());
  89. }
  90. void Layer::sendGeometryToBack(Geometry *geometry)
  91. {
  92. if ( !geometry || !geometries.contains( geometry ) )
  93. {
  94. return;
  95. }
  96. geometries.removeAll(geometry);
  97. geometries.append(geometry);
  98. emit(updateRequest());
  99. }
  100. void Layer::addGeometry(Geometry* geom)
  101. {
  102. if ( !geom || containsGeometry( geom ) )
  103. {
  104. return;
  105. }
  106. geometries.append(geom);
  107. emit(updateRequest(geom->boundingBox()));
  108. //a geometry can request a redraw, e.g. when its position has been changed
  109. connect(geom, SIGNAL(updateRequest(QRectF)),
  110. this, SIGNAL(updateRequest(QRectF)));
  111. }
  112. void Layer::removeGeometry(Geometry* geometry, bool qDeleteObject)
  113. {
  114. if ( !geometry )
  115. {
  116. return;
  117. }
  118. QRectF boundingBox = geometry->boundingBox();
  119. foreach( Geometry* geo, geometries )
  120. {
  121. if ( geo && geo == geometry )
  122. {
  123. disconnect(geometry);
  124. geometries.removeAll( geometry );
  125. if (qDeleteObject)
  126. {
  127. delete geo;
  128. geo = 0;
  129. }
  130. }
  131. }
  132. emit(updateRequest(boundingBox));
  133. }
  134. void Layer::clearGeometries( bool qDeleteObject )
  135. {
  136. foreach(Geometry *geometry, geometries)
  137. {
  138. disconnect(geometry);
  139. if ( qDeleteObject )
  140. {
  141. delete geometry;
  142. geometry = 0;
  143. }
  144. }
  145. geometries.clear();
  146. }
  147. bool Layer::isVisible() const
  148. {
  149. return visible;
  150. }
  151. void Layer::zoomIn() const
  152. {
  153. mapAdapter->zoom_in();
  154. }
  155. void Layer::zoomOut() const
  156. {
  157. mapAdapter->zoom_out();
  158. }
  159. void Layer::mouseEvent(const QMouseEvent* evnt, const QPoint mapmiddle_px)
  160. {
  161. if (takesMouseEvents())
  162. {
  163. if ( geometries.size() > 0 &&
  164. evnt->button() == Qt::LeftButton &&
  165. evnt->type() == QEvent::MouseButtonPress)
  166. {
  167. // check for collision
  168. QPointF c = mapAdapter->displayToCoordinate(QPoint(evnt->x()-screenmiddle.x()+mapmiddle_px.x(),
  169. evnt->y()-screenmiddle.y()+mapmiddle_px.y()));
  170. mapPoint* tmppoint = new mapPoint(c.x(), c.y());
  171. for(QList<Geometry*>::const_iterator iter = geometries.begin(); iter != geometries.end(); ++iter)
  172. {
  173. Geometry *geo = *iter;
  174. if (geo && geo->isVisible() && geo->Touches(tmppoint, mapAdapter))
  175. {
  176. emit(geometryClicked(geo, QPoint(evnt->x(), evnt->y())));
  177. }
  178. }
  179. delete tmppoint;
  180. }
  181. }
  182. }
  183. bool Layer::takesMouseEvents() const
  184. {
  185. return takeevents;
  186. }
  187. void Layer::drawYourImage(QPainter* painter, const QPoint mapmiddle_px) const
  188. {
  189. if (mylayertype == MapLayer)
  190. {
  191. _draw(painter, mapmiddle_px);
  192. }
  193. drawYourGeometries(painter, QPoint(mapmiddle_px.x()-screenmiddle.x(), mapmiddle_px.y()-screenmiddle.y()), myoffscreenViewport);
  194. }
  195. void Layer::drawYourGeometries(QPainter* painter, const QPoint mapmiddle_px, QRect viewport) const
  196. {
  197. QPoint offset;
  198. if (mylayertype == MapLayer)
  199. {
  200. offset = mapmiddle_px;
  201. }
  202. else
  203. {
  204. offset = mapmiddle_px-screenmiddle;
  205. }
  206. painter->translate(-mapmiddle_px+screenmiddle);
  207. for(QList<Geometry*>::const_iterator iter = geometries.begin(); iter != geometries.end(); ++iter)
  208. {
  209. Geometry *geo = *iter;
  210. geo->draw(painter, mapAdapter, viewport, offset);
  211. }
  212. painter->translate(mapmiddle_px-screenmiddle);
  213. }
  214. void Layer::_draw(QPainter* painter, const QPoint mapmiddle_px) const
  215. {
  216. if ( m_ImageManager == 0 )
  217. {
  218. return;
  219. }
  220. // screen middle...
  221. int tilesize = mapAdapter->tilesize();
  222. int cross_x = int(mapmiddle_px.x())%tilesize; // position on middle tile
  223. int cross_y = int(mapmiddle_px.y())%tilesize;
  224. // calculate how many surrounding tiles have to be drawn to fill the display
  225. int space_left = screenmiddle.x() - cross_x;
  226. int tiles_left = space_left/tilesize;
  227. if (space_left>0)
  228. tiles_left+=1;
  229. int space_above = screenmiddle.y() - cross_y;
  230. int tiles_above = space_above/tilesize;
  231. if (space_above>0)
  232. tiles_above+=1;
  233. int space_right = screenmiddle.x() - (tilesize-cross_x);
  234. int tiles_right = space_right/tilesize;
  235. if (space_right>0)
  236. tiles_right+=1;
  237. int space_bottom = screenmiddle.y() - (tilesize-cross_y);
  238. int tiles_bottom = space_bottom/tilesize;
  239. if (space_bottom>0)
  240. tiles_bottom+=1;
  241. //int tiles_displayed = 0;
  242. int mapmiddle_tile_x = mapmiddle_px.x()/tilesize;
  243. int mapmiddle_tile_y = mapmiddle_px.y()/tilesize;
  244. const QPoint from = QPoint((-tiles_left+mapmiddle_tile_x)*tilesize, (-tiles_above+mapmiddle_tile_y)*tilesize);
  245. const QPoint to = QPoint((tiles_right+mapmiddle_tile_x+1)*tilesize, (tiles_bottom+mapmiddle_tile_y+1)*tilesize);
  246. myoffscreenViewport = QRect(from, to);
  247. // for the EmptyMapAdapter no tiles should be loaded and painted.
  248. if (mapAdapter->host().isEmpty())
  249. {
  250. return;
  251. }
  252. //grab the middle tile (under the pointer) first
  253. if (mapAdapter->isTileValid(mapmiddle_tile_x, mapmiddle_tile_y, mapAdapter->currentZoom()))
  254. {
  255. painter->drawPixmap(-cross_x+size.width(),
  256. -cross_y+size.height(),
  257. m_ImageManager->getImage(mapAdapter->host(), mapAdapter->query(mapmiddle_tile_x, mapmiddle_tile_y, mapAdapter->currentZoom())) );
  258. }
  259. for (int i=-tiles_left+mapmiddle_tile_x; i<=tiles_right+mapmiddle_tile_x; ++i)
  260. {
  261. for (int j=-tiles_above+mapmiddle_tile_y; j<=tiles_bottom+mapmiddle_tile_y; ++j)
  262. {
  263. // check if image is valid
  264. if (!(i==mapmiddle_tile_x && j==mapmiddle_tile_y))
  265. {
  266. if (mapAdapter->isTileValid(i, j, mapAdapter->currentZoom()))
  267. {
  268. painter->drawPixmap(((i-mapmiddle_tile_x)*tilesize)-cross_x+size.width(),
  269. ((j-mapmiddle_tile_y)*tilesize)-cross_y+size.height(),
  270. m_ImageManager->getImage(mapAdapter->host(), mapAdapter->query(i, j, mapAdapter->currentZoom())));
  271. }
  272. }
  273. }
  274. }
  275. bool enabledPrefetch = false;
  276. if ( enabledPrefetch )
  277. {
  278. // Prefetch the next set of rows/column tiles (ready for when the user starts panning).
  279. const int prefetch_tile_left = tiles_left - 1;
  280. const int prefetch_tile_top = tiles_above - 1;
  281. const int prefetch_tile_right = tiles_right + 1;
  282. const int prefetch_tile_bottom = tiles_bottom + 1;
  283. // Fetch the top/bottom rows
  284. for (int i=prefetch_tile_left; i<=prefetch_tile_right; ++i)
  285. {
  286. if (mapAdapter->isTileValid(i, prefetch_tile_top, mapAdapter->currentZoom()))
  287. {
  288. m_ImageManager->prefetchImage(mapAdapter->host(), mapAdapter->query(i, prefetch_tile_top, mapAdapter->currentZoom()));
  289. }
  290. if (mapAdapter->isTileValid(i, prefetch_tile_bottom, mapAdapter->currentZoom()))
  291. {
  292. m_ImageManager->prefetchImage(mapAdapter->host(), mapAdapter->query(i, prefetch_tile_bottom, mapAdapter->currentZoom()));
  293. }
  294. }
  295. for (int i=prefetch_tile_top; i<=prefetch_tile_bottom; ++i)
  296. {
  297. if (mapAdapter->isTileValid(prefetch_tile_left, i, mapAdapter->currentZoom()))
  298. {
  299. m_ImageManager->prefetchImage(mapAdapter->host(), mapAdapter->query(prefetch_tile_left, i, mapAdapter->currentZoom()));
  300. }
  301. if (mapAdapter->isTileValid(prefetch_tile_right, i, mapAdapter->currentZoom()))
  302. {
  303. m_ImageManager->prefetchImage(mapAdapter->host(), mapAdapter->query(prefetch_tile_right, i, mapAdapter->currentZoom()));
  304. }
  305. }
  306. }
  307. }
  308. QRect Layer::offscreenViewport() const
  309. {
  310. return myoffscreenViewport;
  311. }
  312. void Layer::moveWidgets(const QPoint mapmiddle_px) const
  313. {
  314. foreach( Geometry* geometry, geometries )
  315. {
  316. if (geometry->GeometryType == "Point")
  317. {
  318. mapPoint* point = dynamic_cast<mapPoint*>(geometry);
  319. if (point !=0)
  320. {
  321. QPoint topleft_relative = QPoint(mapmiddle_px-screenmiddle);
  322. point->drawWidget(mapAdapter, topleft_relative);
  323. }
  324. }
  325. }
  326. }
  327. Layer::LayerType Layer::layertype() const
  328. {
  329. return mylayertype;
  330. }
  331. void Layer::setMapAdapter(MapAdapter* mapadapter)
  332. {
  333. mapAdapter = mapadapter;
  334. emit(updateRequest());
  335. }
  336. void Layer::setImageManager(ImageManager *qImageManager)
  337. {
  338. m_ImageManager = qImageManager;
  339. }
  340. void Layer::geometryClicked(Geometry* geometry, QPoint point){};
  341. void Layer::updateRequest(QRectF rect){};
  342. void Layer::updateRequest(){};