dRonin  adbada4
dRonin GCS
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Groups Pages
homeitem.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 
28 #include "tlmapwidget.h"
29 
30 #include "homeitem.h"
31 #include <QGraphicsSceneMouseEvent>
32 
33 namespace mapcontrol
34 {
35  HomeItem::HomeItem(MapGraphicItem* map, TLMapWidget* parent):
36  safe(true),
37  mapwidget(parent),
38  showsafearea(true),
39  toggleRefresh(true),
40  safearea(1000),
41  localsafearea(0),
42  isDragging(false)
43  {
44  this->map = map;
45  this->altitude = altitude;
46 
47  pic.load(QString::fromUtf8(":/markers/images/home2.svg"));
48  pic=pic.scaled(30,30,Qt::IgnoreAspectRatio);
49  this->setFlag(QGraphicsItem::ItemIgnoresTransformations,true);
50  this->setFlag(QGraphicsItem::ItemIsMovable,false);
51  this->setFlag(QGraphicsItem::ItemIsSelectable,false);
52  localposition=map->FromLatLngToLocal(mapwidget->CurrentPosition());
53  this->setPos(localposition.X(),localposition.Y());
54  this->setZValue(4);
56  RefreshToolTip();
57  setCacheMode(QGraphicsItem::DeviceCoordinateCache);
58  connect(map,SIGNAL(childRefreshPosition()),this,SLOT(RefreshPos()));
59  connect(map,SIGNAL(childSetOpacity(qreal)),this,SLOT(setOpacitySlot(qreal)));
60  }
61 
62  void HomeItem::RefreshToolTip()
63  {
64  QString coord_str = " " + QString::number(coord.Lat(), 'f', 6) + " " + QString::number(coord.Lng(), 'f', 6);
65 
66  setToolTip(QString("Waypoint: Home\nCoordinate:%1\nAltitude:%2\n").arg(coord_str).arg(QString::number(altitude)));
67  }
68 
69 
70  void HomeItem::paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget)
71  {
72  Q_UNUSED(option);
73  Q_UNUSED(widget);
74  painter->drawPixmap(-pic.width()/2,-pic.height()/2,pic);
75  if(showsafearea)
76  {
77  if(safe)
78  painter->setPen(Qt::green);
79  else
80  painter->setPen(Qt::red);
81  painter->drawRect(QRectF(-localsafearea,-localsafearea,localsafearea*2,localsafearea*2));
82  }
83 
84  }
85  QRectF HomeItem::boundingRect()const
86  {
87  if(pic.width()>localsafearea*2 && !toggleRefresh)
88  return QRectF(-pic.width()/2,-pic.height()/2,pic.width(),pic.height());
89  else
90  return QRectF(-localsafearea,-localsafearea,localsafearea*2,localsafearea*2);
91  }
92 
93 
94  int HomeItem::type()const
95  {
96  return Type;
97  }
98 
100  {
101  prepareGeometryChange();
102  localposition=map->FromLatLngToLocal(coord);
103  this->setPos(localposition.X(),localposition.Y());
104  if(showsafearea)
105  localsafearea=safearea/map->Projection()->GetGroundResolution(map->ZoomTotal(),coord.Lat());
106 
107  RefreshToolTip();
108 
109  this->update();
110  toggleRefresh=false;
111 
112  }
113 
114  void HomeItem::setOpacitySlot(qreal opacity)
115  {
116  setOpacity(opacity);
117  }
118 
119  void HomeItem::mousePressEvent(QGraphicsSceneMouseEvent *event)
120  {
121  if(event->button()==Qt::LeftButton)
122  {
123  isDragging=true;
124  }
125  QGraphicsItem::mousePressEvent(event);
126  }
127 
128  void HomeItem::mouseReleaseEvent(QGraphicsSceneMouseEvent *event)
129  {
130  if(event->button()==Qt::LeftButton)
131  {
132  coord=map->FromLocalToLatLng(this->pos().x(),this->pos().y());
133  isDragging=false;
134 
135  emit absolutePositionChanged(coord, Altitude());
136  }
137  QGraphicsItem::mouseReleaseEvent(event);
138 
139  RefreshToolTip();
140  }
141  void HomeItem::mouseDoubleClickEvent(QGraphicsSceneMouseEvent *event)
142  {
143  if(event->button()==Qt::LeftButton)
144  {
145  emit homedoubleclick(this);
146  }
147  }
148  void HomeItem::mouseMoveEvent(QGraphicsSceneMouseEvent *event)
149  {
150 
151  if(isDragging)
152  {
153  coord=map->FromLocalToLatLng(this->pos().x(),this->pos().y());
154  emit absolutePositionChanged(coord, Altitude());
155  }
156  QGraphicsItem::mouseMoveEvent(event);
157  }
158 }
159 
qint64 X() const
Definition: point.h:51
void absolutePositionChanged(internals::PointLatLng coord, float altitude)
The Map Widget, this is the part exposed to the user.
internals::PointLatLng coord
Definition: mappointitem.h:109
internals::PointLatLng CurrentPosition() const
Definition: tlmapwidget.h:224
void mouseMoveEvent(QGraphicsSceneMouseEvent *event)
Definition: homeitem.cpp:148
void setOpacitySlot(qreal opacity)
Definition: gpsitem.cpp:157
double Lng() const
Definition: pointlatlng.h:76
double Lat() const
Definition: pointlatlng.h:64
A graphicsItem representing a Home Location.
void mouseReleaseEvent(QGraphicsSceneMouseEvent *event)
Definition: homeitem.cpp:128
void mouseDoubleClickEvent(QGraphicsSceneMouseEvent *event)
Definition: homeitem.cpp:141
x
Definition: OPPlots.m:100
void mousePressEvent(QGraphicsSceneMouseEvent *event)
Definition: homeitem.cpp:119
void homedoubleclick(HomeItem *homeLocation)
qint64 Y() const
Definition: point.h:52
MapGraphicItem * map
Definition: mappointitem.h:107
void setOpacitySlot(qreal opacity)
Definition: homeitem.cpp:114
y
Definition: OPPlots.m:101