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

#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>
pixhawk's avatar
pixhawk committed
40 41
#include <UASManager.h>
#include <QDebug>
42
#include <QMessageBox>
43
#include <QMouseEvent>
44
#include <QTextEdit>
pixhawk's avatar
pixhawk committed
45

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

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

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

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

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

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

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

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

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

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

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

101
    connect(m_ui->refreshButton, SIGNAL(clicked()), this, SLOT(refresh()));
102

103 104 105 106
    if (WPM) {
        // SET UAS AFTER ALL SIGNALS/SLOTS ARE CONNECTED
        if (!WPM->getUAS())
        {
107 108 109 110 111 112
            // 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);

113
            //FIXME: The whole "Onboard Waypoints"-tab should be hidden, instead of "refresh" button
114 115 116 117 118 119 120 121
            // 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);

122 123 124 125
            showOfflineWarning = true;
        } else {
            setUAS(static_cast<UASInterface*>(WPM->getUAS()));
        }
126

127 128 129 130 131 132 133
        /* 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)));
134 135 136 137 138

        //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();
139
    }
140 141 142

    // STATUS LABEL
    updateStatusLabel("");
143 144

    this->setVisible(false);
145
    loadFileGlobalWP = false;
146
    readGlobalWP = false;
147 148 149
    centerMapCoordinate.setX(0.0);
    centerMapCoordinate.setY(0.0);

pixhawk's avatar
pixhawk committed
150 151 152 153 154 155 156
}

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

157
void WaypointList::updatePosition(UASInterface* uas, double x, double y, double z, quint64 usec)
158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174
{
    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
175 176
void WaypointList::setUAS(UASInterface* uas)
{
177 178 179 180
    if (!uas)
        return;

    if (this->uas != NULL)
lm's avatar
lm committed
181
    {
182 183 184 185 186 187 188 189 190 191 192
        // 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
193
    }
194

195 196 197 198 199 200
    // 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);

201 202 203 204 205 206 207 208 209 210 211 212 213 214
    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)));

215 216
    // Update list
    read();
pixhawk's avatar
pixhawk committed
217 218
}

pixhawk's avatar
pixhawk committed
219 220
void WaypointList::saveWaypoints()
{
221

Don Gagne's avatar
Don Gagne committed
222
    QString fileName = QGCFileDialog::getSaveFileName(this, tr("Save File"), "./waypoints.txt", tr("Waypoint File (*.txt)"));
223
    WPM->saveWaypoints(fileName);
224

pixhawk's avatar
pixhawk committed
225 226 227 228
}

void WaypointList::loadWaypoints()
{
Don Gagne's avatar
Don Gagne committed
229
    QString fileName = QGCFileDialog::getOpenFileName(this, tr("Load File"), ".", tr("Waypoint File (*.txt)"));
230
    WPM->loadWaypoints(fileName);
pixhawk's avatar
pixhawk committed
231 232 233 234
}

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

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

void WaypointList::refresh()
{
    if (uas)
    {
253
        WPM->readWaypoints(false);
pixhawk's avatar
pixhawk committed
254 255 256
    }
}

pixhawk's avatar
pixhawk committed
257
void WaypointList::addEditable()
pixhawk's avatar
pixhawk committed
258
{
259 260
    addEditable(false);
}
pixhawk's avatar
pixhawk committed
261

262 263 264
void WaypointList::addEditable(bool onCurrentPosition)
{

265
    const QList<Waypoint *> &waypoints = WPM->getWaypointEditableList();
266
    Waypoint *wp;
267
    if (waypoints.count() > 0 &&
268 269 270
            !(onCurrentPosition && uas && (uas->localPositionKnown() || uas->globalPositionKnown())))
    {
        // Create waypoint with last frame
271
        Waypoint *last = waypoints.last();
272
        wp = WPM->createWaypoint();
273
//        wp->blockSignals(true);
274 275 276
        MAV_FRAME frame = (MAV_FRAME)last->getFrame();
        wp->setFrame(frame);
        if (frame == MAV_FRAME_GLOBAL || frame == MAV_FRAME_GLOBAL_RELATIVE_ALT)
277
        {
278 279 280 281 282 283 284
            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());
285
        }
286
        wp->setParam1(last->getParam1());
287 288 289
        wp->setParam2(last->getParam2());
        wp->setParam3(last->getParam3());
        wp->setParam4(last->getParam4());
290
        wp->setAutocontinue(last->getAutoContinue());
291
//        wp->blockSignals(false);
292 293 294 295 296 297 298 299 300
        wp->setAction(last->getAction());
//        WPM->addWaypointEditable(wp);
    }
    else
    {
        if (uas)
        {
            // Create first waypoint at current MAV position
            if (uas->globalPositionKnown())
301
            {
302
                if (onCurrentPosition)
303
                {
304 305 306 307 308 309

                    if (WPM->getFrameRecommendation() == MAV_FRAME_GLOBAL) {
                        updateStatusLabel(tr("Added default GLOBAL (Abs alt.) waypoint."));
                    } else {
                        updateStatusLabel(tr("Added default GLOBAL (Relative alt.) waypoint."));
                    }
310
                    wp = new Waypoint(0, uas->getLatitude(), uas->getLongitude(), uas->getAltitudeAMSL(), 0, WPM->getAcceptanceRadiusRecommendation(), 0, 0,true, true, (MAV_FRAME)WPM->getFrameRecommendation(), MAV_CMD_NAV_WAYPOINT);
311 312 313 314 315 316 317 318 319 320 321 322 323
                    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);
324
                }
325 326 327 328
            }
            else if (uas->localPositionKnown())
            {
                if (onCurrentPosition)
329
                {
330 331 332 333 334 335 336
                    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);
337
                }
338 339 340
            }
            else
            {
341 342 343 344 345
                // 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);
346 347
                WPM->addWaypointEditable(wp);
            }
348
        }
349
        else
350
        {
351 352 353 354 355
            //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);
356
            WPM->addWaypointEditable(wp);
pixhawk's avatar
pixhawk committed
357
        }
pixhawk's avatar
pixhawk committed
358 359 360
    }
}

361 362 363 364
void WaypointList::addCurrentPositionWaypoint() {
    addEditable(true);
}

pixhawk's avatar
pixhawk committed
365 366
void WaypointList::updateStatusLabel(const QString &string)
{
367
    // Status label in write widget
pixhawk's avatar
pixhawk committed
368
    m_ui->statusLabel->setText(string);
369 370
    // Status label in read only widget
    m_ui->viewStatusLabel->setText(string);
pixhawk's avatar
pixhawk committed
371 372
}

373
// Request UASWaypointManager to send the SET_CURRENT message to UAV
374
void WaypointList::changeCurrentWaypoint(quint16 seq)
pixhawk's avatar
pixhawk committed
375
{
376 377
    if (uas)
    {
378
        WPM->setCurrentWaypoint(seq);
379
    }
pixhawk's avatar
pixhawk committed
380 381
}

382
// Request UASWaypointManager to set the new "current" and make sure all other waypoints are not "current"
383
void WaypointList::currentWaypointEditableChanged(quint16 seq)
384
{
385
        WPM->setCurrentEditable(seq);
386
        const QList<Waypoint *> waypoints = WPM->getWaypointEditableList();
387

388
        if (seq < waypoints.count())
389
        {
390
            for(int i = 0; i < waypoints.count(); i++)
391
            {
392
                WaypointEditableView* widget = wpEditableViews.value(waypoints[i], NULL);
393

394 395 396 397 398 399 400 401 402
                if (widget) {
                    if (waypoints[i]->getId() == seq)
                    {
                        widget->setCurrent(true);
                    }
                    else
                    {
                        widget->setCurrent(false);
                    }
403 404 405 406 407
                }
            }
        }
}

408
// Update waypointViews to correctly indicate the new current waypoint
409 410
void WaypointList::currentWaypointViewOnlyChanged(quint16 seq)
{
411 412 413
    // First update the edit list
    currentWaypointEditableChanged(seq);

414
    const QList<Waypoint *> waypoints = WPM->getWaypointViewOnlyList();
415

416
    if (seq < waypoints.count())
417
    {
418
        for(int i = 0; i < waypoints.count(); i++)
419
        {
420
            WaypointViewOnlyView* widget = wpViewOnlyViews.value(waypoints[i], NULL);
421

422 423 424 425 426 427 428 429 430
            if (widget) {
                if (waypoints[i]->getId() == seq)
                {
                    widget->setCurrent(true);
                }
                else
                {
                    widget->setCurrent(false);
                }
431 432 433 434 435 436
            }
        }
    }
}

void WaypointList::updateWaypointEditable(int uas, Waypoint* wp)
437 438
{
    Q_UNUSED(uas);
439 440 441 442 443
    WaypointEditableView *wpv = wpEditableViews.value(wp, NULL);
    if (wpv) {
        wpv->updateValues();
    }
        m_ui->tabWidget->setCurrentIndex(0); // XXX magic number
444 445
}

446 447 448
void WaypointList::updateWaypointViewOnly(int uas, Waypoint* wp)
{
    Q_UNUSED(uas);
449 450 451 452 453
    WaypointViewOnlyView *wpv = wpViewOnlyViews.value(wp, NULL);
   if (wpv) {
       wpv->updateValues();
   }
   m_ui->tabWidget->setCurrentIndex(1); // XXX magic number
454 455
}

pixhawk's avatar
pixhawk committed
456 457
void WaypointList::waypointViewOnlyListChanged()
{
458 459
    // Prevent updates to prevent visual flicker
    this->setUpdatesEnabled(false);
460
    const QList<Waypoint *> &waypoints = WPM->getWaypointViewOnlyList();
461 462 463 464 465 466 467 468

    if (!wpViewOnlyViews.empty()) {
        QMapIterator<Waypoint*,WaypointViewOnlyView*> viewIt(wpViewOnlyViews);
        viewIt.toFront();
        while(viewIt.hasNext()) {
            viewIt.next();
            Waypoint *cur = viewIt.key();
            int i;
469
            for (i = 0; i < waypoints.count(); i++) {
470 471
                if (waypoints[i] == cur) {
                    break;
pixhawk's avatar
pixhawk committed
472 473
                }
            }
474
            if (i == waypoints.count()) {
475
                WaypointViewOnlyView* widget = wpViewOnlyViews.value(cur, NULL);
476 477 478 479 480
                if (widget) {
                    widget->hide();
                    viewOnlyListLayout->removeWidget(widget);
                    wpViewOnlyViews.remove(cur);
                }
pixhawk's avatar
pixhawk committed
481
            }
482 483
        }
    }
pixhawk's avatar
pixhawk committed
484

485
    // then add/update the views for each waypoint in the list
486
    for(int i = 0; i < waypoints.count(); i++) {
487 488 489 490 491 492 493 494
        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
495

496 497 498 499
        //check if ordering has changed
        if(viewOnlyListLayout->itemAt(i)->widget() != wpv) {
            viewOnlyListLayout->removeWidget(wpv);
            viewOnlyListLayout->insertWidget(i, wpv);
pixhawk's avatar
pixhawk committed
500
        }
501 502

        wpv->updateValues();    // update the values of the ui elements in the view
pixhawk's avatar
pixhawk committed
503
    }
504 505 506
    this->setUpdatesEnabled(true);
    loadFileGlobalWP = false;

507 508
    m_ui->tabWidget->setCurrentIndex(1);

pixhawk's avatar
pixhawk committed
509 510 511
}


512
void WaypointList::waypointEditableListChanged()
pixhawk's avatar
pixhawk committed
513
{
514 515
    // Prevent updates to prevent visual flicker
    this->setUpdatesEnabled(false);
516
    const QList<Waypoint *> &waypoints = WPM->getWaypointEditableList();
517 518 519 520 521 522 523 524

    if (!wpEditableViews.empty()) {
        QMapIterator<Waypoint*,WaypointEditableView*> viewIt(wpEditableViews);
        viewIt.toFront();
        while(viewIt.hasNext()) {
            viewIt.next();
            Waypoint *cur = viewIt.key();
            int i;
525
            for (i = 0; i < waypoints.count(); i++) {
526 527
                if (waypoints[i] == cur) {
                    break;
lm's avatar
lm committed
528
                }
pixhawk's avatar
pixhawk committed
529
            }
530
            if (i == waypoints.count()) {
531
                WaypointEditableView* widget = wpEditableViews.value(cur, NULL);
532 533 534 535 536 537

                if (widget) {
                    widget->hide();
                    editableListLayout->removeWidget(widget);
                    wpEditableViews.remove(cur);
                }
538
            }
539 540
        }
    }
541

542
    // then add/update the views for each waypoint in the list
543
    for(int i = 0; i < waypoints.count(); i++) {
544 545 546 547 548 549 550 551 552 553 554 555
        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
556

557 558 559 560
        //check if ordering has changed
        if(editableListLayout->itemAt(i)->widget() != wpv) {
            editableListLayout->removeWidget(wpv);
            editableListLayout->insertWidget(i, wpv);
561
        }
lm's avatar
lm committed
562

563 564 565 566
        wpv->updateValues();    // update the values of the ui elements in the view
    }
    this->setUpdatesEnabled(true);
    loadFileGlobalWP = false;
567

568
}
pixhawk's avatar
pixhawk committed
569

570
void WaypointList::moveUp(Waypoint* wp)
pixhawk's avatar
pixhawk committed
571
{
572
    const QList<Waypoint *> &waypoints = WPM->getWaypointEditableList();
573

574 575
    //get the current position of wp in the local storage
    int i;
576
    for (i = 0; i < waypoints.count(); i++) {
577 578 579
        if (waypoints[i] == wp)
            break;
    }
580

581
    // if wp was found and its not the first entry, move it
582
    if (i < waypoints.count() && i > 0) {
583
        WPM->moveWaypoint(i, i-1);
pixhawk's avatar
pixhawk committed
584 585 586
    }
}

587
void WaypointList::moveDown(Waypoint* wp)
588
{    
589
    const QList<Waypoint *> &waypoints = WPM->getWaypointEditableList();
590 591 592

    //get the current position of wp in the local storage
    int i;
593
    for (i = 0; i < waypoints.count(); i++) {
594 595 596
        if (waypoints[i] == wp)
            break;
    }
pixhawk's avatar
pixhawk committed
597

598
    // if wp was found and its not the last entry, move it
599
    if (i < waypoints.count()-1) {
600
        WPM->moveWaypoint(i, i+1);
pixhawk's avatar
pixhawk committed
601 602 603 604
    }
}

void WaypointList::removeWaypoint(Waypoint* wp)
605 606
{    
        WPM->removeWaypoint(wp->getId());
pixhawk's avatar
pixhawk committed
607 608 609 610 611 612 613 614 615 616 617 618 619
}

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

620 621 622 623


void WaypointList::on_clearWPListButton_clicked()
{
624 625
    if (uas) {
        emit clearPathclicked();
626 627
        const QList<Waypoint *> &waypoints = WPM->getWaypointEditableList();
        while(!waypoints.isEmpty()) {
628
            WaypointEditableView* widget = wpEditableViews.value(waypoints[0], NULL);
629 630 631
            if (widget) {
                widget->remove();
            }
632
        }
lm's avatar
lm committed
633
    }
634 635
}

636
void WaypointList::clearWPWidget()
637
{    
638
        // Get list
639
        const QList<Waypoint *> waypoints = WPM->getWaypointEditableList();
640 641 642 643 644


        // XXX delete wps as well

        // Clear UI elements
645
        while(!waypoints.isEmpty()) {
646
            WaypointEditableView* widget = wpEditableViews.value(waypoints[0], NULL);
647 648 649
            if (widget) {
                widget->remove();
            }
650
        }
651
}