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.

389 lines
11 KiB

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