WaypointList.cc 24.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 50
    QWidget(parent),
    uas(NULL),
    mavX(0.0),
    mavY(0.0),
    mavZ(0.0),
    mavYaw(0.0),
    m_ui(new Ui::WaypointList)
pixhawk's avatar
pixhawk committed
51 52 53
{
    m_ui->setupUi(this);

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

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

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

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

73
    // REQUEST WAYPOINTS
74
    connect(m_ui->readButton, SIGNAL(clicked()), this, SLOT(read()));
75

pixhawk's avatar
pixhawk committed
76 77 78 79
    // SAVE/LOAD WAYPOINTS
    connect(m_ui->saveButton, SIGNAL(clicked()), this, SLOT(saveWaypoints()));
    connect(m_ui->loadButton, SIGNAL(clicked()), this, SLOT(loadWaypoints()));

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

pixhawk's avatar
pixhawk committed
82 83 84 85 86 87 88 89 90 91
    //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

92
    connect(m_ui->refreshButton, SIGNAL(clicked()), this, SLOT(refresh()));
93 94


pixhawk's avatar
pixhawk committed
95 96
    // SET UAS AFTER ALL SIGNALS/SLOTS ARE CONNECTED
    setUAS(uas);
97 98 99

    // STATUS LABEL
    updateStatusLabel("");
100 101

    this->setVisible(false);
102
    loadFileGlobalWP = false;
103
    readGlobalWP = false;
104 105 106
    centerMapCoordinate.setX(0.0);
    centerMapCoordinate.setY(0.0);

pixhawk's avatar
pixhawk committed
107 108 109 110 111 112 113
}

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

114
void WaypointList::updatePosition(UASInterface* uas, double x, double y, double z, quint64 usec)
115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131
{
    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
132 133
void WaypointList::setUAS(UASInterface* uas)
{
lm's avatar
lm committed
134 135
    if (this->uas == NULL && uas != NULL)
    {
pixhawk's avatar
pixhawk committed
136
        this->uas = uas;
pixhawk's avatar
pixhawk committed
137

138
        connect(uas, SIGNAL(localPositionChanged(UASInterface*,double,double,double,quint64)),  this, SLOT(updatePosition(UASInterface*,double,double,double,quint64)));
139
        connect(uas, SIGNAL(attitudeChanged(UASInterface*,double,double,double,quint64)),       this, SLOT(updateAttitude(UASInterface*,double,double,double,quint64)));
pixhawk's avatar
pixhawk committed
140

pixhawk's avatar
pixhawk committed
141
        connect(uas->getWaypointManager(), SIGNAL(updateStatusString(const QString &)),        this, SLOT(updateStatusLabel(const QString &)));
142 143
        connect(uas->getWaypointManager(), SIGNAL(waypointEditableListChanged(void)),                  this, SLOT(waypointEditableListChanged(void)));
        connect(uas->getWaypointManager(), SIGNAL(waypointEditableChanged(int,Waypoint*)), this, SLOT(updateWaypointEditable(int,Waypoint*)));
pixhawk's avatar
pixhawk committed
144 145
        connect(uas->getWaypointManager(), SIGNAL(waypointViewOnlyListChanged(void)),                  this, SLOT(waypointViewOnlyListChanged(void)));
        connect(uas->getWaypointManager(), SIGNAL(waypointViewOnlyChanged(int,Waypoint*)), this, SLOT(updateWaypointViewOnly(int,Waypoint*)));
146
        connect(uas->getWaypointManager(), SIGNAL(currentWaypointChanged(quint16)),            this, SLOT(currentWaypointViewOnlyChanged(quint16)));
147 148
        //connect(uas->getWaypointManager(),SIGNAL(loadWPFile()),this,SLOT(setIsLoadFileWP()));
        //connect(uas->getWaypointManager(),SIGNAL(readGlobalWPFromUAS(bool)),this,SLOT(setIsReadGlobalWP(bool)));
pixhawk's avatar
pixhawk committed
149 150 151
    }
}

pixhawk's avatar
pixhawk committed
152 153
void WaypointList::saveWaypoints()
{
lm's avatar
lm committed
154 155
    if (uas)
    {
pixhawk's avatar
pixhawk committed
156
        QString fileName = QFileDialog::getSaveFileName(this, tr("Save File"), "./waypoints.txt", tr("Waypoint File (*.txt)"));
pixhawk's avatar
pixhawk committed
157
        uas->getWaypointManager()->saveWaypoints(fileName);
pixhawk's avatar
pixhawk committed
158 159 160 161 162
    }
}

void WaypointList::loadWaypoints()
{
lm's avatar
lm committed
163 164
    if (uas)
    {
pixhawk's avatar
pixhawk committed
165
        QString fileName = QFileDialog::getOpenFileName(this, tr("Load File"), ".", tr("Waypoint File (*.txt)"));
pixhawk's avatar
pixhawk committed
166
        uas->getWaypointManager()->loadWaypoints(fileName);
167
    }
pixhawk's avatar
pixhawk committed
168 169 170 171
}

void WaypointList::transmit()
{
lm's avatar
lm committed
172 173
    if (uas)
    {
pixhawk's avatar
pixhawk committed
174
        uas->getWaypointManager()->writeWaypoints();
pixhawk's avatar
pixhawk committed
175 176 177 178 179
    }
}

void WaypointList::read()
{
lm's avatar
lm committed
180 181
    if (uas)
    {
pixhawk's avatar
pixhawk committed
182 183 184 185 186 187 188 189 190
        uas->getWaypointManager()->readWaypoints(true);
    }
}

void WaypointList::refresh()
{
    if (uas)
    {
        uas->getWaypointManager()->readWaypoints(false);
pixhawk's avatar
pixhawk committed
191 192 193
    }
}

pixhawk's avatar
pixhawk committed
194
void WaypointList::addEditable()
pixhawk's avatar
pixhawk committed
195
{
196 197
    if (uas)
    {
pixhawk's avatar
pixhawk committed
198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215
        const QVector<Waypoint *> &waypoints = uas->getWaypointManager()->getWaypointEditableList();
        Waypoint *wp;
        if (waypoints.size() > 0)
        {
            // Create waypoint with last frame
            Waypoint *last = waypoints.at(waypoints.size()-1);
            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());
            uas->getWaypointManager()->addWaypointEditable(wp);
        }
        else
        {
            // Create first waypoint at current MAV position
            addCurrentPositionWaypoint();
        }
    }
}

216
/*
pixhawk's avatar
pixhawk committed
217 218 219 220 221
void WaypointList::addViewOnly()
{
    if (uas)
    {
        const QVector<Waypoint *> &waypoints = uas->getWaypointManager()->getWaypointViewOnlyList();
pixhawk's avatar
pixhawk committed
222
        Waypoint *wp;
223 224
        if (waypoints.size() > 0)
        {
pixhawk's avatar
pixhawk committed
225 226
            // Create waypoint with last frame
            Waypoint *last = waypoints.at(waypoints.size()-1);
lm's avatar
lm committed
227 228
            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());
229
            uas->getWaypointManager()->addWaypointEditable(wp);
230 231 232 233
        }
        else
        {
            // Create first waypoint at current MAV position
234
            addCurrentPositionWaypoint();
235
        }
pixhawk's avatar
pixhawk committed
236 237
    }
}
238
*/
pixhawk's avatar
pixhawk committed
239

240
void WaypointList::addCurrentPositionWaypoint()
lm's avatar
lm committed
241
{
pixhawk's avatar
pixhawk committed
242
    if (uas)
pixhawk's avatar
pixhawk committed
243
    {
pixhawk's avatar
pixhawk committed
244
        const QVector<Waypoint *> &waypoints = uas->getWaypointManager()->getWaypointEditableList();
245 246 247
        Waypoint *wp;
        Waypoint *last = 0;
        if (waypoints.size() > 0)
pixhawk's avatar
pixhawk committed
248
        {
249 250 251 252 253 254 255 256 257
            last = waypoints.at(waypoints.size()-1);
        }

        if (uas->globalPositionKnown())
        {
            float acceptanceRadiusGlobal = 10.0f;
            float holdTime = 0.0f;
            float yawGlobal = 0.0f;
            if (last)
258
            {
259 260 261
                acceptanceRadiusGlobal = last->getAcceptanceRadius();
                holdTime = last->getHoldTime();
                yawGlobal = last->getYaw();
262
            }
263
            // Create global frame waypoint per default
264
            wp = new Waypoint(0, uas->getLatitude(), uas->getLongitude(), uas->getAltitude(), 0, acceptanceRadiusGlobal, holdTime, yawGlobal, true, true, MAV_FRAME_GLOBAL_RELATIVE_ALT, MAV_CMD_NAV_WAYPOINT);
265
            uas->getWaypointManager()->addWaypointEditable(wp);
lm's avatar
lm committed
266
            updateStatusLabel(tr("Added GLOBAL, ALTITUDE OVER GROUND waypoint"));
267 268 269 270 271 272
        }
        else if (uas->localPositionKnown())
        {
            float acceptanceRadiusLocal = 0.2f;
            float holdTime = 0.5f;
            if (last)
273
            {
274 275
                acceptanceRadiusLocal = last->getAcceptanceRadius();
                holdTime = last->getHoldTime();
276
            }
277
            // Create local frame waypoint as second option
lm's avatar
lm committed
278
            wp = new Waypoint(0, uas->getLocalX(), uas->getLocalY(), uas->getLocalZ(), uas->getYaw(), acceptanceRadiusLocal, holdTime, 0.0, true, true, MAV_FRAME_LOCAL_NED, MAV_CMD_NAV_WAYPOINT);
279
            uas->getWaypointManager()->addWaypointEditable(wp);
lm's avatar
lm committed
280
            updateStatusLabel(tr("Added LOCAL (NED) waypoint"));
281 282 283 284 285
        }
        else
        {
            // Do nothing
            updateStatusLabel(tr("Not adding waypoint, no position known of MAV known yet."));
pixhawk's avatar
pixhawk committed
286
        }
pixhawk's avatar
pixhawk committed
287 288 289
    }
}

pixhawk's avatar
pixhawk committed
290 291
void WaypointList::updateStatusLabel(const QString &string)
{
292
    // Status label in write widget
pixhawk's avatar
pixhawk committed
293
    m_ui->statusLabel->setText(string);
294 295
    // Status label in read only widget
    m_ui->viewStatusLabel->setText(string);
pixhawk's avatar
pixhawk committed
296 297
}

298
void WaypointList::changeCurrentWaypoint(quint16 seq)
pixhawk's avatar
pixhawk committed
299
{
300 301
    if (uas)
    {
pixhawk's avatar
pixhawk committed
302
        uas->getWaypointManager()->setCurrentWaypoint(seq);
303
    }
pixhawk's avatar
pixhawk committed
304 305
}

306 307

void WaypointList::currentWaypointEditableChanged(quint16 seq)
308
{
309 310
    qDebug() << "WaypointList::currentWaypointEditableChanged";

311 312
    if (uas)
    {
313 314
        uas->getWaypointManager()->setCurrentEditable(seq);
        /*
pixhawk's avatar
pixhawk committed
315
        const QVector<Waypoint *> &waypoints = uas->getWaypointManager()->getWaypointEditableList();
316

317 318 319 320
        if (seq < waypoints.size())
        {
            for(int i = 0; i < waypoints.size(); i++)
            {
pixhawk's avatar
pixhawk committed
321
                WaypointEditableView* widget = wpEditableViews.find(waypoints[i]).value();
322

323 324
                if (waypoints[i]->getId() == seq)
                {
325
                    widget->setCurrent(true);
326 327 328
                }
                else
                {
329 330 331 332
                    widget->setCurrent(false);
                }
            }
        }
333
         */
334
    }
335

336 337
}

338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363
void WaypointList::currentWaypointViewOnlyChanged(quint16 seq)
{
    if (uas)
    {
        const QVector<Waypoint *> &waypoints = uas->getWaypointManager()->getWaypointViewOnlyList();

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

                if (waypoints[i]->getId() == seq)
                {
                    widget->setCurrent(true);
                }
                else
                {
                    widget->setCurrent(false);
                }
            }
        }
    }
}

void WaypointList::updateWaypointEditable(int uas, Waypoint* wp)
364 365
{
    Q_UNUSED(uas);
pixhawk's avatar
pixhawk committed
366
    WaypointEditableView *wpv = wpEditableViews.value(wp);
367 368 369
    wpv->updateValues();
}

370 371 372 373 374 375 376
void WaypointList::updateWaypointViewOnly(int uas, Waypoint* wp)
{
    Q_UNUSED(uas);
    WaypointViewOnlyView *wpv = wpViewOnlyViews.value(wp);
    wpv->updateValues();
}

pixhawk's avatar
pixhawk committed
377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398
void WaypointList::waypointViewOnlyListChanged()
{
    if (uas) {
        // Prevent updates to prevent visual flicker
        this->setUpdatesEnabled(false);
        const QVector<Waypoint *> &waypoints = uas->getWaypointManager()->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;
                    }
                }
                if (i == waypoints.size()) {
                    WaypointViewOnlyView* widget = wpViewOnlyViews.find(cur).value();
                    widget->hide();
399
                    viewOnlyListLayout->removeWidget(widget);
pixhawk's avatar
pixhawk committed
400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429
                    wpViewOnlyViews.remove(cur);
                }
            }
        }

        // 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);

            //check if ordering has changed
            if(viewOnlyListLayout->itemAt(i)->widget() != wpv) {
                viewOnlyListLayout->removeWidget(wpv);
                viewOnlyListLayout->insertWidget(i, wpv);
            }

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


430
void WaypointList::waypointEditableListChanged()
pixhawk's avatar
pixhawk committed
431
{
432
    if (uas) {
pixhawk's avatar
pixhawk committed
433 434
        // Prevent updates to prevent visual flicker
        this->setUpdatesEnabled(false);
pixhawk's avatar
pixhawk committed
435
        const QVector<Waypoint *> &waypoints = uas->getWaypointManager()->getWaypointEditableList();
pixhawk's avatar
pixhawk committed
436

pixhawk's avatar
pixhawk committed
437 438
        if (!wpEditableViews.empty()) {
            QMapIterator<Waypoint*,WaypointEditableView*> viewIt(wpEditableViews);
439
            viewIt.toFront();
440
            while(viewIt.hasNext()) {
441 442 443
                viewIt.next();
                Waypoint *cur = viewIt.key();
                int i;
444 445
                for (i = 0; i < waypoints.size(); i++) {
                    if (waypoints[i] == cur) {
446
                        break;
lm's avatar
lm committed
447 448
                    }
                }
449
                if (i == waypoints.size()) {
pixhawk's avatar
pixhawk committed
450
                    WaypointEditableView* widget = wpEditableViews.find(cur).value();
451
                    widget->hide();
pixhawk's avatar
pixhawk committed
452 453
                    editableListLayout->removeWidget(widget);
                    wpEditableViews.remove(cur);
lm's avatar
lm committed
454
                }
pixhawk's avatar
pixhawk committed
455 456
            }
        }
457

458
        // then add/update the views for each waypoint in the list
459
        for(int i = 0; i < waypoints.size(); i++) {
460
            Waypoint *wp = waypoints[i];
pixhawk's avatar
pixhawk committed
461
            if (!wpEditableViews.contains(wp)) {
462
                WaypointEditableView* wpview = new WaypointEditableView(wp, this);
pixhawk's avatar
pixhawk committed
463
                wpEditableViews.insert(wp, wpview);
464 465 466
                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*)));
467 468
                //connect(wpview, SIGNAL(currentWaypointChanged(quint16)), this, SLOT(currentWaypointChanged(quint16)));
                connect(wpview, SIGNAL(changeCurrentWaypoint(quint16)), this, SLOT(currentWaypointEditableChanged(quint16)));
pixhawk's avatar
pixhawk committed
469
                editableListLayout->insertWidget(i, wpview);
470
            }
pixhawk's avatar
pixhawk committed
471
            WaypointEditableView *wpv = wpEditableViews.value(wp);
472 473

            //check if ordering has changed
pixhawk's avatar
pixhawk committed
474 475 476
            if(editableListLayout->itemAt(i)->widget() != wpv) {
                editableListLayout->removeWidget(wpv);
                editableListLayout->insertWidget(i, wpv);
477
            }
lm's avatar
lm committed
478

479 480 481
            wpv->updateValues();    // update the values of the ui elements in the view
        }
        this->setUpdatesEnabled(true);
lm's avatar
lm committed
482
        loadFileGlobalWP = false;
483
    }
lm's avatar
lm committed
484 485
}

486
//void WaypointList::waypointEditableListChanged()
lm's avatar
lm committed
487 488 489 490 491 492
//{
//    if (uas)
//    {
//        // Prevent updates to prevent visual flicker
//        this->setUpdatesEnabled(false);
//        // Get all waypoints
pixhawk's avatar
pixhawk committed
493
//        const QVector<Waypoint *> &waypoints = uas->getWaypointManager()->getWaypointEditableList();
lm's avatar
lm committed
494 495 496

////        // Store the current state, then check which widgets to update
////        // and which ones to delete
pixhawk's avatar
pixhawk committed
497
////        QList<Waypoint*> oldWaypoints = wpEditableViews.keys();
lm's avatar
lm committed
498 499 500

////        foreach (Waypoint* wp, waypoints)
////        {
501
////            WaypointEditableView* wpview;
lm's avatar
lm committed
502
////            // Create any new waypoint
pixhawk's avatar
pixhawk committed
503
////            if (!wpEditableViews.contains(wp))
lm's avatar
lm committed
504
////            {
505
////                wpview = new WaypointEditableView(wp, this);
pixhawk's avatar
pixhawk committed
506
////                wpEditableViews.insert(wp, wpview);
lm's avatar
lm committed
507 508 509 510 511
////                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(changeCurrentWaypoint(quint16)));
pixhawk's avatar
pixhawk committed
512
////                editableListLayout->addWidget(wpview);
lm's avatar
lm committed
513 514 515 516
////            }
////            else
////            {
////                // Update existing waypoints
pixhawk's avatar
pixhawk committed
517
////                wpview = wpEditableViews.value(wp);
lm's avatar
lm committed
518 519 520 521 522 523 524 525 526 527 528 529 530

////            }
////            // Mark as updated by removing from old list
////            oldWaypoints.removeAt(oldWaypoints.indexOf(wp));

////            wpview->updateValues();    // update the values of the ui elements in the view

////        }

////        // The old list now contains all wps to be deleted
////        foreach (Waypoint* wp, oldWaypoints)
////        {
////            // Delete waypoint view and entry in list
pixhawk's avatar
pixhawk committed
531
////            WaypointEditableView* wpv = wpEditableViews.value(wp);
lm's avatar
lm committed
532 533
////            if (wpv)
////            {
pixhawk's avatar
pixhawk committed
534
////                editableListLayout->removeWidget(wpv);
lm's avatar
lm committed
535 536
////                delete wpv;
////            }
pixhawk's avatar
pixhawk committed
537
////            wpEditableViews.remove(wp);
lm's avatar
lm committed
538
////        }
539

pixhawk's avatar
pixhawk committed
540
//        if (!wpEditableViews.empty())
pixhawk's avatar
pixhawk committed
541
//        {
pixhawk's avatar
pixhawk committed
542
//            QMapIterator<Waypoint*,WaypointEditableView*> viewIt(wpEditableViews);
pixhawk's avatar
pixhawk committed
543 544 545 546 547 548 549 550 551 552 553 554 555 556 557
//            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;
//                    }
//                }
//                if (i == waypoints.size())
//                {
pixhawk's avatar
pixhawk committed
558
//                    WaypointEditableView* widget = wpEditableViews.find(cur).value();
lm's avatar
lm committed
559 560 561
//                    if (widget)
//                    {
//                        widget->hide();
pixhawk's avatar
pixhawk committed
562
//                        editableListLayout->removeWidget(widget);
lm's avatar
lm committed
563
//                    }
pixhawk's avatar
pixhawk committed
564
//                    wpEditableViews.remove(cur);
pixhawk's avatar
pixhawk committed
565 566 567
//                }
//            }
//        }
568

pixhawk's avatar
pixhawk committed
569 570 571 572
//        // then add/update the views for each waypoint in the list
//        for(int i = 0; i < waypoints.size(); i++)
//        {
//            Waypoint *wp = waypoints[i];
pixhawk's avatar
pixhawk committed
573
//            if (!wpEditableViews.contains(wp))
pixhawk's avatar
pixhawk committed
574
//            {
575
//                WaypointEditableView* wpview = new WaypointEditableView(wp, this);
pixhawk's avatar
pixhawk committed
576
//                wpEditableViews.insert(wp, wpview);
pixhawk's avatar
pixhawk committed
577 578 579 580 581 582
//                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(changeCurrentWaypoint(quint16)));
//            }
pixhawk's avatar
pixhawk committed
583
//            WaypointEditableView *wpv = wpEditableViews.value(wp);
pixhawk's avatar
pixhawk committed
584
//            wpv->updateValues();    // update the values of the ui elements in the view
pixhawk's avatar
pixhawk committed
585
//            editableListLayout->addWidget(wpv);
pixhawk's avatar
pixhawk committed
586 587

//        }
lm's avatar
lm committed
588 589 590 591
//        this->setUpdatesEnabled(true);
//    }
////    loadFileGlobalWP = false;
//}
pixhawk's avatar
pixhawk committed
592

593
void WaypointList::moveUp(Waypoint* wp)
pixhawk's avatar
pixhawk committed
594
{
595
    if (uas) {
pixhawk's avatar
pixhawk committed
596
        const QVector<Waypoint *> &waypoints = uas->getWaypointManager()->getWaypointEditableList();
597 598 599

        //get the current position of wp in the local storage
        int i;
600
        for (i = 0; i < waypoints.size(); i++) {
601 602
            if (waypoints[i] == wp)
                break;
603 604
        }

605
        // if wp was found and its not the first entry, move it
606
        if (i < waypoints.size() && i > 0) {
pixhawk's avatar
pixhawk committed
607
            uas->getWaypointManager()->moveWaypoint(i, i-1);
pixhawk's avatar
pixhawk committed
608 609 610 611
        }
    }
}

612
void WaypointList::moveDown(Waypoint* wp)
pixhawk's avatar
pixhawk committed
613
{
614
    if (uas) {
pixhawk's avatar
pixhawk committed
615
        const QVector<Waypoint *> &waypoints = uas->getWaypointManager()->getWaypointEditableList();
616

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

624
        // if wp was found and its not the last entry, move it
625
        if (i < waypoints.size()-1) {
pixhawk's avatar
pixhawk committed
626
            uas->getWaypointManager()->moveWaypoint(i, i+1);
pixhawk's avatar
pixhawk committed
627 628 629 630 631 632
        }
    }
}

void WaypointList::removeWaypoint(Waypoint* wp)
{
633
    if (uas) {
pixhawk's avatar
pixhawk committed
634
        uas->getWaypointManager()->removeWaypoint(wp->getId());
pixhawk's avatar
pixhawk committed
635 636 637 638 639 640 641 642 643 644 645 646 647 648
    }
}

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

649 650 651 652


void WaypointList::on_clearWPListButton_clicked()
{
653

tecnosapiens's avatar
tecnosapiens committed
654

655 656
    if (uas) {
        emit clearPathclicked();
pixhawk's avatar
pixhawk committed
657
        const QVector<Waypoint *> &waypoints = uas->getWaypointManager()->getWaypointEditableList();
658
        while(!waypoints.isEmpty()) { //for(int i = 0; i <= waypoints.size(); i++)
pixhawk's avatar
pixhawk committed
659
            WaypointEditableView* widget = wpEditableViews.find(waypoints[0]).value();
660
            widget->remove();
661
        }
662
    } else {
lm's avatar
lm committed
663 664 665 666 667
//        if(isGlobalWP)
//        {
//           emit clearPathclicked();
//        }
    }
668 669
}

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

pixhawk's avatar
pixhawk committed
672 673 674 675
//void WaypointList::waypointGlobalChanged(QPointF coordinate, int indexWP)
//{
//    if (uas)
//    {
pixhawk's avatar
pixhawk committed
676
//        const QVector<Waypoint *> &waypoints = uas->getWaypointManager()->getWaypointEditableList();
pixhawk's avatar
pixhawk committed
677 678 679
//        if (waypoints.size() > 0)
//        {
//            Waypoint *temp = waypoints.at(indexWP);
680

pixhawk's avatar
pixhawk committed
681 682
//            temp->setX(coordinate.x());
//            temp->setY(coordinate.y());
683

pixhawk's avatar
pixhawk committed
684 685 686 687
//            //WaypointGlobalView* widget = wpGlobalViews.find(waypoints[indexWP]).value();
//            //widget->updateValues();
//        }
//    }
688 689


pixhawk's avatar
pixhawk committed
690
//}
691

692 693 694 695 696 697 698 699 700 701 702 703 704
///** @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);


//}

705
void WaypointList::clearWPWidget()
706
{
707
    if (uas) {
pixhawk's avatar
pixhawk committed
708
        const QVector<Waypoint *> &waypoints = uas->getWaypointManager()->getWaypointEditableList();
709
        while(!waypoints.isEmpty()) { //for(int i = 0; i <= waypoints.size(); i++)
pixhawk's avatar
pixhawk committed
710
            WaypointEditableView* widget = wpEditableViews.find(waypoints[0]).value();
711 712 713
            widget->remove();
        }
    }
714
}
715

pixhawk's avatar
pixhawk committed
716 717 718 719
//void WaypointList::setIsLoadFileWP()
//{
//    loadFileGlobalWP = true;
//}
720

pixhawk's avatar
pixhawk committed
721 722 723 724 725 726
//void WaypointList::setIsReadGlobalWP(bool value)
//{
//    // FIXME James Check this
//    Q_UNUSED(value);
//    // readGlobalWP = value;
//}