WaypointList.cc 21.8 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 36
 *
 */

#include "WaypointList.h"
#include "ui_WaypointList.h"
#include <UASInterface.h>
37
#include <UAS.h>
pixhawk's avatar
pixhawk committed
38 39 40
#include <UASManager.h>
#include <QDebug>
#include <QFileDialog>
41
#include <QMessageBox>
42
#include <QMouseEvent>
pixhawk's avatar
pixhawk committed
43

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

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

pixhawk's avatar
pixhawk committed
58 59 60 61 62 63 64
    //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
65 66 67 68

    // 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()));
69
    connect(m_ui->actionAddWaypoint, SIGNAL(triggered()), this, SLOT(addEditable()));
pixhawk's avatar
pixhawk committed
70

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

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

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

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

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

87
    //connect(UASManager::instance(), SIGNAL(activeUASSet(UASInterface*)), this, SLOT(setUAS(UASInterface*)));
pixhawk's avatar
pixhawk committed
88

pixhawk's avatar
pixhawk committed
89 90 91 92 93 94 95 96 97 98
    //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

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

101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116
    if (WPM) {
        // SET UAS AFTER ALL SIGNALS/SLOTS ARE CONNECTED
        if (!WPM->getUAS())
        {
            // Hide buttons, which don't make sense without valid UAS
            m_ui->positionAddButton->hide();
            m_ui->transmitButton->hide();
            m_ui->readButton->hide();
            m_ui->refreshButton->hide();
            //FIXME: The whole "Onboard Waypoints"-tab should be hidden, instead of "refresh" button
            UnconnectedUASInfoWidget* inf = new UnconnectedUASInfoWidget(this);
            viewOnlyListLayout->insertWidget(0, inf); //insert a "NO UAV" info into the Onboard Tab
            showOfflineWarning = true;
        } else {
            setUAS(static_cast<UASInterface*>(WPM->getUAS()));
        }
117

118 119 120 121 122 123 124
        /* connect slots */
        connect(WPM, SIGNAL(updateStatusString(const QString &)),        this, SLOT(updateStatusLabel(const QString &)));
        connect(WPM, SIGNAL(waypointEditableListChanged(void)),                  this, SLOT(waypointEditableListChanged(void)));
        connect(WPM, SIGNAL(waypointEditableChanged(int,Waypoint*)), this, SLOT(updateWaypointEditable(int,Waypoint*)));
        connect(WPM, SIGNAL(waypointViewOnlyListChanged(void)),                  this, SLOT(waypointViewOnlyListChanged(void)));
        connect(WPM, SIGNAL(waypointViewOnlyChanged(int,Waypoint*)), this, SLOT(updateWaypointViewOnly(int,Waypoint*)));
        connect(WPM, SIGNAL(currentWaypointChanged(quint16)),            this, SLOT(currentWaypointViewOnlyChanged(quint16)));
125 126 127 128 129

        //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();
130
    }
131 132 133

    // STATUS LABEL
    updateStatusLabel("");
134 135

    this->setVisible(false);
136
    loadFileGlobalWP = false;
137
    readGlobalWP = false;
138 139 140
    centerMapCoordinate.setX(0.0);
    centerMapCoordinate.setY(0.0);

pixhawk's avatar
pixhawk committed
141 142 143 144 145 146 147
}

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

148
void WaypointList::updatePosition(UASInterface* uas, double x, double y, double z, quint64 usec)
149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165
{
    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
166 167
void WaypointList::setUAS(UASInterface* uas)
{
168 169 170 171
    if (!uas)
        return;

    if (this->uas != NULL)
lm's avatar
lm committed
172
    {
173 174 175 176 177 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)));
        disconnect(WPM, SIGNAL(waypointEditableChanged(int,Waypoint*)), this, SLOT(updateWaypointEditable(int,Waypoint*)));
        disconnect(WPM, SIGNAL(waypointViewOnlyListChanged(void)),                  this, SLOT(waypointViewOnlyListChanged(void)));
        disconnect(WPM, SIGNAL(waypointViewOnlyChanged(int,Waypoint*)), this, SLOT(updateWaypointViewOnly(int,Waypoint*)));
        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
184
    }
185

186 187 188 189 190 191 192 193 194 195 196 197 198 199
    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)));
    connect(WPM, SIGNAL(waypointEditableChanged(int,Waypoint*)), this, SLOT(updateWaypointEditable(int,Waypoint*)));
    connect(WPM, SIGNAL(waypointViewOnlyListChanged(void)),                  this, SLOT(waypointViewOnlyListChanged(void)));
    connect(WPM, SIGNAL(waypointViewOnlyChanged(int,Waypoint*)), this, SLOT(updateWaypointViewOnly(int,Waypoint*)));
    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)));

200 201
    // Update list
    read();
pixhawk's avatar
pixhawk committed
202 203
}

pixhawk's avatar
pixhawk committed
204 205
void WaypointList::saveWaypoints()
{
206

207 208
    QString fileName = QFileDialog::getSaveFileName(this, tr("Save File"), "./waypoints.txt", tr("Waypoint File (*.txt)"));
    WPM->saveWaypoints(fileName);
209

pixhawk's avatar
pixhawk committed
210 211 212 213
}

void WaypointList::loadWaypoints()
{
214 215
    QString fileName = QFileDialog::getOpenFileName(this, tr("Load File"), ".", tr("Waypoint File (*.txt)"));
    WPM->loadWaypoints(fileName);
pixhawk's avatar
pixhawk committed
216 217 218 219
}

void WaypointList::transmit()
{
lm's avatar
lm committed
220 221
    if (uas)
    {
222
        WPM->writeWaypoints();
pixhawk's avatar
pixhawk committed
223 224 225 226 227
    }
}

void WaypointList::read()
{
lm's avatar
lm committed
228 229
    if (uas)
    {
230
        WPM->readWaypoints(true);
pixhawk's avatar
pixhawk committed
231 232 233 234 235 236 237
    }
}

void WaypointList::refresh()
{
    if (uas)
    {
238
        WPM->readWaypoints(false);
pixhawk's avatar
pixhawk committed
239 240 241
    }
}

pixhawk's avatar
pixhawk committed
242
void WaypointList::addEditable()
pixhawk's avatar
pixhawk committed
243
{
244 245
    addEditable(false);
}
pixhawk's avatar
pixhawk committed
246

247 248 249
void WaypointList::addEditable(bool onCurrentPosition)
{

250
    const QList<Waypoint *> &waypoints = WPM->getWaypointEditableList();
251
    Waypoint *wp;
252
    if (waypoints.count() > 0 &&
253 254 255
            !(onCurrentPosition && uas && (uas->localPositionKnown() || uas->globalPositionKnown())))
    {
        // Create waypoint with last frame
256
        Waypoint *last = waypoints.last();
257
        wp = WPM->createWaypoint();
258
//        wp->blockSignals(true);
259 260 261
        MAV_FRAME frame = (MAV_FRAME)last->getFrame();
        wp->setFrame(frame);
        if (frame == MAV_FRAME_GLOBAL || frame == MAV_FRAME_GLOBAL_RELATIVE_ALT)
262
        {
263 264 265 266 267 268 269
            wp->setLatitude(last->getLatitude());
            wp->setLongitude(last->getLongitude());
            wp->setAltitude(last->getAltitude());
        } else {
            wp->setX(last->getX());
            wp->setY(last->getY());
            wp->setZ(last->getZ());
270
        }
271 272 273 274 275
        wp->setParam1(last->getParam1());
        wp->setParam1(last->getParam2());
        wp->setParam1(last->getParam3());
        wp->setParam1(last->getParam4());
        wp->setAutocontinue(last->getAutoContinue());
276
//        wp->blockSignals(false);
277 278 279 280 281 282 283 284 285
        wp->setAction(last->getAction());
//        WPM->addWaypointEditable(wp);
    }
    else
    {
        if (uas)
        {
            // Create first waypoint at current MAV position
            if (uas->globalPositionKnown())
286
            {
287
                if (onCurrentPosition)
288
                {
289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308

                    if (WPM->getFrameRecommendation() == MAV_FRAME_GLOBAL) {
                        updateStatusLabel(tr("Added default GLOBAL (Abs alt.) waypoint."));
                    } else {
                        updateStatusLabel(tr("Added default GLOBAL (Relative alt.) waypoint."));
                    }
                    wp = new Waypoint(0, uas->getLatitude(), uas->getLongitude(), uas->getAltitude(), 0, WPM->getAcceptanceRadiusRecommendation(), 0, 0,true, true, (MAV_FRAME)WPM->getFrameRecommendation(), MAV_CMD_NAV_WAYPOINT);
                    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."));
                    }
                    wp = new Waypoint(0, UASManager::instance()->getHomeLatitude(),
                                      UASManager::instance()->getHomeLongitude(),
                                      WPM->getAltitudeRecommendation(), 0, WPM->getAcceptanceRadiusRecommendation(), 0, 0,true, true, (MAV_FRAME)WPM->getFrameRecommendation(), MAV_CMD_NAV_WAYPOINT);
                    WPM->addWaypointEditable(wp);
309
                }
310 311 312 313
            }
            else if (uas->localPositionKnown())
            {
                if (onCurrentPosition)
314
                {
315 316 317 318 319 320 321
                    updateStatusLabel(tr("Added default LOCAL (NED) waypoint."));
                    wp = new Waypoint(0, uas->getLocalX(), uas->getLocalY(), uas->getLocalZ(), 0, WPM->getAcceptanceRadiusRecommendation(), 0, 0,true, true, MAV_FRAME_LOCAL_NED, MAV_CMD_NAV_WAYPOINT);
                    WPM->addWaypointEditable(wp);
                } else {
                    updateStatusLabel(tr("Added default LOCAL (NED) waypoint."));
                    wp = new Waypoint(0, 0, 0, -0.50, 0, WPM->getAcceptanceRadiusRecommendation(), 0, 0,true, true, MAV_FRAME_LOCAL_NED, MAV_CMD_NAV_WAYPOINT);
                    WPM->addWaypointEditable(wp);
322
                }
323 324 325
            }
            else
            {
326 327 328 329 330
                // MAV connected, but position unknown, add default waypoint
                updateStatusLabel(tr("WARNING: No position known. Adding default LOCAL (NED) waypoint"));
                wp = new Waypoint(0, UASManager::instance()->getHomeLatitude(),
                                  UASManager::instance()->getHomeLongitude(),
                                  WPM->getAltitudeRecommendation(), 0, WPM->getAcceptanceRadiusRecommendation(), 0, 0,true, true, (MAV_FRAME)WPM->getFrameRecommendation(), MAV_CMD_NAV_WAYPOINT);
331 332
                WPM->addWaypointEditable(wp);
            }
333
        }
334
        else
335
        {
336 337 338 339 340
            //Since no UAV available, create first default waypoint.
            updateStatusLabel(tr("No UAV connected. Adding default GLOBAL (NED) waypoint"));
            wp = new Waypoint(0, UASManager::instance()->getHomeLatitude(),
                              UASManager::instance()->getHomeLongitude(),
                              WPM->getAltitudeRecommendation(), 0, WPM->getAcceptanceRadiusRecommendation(), 0, 0,true, true, (MAV_FRAME)WPM->getFrameRecommendation(), MAV_CMD_NAV_WAYPOINT);
341
            WPM->addWaypointEditable(wp);
pixhawk's avatar
pixhawk committed
342
        }
pixhawk's avatar
pixhawk committed
343 344 345
    }
}

346 347 348 349
void WaypointList::addCurrentPositionWaypoint() {
    addEditable(true);
}

pixhawk's avatar
pixhawk committed
350 351
void WaypointList::updateStatusLabel(const QString &string)
{
352
    // Status label in write widget
pixhawk's avatar
pixhawk committed
353
    m_ui->statusLabel->setText(string);
354 355
    // Status label in read only widget
    m_ui->viewStatusLabel->setText(string);
pixhawk's avatar
pixhawk committed
356 357
}

358
// Request UASWaypointManager to send the SET_CURRENT message to UAV
359
void WaypointList::changeCurrentWaypoint(quint16 seq)
pixhawk's avatar
pixhawk committed
360
{
361 362
    if (uas)
    {
363
        WPM->setCurrentWaypoint(seq);
364
    }
pixhawk's avatar
pixhawk committed
365 366
}

367
// Request UASWaypointManager to set the new "current" and make sure all other waypoints are not "current"
368
void WaypointList::currentWaypointEditableChanged(quint16 seq)
369
{
370
        WPM->setCurrentEditable(seq);
371
        const QList<Waypoint *> &waypoints = WPM->getWaypointEditableList();
372

373
        if (seq < waypoints.count())
374
        {
375
            for(int i = 0; i < waypoints.count(); i++)
376
            {
pixhawk's avatar
pixhawk committed
377
                WaypointEditableView* widget = wpEditableViews.find(waypoints[i]).value();
378

379 380
                if (waypoints[i]->getId() == seq)
                {
381
                    widget->setCurrent(true);
382 383 384
                }
                else
                {
385 386 387 388 389 390
                    widget->setCurrent(false);
                }
            }
        }
}

391
// Update waypointViews to correctly indicate the new current waypoint
392 393
void WaypointList::currentWaypointViewOnlyChanged(quint16 seq)
{
394
    const QList<Waypoint *> &waypoints = WPM->getWaypointViewOnlyList();
395

396
    if (seq < waypoints.count())
397
    {
398
        for(int i = 0; i < waypoints.count(); i++)
399
        {
400
            WaypointViewOnlyView* widget = wpViewOnlyViews.find(waypoints[i]).value();
401

402 403 404 405 406 407 408
            if (waypoints[i]->getId() == seq)
            {
                widget->setCurrent(true);
            }
            else
            {
                widget->setCurrent(false);
409 410 411 412 413 414
            }
        }
    }
}

void WaypointList::updateWaypointEditable(int uas, Waypoint* wp)
415 416
{
    Q_UNUSED(uas);
pixhawk's avatar
pixhawk committed
417
    WaypointEditableView *wpv = wpEditableViews.value(wp);
418 419 420
    wpv->updateValues();
}

421 422 423 424 425 426 427
void WaypointList::updateWaypointViewOnly(int uas, Waypoint* wp)
{
    Q_UNUSED(uas);
    WaypointViewOnlyView *wpv = wpViewOnlyViews.value(wp);
    wpv->updateValues();
}

pixhawk's avatar
pixhawk committed
428 429
void WaypointList::waypointViewOnlyListChanged()
{
430 431
    // Prevent updates to prevent visual flicker
    this->setUpdatesEnabled(false);
432
    const QList<Waypoint *> &waypoints = WPM->getWaypointViewOnlyList();
433 434 435 436 437 438 439 440

    if (!wpViewOnlyViews.empty()) {
        QMapIterator<Waypoint*,WaypointViewOnlyView*> viewIt(wpViewOnlyViews);
        viewIt.toFront();
        while(viewIt.hasNext()) {
            viewIt.next();
            Waypoint *cur = viewIt.key();
            int i;
441
            for (i = 0; i < waypoints.count(); i++) {
442 443
                if (waypoints[i] == cur) {
                    break;
pixhawk's avatar
pixhawk committed
444 445
                }
            }
446
            if (i == waypoints.count()) {
447 448 449 450
                WaypointViewOnlyView* widget = wpViewOnlyViews.find(cur).value();
                widget->hide();
                viewOnlyListLayout->removeWidget(widget);
                wpViewOnlyViews.remove(cur);
pixhawk's avatar
pixhawk committed
451
            }
452 453
        }
    }
pixhawk's avatar
pixhawk committed
454

455
    // then add/update the views for each waypoint in the list
456
    for(int i = 0; i < waypoints.count(); i++) {
457 458 459 460 461 462 463 464
        Waypoint *wp = waypoints[i];
        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
465

466 467 468 469
        //check if ordering has changed
        if(viewOnlyListLayout->itemAt(i)->widget() != wpv) {
            viewOnlyListLayout->removeWidget(wpv);
            viewOnlyListLayout->insertWidget(i, wpv);
pixhawk's avatar
pixhawk committed
470
        }
471 472

        wpv->updateValues();    // update the values of the ui elements in the view
pixhawk's avatar
pixhawk committed
473
    }
474 475 476
    this->setUpdatesEnabled(true);
    loadFileGlobalWP = false;

pixhawk's avatar
pixhawk committed
477 478 479
}


480
void WaypointList::waypointEditableListChanged()
pixhawk's avatar
pixhawk committed
481
{
482 483
    // Prevent updates to prevent visual flicker
    this->setUpdatesEnabled(false);
484
    const QList<Waypoint *> &waypoints = WPM->getWaypointEditableList();
485 486 487 488 489 490 491 492

    if (!wpEditableViews.empty()) {
        QMapIterator<Waypoint*,WaypointEditableView*> viewIt(wpEditableViews);
        viewIt.toFront();
        while(viewIt.hasNext()) {
            viewIt.next();
            Waypoint *cur = viewIt.key();
            int i;
493
            for (i = 0; i < waypoints.count(); i++) {
494 495
                if (waypoints[i] == cur) {
                    break;
lm's avatar
lm committed
496
                }
pixhawk's avatar
pixhawk committed
497
            }
498
            if (i == waypoints.count()) {
499 500 501 502
                WaypointEditableView* widget = wpEditableViews.find(cur).value();
                widget->hide();
                editableListLayout->removeWidget(widget);
                wpEditableViews.remove(cur);
503
            }
504 505
        }
    }
506

507
    // then add/update the views for each waypoint in the list
508
    for(int i = 0; i < waypoints.count(); i++) {
509 510 511 512 513 514 515 516 517 518 519 520
        Waypoint *wp = waypoints[i];
        if (!wpEditableViews.contains(wp)) {
            WaypointEditableView* wpview = new WaypointEditableView(wp, this);
            wpEditableViews.insert(wp, wpview);
            connect(wpview, SIGNAL(moveDownWaypoint(Waypoint*)),    this, SLOT(moveDown(Waypoint*)));
            connect(wpview, SIGNAL(moveUpWaypoint(Waypoint*)),      this, SLOT(moveUp(Waypoint*)));
            connect(wpview, SIGNAL(removeWaypoint(Waypoint*)),      this, SLOT(removeWaypoint(Waypoint*)));
            //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
521

522 523 524 525
        //check if ordering has changed
        if(editableListLayout->itemAt(i)->widget() != wpv) {
            editableListLayout->removeWidget(wpv);
            editableListLayout->insertWidget(i, wpv);
526
        }
lm's avatar
lm committed
527

528 529 530 531
        wpv->updateValues();    // update the values of the ui elements in the view
    }
    this->setUpdatesEnabled(true);
    loadFileGlobalWP = false;
532

pixhawk's avatar
pixhawk committed
533

534
}
pixhawk's avatar
pixhawk committed
535

536
void WaypointList::moveUp(Waypoint* wp)
pixhawk's avatar
pixhawk committed
537
{
538
    const QList<Waypoint *> &waypoints = WPM->getWaypointEditableList();
539

540 541
    //get the current position of wp in the local storage
    int i;
542
    for (i = 0; i < waypoints.count(); i++) {
543 544 545
        if (waypoints[i] == wp)
            break;
    }
546

547
    // if wp was found and its not the first entry, move it
548
    if (i < waypoints.count() && i > 0) {
549
        WPM->moveWaypoint(i, i-1);
pixhawk's avatar
pixhawk committed
550 551 552
    }
}

553
void WaypointList::moveDown(Waypoint* wp)
554
{    
555
    const QList<Waypoint *> &waypoints = WPM->getWaypointEditableList();
556 557 558

    //get the current position of wp in the local storage
    int i;
559
    for (i = 0; i < waypoints.count(); i++) {
560 561 562
        if (waypoints[i] == wp)
            break;
    }
pixhawk's avatar
pixhawk committed
563

564
    // if wp was found and its not the last entry, move it
565
    if (i < waypoints.count()-1) {
566
        WPM->moveWaypoint(i, i+1);
pixhawk's avatar
pixhawk committed
567 568 569 570
    }
}

void WaypointList::removeWaypoint(Waypoint* wp)
571 572
{    
        WPM->removeWaypoint(wp->getId());
pixhawk's avatar
pixhawk committed
573 574 575 576 577 578 579 580 581 582 583 584 585
}

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

586 587 588 589


void WaypointList::on_clearWPListButton_clicked()
{
590 591
    if (uas) {
        emit clearPathclicked();
592 593
        const QList<Waypoint *> &waypoints = WPM->getWaypointEditableList();
        while(!waypoints.isEmpty()) {
pixhawk's avatar
pixhawk committed
594
            WaypointEditableView* widget = wpEditableViews.find(waypoints[0]).value();
595
            widget->remove();
596
        }
lm's avatar
lm committed
597
    }
598 599
}

600
void WaypointList::clearWPWidget()
601
{    
602
        // Get list
603
        const QList<Waypoint *> &waypoints = WPM->getWaypointEditableList();
604 605 606 607 608


        // XXX delete wps as well

        // Clear UI elements
609
        while(!waypoints.isEmpty()) {
pixhawk's avatar
pixhawk committed
610
            WaypointEditableView* widget = wpEditableViews.find(waypoints[0]).value();
611
            widget->remove();
612
        }
613
}