dRonin  adbada4
dRonin GCS
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Groups Pages
modelmapproxy.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 "modelmapproxy.h"
29 #include "../pathplanner/waypointdialog.h"
30 
31 ModelMapProxy::ModelMapProxy(QObject *parent, TLMapWidget *map, FlightDataModel *model,
32  QItemSelectionModel *selectionModel)
33  : QObject(parent)
34  , myMap(map)
35  , model(model)
36  , selection(selectionModel)
37 {
38  connect(model, &QAbstractItemModel::rowsInserted, this, &ModelMapProxy::rowsInserted);
39  connect(model, &QAbstractItemModel::rowsRemoved, this, &ModelMapProxy::rowsRemoved);
40  connect(selection, &QItemSelectionModel::currentRowChanged, this,
41  &ModelMapProxy::currentRowChanged);
42  connect(model, &QAbstractItemModel::dataChanged, this, &ModelMapProxy::dataChanged);
43  connect(myMap, &mapcontrol::TLMapWidget::selectedWPChanged, this,
44  &ModelMapProxy::selectedWPChanged);
46  &ModelMapProxy::WPValuesChanged);
47 }
48 
53 void ModelMapProxy::WPValuesChanged(WayPointItem *wp)
54 {
55  QModelIndex index;
56  index = model->index(wp->Number(), FlightDataModel::LATPOSITION);
57  if (!index.isValid())
58  return;
59  model->setData(index, wp->Coord().Lat(), Qt::EditRole);
60  index = model->index(wp->Number(), FlightDataModel::LNGPOSITION);
61  model->setData(index, wp->Coord().Lng(), Qt::EditRole);
62 
63  index = model->index(wp->Number(), FlightDataModel::ALTITUDE);
64  model->setData(index, wp->Altitude(), Qt::EditRole);
65 }
66 
72 void ModelMapProxy::currentRowChanged(QModelIndex current, QModelIndex previous)
73 {
74  Q_UNUSED(previous);
75 
77  WayPointItem *wp = findWayPointNumber(current.row());
78  if (!wp)
79  return;
80  list.append(wp);
81  myMap->setSelectedWP(list);
82 }
83 
89 void ModelMapProxy::selectedWPChanged(QList<WayPointItem *> list)
90 {
91  selection->clearSelection();
92  foreach (WayPointItem *wp, list) {
93  QModelIndex index = model->index(wp->Number(), 0);
94  selection->setCurrentIndex(index, QItemSelectionModel::Select | QItemSelectionModel::Rows);
95  }
96 }
97 
103 ModelMapProxy::overlayType ModelMapProxy::overlayTranslate(int type)
104 {
105  switch (type) {
106  case Waypoint::MODE_ENDPOINT:
107  case Waypoint::MODE_VECTOR:
108  return OVERLAY_LINE;
109  break;
110  case Waypoint::MODE_CIRCLERIGHT:
111  return OVERLAY_CURVE_RIGHT;
112  break;
113  case Waypoint::MODE_CIRCLELEFT:
114  return OVERLAY_CURVE_LEFT;
115  break;
116  default:
117  break;
118  }
119 
120  // Default value
121  return OVERLAY_LINE;
122 }
123 
131 void ModelMapProxy::createOverlay(WayPointItem *from, WayPointItem *to,
132  ModelMapProxy::overlayType type, QColor color, double radius = 0)
133 {
134  if (from == NULL || to == NULL || from == to)
135  return;
136  switch (type) {
137  case OVERLAY_LINE:
138  myMap->WPLineCreate(from, to, color);
139  break;
140  case OVERLAY_CIRCLE_RIGHT:
141  myMap->WPCircleCreate(to, from, true, color);
142  break;
143  case OVERLAY_CIRCLE_LEFT:
144  myMap->WPCircleCreate(to, from, false, color);
145  break;
146  case OVERLAY_CURVE_RIGHT:
147  myMap->WPCurveCreate(to, from, radius, true, color);
148  break;
149  case OVERLAY_CURVE_LEFT:
150  myMap->WPCurveCreate(to, from, radius, false, color);
151  break;
152  default:
153  break;
154  }
155 }
156 
164 void ModelMapProxy::createOverlay(WayPointItem *from, HomeItem *to, ModelMapProxy::overlayType type,
165  QColor color)
166 {
167  if (from == NULL || to == NULL)
168  return;
169  switch (type) {
170  case OVERLAY_LINE:
171  myMap->WPLineCreate(to, from, color);
172  break;
173  case OVERLAY_CIRCLE_RIGHT:
174  myMap->WPCircleCreate(to, from, true, color);
175  break;
176  case OVERLAY_CIRCLE_LEFT:
177  myMap->WPCircleCreate(to, from, false, color);
178  break;
179  default:
180  break;
181  }
182 }
183 
188 void ModelMapProxy::refreshOverlays()
189 {
190  myMap->deleteAllOverlays();
191  if (model->rowCount() < 1)
192  return;
193  WayPointItem *wp_current = NULL;
194  WayPointItem *wp_next = NULL;
195  overlayType wp_next_overlay;
196 
197  // Get first waypoint type before stepping through path
198  wp_current = findWayPointNumber(0);
199  overlayType wp_current_overlay =
200  overlayTranslate(model->data(model->index(0, FlightDataModel::MODE), Qt::UserRole).toInt());
201  createOverlay(wp_current, myMap->Home, wp_current_overlay, Qt::green);
202 
203  for (int x = 0; x < model->rowCount(); ++x) {
204  wp_current = findWayPointNumber(x);
205 
206  wp_next_overlay = overlayTranslate(
207  model->data(model->index(x + 1, FlightDataModel::MODE), Qt::UserRole).toInt());
208 
209  wp_next = findWayPointNumber(x + 1);
210  createOverlay(wp_current, wp_next, wp_next_overlay, Qt::green,
211  model->data(model->index(x + 1, FlightDataModel::MODE_PARAMS)).toFloat());
212  }
213 }
214 
220 WayPointItem *ModelMapProxy::findWayPointNumber(int number)
221 {
222  if (number < 0)
223  return NULL;
224  return myMap->WPFind(number);
225 }
226 
233 void ModelMapProxy::rowsRemoved(const QModelIndex &parent, int first, int last)
234 {
235  Q_UNUSED(parent);
236 
237  for (int x = last; x > first - 1; x--) {
238  myMap->WPDelete(x);
239  }
240  refreshOverlays();
241 }
242 
248 void ModelMapProxy::dataChanged(const QModelIndex &topLeft, const QModelIndex &bottomRight)
249 {
250  Q_UNUSED(bottomRight);
251 
252  // Abort if no corresponding graphical item
253  WayPointItem *item = findWayPointNumber(topLeft.row());
254  if (!item)
255  return;
256 
257  internals::PointLatLng latlng;
258  double altitude;
259  QModelIndex index;
260  QString desc;
261 
262  for (int x = topLeft.row(); x <= bottomRight.row(); x++) {
263  for (int column = topLeft.column(); column <= bottomRight.column(); column++) {
264  // Action depends on which columns were modified
265  switch (column) {
267  refreshOverlays();
268  break;
270  index = model->index(x, FlightDataModel::WPDESCRIPTION);
271  desc = index.data(Qt::DisplayRole).toString();
272  item->SetDescription(desc);
273  break;
275  latlng = item->Coord();
276  index = model->index(x, FlightDataModel::LATPOSITION);
277  latlng.SetLat(index.data(Qt::DisplayRole).toDouble());
278  item->SetCoord(latlng);
279  break;
281  latlng = item->Coord();
282  index = model->index(x, FlightDataModel::LNGPOSITION);
283  latlng.SetLng(index.data(Qt::DisplayRole).toDouble());
284  item->SetCoord(latlng);
285  break;
287  index = model->index(x, FlightDataModel::ALTITUDE);
288  altitude = index.data(Qt::DisplayRole).toDouble();
289  item->SetAltitude(altitude);
290  break;
292  // Make sure to update radius of arcs
293  refreshOverlays();
294  break;
296  index = model->index(x, FlightDataModel::LOCKED);
297  item->setFlag(QGraphicsItem::ItemIsMovable, !index.data(Qt::DisplayRole).toBool());
298  break;
299  }
300  }
301  }
302 }
303 
311 void ModelMapProxy::rowsInserted(const QModelIndex &parent, int first, int last)
312 {
313  Q_UNUSED(parent);
314  for (int x = first; x < last + 1; x++) {
315  QModelIndex index;
316  internals::PointLatLng latlng;
317  double altitude;
318  index = model->index(x, FlightDataModel::WPDESCRIPTION);
319  QString desc = index.data(Qt::DisplayRole).toString();
320  index = model->index(x, FlightDataModel::LATPOSITION);
321  latlng.SetLat(index.data(Qt::DisplayRole).toDouble());
322  index = model->index(x, FlightDataModel::LNGPOSITION);
323  latlng.SetLng(index.data(Qt::DisplayRole).toDouble());
324  index = model->index(x, FlightDataModel::ALTITUDE);
325  altitude = index.data(Qt::DisplayRole).toDouble();
326  myMap->WPInsert(latlng, altitude, desc, x);
327  }
328  refreshOverlays();
329 }
330 
337 {
338  model->removeRow(number, QModelIndex());
339 }
340 
347 {
348  model->insertRow(model->rowCount(), QModelIndex());
349  QModelIndex index =
350  model->index(model->rowCount() - 1, FlightDataModel::LATPOSITION, QModelIndex());
351  model->setData(index, coord.Lat(), Qt::EditRole);
352  index = model->index(model->rowCount() - 1, FlightDataModel::LNGPOSITION, QModelIndex());
353  model->setData(index, coord.Lng(), Qt::EditRole);
354 }
355 
360 {
361  model->removeRows(0, model->rowCount(), QModelIndex());
362 }
ModelMapProxy(QObject *parent, TLMapWidget *map, FlightDataModel *model, QItemSelectionModel *selectionModel)
void SetLat(const double &value)
Definition: pointlatlng.h:69
Trim off last
void selectedWPChanged(QList< WayPointItem * >)
void deleteAll()
When all the waypoints are deleted graphically, update the model.
void deleteWayPoint(int number)
When a waypoint is deleted graphically, delete from the model.
int rowCount(const QModelIndex &parent=QModelIndex()) const
Return the number of waypoints.
void SetLng(const double &value)
Definition: pointlatlng.h:80
double Lng() const
Definition: pointlatlng.h:76
double Lat() const
Definition: pointlatlng.h:64
bool removeRows(int row, int count, const QModelIndex &parent=QModelIndex())
FlightDataModel::removeRows Remove waypoints from the model.
void createWayPoint(internals::PointLatLng coord)
When a waypoint is created graphically, insert into the end of the model.
bool setData(const QModelIndex &index, const QVariant &value, int role=Qt::EditRole)
FlightDataModel::setData Set the data at a given location.
void WPManualCoordChange(WayPointItem *)
Definition: icore.h:39
WayPointItem * findWayPointNumber(int number)
Get the handle to a waypoint graphical item.
x
Definition: OPPlots.m:100
QVariant data(const QModelIndex &index, int role=Qt::DisplayRole) const
FlightDataModel::data Fetch the data from the model.