MapWidget.cc 37.4 KB
Newer Older
1
/*==================================================================
pixhawk's avatar
pixhawk committed
2 3 4 5
======================================================================*/

/**
 * @file
lm's avatar
lm committed
6
 *   @brief Implementation of MapWidget
pixhawk's avatar
pixhawk committed
7 8
 *
 *   @author Lorenz Meier <mavteam@student.ethz.ch>
lm's avatar
lm committed
9
 *   @author Mariano Lizarraga
pixhawk's avatar
pixhawk committed
10 11 12
 *
 */

lm's avatar
lm committed
13 14
#include <QComboBox>
#include <QGridLayout>
15
#include <QDir>
16

17
#include "QGC.h"
pixhawk's avatar
pixhawk committed
18 19
#include "MapWidget.h"
#include "ui_MapWidget.h"
lm's avatar
lm committed
20 21
#include "UASInterface.h"
#include "UASManager.h"
22 23
#include "MAV2DIcon.h"
#include "Waypoint2DIcon.h"
24
#include "UASWaypointManager.h"
pixhawk's avatar
pixhawk committed
25

pixhawk's avatar
pixhawk committed
26 27
#include "MG.h"

28

pixhawk's avatar
pixhawk committed
29 30 31
MapWidget::MapWidget(QWidget *parent) :
        QWidget(parent),
        zoomLevel(0),
32 33
        uasIcons(),
        uasTrails(),
34
        mav(NULL),
35
        lastUpdate(0),
pixhawk's avatar
pixhawk committed
36 37 38
        m_ui(new Ui::MapWidget)
{
    m_ui->setupUi(this);
39
    mc = new qmapcontrol::MapControl(this->size());
40 41

    //   VISUAL MAP STYLE
42
    QString buttonStyle("QAbstractButton { background-color: rgba(20, 20, 20, 45%); border-color: rgba(10, 10, 10, 50%)} QAbstractButton:checked { border: 2px solid #379AC3; }");
43 44
    mc->setPen(QGC::colorCyan.darker(400));

45 46 47 48 49 50 51 52








53
    waypointIsDrag = false;
pixhawk's avatar
pixhawk committed
54

pixhawk's avatar
pixhawk committed
55 56
    // Accept focus by clicking or keyboard
    this->setFocusPolicy(Qt::StrongFocus);
pixhawk's avatar
pixhawk committed
57

pixhawk's avatar
pixhawk committed
58
    // create MapControl
59

pixhawk's avatar
pixhawk committed
60
    mc->showScale(true);
61
    mc->showCoord(true);
62
    mc->enablePersistentCache();
63
    mc->setMouseTracking(true); // required to update the mouse position for diplay and capture
pixhawk's avatar
pixhawk committed
64

pixhawk's avatar
pixhawk committed
65
    // create MapAdapter to get maps from
lm's avatar
lm committed
66
    //TileMapAdapter* osmAdapter = new TileMapAdapter("tile.openstreetmap.org", "/%1/%2/%3.png", 256, 0, 17);
pixhawk's avatar
pixhawk committed
67

68
    qmapcontrol::MapAdapter* mapadapter_overlay = new qmapcontrol::YahooMapAdapter("us.maps3.yimg.com", "/aerial.maps.yimg.com/png?v=2.2&t=h&s=256&x=%2&y=%3&z=%1");
pixhawk's avatar
pixhawk committed
69

lm's avatar
lm committed
70
    // MAP BACKGROUND
71 72
    mapadapter = new qmapcontrol::GoogleSatMapAdapter();
    l = new qmapcontrol::MapLayer("Google Satellite", mapadapter);
lm's avatar
lm committed
73
    mc->addLayer(l);
pixhawk's avatar
pixhawk committed
74

lm's avatar
lm committed
75
    // STREET OVERLAY
76
    overlay = new qmapcontrol::MapLayer("Overlay", mapadapter_overlay);
lm's avatar
lm committed
77 78
    overlay->setVisible(false);
    mc->addLayer(overlay);
pixhawk's avatar
pixhawk committed
79

80 81 82 83
    // MAV FLIGHT TRACKS
    tracks = new qmapcontrol::MapLayer("Tracking", mapadapter);
    mc->addLayer(tracks);

lm's avatar
lm committed
84 85
    // WAYPOINT LAYER
    // create a layer with the mapadapter and type GeometryLayer (for waypoints)
86
    geomLayer = new qmapcontrol::GeometryLayer("Waypoints", mapadapter);
87
    mc->addLayer(geomLayer);
pixhawk's avatar
pixhawk committed
88 89 90



91 92 93
    //
    //    Layer* gsatLayer = new Layer("Google Satellite", gsat, Layer::MapLayer);
    //    mc->addLayer(gsatLayer);
pixhawk's avatar
pixhawk committed
94

95 96 97 98 99 100 101 102 103 104 105 106 107 108

    // Zurich, ETH

    int lastZoom = 16;
    double lastLat = 47.376889;
    double lastLon = 8.548056;

    QSettings settings;
    settings.beginGroup("QGC_MAPWIDGET");
    lastLat = settings.value("LAST_LATITUDE", lastLat).toDouble();
    lastLon = settings.value("LAST_LONGITUDE", lastLon).toDouble();
    lastZoom = settings.value("LAST_ZOOM", lastZoom).toInt();
    settings.endGroup();

lm's avatar
lm committed
109 110
    // SET INITIAL POSITION AND ZOOM
    // Set default zoom level
111 112
    mc->setZoom(lastZoom);
    mc->setView(QPointF(lastLon, lastLat));
pixhawk's avatar
pixhawk committed
113

114 115
    // Veracruz Mexico
    //mc->setView(QPointF(-96.105208,19.138955));
pixhawk's avatar
pixhawk committed
116

lm's avatar
lm committed
117 118 119
    // Add controls to select map provider
    /////////////////////////////////////////////////
    QActionGroup* mapproviderGroup = new QActionGroup(this);
120 121 122 123 124
    osmAction = new QAction(QIcon(":/images/mapproviders/openstreetmap.png"), tr("OpenStreetMap"), mapproviderGroup);
    yahooActionMap = new QAction(QIcon(":/images/mapproviders/yahoo.png"), tr("Yahoo: Map"), mapproviderGroup);
    yahooActionSatellite = new QAction(QIcon(":/images/mapproviders/yahoo.png"), tr("Yahoo: Satellite"), mapproviderGroup);
    googleActionMap = new QAction(QIcon(":/images/mapproviders/google.png"), tr("Google: Map"), mapproviderGroup);
    googleSatAction = new QAction(QIcon(":/images/mapproviders/google.png"), tr("Google: Sat"), mapproviderGroup);
lm's avatar
lm committed
125 126 127 128 129 130 131 132
    osmAction->setCheckable(true);
    yahooActionMap->setCheckable(true);
    yahooActionSatellite->setCheckable(true);
    googleActionMap->setCheckable(true);
    googleSatAction->setCheckable(true);
    googleSatAction->setChecked(true);
    connect(mapproviderGroup, SIGNAL(triggered(QAction*)),
            this, SLOT(mapproviderSelected(QAction*)));
pixhawk's avatar
pixhawk committed
133

lm's avatar
lm committed
134
    // Overlay seems currently broken
135 136 137 138 139
    //    yahooActionOverlay = new QAction(tr("Yahoo: street overlay"), this);
    //    yahooActionOverlay->setCheckable(true);
    //    yahooActionOverlay->setChecked(overlay->isVisible());
    //    connect(yahooActionOverlay, SIGNAL(toggled(bool)),
    //            overlay, SLOT(setVisible(bool)));
pixhawk's avatar
pixhawk committed
140

141 142 143 144 145 146
    //    mapproviderGroup->addAction(googleSatAction);
    //    mapproviderGroup->addAction(osmAction);
    //    mapproviderGroup->addAction(yahooActionOverlay);
    //    mapproviderGroup->addAction(googleActionMap);
    //    mapproviderGroup->addAction(yahooActionMap);
    //    mapproviderGroup->addAction(yahooActionSatellite);
pixhawk's avatar
pixhawk committed
147

lm's avatar
lm committed
148 149 150 151
    // Create map provider selection menu
    mapMenu = new QMenu(this);
    mapMenu->addActions(mapproviderGroup->actions());
    mapMenu->addSeparator();
152
    //    mapMenu->addAction(yahooActionOverlay);
pixhawk's avatar
pixhawk committed
153

lm's avatar
lm committed
154 155 156
    mapButton = new QPushButton(this);
    mapButton->setText("Map Source");
    mapButton->setMenu(mapMenu);
157
    mapButton->setStyleSheet(buttonStyle);
pixhawk's avatar
pixhawk committed
158

pixhawk's avatar
pixhawk committed
159
    // display the MapControl in the application
lm's avatar
lm committed
160
    QGridLayout* layout = new QGridLayout(this);
pixhawk's avatar
pixhawk committed
161
    layout->setMargin(0);
162 163
    layout->setSpacing(0);
    layout->addWidget(mc, 0, 0);
pixhawk's avatar
pixhawk committed
164
    setLayout(layout);
pixhawk's avatar
pixhawk committed
165

166
    // create buttons to control the map (zoom, GPS tracking and WP capture)
pixhawk's avatar
pixhawk committed
167
    QPushButton* zoomin = new QPushButton(QIcon(":/images/actions/list-add.svg"), "", this);
168
    zoomin->setStyleSheet(buttonStyle);
pixhawk's avatar
pixhawk committed
169
    QPushButton* zoomout = new QPushButton(QIcon(":/images/actions/list-remove.svg"), "", this);
170
    zoomout->setStyleSheet(buttonStyle);
171
    createPath = new QPushButton(QIcon(":/images/actions/go-bottom.svg"), "", this);
172
    createPath->setStyleSheet(buttonStyle);
173 174
    createPath->setToolTip(tr("Start / end waypoint add mode"));
    createPath->setStatusTip(tr("Start / end waypoint add mode"));
175 176
//    clearTracking = new QPushButton(QIcon(""), "", this);
//    clearTracking->setStyleSheet(buttonStyle);
pixhawk's avatar
pixhawk committed
177
    followgps = new QPushButton(QIcon(":/images/actions/system-lock-screen.svg"), "", this);
178
    followgps->setStyleSheet(buttonStyle);
179 180
    followgps->setToolTip(tr("Follow the position of the current MAV with the map center"));
    followgps->setStatusTip(tr("Follow the position of the current MAV with the map center"));
181 182
    QPushButton* goToButton = new QPushButton(QIcon(""), "T", this);
    goToButton->setStyleSheet(buttonStyle);
183 184
    goToButton->setToolTip(tr("Enter a latitude/longitude position to move the map to"));
    goToButton->setStatusTip(tr("Enter a latitude/longitude position to move the map to"));
pixhawk's avatar
pixhawk committed
185

186 187 188
    zoomin->setMaximumWidth(30);
    zoomout->setMaximumWidth(30);
    createPath->setMaximumWidth(30);
189
//    clearTracking->setMaximumWidth(30);
190
    followgps->setMaximumWidth(30);
191
    goToButton->setMaximumWidth(30);
pixhawk's avatar
pixhawk committed
192

193 194 195 196 197
    // Set checkable buttons
    // TODO: Currently checked buttons are are very difficult to distinguish when checked.
    //       create a style and the slots to change the background so it is easier to distinguish
    followgps->setCheckable(true);
    createPath->setCheckable(true);
pixhawk's avatar
pixhawk committed
198

199
    // add buttons to control the map (zoom, GPS tracking and WP capture)
lm's avatar
lm committed
200
    QGridLayout* innerlayout = new QGridLayout(mc);
201 202
    innerlayout->setMargin(3);
    innerlayout->setSpacing(3);
lm's avatar
lm committed
203 204 205 206
    innerlayout->addWidget(zoomin, 0, 0);
    innerlayout->addWidget(zoomout, 1, 0);
    innerlayout->addWidget(followgps, 2, 0);
    innerlayout->addWidget(createPath, 3, 0);
207
    //innerlayout->addWidget(clearTracking, 4, 0);
lm's avatar
lm committed
208
    // Add spacers to compress buttons on the top left
209
    innerlayout->addItem(new QSpacerItem(0, 0, QSizePolicy::Expanding, QSizePolicy::Expanding), 5, 0);
210
    innerlayout->addItem(new QSpacerItem(0, 0, QSizePolicy::Expanding, QSizePolicy::Expanding), 0, 1, 0, 7);
211 212
    innerlayout->addWidget(mapButton, 0, 6);
    innerlayout->addWidget(goToButton, 0, 7);
lm's avatar
lm committed
213 214
    innerlayout->setRowStretch(0, 1);
    innerlayout->setRowStretch(1, 100);
pixhawk's avatar
pixhawk committed
215
    mc->setLayout(innerlayout);
pixhawk's avatar
pixhawk committed
216 217


218
    // Connect the required signals-slots
219 220
    connect(zoomin, SIGNAL(clicked(bool)),
            mc, SLOT(zoomIn()));
pixhawk's avatar
pixhawk committed
221

222 223
    connect(zoomout, SIGNAL(clicked(bool)),
            mc, SLOT(zoomOut()));
pixhawk's avatar
pixhawk committed
224

225 226
    connect(goToButton, SIGNAL(clicked()), this, SLOT(goTo()));

pixhawk's avatar
pixhawk committed
227 228 229 230 231 232
    QList<UASInterface*> systems = UASManager::instance()->getUASList();
    foreach(UASInterface* system, systems)
    {
        addUAS(system);
    }

233 234
    connect(UASManager::instance(), SIGNAL(UASCreated(UASInterface*)),
            this, SLOT(addUAS(UASInterface*)));
pixhawk's avatar
pixhawk committed
235 236

    activeUASSet(UASManager::instance()->getActiveUAS());
237
    connect(UASManager::instance(), SIGNAL(activeUASSet(UASInterface*)), this, SLOT(activeUASSet(UASInterface*)));
pixhawk's avatar
pixhawk committed
238

239 240
    connect(mc, SIGNAL(mouseEventCoordinate(const QMouseEvent*, const QPointF)),
            this, SLOT(captureMapClick(const QMouseEvent*, const QPointF)));
pixhawk's avatar
pixhawk committed
241

242
    connect(createPath, SIGNAL(clicked(bool)),
243
            this, SLOT(createPathButtonClicked(bool)));
pixhawk's avatar
pixhawk committed
244 245


246 247
    connect(geomLayer, SIGNAL(geometryClicked(Geometry*,QPoint)),
            this, SLOT(captureGeometryClick(Geometry*, QPoint)));
pixhawk's avatar
pixhawk committed
248

249 250
    connect(geomLayer, SIGNAL(geometryDragged(Geometry*, QPointF)),
            this, SLOT(captureGeometryDrag(Geometry*, QPointF)));
pixhawk's avatar
pixhawk committed
251

252 253
    connect(geomLayer, SIGNAL(geometryEndDrag(Geometry*, QPointF)),
            this, SLOT(captureGeometryEndDrag(Geometry*, QPointF)));
pixhawk's avatar
pixhawk committed
254

255 256 257
    // Configure the WP Path's pen
    pointPen = new QPen(QColor(0, 255,0));
    pointPen->setWidth(3);
258 259
    waypointPath = new qmapcontrol::LineString (wps, "Waypoint path", pointPen);
    mc->layer("Waypoints")->addGeometry(waypointPath);
pixhawk's avatar
pixhawk committed
260

261 262 263 264 265
    //Camera Control
    // CAMERA INDICATOR LAYER
    // create a layer with the mapadapter and type GeometryLayer (for camera indicator)
    camLayer = new qmapcontrol::GeometryLayer("Camera", mapadapter);
    mc->addLayer(camLayer);
pixhawk's avatar
pixhawk committed
266

267
    //camLine = new qmapcontrol::LineString(camPoints,"Camera Eje", camBorderPen);
pixhawk's avatar
pixhawk committed
268

269 270
    drawCamBorder = false;
    radioCamera = 10;
lm's avatar
lm committed
271 272
}

273 274 275
void MapWidget::goTo()
{
    bool ok;
276 277
    QString text = QInputDialog::getText(this, tr("Please enter coordinates"),
                                         tr("Coordinates (Lat,Lon):"), QLineEdit::Normal,
278
                                         QString("%1,%2").arg(mc->currentCoordinate().y()).arg(mc->currentCoordinate().x()), &ok);
279 280 281 282 283 284 285 286 287 288 289 290 291
    if (ok && !text.isEmpty())
    {
        QStringList split = text.split(",");
        if (split.length() == 2)
        {
            bool convert;
            double latitude = split.first().toDouble(&convert);
            ok &= convert;
            double longitude = split.last().toDouble(&convert);
            ok &= convert;

            if (ok)
            {
292
                mc->setView(QPointF(longitude, latitude));
293 294 295
            }
        }
    }
296 297
}

298

lm's avatar
lm committed
299 300 301 302 303 304 305 306
void MapWidget::mapproviderSelected(QAction* action)
{
    //delete mapadapter;
    mapButton->setText(action->text());
    if (action == osmAction)
    {
        int zoom = mapadapter->adaptedZoom();
        mc->setZoom(0);
pixhawk's avatar
pixhawk committed
307

308
        mapadapter = new qmapcontrol::OSMMapAdapter();
lm's avatar
lm committed
309 310
        l->setMapAdapter(mapadapter);
        geomLayer->setMapAdapter(mapadapter);
pixhawk's avatar
pixhawk committed
311

312
        if (isVisible()) mc->updateRequestNew();
lm's avatar
lm committed
313
        mc->setZoom(zoom);
314
        //        yahooActionOverlay->setEnabled(false);
lm's avatar
lm committed
315
        overlay->setVisible(false);
316
        //        yahooActionOverlay->setChecked(false);
pixhawk's avatar
pixhawk committed
317

lm's avatar
lm committed
318 319 320 321 322
    }
    else if (action == yahooActionMap)
    {
        int zoom = mapadapter->adaptedZoom();
        mc->setZoom(0);
pixhawk's avatar
pixhawk committed
323

324
        mapadapter = new qmapcontrol::YahooMapAdapter();
lm's avatar
lm committed
325 326
        l->setMapAdapter(mapadapter);
        geomLayer->setMapAdapter(mapadapter);
pixhawk's avatar
pixhawk committed
327

328
        if (isVisible()) mc->updateRequestNew();
lm's avatar
lm committed
329
        mc->setZoom(zoom);
330
        //        yahooActionOverlay->setEnabled(false);
lm's avatar
lm committed
331
        overlay->setVisible(false);
332
        //        yahooActionOverlay->setChecked(false);
lm's avatar
lm committed
333 334 335 336 337 338
    }
    else if (action == yahooActionSatellite)
    {
        int zoom = mapadapter->adaptedZoom();
        QPointF a = mc->currentCoordinate();
        mc->setZoom(0);
pixhawk's avatar
pixhawk committed
339

340
        mapadapter = new qmapcontrol::YahooMapAdapter("us.maps3.yimg.com", "/aerial.maps.yimg.com/png?v=1.7&t=a&s=256&x=%2&y=%3&z=%1");
lm's avatar
lm committed
341
        l->setMapAdapter(mapadapter);
342
        geomLayer->setMapAdapter(mapadapter);
pixhawk's avatar
pixhawk committed
343

344
        if (isVisible()) mc->updateRequestNew();
lm's avatar
lm committed
345
        mc->setZoom(zoom);
346
        overlay->setVisible(false);
347
        //        yahooActionOverlay->setEnabled(true);
lm's avatar
lm committed
348 349 350 351 352
    }
    else if (action == googleActionMap)
    {
        int zoom = mapadapter->adaptedZoom();
        mc->setZoom(0);
353
        mapadapter = new qmapcontrol::GoogleMapAdapter();
lm's avatar
lm committed
354 355
        l->setMapAdapter(mapadapter);
        geomLayer->setMapAdapter(mapadapter);
pixhawk's avatar
pixhawk committed
356

357
        if (isVisible()) mc->updateRequestNew();
lm's avatar
lm committed
358
        mc->setZoom(zoom);
359
        //        yahooActionOverlay->setEnabled(false);
lm's avatar
lm committed
360
        overlay->setVisible(false);
361
        //        yahooActionOverlay->setChecked(false);
lm's avatar
lm committed
362 363 364 365 366
    }
    else if (action == googleSatAction)
    {
        int zoom = mapadapter->adaptedZoom();
        mc->setZoom(0);
367
        mapadapter = new qmapcontrol::GoogleSatMapAdapter();
lm's avatar
lm committed
368 369
        l->setMapAdapter(mapadapter);
        geomLayer->setMapAdapter(mapadapter); 
pixhawk's avatar
pixhawk committed
370

371
        if (isVisible()) mc->updateRequestNew();
lm's avatar
lm committed
372
        mc->setZoom(zoom);
373
        //        yahooActionOverlay->setEnabled(false);
lm's avatar
lm committed
374
        overlay->setVisible(false);
375
        //        yahooActionOverlay->setChecked(false);
lm's avatar
lm committed
376 377 378 379 380
    }
    else
    {
        mapButton->setText("Select..");
    }
381
}
lm's avatar
lm committed
382

lm's avatar
lm committed
383

384
void MapWidget::createPathButtonClicked(bool checked)
lm's avatar
lm committed
385
{
386
    Q_UNUSED(checked);
pixhawk's avatar
pixhawk committed
387

lm's avatar
lm committed
388 389 390 391
    if (createPath->isChecked())
    {
        // change the cursor shape
        this->setCursor(Qt::PointingHandCursor);
392
        mc->setMouseMode(qmapcontrol::MapControl::None);
pixhawk's avatar
pixhawk committed
393 394


395
        // emit signal start to create a Waypoint global
396
        //emit createGlobalWP(true, mc->currentCoordinate());
pixhawk's avatar
pixhawk committed
397

398 399 400 401 402 403 404
        //        // Clear the previous WP track
        //        // TODO: Move this to an actual clear track button and add a warning dialog
        //        mc->layer("Waypoints")->clearGeometries();
        //        wps.clear();
        //        path->setPoints(wps);
        //        mc->layer("Waypoints")->addGeometry(path);
        //        wpIndex.clear();
405 406 407
    }
    else
    {
pixhawk's avatar
pixhawk committed
408

lm's avatar
lm committed
409
        this->setCursor(Qt::ArrowCursor);
410
        mc->setMouseMode(qmapcontrol::MapControl::Panning);
lm's avatar
lm committed
411
    }
pixhawk's avatar
pixhawk committed
412

413
}
414

415 416 417 418 419 420 421
/**
 * Captures a click on the map and if in create WP path mode, it adds the WP on MouseButtonRelease
 *
 * @param event The mouse event
 * @param coordinate The coordinate in which it occured the mouse event
 * @note  This slot is connected to the mouseEventCoordinate of the QMapControl object
 */
422

423 424
void MapWidget::captureMapClick(const QMouseEvent* event, const QPointF coordinate)
{
425
    if (QEvent::MouseButtonRelease == event->type() && createPath->isChecked())
426
    {
427 428
        // Create waypoint name
        QString str;
pixhawk's avatar
pixhawk committed
429

430 431
        // create the WP and set everything in the LineString to display the path
        Waypoint2DIcon* tempCirclePoint;
pixhawk's avatar
pixhawk committed
432

433 434
        if (mav)
        {
435 436 437 438 439 440 441 442 443
            double altitude = 0.0;
            double yaw = 0.0;
            int wpListCount = mav->getWaypointManager()->getWaypointList().count();
            if (wpListCount > 0)
            {
                altitude = mav->getWaypointManager()->getWaypointList().at(wpListCount-1)->getAltitude();
                yaw = mav->getWaypointManager()->getWaypointList().at(wpListCount-1)->getYaw();
            }
            mav->getWaypointManager()->addWaypoint(new Waypoint(wpListCount, coordinate.y(), coordinate.x(), altitude, yaw, true));
444 445 446
        }
        else
        {
447
            str = QString("%1").arg(waypointPath->numberOfPoints());
448
            tempCirclePoint = new Waypoint2DIcon(coordinate.x(), coordinate.y(), 20, str, qmapcontrol::Point::Middle);
449
            wpIcons.append(tempCirclePoint);
pixhawk's avatar
pixhawk committed
450

451
            mc->layer("Waypoints")->addGeometry(tempCirclePoint);
pixhawk's avatar
pixhawk committed
452

453 454 455 456 457
            qmapcontrol::Point* tempPoint = new qmapcontrol::Point(coordinate.x(), coordinate.y(),str);
            wps.append(tempPoint);
            waypointPath->addPoint(tempPoint);

            // Refresh the screen
458
            if (isVisible()) mc->updateRequest(tempPoint->boundingBox().toRect());
459
        }
pixhawk's avatar
pixhawk committed
460

461
        // emit signal mouse was clicked
462 463 464 465 466 467
        //emit captureMapCoordinateClick(coordinate);
    }
}

void MapWidget::updateWaypoint(int uas, Waypoint* wp)
{
468
    // Update waypoint list and redraw map (last parameter)
469 470 471
    updateWaypoint(uas, wp, true);
}

472 473 474 475
/**
 * This function is called if a a single waypoint is updated and
 * also if the whole list changes.
 */
476 477
void MapWidget::updateWaypoint(int uas, Waypoint* wp, bool updateView)
{
478
    // Make sure this is the right UAS
479 480
    if (uas == this->mav->getUASID())
    {
481
        // Only accept waypoints in global coordinate frame
482
        if (wp->getFrame() == MAV_FRAME_GLOBAL)
483 484 485
        {
            // We're good, this is a global waypoint

486 487 488
            // Get the index of this waypoint
            // note the call to getGlobalFrameIndexOf()
            // as we're only handling global waypoints
489
            int wpindex = UASManager::instance()->getUASForId(uas)->getWaypointManager()->getGlobalFrameIndexOf(wp);
490
            // If not found, return (this should never happen, but helps safety)
491
            if (wpindex == -1) return;
492 493

            // Check if wp exists yet in map
494
            if (!(wpIcons.count() > wpindex))
495
            {
496
                // Waypoint is new, a new icon is created
497
                QPointF coordinate;
498 499
                coordinate.setX(wp->getLongitude());
                coordinate.setY(wp->getLatitude());
500 501 502 503
                createWaypointGraphAtMap(wpindex, coordinate);
            }
            else
            {
504 505
                // Waypoint exists, update it if we're not
                // currently dragging it with the mouse
506
                if(!waypointIsDrag)
507
                {
508
                    QPointF coordinate;
509 510
                    coordinate.setX(wp->getLongitude());
                    coordinate.setY(wp->getLatitude());
511

512
                    Point* waypoint;
513
                    waypoint = wps.at(wpindex);
514
                    if (waypoint)
515
                    {
516 517 518 519
                        // First set waypoint coordinate
                        waypoint->setCoordinate(coordinate);
                        // Now update icon position
                        wpIcons.at(wpindex)->setCoordinate(coordinate);
520
                        // Update pen
521 522 523
                        wpIcons.at(wpindex)->setPen(mavPens.value(uas));
                        // Then waypoint line coordinate
                        Point* linesegment = NULL;
524 525
                        // If the line segment already exists, just update it
                        // else create a new one
526 527 528
                        if (waypointPath->points().size() > wpindex)
                        {
                            linesegment = waypointPath->points().at(wpindex);
529
                            if (linesegment) linesegment->setCoordinate(coordinate);
530 531 532 533 534 535
                        }
                        else
                        {
                            waypointPath->addPoint(waypoint);
                        }

536
                        // Update view
537
                        if (updateView) if (isVisible()) mc->updateRequest(waypoint->boundingBox().toRect());
538 539 540 541
                    }
                }
            }
        }
542 543
        else
        {
544 545 546 547
            // Check if the index of this waypoint is larger than the global
            // waypoint list. This implies that the coordinate frame of this
            // waypoint was changed and the list containing only global
            // waypoints was shortened. Thus update the whole list
548 549 550 551
            if (waypointPath->points().count() > UASManager::instance()->getUASForId(uas)->getWaypointManager()->getGlobalFrameCount())
            {
                updateWaypointList(uas);
            }
552
        }
553
    }
pixhawk's avatar
pixhawk committed
554 555
}

556
void MapWidget::createWaypointGraphAtMap(int id, const QPointF coordinate)
557
{
558
    //if (!wpExists(coordinate))
559
    {
560 561
        // Create waypoint name
        QString str;
pixhawk's avatar
pixhawk committed
562

563 564 565
        // create the WP and set everything in the LineString to display the path
        //CirclePoint* tempCirclePoint = new CirclePoint(coordinate.x(), coordinate.y(), 10, str);
        Waypoint2DIcon* tempCirclePoint;
pixhawk's avatar
pixhawk committed
566

567 568
        if (mav)
        {
569
            int uas = mav->getUASID();
570
            str = QString("%1").arg(id);
571 572
            qDebug() << "Waypoint list count:" << str;
            tempCirclePoint = new Waypoint2DIcon(coordinate.x(), coordinate.y(), 20, str, qmapcontrol::Point::Middle, mavPens.value(uas));
573 574 575
        }
        else
        {
576
            str = QString("%1").arg(id);
577 578
            tempCirclePoint = new Waypoint2DIcon(coordinate.x(), coordinate.y(), 20, str, qmapcontrol::Point::Middle);
        }
pixhawk's avatar
pixhawk committed
579 580


581
        mc->layer("Waypoints")->addGeometry(tempCirclePoint);
pixhawk's avatar
pixhawk committed
582
        wpIcons.append(tempCirclePoint);
pixhawk's avatar
pixhawk committed
583

584 585
        Point* tempPoint = new Point(coordinate.x(), coordinate.y(),str);
        wps.append(tempPoint);
586
        waypointPath->addPoint(tempPoint);
pixhawk's avatar
pixhawk committed
587

588
        //wpIndex.insert(str,tempPoint);
tecnosapiens's avatar
tecnosapiens committed
589
        qDebug()<<"Funcion createWaypointGraphAtMap WP= "<<str<<" -> x= "<<tempPoint->latitude()<<" y= "<<tempPoint->longitude();
pixhawk's avatar
pixhawk committed
590

tecnosapiens's avatar
tecnosapiens committed
591
        // Refresh the screen
592
        if (isVisible()) if (isVisible()) mc->updateRequest(tempPoint->boundingBox().toRect());
593
    }
pixhawk's avatar
pixhawk committed
594

595 596
    ////    // emit signal mouse was clicked
    //    emit captureMapCoordinateClick(coordinate);
597 598
}

599 600
int MapWidget::wpExists(const QPointF coordinate)
{
601 602
    for (int i = 0; i < wps.size(); i++){
        if (wps.at(i)->latitude() == coordinate.y() &&
603 604
            wps.at(i)->longitude()== coordinate.x())
        {
605 606
            return 1;
        }
607
    }
608
    return 0;
609 610
}

611

612 613
void MapWidget::captureGeometryClick(Geometry* geom, QPoint point)
{
614 615
    Q_UNUSED(geom);
    Q_UNUSED(point);
pixhawk's avatar
pixhawk committed
616

617
    mc->setMouseMode(qmapcontrol::MapControl::None);
618 619
}

620 621
void MapWidget::captureGeometryDrag(Geometry* geom, QPointF coordinate)
{
622
    waypointIsDrag = true;
pixhawk's avatar
pixhawk committed
623

624
    // Refresh the screen
625
    if (isVisible()) mc->updateRequest(geom->boundingBox().toRect());
pixhawk's avatar
pixhawk committed
626

627
    int temp = 0;
628 629 630 631 632

    // Get waypoint index in list
    bool wpIndexOk;
    int index = geom->name().toInt(&wpIndexOk);

633
    Waypoint2DIcon* point2Find = dynamic_cast <Waypoint2DIcon*> (geom);
pixhawk's avatar
pixhawk committed
634

635
    if (wpIndexOk && point2Find && wps.count() > index)
636
    {
637
        // Update visual
638
        point2Find->setCoordinate(coordinate);
639 640
        waypointPath->points().at(index)->setCoordinate(coordinate);
        if (isVisible()) mc->updateRequest(waypointPath->boundingBox().toRect());
pixhawk's avatar
pixhawk committed
641

642 643
        // Update waypoint data storage
        if (mav)
644
        {
645
            QVector<Waypoint*> wps = mav->getWaypointManager()->getGlobalFrameWaypointList();
pixhawk's avatar
pixhawk committed
646

647
            if (wps.size() > index)
648
            {
649
                Waypoint* wp = wps.at(index);
650 651
                wp->setLatitude(coordinate.y());
                wp->setLongitude(coordinate.x());
652
                mav->getWaypointManager()->notifyOfChange(wp);
653
            }
654
        }
655 656 657 658 659

        // qDebug() << geom->name();
        temp = geom->get_myIndex();
        //qDebug() << temp;
        emit sendGeometryEndDrag(coordinate,temp);
660
    }
pixhawk's avatar
pixhawk committed
661

662
    waypointIsDrag = false;
663 664
}

665
void MapWidget::captureGeometryEndDrag(Geometry* geom, QPointF coordinate)
666
{
667 668 669
    Q_UNUSED(geom);
    Q_UNUSED(coordinate);
    // TODO: Investigate why when creating the waypoint path this slot is being called
pixhawk's avatar
pixhawk committed
670

671 672 673 674 675 676
    // Only change the mouse mode back to panning when not creating a WP path
    if (!createPath->isChecked())
    {
        waypointIsDrag = false;
        mc->setMouseMode(qmapcontrol::MapControl::Panning);
    }
pixhawk's avatar
pixhawk committed
677

678 679
}

pixhawk's avatar
pixhawk committed
680 681 682 683
MapWidget::~MapWidget()
{
    delete m_ui;
}
lm's avatar
lm committed
684 685 686 687 688 689
/**
 *
 * @param uas the UAS/MAV to monitor/display with the HUD
 */
void MapWidget::addUAS(UASInterface* uas)
{
690
    connect(uas, SIGNAL(globalPositionChanged(UASInterface*,double,double,double,quint64)), this, SLOT(updateGlobalPosition(UASInterface*,double,double,double,quint64)));
691
    connect(uas, SIGNAL(attitudeChanged(UASInterface*,double,double,double,quint64)), this, SLOT(updateAttitude(UASInterface*,double,double,double,quint64)));
692
    connect(uas, SIGNAL(systemSpecsChanged(int)), this, SLOT(updateSystemSpecs(int)));
693 694
}

695 696 697 698 699
/**
 * Update the whole list of waypoints. This is e.g. necessary if the list order changed.
 * The UAS manager will emit the appropriate signal whenever updating the list
 * is necessary.
 */
700 701 702 703 704 705
void MapWidget::updateWaypointList(int uas)
{
    // Get already existing waypoints
    UASInterface* uasInstance = UASManager::instance()->getUASForId(uas);
    if (uasInstance)
    {
706 707
        // Get update rect of old content, this is what will be redrawn
        // in the last step
708 709
        QRect updateRect = waypointPath->boundingBox().toRect();

710
        // Get all waypoints, including non-global waypoints
711 712 713 714 715 716 717 718 719
        QVector<Waypoint*> wpList = uasInstance->getWaypointManager()->getWaypointList();

        // Clear if necessary
        if (wpList.count() == 0)
        {
            clearWaypoints(uas);
            return;
        }

720
        // Trim internal list to number of global waypoints in the waypoint manager list
721 722
        int overSize = waypointPath->points().count() - uasInstance->getWaypointManager()->getGlobalFrameCount();
        if (overSize > 0)
723
        {
724 725 726
            // Remove n waypoints at the end of the list
            // the remaining waypoints will be updated
            // in the next step
727
            for (int i = 0; i < overSize; ++i)
728
            {
729 730 731 732
                wps.removeLast();
                mc->layer("Waypoints")->removeGeometry(wpIcons.last());
                wpIcons.removeLast();
                waypointPath->points().removeLast();
733 734
            }
        }
pixhawk's avatar
pixhawk committed
735

736 737 738
        // Load all existing waypoints into map view
        foreach (Waypoint* wp, wpList)
        {
739 740 741
            // Block map draw updates, since we update everything in the next step
            // but update internal data structures.
            // Please note that updateWaypoint() ignores non-global waypoints
742 743 744
            updateWaypoint(mav->getUASID(), wp, false);
        }

745
        // Update view
746
        if (isVisible()) mc->updateRequest(updateRect);
747 748 749 750 751 752 753 754 755 756 757 758
    }
}

void MapWidget::redoWaypoints(int uas)
{
    //    QObject* sender = QObject::sender();
    //    UASWaypointManager* manager = dynamic_cast<UASWaypointManager*>(sender);
    //    if (sender)
    //    {
    // Get waypoint list for this MAV

    // Clear all waypoints
759
    //clearWaypoints();
760 761 762 763 764
    // Re-add the updated waypoints

    //    }

    updateWaypointList(uas);
lm's avatar
lm committed
765 766
}

767 768
void MapWidget::activeUASSet(UASInterface* uas)
{
769 770 771
    // Disconnect old MAV
    if (mav)
    {
772
        // Disconnect the waypoint manager / data storage from the UI
773
        disconnect(mav->getWaypointManager(), SIGNAL(waypointListChanged(int)), this, SLOT(updateWaypointList(int)));
774
        disconnect(mav->getWaypointManager(), SIGNAL(waypointChanged(int, Waypoint*)), this, SLOT(updateWaypoint(int,Waypoint*)));
775 776 777
        disconnect(this, SIGNAL(waypointCreated(Waypoint*)), mav->getWaypointManager(), SLOT(addWaypoint(Waypoint*)));
    }

778 779 780
    if (uas)
    {
        mav = uas;
781 782
        QColor color = mav->getColor();
        color.setAlphaF(0.9);
783
        QPen* pen = new QPen(color);
784
        pen->setWidth(3.0);
785 786 787 788
        mavPens.insert(mav->getUASID(), pen);
        // FIXME Remove after refactoring
        waypointPath->setPen(pen);

789
        // Delete all waypoints and add waypoint from new system
790 791
        //redoWaypoints();
        updateWaypointList(uas->getUASID());
792

793
        // Connect the waypoint manager / data storage to the UI
794
        connect(mav->getWaypointManager(), SIGNAL(waypointListChanged(int)), this, SLOT(updateWaypointList(int)));
795
        connect(mav->getWaypointManager(), SIGNAL(waypointChanged(int, Waypoint*)), this, SLOT(updateWaypoint(int,Waypoint*)));
796
        connect(this, SIGNAL(waypointCreated(Waypoint*)), mav->getWaypointManager(), SLOT(addWaypoint(Waypoint*)));
797

798
        updateSystemSpecs(mav->getUASID());
799
        updateSelectedSystem(mav->getUASID());
800
        mc->updateRequest(waypointPath->boundingBox().toRect());
801 802 803
    }
}

804 805 806 807 808 809 810 811 812 813 814 815 816 817
void MapWidget::updateSystemSpecs(int uas)
{
    foreach (qmapcontrol::Point* p, uasIcons.values())
    {
        MAV2DIcon* icon = dynamic_cast<MAV2DIcon*>(p);
        if (icon && icon->getUASId() == uas)
        {
            // Set new airframe
            icon->setAirframe(UASManager::instance()->getUASForId(uas)->getAirframe());
            icon->drawIcon();
        }
    }
}

818 819 820 821 822 823 824 825 826 827
void MapWidget::updateSelectedSystem(int uas)
{
    foreach (qmapcontrol::Point* p, uasIcons.values())
    {
        MAV2DIcon* icon = dynamic_cast<MAV2DIcon*>(p);
        if (icon)
        {
            // Set as selected if ids match
            icon->setSelectedUAS((icon->getUASId() == uas));
        }
828 829 830
    }
}

831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846
void MapWidget::updateAttitude(UASInterface* uas, double roll, double pitch, double yaw, quint64 usec)
{
    Q_UNUSED(roll);
    Q_UNUSED(pitch);
    Q_UNUSED(usec);

    if (uas)
    {
        MAV2DIcon* icon = dynamic_cast<MAV2DIcon*>(uasIcons.value(uas->getUASID(), NULL));
        if (icon)
        {
            icon->setYaw(yaw);
        }
    }
}

lm's avatar
lm committed
847 848 849 850 851 852 853 854 855
/**
 * Updates the global position of one MAV and append the last movement to the trail
 *
 * @param uas The unmanned air system
 * @param lat Latitude in WGS84 ellipsoid
 * @param lon Longitutde in WGS84 ellipsoid
 * @param alt Altitude over mean sea level
 * @param usec Timestamp of the position message in milliseconds FIXME will move to microseconds
 */
lm's avatar
lm committed
856 857 858
void MapWidget::updateGlobalPosition(UASInterface* uas, double lat, double lon, double alt, quint64 usec)
{
    Q_UNUSED(usec);
859
    Q_UNUSED(alt); // FIXME Use altitude
860 861 862 863 864 865 866 867

    // create a LineString
    //QList<Point*> points;
    // Points with a circle
    // A QPen can be used to customize the
    //pointpen->setWidth(3);
    //points.append(new CirclePoint(lat, lon, 10, uas->getUASName(), Point::Middle, pointpen));

868 869
    qmapcontrol::Point* p;
    QPointF coordinate;
870 871
    coordinate.setX(lon);
    coordinate.setY(lat);
872 873 874 875 876 877 878

    if (!uasIcons.contains(uas->getUASID()))
    {
        // Get the UAS color
        QColor uasColor = uas->getColor();

        // Icon
879 880
        //QPen* pointpen = new QPen(uasColor);
        qDebug() << "2D MAP: ADDING" << uas->getUASName() << __FILE__ << __LINE__;
881
        p = new MAV2DIcon(uas, 68, uas->getSystemType(), uas->getColor(), QString("%1").arg(uas->getUASID()), qmapcontrol::Point::Middle);
882
        uasIcons.insert(uas->getUASID(), p);
883
        mc->layer("Waypoints")->addGeometry(p);
884 885 886 887

        // Line
        // A QPen also can use transparency

888 889 890 891
        //        QList<qmapcontrol::Point*> points;
        //        points.append(new qmapcontrol::Point(coordinate.x(), coordinate.y()));
        //        QPen* linepen = new QPen(uasColor.darker());
        //        linepen->setWidth(2);
892

893 894 895
        //        // Create tracking line string
        //        qmapcontrol::LineString* ls = new qmapcontrol::LineString(points, QString("%1").arg(uas->getUASID()), linepen);
        //        uasTrails.insert(uas->getUASID(), ls);
896

897 898
        //        // Add the LineString to the layer
        //        mc->layer("Waypoints")->addGeometry(ls);
899 900 901
    }
    else
    {
902 903 904 905
        //        p = dynamic_cast<MAV2DIcon*>(uasIcons.value(uas->getUASID()));
        //        if (p)
        //        {
        p = uasIcons.value(uas->getUASID());
906
        p->setCoordinate(QPointF(lon, lat));
907 908
        //p->setYaw(uas->getYaw());
        //        }
909
        // Extend trail
910
        //        uasTrails.value(uas->getUASID())->addPoint(new qmapcontrol::Point(coordinate.x(), coordinate.y()));
911 912
    }

913
    if (isVisible()) mc->updateRequest(p->boundingBox().toRect());
914

915
    //if (isVisible()) mc->updateRequestNew();//(uasTrails.value(uas->getUASID())->boundingBox().toRect());
916

917
    if (this->mav && uas->getUASID() == this->mav->getUASID())
pixhawk's avatar
pixhawk committed
918
    {
919 920 921
        // Limit the position update rate
        quint64 currTime = MG::TIME::getGroundTimeNow();
        if (currTime - lastUpdate > 120)
922
        {
923 924 925 926
            lastUpdate = currTime;
            // Sets the view to the interesting area
            if (followgps->isChecked())
            {
927
                updatePosition(0, lon, lat);
928 929 930 931 932 933
            }
            else
            {
                // Refresh the screen
                //if (isVisible()) mc->updateRequestNew();
            }
934
        }
pixhawk's avatar
pixhawk committed
935 936
    }
}
pixhawk's avatar
pixhawk committed
937

lm's avatar
lm committed
938 939 940
/**
 * Center the view on this position
 */
lm's avatar
lm committed
941
void MapWidget::updatePosition(float time, double lat, double lon)
pixhawk's avatar
pixhawk committed
942
{
lm's avatar
lm committed
943
    Q_UNUSED(time);
944
    //gpsposition->setText(QString::number(time) + " / " + QString::number(lat) + " / " + QString::number(lon));
945
    if (followgps->isChecked() && isVisible())
pixhawk's avatar
pixhawk committed
946
    {
lm's avatar
lm committed
947
        mc->setView(QPointF(lat, lon));
pixhawk's avatar
pixhawk committed
948 949 950 951 952 953 954 955 956 957 958 959 960 961
    }
}

void MapWidget::wheelEvent(QWheelEvent *event)
{
    int numDegrees = event->delta() / 8;
    int numSteps = numDegrees / 15;
    // Calculate new zoom level
    int newZoom = mc->currentZoom()+numSteps;
    // Set new zoom level, level is bounded by map control
    mc->setZoom(newZoom);
    // Detail zoom level is the number of steps zoomed in further
    // after the bounding has taken effect
    detailZoom = qAbs(qMin(0, mc->currentZoom()-newZoom));
pixhawk's avatar
pixhawk committed
962

963
    // visual field of camera
964
    updateCameraPosition(20*newZoom,0,"no");
pixhawk's avatar
pixhawk committed
965

pixhawk's avatar
pixhawk committed
966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993
}

void MapWidget::keyPressEvent(QKeyEvent *event)
{
    switch (event->key()) {
    case Qt::Key_Plus:
        mc->zoomIn();
        break;
    case Qt::Key_Minus:
        mc->zoomOut();
        break;
    case Qt::Key_Left:
        mc->scrollLeft(this->width()/scrollStep);
        break;
    case Qt::Key_Right:
        mc->scrollRight(this->width()/scrollStep);
        break;
    case Qt::Key_Down:
        mc->scrollDown(this->width()/scrollStep);
        break;
    case Qt::Key_Up:
        mc->scrollUp(this->width()/scrollStep);
        break;
    default:
        QWidget::keyPressEvent(event);
    }
}

994
void MapWidget::resizeEvent(QResizeEvent* event )
pixhawk's avatar
pixhawk committed
995
{
996 997
    Q_UNUSED(event);
    mc->resize(this->size());
pixhawk's avatar
pixhawk committed
998 999
}

1000 1001 1002 1003 1004 1005 1006 1007
void MapWidget::showEvent(QShowEvent* event)
{
    Q_UNUSED(event);
}

void MapWidget::hideEvent(QHideEvent* event)
{
    Q_UNUSED(event);
1008 1009 1010 1011 1012 1013 1014 1015
    QSettings settings;
    settings.beginGroup("QGC_MAPWIDGET");
    QPointF currentPos = mc->currentCoordinate();
    settings.setValue("LAST_LATITUDE", currentPos.y());
    settings.setValue("LAST_LONGITUDE", currentPos.x());
    settings.setValue("LAST_ZOOM", mc->currentZoom());
    settings.endGroup();
    settings.sync();
1016 1017
}

pixhawk's avatar
pixhawk committed
1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029

void MapWidget::changeEvent(QEvent *e)
{
    QWidget::changeEvent(e);
    switch (e->type()) {
    case QEvent::LanguageChange:
        m_ui->retranslateUi(this);
        break;
    default:
        break;
    }
}
1030

1031
void MapWidget::clearWaypoints(int uas)
1032
{
pixhawk's avatar
pixhawk committed
1033
    Q_UNUSED(uas);
1034
    // Clear the previous WP track
pixhawk's avatar
pixhawk committed
1035

1036
    //mc->layer("Waypoints")->clearGeometries();
1037
    wps.clear();
1038 1039 1040 1041 1042 1043 1044 1045 1046 1047
    foreach (Point* p, wpIcons)
    {
        mc->layer("Waypoints")->removeGeometry(p);
    }
    wpIcons.clear();

    // Get bounding box of this object BEFORE deleting the content
    QRect box = waypointPath->boundingBox().toRect();

    // Delete the content
1048
    waypointPath->points().clear();
1049

1050 1051
    //delete waypointPath;
    //waypointPath = new
1052
    //mc->layer("Waypoints")->addGeometry(waypointPath);
1053
    //wpIndex.clear();
1054
    if (isVisible()) mc->updateRequest(box);//(waypointPath->boundingBox().toRect());
pixhawk's avatar
pixhawk committed
1055

1056 1057 1058 1059
    if(createPath->isChecked())
    {
        createPath->click();
    }
1060 1061

    qDebug() << "CLEARING WAYPOINTS";
1062
}
pixhawk's avatar
pixhawk committed
1063

1064
void MapWidget::clearPath(int uas)
1065
{
pixhawk's avatar
pixhawk committed
1066
    Q_UNUSED(uas);
1067 1068 1069 1070 1071 1072 1073 1074 1075
    mc->layer("Tracking")->clearGeometries();
    foreach (qmapcontrol::LineString* ls, uasTrails)
    {
        QPen* linepen = ls->pen();
        delete ls;
        qmapcontrol::LineString* lsNew = new qmapcontrol::LineString(QList<qmapcontrol::Point*>(), "", linepen);
        mc->layer("Tracking")->addGeometry(lsNew);
    }
    // FIXME update this with update request only for bounding box of trails
1076
    if (isVisible()) mc->updateRequestNew();//(QRect(0, 0, width(), height()));
1077
}
1078

1079 1080
void MapWidget::updateCameraPosition(double radio, double bearing, QString dir)
{
pixhawk's avatar
pixhawk committed
1081 1082
    Q_UNUSED(dir);
    Q_UNUSED(bearing);
1083
    // FIXME Mariano
1084 1085
    //camPoints.clear();
    QPointF currentPos = mc->currentCoordinate();
1086
    //    QPointF actualPos = getPointxBearing_Range(currentPos.y(),currentPos.x(),bearing,distance);
pixhawk's avatar
pixhawk committed
1087

1088 1089
    //    qmapcontrol::Point* tempPoint1 = new qmapcontrol::Point(currentPos.x(), currentPos.y(),"inicial",qmapcontrol::Point::Middle);
    //    qmapcontrol::Point* tempPoint2 = new qmapcontrol::Point(actualPos.x(), actualPos.y(),"final",qmapcontrol::Point::Middle);
pixhawk's avatar
pixhawk committed
1090

1091 1092
    //    camPoints.append(tempPoint1);
    //    camPoints.append(tempPoint2);
pixhawk's avatar
pixhawk committed
1093

1094
    //    camLine->setPoints(camPoints);
pixhawk's avatar
pixhawk committed
1095

1096
    QPen* camBorderPen = new QPen(QColor(255,0,0));
1097
    camBorderPen->setWidth(2);
pixhawk's avatar
pixhawk committed
1098

1099
    //radio = mc->currentZoom()
pixhawk's avatar
pixhawk committed
1100

1101 1102 1103 1104
    if(drawCamBorder)
    {
        //clear camera borders
        mc->layer("Camera")->clearGeometries();
pixhawk's avatar
pixhawk committed
1105

1106 1107
        //create a camera borders
        qmapcontrol::CirclePoint* camBorder = new qmapcontrol::CirclePoint(currentPos.x(), currentPos.y(), radio, "camBorder", qmapcontrol::Point::Middle, camBorderPen);
pixhawk's avatar
pixhawk committed
1108

1109
        //camBorder->setCoordinate(currentPos);
pixhawk's avatar
pixhawk committed
1110

1111
        mc->layer("Camera")->addGeometry(camBorder);
1112
        // mc->layer("Camera")->addGeometry(camLine);
1113
        if (isVisible()) mc->updateRequestNew();
pixhawk's avatar
pixhawk committed
1114

1115
    }
1116 1117 1118 1119
    else
    {
        //clear camera borders
        mc->layer("Camera")->clearGeometries();
1120
        if (isVisible()) mc->updateRequestNew();
pixhawk's avatar
pixhawk committed
1121

1122
    }
pixhawk's avatar
pixhawk committed
1123 1124


1125 1126 1127 1128 1129 1130
}

void MapWidget::drawBorderCamAtMap(bool status)
{
    drawCamBorder = status;
    updateCameraPosition(20,0,"no");
pixhawk's avatar
pixhawk committed
1131

1132 1133 1134 1135 1136
}

QPointF MapWidget::getPointxBearing_Range(double lat1, double lon1, double bearing, double distance)
{
    QPointF temp;
pixhawk's avatar
pixhawk committed
1137

1138
    double rad = M_PI/180;
pixhawk's avatar
pixhawk committed
1139

1140 1141 1142
    bearing = bearing*rad;
    temp.setX((lon1 + ((distance/60) * (sin(bearing)))));
    temp.setY((lat1 + ((distance/60) * (cos(bearing)))));
pixhawk's avatar
pixhawk committed
1143

1144 1145 1146
    return temp;
}