WaypointList.cc 25.3 KB
Newer Older
pixhawk's avatar
pixhawk committed
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29
/*=====================================================================

PIXHAWK Micro Air Vehicle Flying Robotics Toolkit

(c) 2009, 2010 PIXHAWK PROJECT  <http://pixhawk.ethz.ch>

This file is part of the PIXHAWK project

    PIXHAWK is free software: you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation, either version 3 of the License, or
    (at your option) any later version.

    PIXHAWK is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.

    You should have received a copy of the GNU General Public License
    along with PIXHAWK. If not, see <http://www.gnu.org/licenses/>.

======================================================================*/

/**
 * @file
 *   @brief Waypoint list widget
 *
 *   @author Lorenz Meier <mavteam@student.ethz.ch>
 *   @author Benjamin Knecht <mavteam@student.ethz.ch>
30
 *   @author Petri Tanskanen <mavteam@student.ethz.ch>
pixhawk's avatar
pixhawk committed
31 32 33 34 35
 *
 */

#include "WaypointList.h"
#include "ui_WaypointList.h"
Don Gagne's avatar
Don Gagne committed
36 37
#include "QGCFileDialog.h"

pixhawk's avatar
pixhawk committed
38
#include <UASInterface.h>
39
#include <UAS.h>
40
#include <HomePositionManager.h>
pixhawk's avatar
pixhawk committed
41
#include <QDebug>
42
#include <QMouseEvent>
43
#include <QTextEdit>
pixhawk's avatar
pixhawk committed
44

45
WaypointList::WaypointList(QWidget *parent, UASWaypointManager* wpm) :
46 47
    QWidget(parent),
    uas(NULL),
48
    WPM(wpm),
49 50 51 52
    mavX(0.0),
    mavY(0.0),
    mavZ(0.0),
    mavYaw(0.0),
53
    showOfflineWarning(false),
54
    m_ui(new Ui::WaypointList)
pixhawk's avatar
pixhawk committed
55
{
56

pixhawk's avatar
pixhawk committed
57 58
    m_ui->setupUi(this);

pixhawk's avatar
pixhawk committed
59 60 61 62 63 64 65
    //EDIT TAB

    editableListLayout = new QVBoxLayout(m_ui->editableListWidget);
    editableListLayout->setSpacing(0);
    editableListLayout->setMargin(0);
    editableListLayout->setAlignment(Qt::AlignTop);
    m_ui->editableListWidget->setLayout(editableListLayout);
pixhawk's avatar
pixhawk committed
66 67 68 69

    // ADD WAYPOINT
    // Connect add action, set right button icon and connect action to this class
    connect(m_ui->addButton, SIGNAL(clicked()), m_ui->actionAddWaypoint, SIGNAL(triggered()));
70
    connect(m_ui->actionAddWaypoint, SIGNAL(triggered()), this, SLOT(addEditable()));
pixhawk's avatar
pixhawk committed
71

72
    // ADD WAYPOINT AT CURRENT POSITION
73
    connect(m_ui->positionAddButton, SIGNAL(clicked()), this, SLOT(addCurrentPositionWaypoint()));
74

pixhawk's avatar
pixhawk committed
75 76 77
    // SEND WAYPOINTS
    connect(m_ui->transmitButton, SIGNAL(clicked()), this, SLOT(transmit()));

78 79 80
    // DELETE ALL WAYPOINTS
    connect(m_ui->clearWPListButton, SIGNAL(clicked()), this, SLOT(clearWPWidget()));

81
    // REQUEST WAYPOINTS
82
    connect(m_ui->readButton, SIGNAL(clicked()), this, SLOT(read()));
83

pixhawk's avatar
pixhawk committed
84 85 86 87
    // SAVE/LOAD WAYPOINTS
    connect(m_ui->saveButton, SIGNAL(clicked()), this, SLOT(saveWaypoints()));
    connect(m_ui->loadButton, SIGNAL(clicked()), this, SLOT(loadWaypoints()));

pixhawk's avatar
pixhawk committed
88 89 90 91 92 93 94 95 96 97
    //VIEW TAB

    viewOnlyListLayout = new QVBoxLayout(m_ui->viewOnlyListWidget);
    viewOnlyListLayout->setSpacing(0);
    viewOnlyListLayout->setMargin(0);
    viewOnlyListLayout->setAlignment(Qt::AlignTop);
    m_ui->viewOnlyListWidget->setLayout(viewOnlyListLayout);

    // REFRESH VIEW TAB

98
    connect(m_ui->refreshButton, SIGNAL(clicked()), this, SLOT(refresh()));
99

100 101 102 103
    if (WPM) {
        // SET UAS AFTER ALL SIGNALS/SLOTS ARE CONNECTED
        if (!WPM->getUAS())
        {
104 105 106 107 108 109
            // Disable buttons which don't make sense without valid UAS.
            m_ui->positionAddButton->setEnabled(false);
            m_ui->transmitButton->setEnabled(false);
            m_ui->readButton->setEnabled(false);
            m_ui->refreshButton->setEnabled(false);

110
            //FIXME: The whole "Onboard Waypoints"-tab should be hidden, instead of "refresh" button
111 112 113 114 115 116 117 118
            // Insert a "NO UAV" info into the Onboard Tab
            QLabel* noUas = new QLabel(this);
            noUas->setObjectName("noUas");
            noUas->setText("NO UAS");
            noUas->setEnabled(false);
            noUas->setAlignment(Qt::AlignCenter);
            viewOnlyListLayout->insertWidget(0, noUas);

119 120 121 122
            showOfflineWarning = true;
        } else {
            setUAS(static_cast<UASInterface*>(WPM->getUAS()));
        }
123

124 125 126
        /* connect slots */
        connect(WPM, SIGNAL(updateStatusString(const QString &)),        this, SLOT(updateStatusLabel(const QString &)));
        connect(WPM, SIGNAL(waypointEditableListChanged(void)),                  this, SLOT(waypointEditableListChanged(void)));
127
        connect(WPM, SIGNAL(waypointEditableChanged(int,MissionItem*)), this, SLOT(updateWaypointEditable(int,MissionItem*)));
128
        connect(WPM, SIGNAL(waypointViewOnlyListChanged(void)),                  this, SLOT(waypointViewOnlyListChanged(void)));
129
        connect(WPM, SIGNAL(waypointViewOnlyChanged(int,MissionItem*)), this, SLOT(updateWaypointViewOnly(int,MissionItem*)));
130
        connect(WPM, SIGNAL(currentWaypointChanged(quint16)),            this, SLOT(currentWaypointViewOnlyChanged(quint16)));
131 132 133 134 135

        //Even if there are no waypoints, since this is a new instance and there is an
        //existing WPM, then we need to assume things have changed, and act appropriatly.
        waypointEditableListChanged();
        waypointViewOnlyListChanged();
136
    }
137 138 139

    // STATUS LABEL
    updateStatusLabel("");
140 141

    this->setVisible(false);
142
    loadFileGlobalWP = false;
143
    readGlobalWP = false;
144 145 146
    centerMapCoordinate.setX(0.0);
    centerMapCoordinate.setY(0.0);

pixhawk's avatar
pixhawk committed
147 148 149 150 151 152 153
}

WaypointList::~WaypointList()
{
    delete m_ui;
}

154
void WaypointList::updatePosition(UASInterface* uas, double x, double y, double z, quint64 usec)
155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171
{
    Q_UNUSED(uas);
    Q_UNUSED(usec);
    mavX = x;
    mavY = y;
    mavZ = z;
}

void WaypointList::updateAttitude(UASInterface* uas, double roll, double pitch, double yaw, quint64 usec)
{
    Q_UNUSED(uas);
    Q_UNUSED(usec);
    Q_UNUSED(roll);
    Q_UNUSED(pitch);
    mavYaw = yaw;
}

pixhawk's avatar
pixhawk committed
172 173
void WaypointList::setUAS(UASInterface* uas)
{
174 175 176 177
    if (!uas)
        return;

    if (this->uas != NULL)
lm's avatar
lm committed
178
    {
179 180 181 182 183
        // Clear current list
        on_clearWPListButton_clicked();
        // Disconnect everything
        disconnect(WPM, SIGNAL(updateStatusString(const QString &)),        this, SLOT(updateStatusLabel(const QString &)));
        disconnect(WPM, SIGNAL(waypointEditableListChanged(void)),                  this, SLOT(waypointEditableListChanged(void)));
184
        disconnect(WPM, SIGNAL(waypointEditableChanged(int,MissionItem*)), this, SLOT(updateWaypointEditable(int,MissionItem*)));
185
        disconnect(WPM, SIGNAL(waypointViewOnlyListChanged(void)),                  this, SLOT(waypointViewOnlyListChanged(void)));
186
        disconnect(WPM, SIGNAL(waypointViewOnlyChanged(int,MissionItem*)), this, SLOT(updateWaypointViewOnly(int,MissionItem*)));
187 188 189
        disconnect(WPM, SIGNAL(currentWaypointChanged(quint16)),            this, SLOT(currentWaypointViewOnlyChanged(quint16)));
        disconnect(this->uas, SIGNAL(localPositionChanged(UASInterface*,double,double,double,quint64)),  this, SLOT(updatePosition(UASInterface*,double,double,double,quint64)));
        disconnect(this->uas, SIGNAL(attitudeChanged(UASInterface*,double,double,double,quint64)),       this, SLOT(updateAttitude(UASInterface*,double,double,double,quint64)));
pixhawk's avatar
pixhawk committed
190
    }
191

192 193 194 195 196 197
    // Now that there's a valid UAS, enable the UI.
    m_ui->positionAddButton->setEnabled(true);
    m_ui->transmitButton->setEnabled(true);
    m_ui->readButton->setEnabled(true);
    m_ui->refreshButton->setEnabled(true);

198 199 200 201 202
    WPM = uas->getWaypointManager();

    this->uas = uas;
    connect(WPM, SIGNAL(updateStatusString(const QString &)),        this, SLOT(updateStatusLabel(const QString &)));
    connect(WPM, SIGNAL(waypointEditableListChanged(void)),                  this, SLOT(waypointEditableListChanged(void)));
203
    connect(WPM, SIGNAL(waypointEditableChanged(int,MissionItem*)), this, SLOT(updateWaypointEditable(int,MissionItem*)));
204
    connect(WPM, SIGNAL(waypointViewOnlyListChanged(void)),                  this, SLOT(waypointViewOnlyListChanged(void)));
205
    connect(WPM, SIGNAL(waypointViewOnlyChanged(int,MissionItem*)), this, SLOT(updateWaypointViewOnly(int,MissionItem*)));
206 207 208 209 210 211
    connect(WPM, SIGNAL(currentWaypointChanged(quint16)),            this, SLOT(currentWaypointViewOnlyChanged(quint16)));
    connect(uas, SIGNAL(localPositionChanged(UASInterface*,double,double,double,quint64)),  this, SLOT(updatePosition(UASInterface*,double,double,double,quint64)));
    connect(uas, SIGNAL(attitudeChanged(UASInterface*,double,double,double,quint64)),       this, SLOT(updateAttitude(UASInterface*,double,double,double,quint64)));
    //connect(WPM,SIGNAL(loadWPFile()),this,SLOT(setIsLoadFileWP()));
    //connect(WPM,SIGNAL(readGlobalWPFromUAS(bool)),this,SLOT(setIsReadGlobalWP(bool)));

212 213
    // Update list
    read();
pixhawk's avatar
pixhawk committed
214 215
}

pixhawk's avatar
pixhawk committed
216 217
void WaypointList::saveWaypoints()
{
218
    // TODO Need better default directory
219
    QString fileName = QGCFileDialog::getSaveFileName(this, tr("Save Waypoint File"), "./untitled.waypoints", tr("Waypoint Files (*.waypoints)"), "waypoints", true);
220
    WPM->saveWaypoints(fileName);
pixhawk's avatar
pixhawk committed
221 222 223 224
}

void WaypointList::loadWaypoints()
{
225
    // TODO Need better default directory
226
    QString fileName = QGCFileDialog::getOpenFileName(this, tr("Load Waypoint File"), ".", tr("Waypoint Files (*.waypoints);;All Files (*)"));
227
    WPM->loadWaypoints(fileName);
pixhawk's avatar
pixhawk committed
228 229 230 231
}

void WaypointList::transmit()
{
lm's avatar
lm committed
232 233
    if (uas)
    {
234
        WPM->writeWaypoints();
pixhawk's avatar
pixhawk committed
235 236 237 238 239
    }
}

void WaypointList::read()
{
lm's avatar
lm committed
240 241
    if (uas)
    {
242
        WPM->readWaypoints(true);
pixhawk's avatar
pixhawk committed
243 244 245 246 247 248 249
    }
}

void WaypointList::refresh()
{
    if (uas)
    {
250
        WPM->readWaypoints(false);
pixhawk's avatar
pixhawk committed
251 252 253
    }
}

pixhawk's avatar
pixhawk committed
254
void WaypointList::addEditable()
pixhawk's avatar
pixhawk committed
255
{
256 257
    addEditable(false);
}
pixhawk's avatar
pixhawk committed
258

259 260 261
void WaypointList::addEditable(bool onCurrentPosition)
{

262 263
    const QList<MissionItem *> &waypoints = WPM->getWaypointEditableList();
    MissionItem *wp;
264
    if (waypoints.count() > 0 &&
265 266 267
            !(onCurrentPosition && uas && (uas->localPositionKnown() || uas->globalPositionKnown())))
    {
        // Create waypoint with last frame
268
        MissionItem *last = waypoints.last();
269
        wp = WPM->createWaypoint();
270
//        wp->blockSignals(true);
Don Gagne's avatar
Don Gagne committed
271
        MAV_FRAME frame = (MAV_FRAME)last->frame();
272 273
        wp->setFrame(frame);
        if (frame == MAV_FRAME_GLOBAL || frame == MAV_FRAME_GLOBAL_RELATIVE_ALT)
274
        {
Don Gagne's avatar
Don Gagne committed
275 276 277
            wp->setLatitude(last->latitude());
            wp->setLongitude(last->longitude());
            wp->setAltitude(last->altitude());
278
        } else {
Don Gagne's avatar
Don Gagne committed
279 280 281
            wp->setX(last->x());
            wp->setY(last->y());
            wp->setZ(last->z());
282
        }
Don Gagne's avatar
Don Gagne committed
283 284 285 286 287
        wp->setParam1(last->param1());
        wp->setParam2(last->param2());
        wp->setParam3(last->param3());
        wp->setParam4(last->param4());
        wp->setAutocontinue(last->autoContinue());
288
//        wp->blockSignals(false);
Don Gagne's avatar
Don Gagne committed
289
        wp->setAction(last->command());
290 291 292 293 294 295 296 297
//        WPM->addWaypointEditable(wp);
    }
    else
    {
        if (uas)
        {
            // Create first waypoint at current MAV position
            if (uas->globalPositionKnown())
298
            {
299
                if (onCurrentPosition)
300
                {
301 302 303 304 305 306

                    if (WPM->getFrameRecommendation() == MAV_FRAME_GLOBAL) {
                        updateStatusLabel(tr("Added default GLOBAL (Abs alt.) waypoint."));
                    } else {
                        updateStatusLabel(tr("Added default GLOBAL (Relative alt.) waypoint."));
                    }
Don Gagne's avatar
Don Gagne committed
307 308
                    wp = new MissionItem(NULL,
                                         0,
Don Gagne's avatar
Don Gagne committed
309
                                         QGeoCoordinate(uas->getLatitude(), uas->getLongitude(), uas->getAltitudeAMSL()),
Don Gagne's avatar
Don Gagne committed
310 311 312 313 314 315 316 317
                                         MAV_CMD_NAV_WAYPOINT,
                                         0,
                                         WPM->getAcceptanceRadiusRecommendation(),
                                         0,
                                         0,
                                         true,
                                         true,
                                         (MAV_FRAME)WPM->getFrameRecommendation());
318 319 320 321 322 323 324 325 326
                    WPM->addWaypointEditable(wp);

                } else {

                    if (WPM->getFrameRecommendation() == MAV_FRAME_GLOBAL) {
                        updateStatusLabel(tr("Added default GLOBAL (Abs alt.) waypoint."));
                    } else {
                        updateStatusLabel(tr("Added default GLOBAL (Relative alt.) waypoint."));
                    }
Don Gagne's avatar
Don Gagne committed
327 328
                    wp = new MissionItem(NULL,
                                         0,
Don Gagne's avatar
Don Gagne committed
329
                                         QGeoCoordinate(HomePositionManager::instance()->getHomeLatitude(), HomePositionManager::instance()->getHomeLongitude(), WPM->getAltitudeRecommendation()),
Don Gagne's avatar
Don Gagne committed
330 331 332 333 334 335 336 337
                                         MAV_CMD_NAV_WAYPOINT,
                                         0,
                                         WPM->getAcceptanceRadiusRecommendation(),
                                         0,
                                         0,
                                         true,
                                         true,
                                         (MAV_FRAME)WPM->getFrameRecommendation());
338
                    WPM->addWaypointEditable(wp);
339
                }
340 341 342 343
            }
            else if (uas->localPositionKnown())
            {
                if (onCurrentPosition)
344
                {
345
                    updateStatusLabel(tr("Added default LOCAL (NED) waypoint."));
Don Gagne's avatar
Don Gagne committed
346 347
                    wp = new MissionItem(NULL,
                                         0,
Don Gagne's avatar
Don Gagne committed
348
                                         QGeoCoordinate(uas->getLocalX(), uas->getLocalY(), uas->getLocalZ()),
Don Gagne's avatar
Don Gagne committed
349 350 351 352 353 354 355 356
                                         MAV_CMD_NAV_WAYPOINT,
                                         0,
                                         WPM->getAcceptanceRadiusRecommendation(),
                                         0,
                                         0,
                                         true,
                                         true,
                                         MAV_FRAME_LOCAL_NED);
357 358 359
                    WPM->addWaypointEditable(wp);
                } else {
                    updateStatusLabel(tr("Added default LOCAL (NED) waypoint."));
Don Gagne's avatar
Don Gagne committed
360 361
                    wp = new MissionItem(0,
                                         0,
Don Gagne's avatar
Don Gagne committed
362
                                         QGeoCoordinate(0, 0, -0.50),
Don Gagne's avatar
Don Gagne committed
363 364 365 366 367 368 369 370
                                         MAV_CMD_NAV_WAYPOINT,
                                         0,
                                         WPM->getAcceptanceRadiusRecommendation(),
                                         0,
                                         0,
                                         true,
                                         true,
                                         MAV_FRAME_LOCAL_NED);
371
                    WPM->addWaypointEditable(wp);
372
                }
373 374 375
            }
            else
            {
376 377
                // MAV connected, but position unknown, add default waypoint
                updateStatusLabel(tr("WARNING: No position known. Adding default LOCAL (NED) waypoint"));
Don Gagne's avatar
Don Gagne committed
378 379
                wp = new MissionItem(NULL,
                                     0,
Don Gagne's avatar
Don Gagne committed
380
                                     QGeoCoordinate(HomePositionManager::instance()->getHomeLatitude(), HomePositionManager::instance()->getHomeLongitude(), WPM->getAltitudeRecommendation()),
Don Gagne's avatar
Don Gagne committed
381 382 383 384 385 386 387 388
                                     MAV_CMD_NAV_WAYPOINT,
                                     0,
                                     WPM->getAcceptanceRadiusRecommendation(),
                                     0,
                                     0,
                                     true,
                                     true,
                                     (MAV_FRAME)WPM->getFrameRecommendation());
389 390
                WPM->addWaypointEditable(wp);
            }
391
        }
392
        else
393
        {
394 395
            //Since no UAV available, create first default waypoint.
            updateStatusLabel(tr("No UAV connected. Adding default GLOBAL (NED) waypoint"));
Don Gagne's avatar
Don Gagne committed
396 397
            wp = new MissionItem(NULL,
                                 0,
Don Gagne's avatar
Don Gagne committed
398
                                 QGeoCoordinate(HomePositionManager::instance()->getHomeLatitude(), HomePositionManager::instance()->getHomeLongitude(), WPM->getAltitudeRecommendation()),
Don Gagne's avatar
Don Gagne committed
399 400 401 402 403 404 405 406
                                 MAV_CMD_NAV_WAYPOINT,
                                 0,
                                 WPM->getAcceptanceRadiusRecommendation(),
                                 0,
                                 0,
                                 true,
                                 true,
                                 (MAV_FRAME)WPM->getFrameRecommendation());
407
            WPM->addWaypointEditable(wp);
pixhawk's avatar
pixhawk committed
408
        }
pixhawk's avatar
pixhawk committed
409 410 411
    }
}

412 413 414 415
void WaypointList::addCurrentPositionWaypoint() {
    addEditable(true);
}

pixhawk's avatar
pixhawk committed
416 417
void WaypointList::updateStatusLabel(const QString &string)
{
418
    // Status label in write widget
pixhawk's avatar
pixhawk committed
419
    m_ui->statusLabel->setText(string);
420 421
    // Status label in read only widget
    m_ui->viewStatusLabel->setText(string);
pixhawk's avatar
pixhawk committed
422 423
}

424
// Request UASWaypointManager to send the SET_CURRENT message to UAV
425
void WaypointList::changeCurrentWaypoint(quint16 seq)
pixhawk's avatar
pixhawk committed
426
{
427 428
    if (uas)
    {
429
        WPM->setCurrentWaypoint(seq);
430
    }
pixhawk's avatar
pixhawk committed
431 432
}

433
// Request UASWaypointManager to set the new "current" and make sure all other waypoints are not "current"
434
void WaypointList::currentWaypointEditableChanged(quint16 seq)
435
{
436
        WPM->setCurrentEditable(seq);
437
        const QList<MissionItem *> waypoints = WPM->getWaypointEditableList();
438

439
        if (seq < waypoints.count())
440
        {
441
            for(int i = 0; i < waypoints.count(); i++)
442
            {
443
                WaypointEditableView* widget = wpEditableViews.value(waypoints[i], NULL);
444

445
                if (widget) {
Don Gagne's avatar
Don Gagne committed
446
                    if (waypoints[i]->sequenceNumber() == seq)
447 448 449 450 451 452 453
                    {
                        widget->setCurrent(true);
                    }
                    else
                    {
                        widget->setCurrent(false);
                    }
454 455 456 457 458
                }
            }
        }
}

459
// Update waypointViews to correctly indicate the new current waypoint
460 461
void WaypointList::currentWaypointViewOnlyChanged(quint16 seq)
{
462 463 464
    // First update the edit list
    currentWaypointEditableChanged(seq);

465
    const QList<MissionItem *> waypoints = WPM->getWaypointViewOnlyList();
466

467
    if (seq < waypoints.count())
468
    {
469
        for(int i = 0; i < waypoints.count(); i++)
470
        {
471
            WaypointViewOnlyView* widget = wpViewOnlyViews.value(waypoints[i], NULL);
472

473
            if (widget) {
Don Gagne's avatar
Don Gagne committed
474
                if (waypoints[i]->sequenceNumber() == seq)
475 476 477 478 479 480 481
                {
                    widget->setCurrent(true);
                }
                else
                {
                    widget->setCurrent(false);
                }
482 483 484 485 486
            }
        }
    }
}

487
void WaypointList::updateWaypointEditable(int uas, MissionItem* wp)
488 489
{
    Q_UNUSED(uas);
490 491 492 493 494
    WaypointEditableView *wpv = wpEditableViews.value(wp, NULL);
    if (wpv) {
        wpv->updateValues();
    }
        m_ui->tabWidget->setCurrentIndex(0); // XXX magic number
495 496
}

497
void WaypointList::updateWaypointViewOnly(int uas, MissionItem* wp)
498 499
{
    Q_UNUSED(uas);
500 501 502 503 504
    WaypointViewOnlyView *wpv = wpViewOnlyViews.value(wp, NULL);
   if (wpv) {
       wpv->updateValues();
   }
   m_ui->tabWidget->setCurrentIndex(1); // XXX magic number
505 506
}

pixhawk's avatar
pixhawk committed
507 508
void WaypointList::waypointViewOnlyListChanged()
{
509 510
    // Prevent updates to prevent visual flicker
    this->setUpdatesEnabled(false);
511
    const QList<MissionItem *> &waypoints = WPM->getWaypointViewOnlyList();
512 513

    if (!wpViewOnlyViews.empty()) {
514
        QMapIterator<MissionItem*,WaypointViewOnlyView*> viewIt(wpViewOnlyViews);
515 516 517
        viewIt.toFront();
        while(viewIt.hasNext()) {
            viewIt.next();
518
            MissionItem *cur = viewIt.key();
519
            int i;
520
            for (i = 0; i < waypoints.count(); i++) {
521 522
                if (waypoints[i] == cur) {
                    break;
pixhawk's avatar
pixhawk committed
523 524
                }
            }
525
            if (i == waypoints.count()) {
526
                WaypointViewOnlyView* widget = wpViewOnlyViews.value(cur, NULL);
527 528 529 530 531
                if (widget) {
                    widget->hide();
                    viewOnlyListLayout->removeWidget(widget);
                    wpViewOnlyViews.remove(cur);
                }
pixhawk's avatar
pixhawk committed
532
            }
533 534
        }
    }
pixhawk's avatar
pixhawk committed
535

536
    // then add/update the views for each waypoint in the list
537
    for(int i = 0; i < waypoints.count(); i++) {
538
        MissionItem *wp = waypoints[i];
539 540 541 542 543 544 545
        if (!wpViewOnlyViews.contains(wp)) {
            WaypointViewOnlyView* wpview = new WaypointViewOnlyView(wp, this);
            wpViewOnlyViews.insert(wp, wpview);
            connect(wpview, SIGNAL(changeCurrentWaypoint(quint16)), this, SLOT(changeCurrentWaypoint(quint16)));
            viewOnlyListLayout->insertWidget(i, wpview);
        }
        WaypointViewOnlyView *wpv = wpViewOnlyViews.value(wp);
pixhawk's avatar
pixhawk committed
546

547 548 549 550
        //check if ordering has changed
        if(viewOnlyListLayout->itemAt(i)->widget() != wpv) {
            viewOnlyListLayout->removeWidget(wpv);
            viewOnlyListLayout->insertWidget(i, wpv);
pixhawk's avatar
pixhawk committed
551
        }
552 553

        wpv->updateValues();    // update the values of the ui elements in the view
pixhawk's avatar
pixhawk committed
554
    }
555 556 557
    this->setUpdatesEnabled(true);
    loadFileGlobalWP = false;

558 559
    m_ui->tabWidget->setCurrentIndex(1);

pixhawk's avatar
pixhawk committed
560 561 562
}


563
void WaypointList::waypointEditableListChanged()
pixhawk's avatar
pixhawk committed
564
{
565 566
    // Prevent updates to prevent visual flicker
    this->setUpdatesEnabled(false);
567
    const QList<MissionItem *> &waypoints = WPM->getWaypointEditableList();
568 569

    if (!wpEditableViews.empty()) {
570
        QMapIterator<MissionItem*,WaypointEditableView*> viewIt(wpEditableViews);
571 572 573
        viewIt.toFront();
        while(viewIt.hasNext()) {
            viewIt.next();
574
            MissionItem *cur = viewIt.key();
575
            int i;
576
            for (i = 0; i < waypoints.count(); i++) {
577 578
                if (waypoints[i] == cur) {
                    break;
lm's avatar
lm committed
579
                }
pixhawk's avatar
pixhawk committed
580
            }
581
            if (i == waypoints.count()) {
582
                WaypointEditableView* widget = wpEditableViews.value(cur, NULL);
583 584 585 586 587 588

                if (widget) {
                    widget->hide();
                    editableListLayout->removeWidget(widget);
                    wpEditableViews.remove(cur);
                }
589
            }
590 591
        }
    }
592

593
    // then add/update the views for each waypoint in the list
594
    for(int i = 0; i < waypoints.count(); i++) {
595
        MissionItem *wp = waypoints[i];
596 597 598
        if (!wpEditableViews.contains(wp)) {
            WaypointEditableView* wpview = new WaypointEditableView(wp, this);
            wpEditableViews.insert(wp, wpview);
599 600 601
            connect(wpview, SIGNAL(moveDownWaypoint(MissionItem*)),    this, SLOT(moveDown(MissionItem*)));
            connect(wpview, SIGNAL(moveUpWaypoint(MissionItem*)),      this, SLOT(moveUp(MissionItem*)));
            connect(wpview, SIGNAL(removeWaypoint(MissionItem*)),      this, SLOT(removeWaypoint(MissionItem*)));
602 603 604 605 606
            //connect(wpview, SIGNAL(currentWaypointChanged(quint16)), this, SLOT(currentWaypointChanged(quint16)));
            connect(wpview, SIGNAL(changeCurrentWaypoint(quint16)), this, SLOT(currentWaypointEditableChanged(quint16)));
            editableListLayout->insertWidget(i, wpview);
        }
        WaypointEditableView *wpv = wpEditableViews.value(wp);
lm's avatar
lm committed
607

608 609 610 611
        //check if ordering has changed
        if(editableListLayout->itemAt(i)->widget() != wpv) {
            editableListLayout->removeWidget(wpv);
            editableListLayout->insertWidget(i, wpv);
612
        }
lm's avatar
lm committed
613

614 615 616 617
        wpv->updateValues();    // update the values of the ui elements in the view
    }
    this->setUpdatesEnabled(true);
    loadFileGlobalWP = false;
618

619
}
pixhawk's avatar
pixhawk committed
620

621
void WaypointList::moveUp(MissionItem* wp)
pixhawk's avatar
pixhawk committed
622
{
623
    const QList<MissionItem *> &waypoints = WPM->getWaypointEditableList();
624

625 626
    //get the current position of wp in the local storage
    int i;
627
    for (i = 0; i < waypoints.count(); i++) {
628 629 630
        if (waypoints[i] == wp)
            break;
    }
631

632
    // if wp was found and its not the first entry, move it
633
    if (i < waypoints.count() && i > 0) {
634
        WPM->moveWaypoint(i, i-1);
pixhawk's avatar
pixhawk committed
635 636 637
    }
}

638
void WaypointList::moveDown(MissionItem* wp)
639
{    
640
    const QList<MissionItem *> &waypoints = WPM->getWaypointEditableList();
641 642 643

    //get the current position of wp in the local storage
    int i;
644
    for (i = 0; i < waypoints.count(); i++) {
645 646 647
        if (waypoints[i] == wp)
            break;
    }
pixhawk's avatar
pixhawk committed
648

649
    // if wp was found and its not the last entry, move it
650
    if (i < waypoints.count()-1) {
651
        WPM->moveWaypoint(i, i+1);
pixhawk's avatar
pixhawk committed
652 653 654
    }
}

655
void WaypointList::removeWaypoint(MissionItem* wp)
656
{    
Don Gagne's avatar
Don Gagne committed
657
        WPM->removeWaypoint(wp->sequenceNumber());
pixhawk's avatar
pixhawk committed
658 659 660 661 662 663 664 665 666 667 668 669 670
}

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

671 672 673 674


void WaypointList::on_clearWPListButton_clicked()
{
675 676
    if (uas) {
        emit clearPathclicked();
677
        const QList<MissionItem *> &waypoints = WPM->getWaypointEditableList();
678
        while(!waypoints.isEmpty()) {
679
            WaypointEditableView* widget = wpEditableViews.value(waypoints[0], NULL);
680 681 682
            if (widget) {
                widget->remove();
            }
683
        }
lm's avatar
lm committed
684
    }
685 686
}

687
void WaypointList::clearWPWidget()
688
{    
689
        // Get list
690
        const QList<MissionItem *> waypoints = WPM->getWaypointEditableList();
691 692 693 694 695


        // XXX delete wps as well

        // Clear UI elements
696
        while(!waypoints.isEmpty()) {
697
            WaypointEditableView* widget = wpEditableViews.value(waypoints[0], NULL);
698 699 700
            if (widget) {
                widget->remove();
            }
701
        }
702
}