WaypointList.cc 21.2 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 37 38 39
 *
 */

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

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

pixhawk's avatar
pixhawk committed
54 55
    m_ui->setupUi(this);

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

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

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

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

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

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

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

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

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

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


pixhawk's avatar
pixhawk committed
100
    // SET UAS AFTER ALL SIGNALS/SLOTS ARE CONNECTED
101 102
    if (uas)
    {
103 104 105 106 107 108 109 110 111
        WPM = uas->getWaypointManager();
    }
    else
    {
        // 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();
112
        //FIXME: The whole "Onboard Waypoints"-tab should be hidden, instead of "refresh" button       
113
        UnconnectedUASInfoWidget* inf = new UnconnectedUASInfoWidget(this);
114 115
        viewOnlyListLayout->insertWidget(0, inf); //insert a "NO UAV" info into the Onboard Tab
        showOfflineWarning = true;
116
        WPM = new UASWaypointManager(NULL);
117
    }
118

pixhawk's avatar
pixhawk committed
119
    setUAS(uas);
120 121 122

    // STATUS LABEL
    updateStatusLabel("");
123 124

    this->setVisible(false);
125
    loadFileGlobalWP = false;
126
    readGlobalWP = false;
127 128 129
    centerMapCoordinate.setX(0.0);
    centerMapCoordinate.setY(0.0);

pixhawk's avatar
pixhawk committed
130 131 132 133 134 135 136
}

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

137
void WaypointList::updatePosition(UASInterface* uas, double x, double y, double z, quint64 usec)
138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154
{
    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
155 156
void WaypointList::setUAS(UASInterface* uas)
{
LM's avatar
LM committed
157 158 159 160
    if (this->uas == NULL && uas != NULL)
    {
        WPM = uas->getWaypointManager();
    }
161
    if (this->uas == NULL)
lm's avatar
lm committed
162
    {
pixhawk's avatar
pixhawk committed
163
        this->uas = uas;
164 165 166 167 168 169
        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)));
170 171 172 173 174
        if (uas != NULL)
        {
            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)));
        }
175 176
        //connect(WPM,SIGNAL(loadWPFile()),this,SLOT(setIsLoadFileWP()));
        //connect(WPM,SIGNAL(readGlobalWPFromUAS(bool)),this,SLOT(setIsReadGlobalWP(bool)));
pixhawk's avatar
pixhawk committed
177
    }
178 179 180

    // Update list
    read();
pixhawk's avatar
pixhawk committed
181 182
}

pixhawk's avatar
pixhawk committed
183 184
void WaypointList::saveWaypoints()
{
185

pixhawk's avatar
pixhawk committed
186
        QString fileName = QFileDialog::getSaveFileName(this, tr("Save File"), "./waypoints.txt", tr("Waypoint File (*.txt)"));
187 188
        WPM->saveWaypoints(fileName);

pixhawk's avatar
pixhawk committed
189 190 191 192
}

void WaypointList::loadWaypoints()
{
193 194 195 196 197 198 199 200 201
    //create a popup notifying the user about the limitations of offline editing
    if (showOfflineWarning == true)
    {
        QMessageBox msgBox;
        msgBox.setIcon(QMessageBox::Warning);
        msgBox.setText("Offline editor!");
        msgBox.setInformativeText("You are using the offline mission editor. Please don't forget to save your mission plan before connecting the UAV, otherwise it will be lost.");
        msgBox.setStandardButtons(QMessageBox::Ok);
        msgBox.setDefaultButton(QMessageBox::Ok);
202
        msgBox.exec();
203 204 205 206
        showOfflineWarning = false;
    }
    QString fileName = QFileDialog::getOpenFileName(this, tr("Load File"), ".", tr("Waypoint File (*.txt)"));
    WPM->loadWaypoints(fileName);
pixhawk's avatar
pixhawk committed
207 208 209 210
}

void WaypointList::transmit()
{
lm's avatar
lm committed
211 212
    if (uas)
    {
213
        WPM->writeWaypoints();
pixhawk's avatar
pixhawk committed
214 215 216 217 218
    }
}

void WaypointList::read()
{
lm's avatar
lm committed
219 220
    if (uas)
    {
221
        WPM->readWaypoints(true);
pixhawk's avatar
pixhawk committed
222 223 224 225 226 227 228
    }
}

void WaypointList::refresh()
{
    if (uas)
    {
229
        WPM->readWaypoints(false);
pixhawk's avatar
pixhawk committed
230 231 232
    }
}

pixhawk's avatar
pixhawk committed
233
void WaypointList::addEditable()
pixhawk's avatar
pixhawk committed
234
{
pixhawk's avatar
pixhawk committed
235

236
        const QVector<Waypoint *> &waypoints = WPM->getWaypointEditableList();
pixhawk's avatar
pixhawk committed
237
        Waypoint *wp;
238 239
        if (waypoints.size() > 0)
        {
pixhawk's avatar
pixhawk committed
240 241
            // Create waypoint with last frame
            Waypoint *last = waypoints.at(waypoints.size()-1);
lm's avatar
lm committed
242 243
            wp = new Waypoint(0, last->getX(), last->getY(), last->getZ(), last->getParam1(), last->getParam2(), last->getParam3(), last->getParam4(),
                              last->getAutoContinue(), false, last->getFrame(), last->getAction());
244
            WPM->addWaypointEditable(wp);
245 246
        }
        else
247 248 249 250
        {            
            if (uas)
            {
                // Create first waypoint at current MAV position
251 252
                if (uas->localPositionKnown() || uas->globalPositionKnown())
                {
253 254 255 256 257 258
                	if(addCurrentPositionWaypoint())
                	{
                 	   updateStatusLabel(tr("Added default LOCAL (NED) waypoint."));
                 	  wp = new Waypoint(0, 0, 0, -0.50, 0, 0.20, 0, 0,true, true, MAV_FRAME_LOCAL_NED, MAV_CMD_NAV_WAYPOINT);
                 	  WPM->addWaypointEditable(wp);
                	}
259 260 261 262 263 264 265 266
                }
                else
                {
                    // MAV connected, but position unknown, add default waypoint
                   updateStatusLabel(tr("WARNING: No position known. Adding default LOCAL (NED) waypoint"));
                   wp = new Waypoint(0, 0, 0, -0.50, 0, 0.20, 0, 0,true, true, MAV_FRAME_LOCAL_NED, MAV_CMD_NAV_WAYPOINT);
                   WPM->addWaypointEditable(wp);
                }
267 268 269 270
            }
            else
            {
                //Since no UAV available, create first default waypoint.
271
                 updateStatusLabel(tr("No UAV connected. Adding default LOCAL (NED) waypoint"));
272 273
                wp = new Waypoint(0, 0, 0, -0.50, 0, 0.20, 0, 0,true, true, MAV_FRAME_LOCAL_NED, MAV_CMD_NAV_WAYPOINT);
                WPM->addWaypointEditable(wp);
274
                //create a popup notifying the user about the limitations of offline editing
275 276 277 278 279 280 281 282
                if (showOfflineWarning == true)
                {
                    QMessageBox msgBox;
                    msgBox.setIcon(QMessageBox::Warning);
                    msgBox.setText("Offline editor!");
                    msgBox.setInformativeText("You are using the offline mission editor. Please don't forget to save your mission plan before connecting the UAV, otherwise it will be lost.");
                    msgBox.setStandardButtons(QMessageBox::Ok);
                    msgBox.setDefaultButton(QMessageBox::Ok);
283
                    msgBox.exec();
284 285
                    showOfflineWarning = false;
                }
286
            }
287
        }
288

pixhawk's avatar
pixhawk committed
289
}
290

pixhawk's avatar
pixhawk committed
291

292
int WaypointList::addCurrentPositionWaypoint()
293
{    
pixhawk's avatar
pixhawk committed
294
    if (uas)
pixhawk's avatar
pixhawk committed
295
    {
296
        const QVector<Waypoint *> &waypoints = WPM->getWaypointEditableList();
297 298 299
        Waypoint *wp;
        Waypoint *last = 0;
        if (waypoints.size() > 0)
pixhawk's avatar
pixhawk committed
300
        {
301 302 303 304 305 306 307 308 309
            last = waypoints.at(waypoints.size()-1);
        }

        if (uas->globalPositionKnown())
        {
            float acceptanceRadiusGlobal = 10.0f;
            float holdTime = 0.0f;
            float yawGlobal = 0.0f;
            if (last)
310
            {
311 312 313
                acceptanceRadiusGlobal = last->getAcceptanceRadius();
                holdTime = last->getHoldTime();
                yawGlobal = last->getYaw();
314
            }
315
            // Create global frame waypoint per default
316
            wp = new Waypoint(0, uas->getLatitude(), uas->getLongitude(), uas->getAltitude(), 0, acceptanceRadiusGlobal, holdTime, yawGlobal, true, false, MAV_FRAME_GLOBAL_RELATIVE_ALT, MAV_CMD_NAV_WAYPOINT);
317
            WPM->addWaypointEditable(wp);
lm's avatar
lm committed
318
            updateStatusLabel(tr("Added GLOBAL, ALTITUDE OVER GROUND waypoint"));
319
            return 0;
320 321 322 323 324 325
        }
        else if (uas->localPositionKnown())
        {
            float acceptanceRadiusLocal = 0.2f;
            float holdTime = 0.5f;
            if (last)
326
            {
327 328
                acceptanceRadiusLocal = last->getAcceptanceRadius();
                holdTime = last->getHoldTime();
329
            }
330
            // Create local frame waypoint as second option
331
            wp = new Waypoint(0, uas->getLocalX(), uas->getLocalY(), uas->getLocalZ(), uas->getYaw(), acceptanceRadiusLocal, holdTime, 0.0, true, false, MAV_FRAME_LOCAL_NED, MAV_CMD_NAV_WAYPOINT);
332
            WPM->addWaypointEditable(wp);
lm's avatar
lm committed
333
            updateStatusLabel(tr("Added LOCAL (NED) waypoint"));
334
            return 0;
335 336 337 338
        }
        else
        {
            // Do nothing
339
            updateStatusLabel(tr("Not adding waypoint, no position of MAV known yet."));
340
            return 1;
pixhawk's avatar
pixhawk committed
341
        }
pixhawk's avatar
pixhawk committed
342
    }
343
    return 1;
pixhawk's avatar
pixhawk committed
344 345
}

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

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

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

369 370 371 372
        if (seq < waypoints.size())
        {
            for(int i = 0; i < waypoints.size(); i++)
            {
pixhawk's avatar
pixhawk committed
373
                WaypointEditableView* widget = wpEditableViews.find(waypoints[i]).value();
374

375 376
                if (waypoints[i]->getId() == seq)
                {
377
                    widget->setCurrent(true);
378 379 380
                }
                else
                {
381 382 383 384 385 386
                    widget->setCurrent(false);
                }
            }
        }
}

387
// Update waypointViews to correctly indicate the new current waypoint
388 389
void WaypointList::currentWaypointViewOnlyChanged(quint16 seq)
{
390
    const QVector<Waypoint *> &waypoints = WPM->getWaypointViewOnlyList();
391

392 393 394
    if (seq < waypoints.size())
    {
        for(int i = 0; i < waypoints.size(); i++)
395
        {
396
            WaypointViewOnlyView* widget = wpViewOnlyViews.find(waypoints[i]).value();
397

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

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

417 418 419 420 421 422 423
void WaypointList::updateWaypointViewOnly(int uas, Waypoint* wp)
{
    Q_UNUSED(uas);
    WaypointViewOnlyView *wpv = wpViewOnlyViews.value(wp);
    wpv->updateValues();
}

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

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

451 452 453 454 455 456 457 458 459 460
    // then add/update the views for each waypoint in the list
    for(int i = 0; i < waypoints.size(); i++) {
        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
461

462 463 464 465
        //check if ordering has changed
        if(viewOnlyListLayout->itemAt(i)->widget() != wpv) {
            viewOnlyListLayout->removeWidget(wpv);
            viewOnlyListLayout->insertWidget(i, wpv);
pixhawk's avatar
pixhawk committed
466
        }
467 468

        wpv->updateValues();    // update the values of the ui elements in the view
pixhawk's avatar
pixhawk committed
469
    }
470 471 472
    this->setUpdatesEnabled(true);
    loadFileGlobalWP = false;

pixhawk's avatar
pixhawk committed
473 474 475
}


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

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

503 504 505 506 507 508 509 510 511 512 513 514 515 516
    // then add/update the views for each waypoint in the list
    for(int i = 0; i < waypoints.size(); i++) {
        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
517

518 519 520 521
        //check if ordering has changed
        if(editableListLayout->itemAt(i)->widget() != wpv) {
            editableListLayout->removeWidget(wpv);
            editableListLayout->insertWidget(i, wpv);
522
        }
lm's avatar
lm committed
523

524 525 526 527
        wpv->updateValues();    // update the values of the ui elements in the view
    }
    this->setUpdatesEnabled(true);
    loadFileGlobalWP = false;
528

pixhawk's avatar
pixhawk committed
529

530
}
pixhawk's avatar
pixhawk committed
531

532
void WaypointList::moveUp(Waypoint* wp)
pixhawk's avatar
pixhawk committed
533
{
534
    const QVector<Waypoint *> &waypoints = WPM->getWaypointEditableList();
535

536 537 538 539 540 541
    //get the current position of wp in the local storage
    int i;
    for (i = 0; i < waypoints.size(); i++) {
        if (waypoints[i] == wp)
            break;
    }
542

543 544 545
    // if wp was found and its not the first entry, move it
    if (i < waypoints.size() && i > 0) {
        WPM->moveWaypoint(i, i-1);
pixhawk's avatar
pixhawk committed
546 547 548
    }
}

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

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

560 561 562
    // if wp was found and its not the last entry, move it
    if (i < waypoints.size()-1) {
        WPM->moveWaypoint(i, i+1);
pixhawk's avatar
pixhawk committed
563 564 565 566
    }
}

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

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

582 583 584 585


void WaypointList::on_clearWPListButton_clicked()
{
586

tecnosapiens's avatar
tecnosapiens committed
587

588 589
    if (uas) {
        emit clearPathclicked();
590
        const QVector<Waypoint *> &waypoints = WPM->getWaypointEditableList();
591
        while(!waypoints.isEmpty()) { //for(int i = 0; i <= waypoints.size(); i++)
pixhawk's avatar
pixhawk committed
592
            WaypointEditableView* widget = wpEditableViews.find(waypoints[0]).value();
593
            widget->remove();
594
        }
595
    } else {
lm's avatar
lm committed
596 597 598 599 600
//        if(isGlobalWP)
//        {
//           emit clearPathclicked();
//        }
    }
601 602
}

pixhawk's avatar
pixhawk committed
603
///** @brief The MapWidget informs that a waypoint global was changed on the map */
604

pixhawk's avatar
pixhawk committed
605 606 607 608
//void WaypointList::waypointGlobalChanged(QPointF coordinate, int indexWP)
//{
//    if (uas)
//    {
609
//        const QVector<Waypoint *> &waypoints = WPM->getWaypointEditableList();
pixhawk's avatar
pixhawk committed
610 611 612
//        if (waypoints.size() > 0)
//        {
//            Waypoint *temp = waypoints.at(indexWP);
613

pixhawk's avatar
pixhawk committed
614 615
//            temp->setX(coordinate.x());
//            temp->setY(coordinate.y());
616

pixhawk's avatar
pixhawk committed
617 618 619 620
//            //WaypointGlobalView* widget = wpGlobalViews.find(waypoints[indexWP]).value();
//            //widget->updateValues();
//        }
//    }
621 622


pixhawk's avatar
pixhawk committed
623
//}
624

625 626 627 628 629 630 631 632 633 634 635 636 637
///** @brief The MapWidget informs that a waypoint global was changed on the map */

//void WaypointList::waypointGlobalPositionChanged(Waypoint* wp)
//{
//    QPointF coordinate;
//    coordinate.setX(wp->getX());
//    coordinate.setY(wp->getY());

//   emit ChangeWaypointGlobalPosition(wp->getId(), coordinate);


//}

638
void WaypointList::clearWPWidget()
639 640
{    
        const QVector<Waypoint *> &waypoints = WPM->getWaypointEditableList();
641
        while(!waypoints.isEmpty()) { //for(int i = 0; i <= waypoints.size(); i++)
pixhawk's avatar
pixhawk committed
642
            WaypointEditableView* widget = wpEditableViews.find(waypoints[0]).value();
643
            widget->remove();
644
        }    
645
}
646

pixhawk's avatar
pixhawk committed
647 648 649 650
//void WaypointList::setIsLoadFileWP()
//{
//    loadFileGlobalWP = true;
//}
651

pixhawk's avatar
pixhawk committed
652 653 654 655 656 657
//void WaypointList::setIsReadGlobalWP(bool value)
//{
//    // FIXME James Check this
//    Q_UNUSED(value);
//    // readGlobalWP = value;
//}