dRonin  adbada4
dRonin GCS
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Groups Pages
mapline.cpp
Go to the documentation of this file.
1 
13 /*
14 * This program is free software; you can redistribute it and/or modify
15 * it under the terms of the GNU General Public License as published by
16 * the Free Software Foundation; either version 3 of the License, or
17 * (at your option) any later version.
18 *
19 * This program is distributed in the hope that it will be useful, but
20 * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
21 * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
22 * for more details.
23 *
24 * You should have received a copy of the GNU General Public License along
25 * with this program; if not, see <http://www.gnu.org/licenses/>
26 */
27 #include "mapline.h"
28 #include <math.h>
29 #include "homeitem.h"
30 
31 namespace mapcontrol
32 {
33 MapLine::MapLine(MapPointItem *from, MapPointItem *to, MapGraphicItem *map, QColor color) :
34  QGraphicsLineItem(map), source(from), destination(to), my_map(map), myColor(color)
35 {
36  this->setLine(to->pos().x(),to->pos().y(),from->pos().x(),from->pos().y());
37  connect(from, SIGNAL(relativePositionChanged(QPointF, MapPointItem*)), this, SLOT(refreshLocations()));
38  connect(to, SIGNAL(relativePositionChanged(QPointF, MapPointItem*)), this, SLOT(refreshLocations()));
39  connect(from, SIGNAL(aboutToBeDeleted(MapPointItem*)), this, SLOT(pointdeleted()));
40  connect(to, SIGNAL(aboutToBeDeleted(MapPointItem*)), this, SLOT(pointdeleted()));
41  if(myColor==Qt::green)
42  this->setZValue(10);
43  else if(myColor==Qt::yellow)
44  this->setZValue(9);
45  else if(myColor==Qt::red)
46  this->setZValue(8);
47  connect(map,SIGNAL(childSetOpacity(qreal)),this,SLOT(setOpacitySlot(qreal)));
48 }
49 
50 MapLine::MapLine(HomeItem *from, MapPointItem *to, MapGraphicItem *map, QColor color) :
51  QGraphicsLineItem(map), source(from), destination(to), my_map(map), myColor(color)
52 {
53  this->setLine(to->pos().x(),to->pos().y(),from->pos().x(),from->pos().y());
54  connect(from, SIGNAL(absolutePositionChanged(internals::PointLatLng, float)), this, SLOT(refreshLocations()));
55  connect(to, SIGNAL(relativePositionChanged(QPointF, MapPointItem*)), this, SLOT(refreshLocations()));
56  connect(to, SIGNAL(aboutToBeDeleted(MapPointItem*)), this, SLOT(pointdeleted()));
57  if(myColor==Qt::green)
58  this->setZValue(10);
59  else if(myColor==Qt::yellow)
60  this->setZValue(9);
61  else if(myColor==Qt::red)
62  this->setZValue(8);
63  connect(map,SIGNAL(childSetOpacity(qreal)),this,SLOT(setOpacitySlot(qreal)));
64 }
65 
66 int MapLine::type() const
67 {
68  // Enable the use of qgraphicsitem_cast with this item.
69  return Type;
70 }
71 
72 QPainterPath MapLine::shape() const
73 {
74  QPainterPath path = QGraphicsLineItem::shape();
75  path.addPolygon(arrowHead);
76  return path;
77 }
78 
79 void MapLine::paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget)
80 {
81  Q_UNUSED(option);
82  Q_UNUSED(widget);
83 
84  QPen myPen = pen();
85  myPen.setColor(myColor);
86  qreal arrowSize = 10;
87  painter->setPen(myPen);
88  painter->setBrush(myColor);
89 
90  // Prevent segfaults when length is zero
91  double angle = (fabs(line().length()) < 1e-3) ? 0 : ::acos(line().dx() / line().length());
92 
93  if (line().dy() >= 0)
94  angle = (M_PI * 2) - angle;
95 
96  QPointF arrowP1 = line().pointAt(0.5) + QPointF(sin(angle + M_PI / 3) * arrowSize,
97  cos(angle + M_PI / 3) * arrowSize);
98  QPointF arrowP2 = line().pointAt(0.5) + QPointF(sin(angle + M_PI - M_PI / 3) * arrowSize,
99  cos(angle + M_PI - M_PI / 3) * arrowSize);
100  arrowHead.clear();
101  arrowHead << line().pointAt(0.5) << arrowP1 << arrowP2;
102 
103  painter->drawPolygon(arrowHead);
104  if(myColor==Qt::red)
105  myPen.setWidth(3);
106  else if(myColor==Qt::yellow)
107  myPen.setWidth(2);
108  else if(myColor==Qt::green)
109  myPen.setWidth(1);
110  painter->setPen(myPen);
111  painter->drawLine(line());
112 }
113 
115 {
116  this->setLine(destination->pos().x(),destination->pos().y(),source->pos().x(),source->pos().y());
117 }
118 
120 {
121  this->deleteLater();
122 }
123 
124 void MapLine::setOpacitySlot(qreal opacity)
125 {
126  setOpacity(opacity);
127 }
128 
129 }
void childSetOpacity(qreal value)
A graphicsItem representing a line connecting 2 map points.
A graphicsItem representing a Home Location.
void paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget)
Definition: mapline.cpp:79
void refreshLocations()
Definition: mapline.cpp:114
void setOpacitySlot(qreal opacity)
Definition: mapline.cpp:124
e
Definition: OPPlots.m:99