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.

222 lines
5.5 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 "linestring.h"
  26. LineString::LineString()
  27. : Curve()
  28. {
  29. GeometryType = "LineString";
  30. }
  31. LineString::LineString(QList<mapPoint*> const points, QString name, QPen* pen)
  32. :Curve(name)
  33. {
  34. mypen = pen;
  35. LineString();
  36. setPoints(points);
  37. }
  38. LineString::~LineString()
  39. {
  40. removePoints();
  41. }
  42. void LineString::removePoints()
  43. {
  44. QListIterator<mapPoint*> iter(childPoints);
  45. while(iter.hasNext())
  46. {
  47. mapPoint *pt = iter.next();
  48. if (pt && pt->parentGeometry() == this)
  49. {
  50. delete pt;
  51. pt = 0;
  52. }
  53. }
  54. childPoints.clear();
  55. }
  56. void LineString::addPoint(mapPoint* point)
  57. {
  58. point->setParentGeometry(this);
  59. childPoints.append(point);
  60. }
  61. QList<mapPoint*> LineString::points()
  62. {
  63. return childPoints;
  64. }
  65. void LineString::setPoints(QList<mapPoint*> points)
  66. {
  67. removePoints();
  68. for (int i=0; i<points.size(); i++)
  69. {
  70. points.at(i)->setParentGeometry(this);
  71. }
  72. childPoints = points;
  73. }
  74. void LineString::draw(QPainter* painter, const MapAdapter* mapadapter, const QRect &screensize, const QPoint offset)
  75. {
  76. if (!visible)
  77. return;
  78. QPolygon p = QPolygon();
  79. QPointF c;
  80. for (int i=0; i<childPoints.size(); i++)
  81. {
  82. c = childPoints[i]->coordinate();
  83. p.append(mapadapter->coordinateToDisplay(c));
  84. }
  85. if (mypen != 0)
  86. {
  87. painter->save();
  88. painter->setPen(*mypen);
  89. }
  90. painter->drawPolyline(p);
  91. if (mypen != 0)
  92. {
  93. painter->restore();
  94. }
  95. for (int i=0; i<childPoints.size(); i++)
  96. {
  97. childPoints[i]->draw(painter, mapadapter, screensize, offset);
  98. }
  99. }
  100. int LineString::numberOfPoints() const
  101. {
  102. return childPoints.count();
  103. }
  104. bool LineString::Touches(mapPoint* geom, const MapAdapter* mapadapter)
  105. {
  106. touchedPoints.clear();
  107. if (points().size() < 2)
  108. {
  109. // really shouldn't end up here since we always add atleast points to create a line
  110. return false;
  111. }
  112. bool touches = false;
  113. QPointF clickPt = mapadapter->coordinateToDisplay(geom->coordinate());
  114. qreal halfwidth = 2; // use 2 pixels by default
  115. if (mypen && mypen->width() > 0)
  116. {
  117. halfwidth = static_cast<qreal> (mypen->width())/ static_cast<qreal> (2);
  118. }
  119. QPointF pt1 = mapadapter->coordinateToDisplay(points().at(0)->coordinate());
  120. qreal pt1x1 = pt1.x() - halfwidth;
  121. qreal pt1x2 = pt1.x() + halfwidth;
  122. qreal pt1y1 = pt1.y() - halfwidth;
  123. qreal pt1y2 = pt1.y() + halfwidth;
  124. for (int i = 1; i < childPoints.size(); ++i)
  125. {
  126. QPointF pt2 = mapadapter->coordinateToDisplay(childPoints.at(i)->coordinate());
  127. qreal pt2x1 = pt2.x() - halfwidth;
  128. qreal pt2x2 = pt2.x() + halfwidth;
  129. qreal pt2y1 = pt2.y() - halfwidth;
  130. qreal pt2y2 = pt2.y() + halfwidth;
  131. // build lazy bounding box
  132. qreal upperLeftX = qMin(pt1x1, qMin(pt1x2, qMin(pt2x1, pt2x2)));
  133. qreal upperLeftY = qMin(pt1y1, qMin(pt1y2, qMin(pt2y1, pt2y2)));
  134. qreal lowerRightX = qMax(pt1x1, qMax(pt1x2, qMax(pt2x1, pt2x2)));
  135. qreal lowerRightY = qMax(pt1y1, qMax(pt1y2, qMax(pt2y1, pt2y2)));
  136. QRectF bounds(QPointF(upperLeftX, upperLeftY),
  137. QPointF(lowerRightX,lowerRightY));
  138. if (bounds.contains(clickPt))
  139. {
  140. touchedPoints.append(childPoints.at(i));
  141. touches = true;
  142. }
  143. pt1x1 = pt2x1;
  144. pt1x2 = pt2x2;
  145. pt1y1 = pt2y1;
  146. pt1y2 = pt2y2;
  147. }
  148. if (touches)
  149. {
  150. emit(geometryClicked(this, QPoint(0,0)));
  151. }
  152. return touches;
  153. }
  154. bool LineString::Touches(Geometry* /*geom*/, const MapAdapter* /*mapadapter*/)
  155. {
  156. touchedPoints.clear();
  157. return false;
  158. }
  159. QList<Geometry*>& LineString::clickedPoints()
  160. {
  161. return Geometry::clickedPoints();
  162. }
  163. bool LineString::hasPoints() const
  164. {
  165. return childPoints.size() > 0 ? true : false;
  166. }
  167. bool LineString::hasClickedPoints() const
  168. {
  169. return touchedPoints.size() > 0 ? true : false;
  170. }
  171. QRectF LineString::boundingBox()
  172. {
  173. qreal minlon=180;
  174. qreal maxlon=-180;
  175. qreal minlat=90;
  176. qreal maxlat=-90;
  177. for (int i=0; i<childPoints.size(); ++i)
  178. {
  179. mapPoint* tmp = childPoints.at(i);
  180. if (tmp->longitude() < minlon) minlon = tmp->longitude();
  181. if (tmp->longitude() > maxlon) maxlon = tmp->longitude();
  182. if (tmp->latitude() < minlat) minlat = tmp->latitude();
  183. if (tmp->latitude() > maxlat) maxlat = tmp->latitude();
  184. }
  185. QPointF min = QPointF(minlon, minlat);
  186. QPointF max = QPointF(maxlon, maxlat);
  187. QPointF dist = max - min;
  188. QSizeF si = QSizeF(dist.x(), dist.y());
  189. return QRectF(min, si);
  190. }