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.

370 lines
9.3 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 "point.h"
  26. mapPoint::mapPoint(){}
  27. mapPoint::mapPoint(const mapPoint& point)
  28. :Geometry(point.name()), X(point.longitude()), Y(point.latitude())
  29. {
  30. visible = point.isVisible();
  31. mywidget = 0;
  32. mypixmap = QPixmap();
  33. mypen = point.mypen;
  34. homelevel = -1;
  35. minsize = QSize(-1,-1);
  36. maxsize = QSize(-1,-1);
  37. }
  38. mapPoint::mapPoint(qreal x, qreal y, QString name, enum Alignment alignment)
  39. : Geometry(name), X(x), Y(y), myalignment(alignment)
  40. {
  41. GeometryType = "Point";
  42. mywidget = 0;
  43. mypixmap = QPixmap();
  44. visible = true;
  45. homelevel = -1;
  46. minsize = QSize(-1,-1);
  47. maxsize = QSize(-1,-1);
  48. }
  49. mapPoint::mapPoint(qreal x, qreal y, QWidget* widget, QString name, enum Alignment alignment)
  50. : Geometry(name), X(x), Y(y), mywidget(widget), myalignment(alignment)
  51. {
  52. // Point(x, y, name, alignment);
  53. GeometryType = "Point";
  54. mypixmap = QPixmap();
  55. visible = true;
  56. size = widget->size();
  57. homelevel = -1;
  58. minsize = QSize(-1,-1);
  59. maxsize = QSize(-1,-1);
  60. if(mywidget!=0)
  61. {
  62. mywidget->show();
  63. }
  64. }
  65. mapPoint::mapPoint(qreal x, qreal y, QPixmap pixmap, QString name, enum Alignment alignment)
  66. : Geometry(name), X(x), Y(y), mypixmap(pixmap), myalignment(alignment)
  67. {
  68. GeometryType = "Point";
  69. mywidget = 0;
  70. visible = true;
  71. size = pixmap.size();
  72. homelevel = -1;
  73. minsize = QSize(-1,-1);
  74. maxsize = QSize(-1,-1);
  75. }
  76. /*
  77. Point& Point::operator=(const Point& rhs)
  78. {
  79. if (this == &rhs)
  80. return *this;
  81. else
  82. {
  83. X = rhs.X;
  84. Y = rhs.Y;
  85. size = rhs.size;
  86. mywidget = rhs.mywidget;
  87. mypixmap = rhs.mypixmap;
  88. alignment = rhs.alignment;
  89. homelevel = rhs.homelevel;
  90. minsize = rhs.minsize;
  91. maxsize = rhs.maxsize;
  92. }
  93. }
  94. */
  95. mapPoint::~mapPoint()
  96. {
  97. if(mywidget!=0)
  98. {
  99. delete mywidget;
  100. mywidget = 0;
  101. }
  102. }
  103. void mapPoint::setPixmap(QPixmap qPixmap)
  104. {
  105. mypixmap = qPixmap;
  106. size = mypixmap.size();
  107. //forces redraw
  108. emit(updateRequest(QRectF(X, Y, size.width(), size.height())));
  109. emit(positionChanged(this));
  110. }
  111. void mapPoint::setVisible(bool visible)
  112. {
  113. this->visible = visible;
  114. if (mywidget !=0)
  115. {
  116. mywidget->setVisible(visible);
  117. }
  118. }
  119. QRectF mapPoint::boundingBox()
  120. {
  121. qreal minlon=180;
  122. qreal maxlon=-180;
  123. qreal minlat=90;
  124. qreal maxlat=-90;
  125. if (longitude() < minlon) minlon = longitude();
  126. if (longitude() > maxlon) maxlon = longitude();
  127. if (latitude() < minlat) minlat = latitude();
  128. if (latitude() > maxlat) maxlat = latitude();
  129. QPointF min = QPointF(minlon, minlat);
  130. QPointF max = QPointF(maxlon, maxlat);
  131. QPointF dist = max - min;
  132. QSizeF si = QSizeF(dist.x(), dist.y());
  133. return QRectF(min, si);
  134. }
  135. qreal mapPoint::longitude() const
  136. {
  137. return X;
  138. }
  139. qreal mapPoint::latitude() const
  140. {
  141. return Y;
  142. }
  143. QPointF mapPoint::coordinate() const
  144. {
  145. return QPointF(X, Y);
  146. }
  147. void mapPoint::draw(QPainter* painter, const MapAdapter* mapadapter, const QRect &viewport, const QPoint offset)
  148. {
  149. if (!visible)
  150. return;
  151. if (homelevel > 0)
  152. {
  153. int currentzoom = mapadapter->maxZoom() < mapadapter->minZoom() ? mapadapter->minZoom() - mapadapter->currentZoom() : mapadapter->currentZoom();
  154. // int currentzoom = mapadapter->getZoom();
  155. int diffzoom = homelevel-currentzoom;
  156. int viewheight = size.height();
  157. int viewwidth = size.width();
  158. viewheight = int(viewheight / pow(2.0, diffzoom));
  159. viewwidth = int(viewwidth / pow(2.0, diffzoom));
  160. if (minsize.height()!= -1 && viewheight < minsize.height())
  161. viewheight = minsize.height();
  162. else if (maxsize.height() != -1 && viewheight > maxsize.height())
  163. viewheight = maxsize.height();
  164. if (minsize.width()!= -1 && viewwidth < minsize.width())
  165. viewwidth = minsize.width();
  166. else if (maxsize.width() != -1 && viewwidth > maxsize.width())
  167. viewwidth = maxsize.width();
  168. displaysize = QSize(viewwidth, viewheight);
  169. }
  170. else
  171. {
  172. displaysize = size;
  173. }
  174. if (mypixmap.size().width() > 0)
  175. {
  176. const QPointF c = QPointF(X, Y);
  177. QPoint point = mapadapter->coordinateToDisplay(c);
  178. if (viewport.contains(point))
  179. {
  180. QPoint alignedtopleft = alignedPoint(point);
  181. painter->drawPixmap(alignedtopleft.x(), alignedtopleft.y(), displaysize.width(), displaysize.height(), mypixmap);
  182. }
  183. }
  184. else if (mywidget!=0)
  185. {
  186. drawWidget(mapadapter, offset);
  187. }
  188. }
  189. void mapPoint::drawWidget(const MapAdapter* mapadapter, const QPoint offset)
  190. {
  191. const QPointF c = QPointF(X, Y);
  192. QPoint point = mapadapter->coordinateToDisplay(c);
  193. point -= offset;
  194. QPoint alignedtopleft = alignedPoint(point);
  195. if (mywidget!=0)
  196. {
  197. mywidget->setGeometry(alignedtopleft.x(), alignedtopleft.y(), displaysize.width(), displaysize.height());
  198. }
  199. }
  200. QPoint mapPoint::alignedPoint(const QPoint point) const
  201. {
  202. QPoint alignedtopleft;
  203. if (myalignment == Middle)
  204. {
  205. alignedtopleft.setX(point.x()-displaysize.width()/2);
  206. alignedtopleft.setY(point.y()-displaysize.height()/2);
  207. }
  208. else if (myalignment == TopLeft)
  209. {
  210. alignedtopleft.setX(point.x());
  211. alignedtopleft.setY(point.y());
  212. }
  213. else if (myalignment == TopRight)
  214. {
  215. alignedtopleft.setX(point.x()-displaysize.width());
  216. alignedtopleft.setY(point.y());
  217. }
  218. else if (myalignment == BottomLeft)
  219. {
  220. alignedtopleft.setX(point.x());
  221. alignedtopleft.setY(point.y()-displaysize.height());
  222. }
  223. else if (myalignment == BottomRight)
  224. {
  225. alignedtopleft.setX(point.x()-displaysize.width());
  226. alignedtopleft.setY(point.y()-displaysize.height());
  227. }
  228. else if (myalignment == BottomMiddle)
  229. {
  230. alignedtopleft.setX(point.x()-displaysize.width()/2);
  231. alignedtopleft.setY(point.y()-displaysize.height());
  232. }
  233. else if (myalignment == TopMiddle)
  234. {
  235. alignedtopleft.setX(point.x()-displaysize.width()/2);
  236. alignedtopleft.setY(point.y());
  237. }
  238. return alignedtopleft;
  239. }
  240. bool mapPoint::Touches(mapPoint* click, const MapAdapter* mapadapter)
  241. {
  242. if (this->isVisible() == false)
  243. return false;
  244. if ( !click || !mapadapter )
  245. return false;
  246. if (points().size() < 1)
  247. {
  248. return false;
  249. }
  250. QPointF clickPt = mapadapter->coordinateToDisplay(click->coordinate());
  251. qreal halfwidth = 2; // use 2 pixels by default
  252. if (mypixmap.width() > 0)
  253. {
  254. halfwidth = static_cast<qreal> (mypixmap.width()) / static_cast<qreal> (2);
  255. }
  256. QPointF pt1 = mapadapter->coordinateToDisplay(coordinate());
  257. qreal pt1x1 = pt1.x() - halfwidth;
  258. qreal pt1x2 = pt1.x() + halfwidth;
  259. qreal pt1y1 = pt1.y() - halfwidth;
  260. qreal pt1y2 = pt1.y() + halfwidth;
  261. QPointF pt2 = mapadapter->coordinateToDisplay(coordinate());
  262. qreal pt2x1 = pt2.x() - halfwidth;
  263. qreal pt2x2 = pt2.x() + halfwidth;
  264. qreal pt2y1 = pt2.y() - halfwidth;
  265. qreal pt2y2 = pt2.y() + halfwidth;
  266. // build lazy bounding box
  267. qreal upperLeftX = qMin(pt1x1, qMin(pt1x2, qMin(pt2x1, pt2x2)));
  268. qreal upperLeftY = qMin(pt1y1, qMin(pt1y2, qMin(pt2y1, pt2y2)));
  269. qreal lowerRightX = qMax(pt1x1, qMax(pt1x2, qMax(pt2x1, pt2x2)));
  270. qreal lowerRightY = qMax(pt1y1, qMax(pt1y2, qMax(pt2y1, pt2y2)));
  271. QRectF bounds(QPointF(upperLeftX, upperLeftY), QPointF(lowerRightX,
  272. lowerRightY));
  273. if ( bounds.contains(clickPt) )
  274. {
  275. emit(geometryClicked(this, QPoint(0, 0)));
  276. return true;
  277. }
  278. return false;
  279. }
  280. void mapPoint::setCoordinate(QPointF point)
  281. {
  282. if ( X == point.x() &&
  283. Y == point.y() )
  284. {
  285. //no change, prevent unessessary update/redraw
  286. return;
  287. }
  288. X = point.x();
  289. Y = point.y();
  290. emit(updateRequest(QRectF(X, Y, size.width(), size.height())));
  291. emit(positionChanged(this));
  292. }
  293. QList<mapPoint*> mapPoint::points()
  294. {
  295. //TODO: assigning temp?!
  296. QList<mapPoint*> points;
  297. points.append(this);
  298. return points;
  299. }
  300. QWidget* mapPoint::widget()
  301. {
  302. return mywidget;
  303. }
  304. QPixmap mapPoint::pixmap()
  305. {
  306. return mypixmap;
  307. }
  308. void mapPoint::setBaselevel(int zoomlevel)
  309. {
  310. homelevel = zoomlevel;
  311. }
  312. void mapPoint::setMinsize(QSize minsize)
  313. {
  314. this->minsize = minsize;
  315. }
  316. void mapPoint::setMaxsize(QSize maxsize)
  317. {
  318. this->maxsize = maxsize;
  319. }
  320. mapPoint::Alignment mapPoint::alignment() const
  321. {
  322. return myalignment;
  323. }