dRonin  adbada4
dRonin GCS
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Groups Pages
waypointitem.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 "waypointitem.h"
28 #include "homeitem.h"
29 #include <QGraphicsSceneMouseEvent>
30 
31 namespace mapcontrol
32 {
33  WayPointItem::WayPointItem(const internals::PointLatLng &coord,int const& altitude, MapGraphicItem *map,wptype type) :
34  reached(false),
35  shownumber(true),
36  isDragging(false),
37  myType(type)
38  {
39  this->map = map;
40  this->coord = coord;
41  this->altitude = altitude;
42  description = "";
43 
44  text=nullptr;
45  numberI=nullptr;
46  isMagic=false;
47  picture.load(QString::fromUtf8(":/markers/images/marker.png"));
48  number=WayPointItem::snumber;
49  ++WayPointItem::snumber;
50  this->setFlag(QGraphicsItem::ItemIsMovable,true);
51  this->setFlag(QGraphicsItem::ItemIgnoresTransformations,true);
52  this->setFlag(QGraphicsItem::ItemIsSelectable,true);
53  SetShowNumber(shownumber);
54  RefreshToolTip();
55  RefreshPos();
56  myHome=NULL;
57  QList<QGraphicsItem *> list=map->childItems();
58  foreach(QGraphicsItem * obj,list)
59  {
60  HomeItem* h=qgraphicsitem_cast <HomeItem*>(obj);
61  if(h)
62  myHome=h;
63  }
64 
65  if(myHome)
66  {
67  map->Projection()->offSetFromLatLngs(myHome->Coord(),coord,relativeCoord.distance,relativeCoord.bearing);
68  connect(myHome, SIGNAL(absolutePositionChanged(internals::PointLatLng, float)), this, SLOT(onHomePositionChanged(internals::PointLatLng, float)));
69  }
70  connect(this,SIGNAL(waypointdoubleclick(WayPointItem*)),map,SIGNAL(wpdoubleclicked(WayPointItem*)));
71  emit manualCoordChange(this);
72  connect(map,SIGNAL(childRefreshPosition()),this,SLOT(RefreshPos()));
73  connect(map,SIGNAL(childSetOpacity(qreal)),this,SLOT(setOpacitySlot(qreal)));
74  }
75 
76  WayPointItem::WayPointItem(MapGraphicItem *map, bool magicwaypoint):
77  reached(false),
78  shownumber(true),
79  isDragging(false)
80  {
81  this->map = map;
82  this->altitude = 0;
83  description = "";
84 
85  myType=relative;
86  if(magicwaypoint)
87  {
88  isMagic=true;
89  picture.load(QString::fromUtf8(":/opmap/images/waypoint_marker3.png"));
90  number=-1;
91  }
92  else
93  {
94  isMagic=false;
95  number=WayPointItem::snumber;
96  ++WayPointItem::snumber;
97  }
98  text=nullptr;
99  numberI=nullptr;
100  this->setFlag(QGraphicsItem::ItemIsMovable,true);
101  this->setFlag(QGraphicsItem::ItemIgnoresTransformations,true);
102  this->setFlag(QGraphicsItem::ItemIsSelectable,true);
103  SetShowNumber(shownumber);
104  RefreshToolTip();
105  RefreshPos();
106  myHome=NULL;
107  QList<QGraphicsItem *> list=map->childItems();
108  foreach(QGraphicsItem * obj,list)
109  {
110  HomeItem* h=qgraphicsitem_cast <HomeItem*>(obj);
111  if(h)
112  myHome=h;
113  }
114 
115  if(myHome)
116  {
117  coord=map->Projection()->translate(myHome->Coord(),relativeCoord.distance,relativeCoord.bearing);
118  connect(myHome,SIGNAL(absolutePositionChanged(internals::PointLatLng, float)), this, SLOT(onHomePositionChanged(internals::PointLatLng, float)));
119  }
120  connect(this,SIGNAL(waypointdoubleclick(WayPointItem*)),map,SIGNAL(wpdoubleclicked(WayPointItem*)));
121  emit manualCoordChange(this);
122  connect(map,SIGNAL(childRefreshPosition()),this,SLOT(RefreshPos()));
123  connect(map,SIGNAL(childSetOpacity(qreal)),this,SLOT(setOpacitySlot(qreal)));
124  }
125 
126  WayPointItem::WayPointItem(const internals::PointLatLng &coord,int const& altitude, const QString &description, MapGraphicItem *map,wptype type):
127  reached(false),
128  shownumber(true),
129  isDragging(false),
130  myType(type)
131  {
132  this->map = map;
133  this->coord = coord;
134  this->altitude = altitude;
135  this->description = description;
136 
137  text=nullptr;
138  numberI=nullptr;
139  isMagic=false;
140  picture.load(QString::fromUtf8(":/markers/images/marker.png"));
141  number=WayPointItem::snumber;
142  ++WayPointItem::snumber;
143  this->setFlag(QGraphicsItem::ItemIsMovable,true);
144  this->setFlag(QGraphicsItem::ItemIgnoresTransformations,true);
145  this->setFlag(QGraphicsItem::ItemIsSelectable,true);
146  SetShowNumber(shownumber);
147  RefreshToolTip();
148  RefreshPos();
149  myHome=NULL;
150  QList<QGraphicsItem *> list=map->childItems();
151  foreach(QGraphicsItem * obj,list)
152  {
153  HomeItem* h=qgraphicsitem_cast <HomeItem*>(obj);
154  if(h)
155  myHome=h;
156  }
157  if(myHome)
158  {
159  map->Projection()->offSetFromLatLngs(myHome->Coord(),coord,relativeCoord.distance,relativeCoord.bearing);
160  connect(myHome, SIGNAL(absolutePositionChanged(internals::PointLatLng, float)), this, SLOT(onHomePositionChanged(internals::PointLatLng, float)));
161  }
162  connect(this,SIGNAL(waypointdoubleclick(WayPointItem*)),map,SIGNAL(wpdoubleclicked(WayPointItem*)));
163  emit manualCoordChange(this);
164  connect(map,SIGNAL(childRefreshPosition()),this,SLOT(RefreshPos()));
165  connect(map,SIGNAL(childSetOpacity(qreal)),this,SLOT(setOpacitySlot(qreal)));
166  }
167 
168  WayPointItem::WayPointItem(const distBearingAltitude &relativeCoordinates, const QString &description, MapGraphicItem *map):
169  reached(false),
170  shownumber(true),
171  isDragging(false)
172  {
173  this->map = map;
174  this->relativeCoord = relativeCoordinates;
175  this->altitude = altitude;
176  this->description = description;
177 
178  myHome=NULL;
179  QList<QGraphicsItem *> list=map->childItems();
180  foreach(QGraphicsItem * obj,list)
181  {
182  HomeItem* h=qgraphicsitem_cast <HomeItem*>(obj);
183  if(h)
184  myHome=h;
185  }
186  if(myHome)
187  {
188  connect(myHome, SIGNAL(absolutePositionChanged(internals::PointLatLng, float)), this, SLOT(onHomePositionChanged(internals::PointLatLng, float)));
189  coord=map->Projection()->translate(myHome->Coord(),relativeCoord.distance,relativeCoord.bearing);
190  }
191  myType=relative;
192  text=nullptr;
193  numberI=nullptr;
194  isMagic=false;
195  picture.load(QString::fromUtf8(":/markers/images/marker.png"));
196  number=WayPointItem::snumber;
197  ++WayPointItem::snumber;
198  this->setFlag(QGraphicsItem::ItemIsMovable,true);
199  this->setFlag(QGraphicsItem::ItemIgnoresTransformations,true);
200  this->setFlag(QGraphicsItem::ItemIsSelectable,true);
201  SetShowNumber(shownumber);
202  RefreshToolTip();
203  RefreshPos();
204  connect(this,SIGNAL(waypointdoubleclick(WayPointItem*)),map,SIGNAL(wpdoubleclicked(WayPointItem*)));
205  emit manualCoordChange(this);
206  connect(map,SIGNAL(childRefreshPosition()),this,SLOT(RefreshPos()));
207  connect(map,SIGNAL(childSetOpacity(qreal)),this,SLOT(setOpacitySlot(qreal)));
208  }
209 
210  void WayPointItem::setWPType(wptype type)
211  {
212  myType=type;
213  emit WPValuesChanged(this);
214  RefreshPos();
215  RefreshToolTip();
216  this->update();
217  }
218 
219  QRectF WayPointItem::boundingRect() const
220  {
221  return QRectF(-picture.width()/2,-picture.height(),picture.width(),picture.height());
222  }
223  void WayPointItem::paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget)
224  {
225  Q_UNUSED(option);
226  Q_UNUSED(widget);
227  painter->drawPixmap(-picture.width()/2,-picture.height(),picture);
228  painter->setPen(Qt::green);
229  if(this->isSelected())
230  painter->drawRect(QRectF(-picture.width()/2,-picture.height(),picture.width()-1,picture.height()-1));
231  }
232  void WayPointItem::mouseDoubleClickEvent(QGraphicsSceneMouseEvent *event)
233  {
234  if(event->button()==Qt::LeftButton)
235  {
236  emit waypointdoubleclick(this);
237  }
238  }
239 
240  void WayPointItem::mousePressEvent(QGraphicsSceneMouseEvent *event)
241  {
242  if(event->button()==Qt::LeftButton)
243  {
244  text=new QGraphicsSimpleTextItem(this);
245  textBG=new QGraphicsRectItem(this);
246 
247  textBG->setBrush(Qt::yellow);
248 
249  text->setPen(QPen(Qt::red));
250  text->setPos(10,-picture.height());
251  textBG->setPos(10,-picture.height());
252  text->setZValue(3);
253  RefreshToolTip();
254  isDragging=true;
255  }
256  QGraphicsItem::mousePressEvent(event);
257  }
258  void WayPointItem::mouseReleaseEvent(QGraphicsSceneMouseEvent *event)
259  {
260  QGraphicsItem::mouseReleaseEvent(event);
261  if(event->button()==Qt::LeftButton &&
262  event->buttonDownScenePos(Qt::LeftButton) != event->lastScenePos())
263  {
264  if(text)
265  {
266  delete text;
267  text=NULL;
268  }
269  if(textBG)
270  {
271  delete textBG;
272  textBG=NULL;
273  }
274  coord=map->FromLocalToLatLng(this->pos().x(),this->pos().y());
275 
276  isDragging=false;
277  RefreshToolTip();
278  emit manualCoordChange(this);
279  emit relativePositionChanged(this->pos(),this);
280  emit WPValuesChanged(this);
281  emit WPDropped(this);
282  } else if(event->button()==Qt::LeftButton) {
283  if(text)
284  {
285  delete text;
286  text=NULL;
287  }
288  if(textBG)
289  {
290  delete textBG;
291  textBG=NULL;
292  }
293  coord=map->FromLocalToLatLng(this->pos().x(),this->pos().y());
294 
295  isDragging=false;
296  RefreshToolTip();
297  }
298  }
299  void WayPointItem::mouseMoveEvent(QGraphicsSceneMouseEvent *event)
300  {
301 
302  if(isDragging)
303  {
304  coord=map->FromLocalToLatLng(this->pos().x(),this->pos().y());
305  QString coord_str = " " + QString::number(coord.Lat(), 'f', 6) + " " + QString::number(coord.Lng(), 'f', 6);
306  if(myHome)
307  {
308  map->Projection()->offSetFromLatLngs(myHome->Coord(),coord,relativeCoord.distance,relativeCoord.bearing);
309  }
310  QString relativeCoord_str = QString::number(relativeCoord.distance) + "m " + QString::number(relativeCoord.bearing*180/M_PI)+"deg";
311  text->setText(coord_str+"\n"+relativeCoord_str);
312  textBG->setRect(text->boundingRect());
313  emit relativePositionChanged(this->pos(),this);
314  emit WPValuesChanged(this);
315  }
316  QGraphicsItem::mouseMoveEvent(event);
317  }
318  void WayPointItem::SetAltitude(const float &value)
319  {
320  if(altitude==value)
321  return;
322  altitude=value;
323  RefreshToolTip();
324  emit WPValuesChanged(this);
325  this->update();
326  }
327 
328  void WayPointItem::setRelativeCoord(distBearingAltitude value)
329  {
330 // if(relativeCoord == value)
331 // return;
332  relativeCoord=value;
333  if(myHome)
334  {
335  SetCoord(map->Projection()->translate(myHome->Coord(),relativeCoord.distance,relativeCoord.bearing));
336  }
337  RefreshPos();
338  RefreshToolTip();
339  emit WPValuesChanged(this);
340  this->update();
341  }
342 
343  void WayPointItem::SetCoord(const internals::PointLatLng &value)
344  {
345  if(coord == value)
346  return;
347  coord=value;
348  distBearingAltitude back=relativeCoord;
349  if(myHome)
350  map->Projection()->offSetFromLatLngs(myHome->Coord(),Coord(),back.distance,back.bearing);
351  if(qAbs(back.bearing-relativeCoord.bearing)>0.01 || qAbs(back.distance-relativeCoord.distance)>0.1)
352  {
353  relativeCoord=back;
354  }
355  emit WPValuesChanged(this);
356  RefreshPos();
357  RefreshToolTip();
358  this->update();
359  }
360  void WayPointItem::SetDescription(const QString &value)
361  {
362  if(description==value)
363  return;
364  description=value;
365  RefreshToolTip();
366  emit WPValuesChanged(this);
367  this->update();
368  }
369  void WayPointItem::SetNumber(const int &value)
370  {
371  int oldnumber=number;
372  number=value;
373  RefreshToolTip();
374  numberI->setText(QString::number(numberAdjusted()));
375  numberIBG->setRect(numberI->boundingRect().adjusted(-2,0,1,0));
376  this->update();
377  emit WPNumberChanged(oldnumber,value,this);
378  }
379  void WayPointItem::SetReached(const bool &value)
380  {
381  reached=value;
382  emit WPValuesChanged(this);
383  if(value)
384  picture.load(QString::fromUtf8(":/markers/images/bigMarkerGreen.png"));
385  else
386  {
387  if(!isMagic)
388  {
389  if(this->flags() & QGraphicsItem::ItemIsMovable)
390  picture.load(QString::fromUtf8(":/markers/images/marker.png"));
391  else
392  picture.load(QString::fromUtf8(":/markers/images/waypoint_marker2.png"));
393  }
394  else
395  {
396  picture.load(QString::fromUtf8(":/opmap/images/waypoint_marker3.png"));
397  }
398  }
399  this->update();
400 
401  }
402  void WayPointItem::SetShowNumber(const bool &value)
403  {
404  shownumber=value;
405  if((numberI==nullptr) && value)
406  {
407  numberI=new QGraphicsSimpleTextItem(this);
408  numberIBG=new QGraphicsRectItem(this);
409  numberIBG->setBrush(Qt::white);
410  numberIBG->setOpacity(0.5);
411  numberI->setZValue(3);
412  numberI->setPen(QPen(Qt::blue));
413  numberI->setPos(0,-13-picture.height());
414  numberIBG->setPos(0,-13-picture.height());
415  numberI->setText(QString::number(numberAdjusted()));
416  numberIBG->setRect(numberI->boundingRect().adjusted(-2,0,1,0));
417  }
418  else if (!value && numberI)
419  {
420  delete numberI;
421  delete numberIBG;
422  }
423  this->update();
424  }
425  void WayPointItem::WPDeleted(const int &onumber,WayPointItem *waypoint)
426  {
427  Q_UNUSED(waypoint);
428  int n=number;
429  if(number>onumber) SetNumber(--n);
430  }
431  void WayPointItem::WPInserted(const int &onumber, WayPointItem *waypoint)
432  {
433  if(Number()==-1)
434  return;
435 
436  if(waypoint!=this)
437  {
438  if(onumber<=number) SetNumber(++number);
439  }
440  }
441 
443  {
444  Q_UNUSED(homeAltitude);
445 
446  if(myType==relative)
447  {
448  coord=map->Projection()->translate(homepos,relativeCoord.distance,relativeCoord.bearing);
449  emit WPValuesChanged(this);
450  RefreshPos();
451  RefreshToolTip();
452  this->update();
453  }
454  else
455  {
456  if(myHome)
457  {
458  map->Projection()->offSetFromLatLngs(myHome->Coord(),coord,relativeCoord.distance,relativeCoord.bearing);
459  }
460  emit WPValuesChanged(this);
461  }
462  }
463 
464  void WayPointItem::WPRenumbered(const int &oldnumber, const int &newnumber, WayPointItem *waypoint)
465  {
466  if (waypoint!=this)
467  {
468  if(((oldnumber>number) && (newnumber<=number)))
469  {
470  SetNumber(++number);
471  }
472  else if (((oldnumber<number) && (newnumber>number)))
473  {
474  SetNumber(--number);
475  }
476  else if (newnumber==number)
477  {
478  SetNumber(++number);
479  }
480  }
481  }
482  int WayPointItem::type() const
483  {
484  // Enable the use of qgraphicsitem_cast with this item.
485  return Type;
486  }
487 
488  WayPointItem::~WayPointItem()
489  {
490  emit aboutToBeDeleted(this);
491  --WayPointItem::snumber;
492  }
494  {
495  core::Point point=map->FromLatLngToLocal(coord);
496  this->setPos(point.X(),point.Y());
497  emit relativePositionChanged(this->pos(),this);
498  }
499 
500  void WayPointItem::setOpacitySlot(qreal opacity)
501  {
502  setOpacity(opacity);
503  }
504 
509  void WayPointItem::RefreshToolTip()
510  {
511  QString coord_str = " " + QString::number(coord.Lat(), 'f', 6) + " " + QString::number(coord.Lng(), 'f', 6);
512  QString relativeCoord_str = " Distance:" + QString::number(relativeCoord.distance) + " Bearing:" + QString::number(relativeCoord.bearing*180/M_PI);
513  if(Number()!=-1)
514  setToolTip(QString("WayPoint Number:%1\nDescription:%2\nCoordinate:%4\nFrom Home:%5\nAltitude:%6\n%7").arg(QString::number(numberAdjusted())).arg(description).arg(coord_str).arg(relativeCoord_str).arg(QString::number(altitude)).arg(myCustomString));
515  else
516  setToolTip(QString("Magic WayPoint\nCoordinate:%1\nFrom Home:%2\nAltitude:%3\n\n%4").arg(coord_str).arg(relativeCoord_str).arg(QString::number(altitude)).arg(myCustomString));
517  }
518 
519  void WayPointItem::setFlag(QGraphicsItem::GraphicsItemFlag flag, bool enabled)
520  {
521  if(isMagic)
522  {
523  QGraphicsItem::setFlag(flag,enabled);
524  return;
525  }
526  else if(flag==QGraphicsItem::ItemIsMovable)
527  {
528  if(enabled)
529  picture.load(QString::fromUtf8(":/markers/images/marker.png"));
530  else
531  picture.load(QString::fromUtf8(":/markers/images/waypoint_marker2.png"));
532  }
533  QGraphicsItem::setFlag(flag,enabled);
534  }
535 
536  int WayPointItem::snumber=0;
537 }
void relativePositionChanged(QPointF point, MapPointItem *mappoint)
qint64 X() const
Definition: point.h:51
A graphicsItem representing a WayPoint.
internals::PointLatLng coord
Definition: mappointitem.h:109
void WPDropped(WayPointItem *waypoint)
Fired when the description, altitude or coordinates change.
void aboutToBeDeleted(MapPointItem *)
void setOpacitySlot(qreal opacity)
void WPValuesChanged(WayPointItem *waypoint)
distBearingAltitude relativeCoord
Definition: mappointitem.h:111
void mouseDoubleClickEvent(QGraphicsSceneMouseEvent *event)
void onHomePositionChanged(internals::PointLatLng, float altitude)
double Lng() const
Definition: pointlatlng.h:76
double Lat() const
Definition: pointlatlng.h:64
void mousePressEvent(QGraphicsSceneMouseEvent *event)
A graphicsItem representing a Home Location.
void mouseReleaseEvent(QGraphicsSceneMouseEvent *event)
Eccentricity n
Definition: OPPlots.m:137
void WPDeleted(int const &number, WayPointItem *waypoint)
Called when a WayPoint is deleted.
void waypointdoubleclick(WayPointItem *waypoint)
void setOpacitySlot(qreal opacity)
A QGraphicsItem representing a WayPoint.
Definition: waypointitem.h:41
void manualCoordChange(WayPointItem *)
void mouseMoveEvent(QGraphicsSceneMouseEvent *event)
Definition: icore.h:39
x
Definition: OPPlots.m:100
void WPInserted(int const &number, WayPointItem *waypoint)
Called when a WayPoint is inserted.
void WPRenumbered(int const &oldnumber, int const &newnumber, WayPointItem *waypoint)
Called when a WayPoint is renumbered.
qint64 Y() const
Definition: point.h:52
void WPNumberChanged(int const &oldnumber, int const &newnumber, WayPointItem *waypoint)
fires when this WayPoint number changes (not fired if due to a auto-renumbering)
y
Definition: OPPlots.m:101