MissionEditor.cc 11.9 KB
Newer Older
Don Gagne's avatar
Don Gagne 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
/*=====================================================================

QGroundControl Open Source Ground Control Station

(c) 2009, 2015 QGROUNDCONTROL PROJECT <http://www.qgroundcontrol.org>

This file is part of the QGROUNDCONTROL project

    QGROUNDCONTROL 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.

    QGROUNDCONTROL 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 QGROUNDCONTROL. If not, see <http://www.gnu.org/licenses/>.

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

#include "MissionEditor.h"
#include "ScreenToolsController.h"
Don Gagne's avatar
Don Gagne committed
26 27
#include "MultiVehicleManager.h"
#include "MissionManager.h"
Don Gagne's avatar
Don Gagne committed
28
#include "QGCFileDialog.h"
29
#include "CoordinateVector.h"
Don Gagne's avatar
Don Gagne committed
30 31 32 33 34 35 36 37 38

#include <QQmlContext>
#include <QQmlEngine>
#include <QSettings>

const char* MissionEditor::_settingsGroup = "MissionEditor";

MissionEditor::MissionEditor(QWidget *parent)
    : QGCQmlWidgetHolder(parent)
Don Gagne's avatar
Don Gagne committed
39
    , _missionItems(NULL)
Don Gagne's avatar
Don Gagne committed
40
    , _canEdit(true)
Don Gagne's avatar
Don Gagne committed
41 42 43 44 45 46 47
{
    // Get rid of layout default margins
    QLayout* pl = layout();
    if(pl) {
        pl->setContentsMargins(0,0,0,0);
    }
    
Don Gagne's avatar
Don Gagne committed
48 49 50 51 52 53 54
    Vehicle* activeVehicle = MultiVehicleManager::instance()->activeVehicle();
    if (activeVehicle) {
        MissionManager* missionManager = activeVehicle->missionManager();
        connect(missionManager, &MissionManager::newMissionItemsAvailable, this, &MissionEditor::_newMissionItemsAvailable);
        _newMissionItemsAvailable();
    } else {
        _missionItems = new QmlObjectListModel(this);
Don Gagne's avatar
Don Gagne committed
55
        _initAllMissionItems();
Don Gagne's avatar
Don Gagne committed
56 57
    }
    
Don Gagne's avatar
Don Gagne committed
58 59 60 61 62 63 64 65 66
    setContextPropertyObject("controller", this);

    setSource(QUrl::fromUserInput("qrc:/qml/MissionEditor.qml"));
}

MissionEditor::~MissionEditor()
{
}

Don Gagne's avatar
Don Gagne committed
67 68 69
void MissionEditor::_newMissionItemsAvailable(void)
{
    if (_missionItems) {
Don Gagne's avatar
Don Gagne committed
70
        _deinitAllMissionItems();
Don Gagne's avatar
Don Gagne committed
71 72 73
        _missionItems->deleteLater();
    }
    
Don Gagne's avatar
Don Gagne committed
74 75 76 77
    MissionManager* missionManager = MultiVehicleManager::instance()->activeVehicle()->missionManager();
    
    _canEdit = missionManager->canEdit();
    _missionItems = missionManager->copyMissionItems();
78
    
Don Gagne's avatar
Don Gagne committed
79
    _initAllMissionItems();
Don Gagne's avatar
Don Gagne committed
80 81 82 83 84 85 86
}

void MissionEditor::getMissionItems(void)
{
    Vehicle* activeVehicle = MultiVehicleManager::instance()->activeVehicle();
    
    if (activeVehicle) {
Don Gagne's avatar
Don Gagne committed
87 88
        MissionManager* missionManager = activeVehicle->missionManager();
        connect(missionManager, &MissionManager::newMissionItemsAvailable, this, &MissionEditor::_newMissionItemsAvailable);
Don Gagne's avatar
Don Gagne committed
89 90 91 92 93 94
        activeVehicle->missionManager()->requestMissionItems();
    }
}

void MissionEditor::setMissionItems(void)
{
Don Gagne's avatar
Don Gagne committed
95
    // FIXME: Need to pull out home position
Don Gagne's avatar
Don Gagne committed
96 97 98
    Vehicle* activeVehicle = MultiVehicleManager::instance()->activeVehicle();
    
    if (activeVehicle) {
Don Gagne's avatar
Don Gagne committed
99
        activeVehicle->missionManager()->writeMissionItems(*_missionItems, true /* skipFirstItem */);
Don Gagne's avatar
Don Gagne committed
100
        _missionItems->setDirty(false);
Don Gagne's avatar
Don Gagne committed
101 102 103
    }
}

104
int MissionEditor::addMissionItem(QGeoCoordinate coordinate)
Don Gagne's avatar
Don Gagne committed
105
{
Don Gagne's avatar
Don Gagne committed
106 107 108 109
    if (!_canEdit) {
        qWarning() << "addMissionItem called with _canEdit == false";
    }
    
Don Gagne's avatar
Don Gagne committed
110
    MissionItem * newItem = new MissionItem(this, _missionItems->count(), coordinate, MAV_CMD_NAV_WAYPOINT);
Don Gagne's avatar
Don Gagne committed
111
    _initMissionItem(newItem);
Don Gagne's avatar
Don Gagne committed
112
    newItem->setAltitude(30);
Don Gagne's avatar
Don Gagne committed
113
    if (_missionItems->count() == 1) {
114 115 116 117
        newItem->setCommand(MavlinkQmlSingleton::MAV_CMD_NAV_TAKEOFF);
    }
    qDebug() << "MissionItem" << newItem->coordinate();
    _missionItems->append(newItem);
Don Gagne's avatar
Don Gagne committed
118
    
Don Gagne's avatar
Don Gagne committed
119 120
    _recalcAll();
    
121
    return _missionItems->count() - 1;
Don Gagne's avatar
Don Gagne committed
122 123
}

124 125
void MissionEditor::removeMissionItem(int index)
{
Don Gagne's avatar
Don Gagne committed
126 127 128 129 130
    if (!_canEdit) {
        qWarning() << "addMissionItem called with _canEdit == false";
        return;
    }
    
Don Gagne's avatar
Don Gagne committed
131 132 133 134 135
    MissionItem* item = qobject_cast<MissionItem*>(_missionItems->removeAt(index));
    
    _deinitMissionItem(item);
    
    _recalcAll();
136 137 138 139
}

void MissionEditor::moveUp(int index)
{
Don Gagne's avatar
Don Gagne committed
140 141 142 143 144
    if (!_canEdit) {
        qWarning() << "addMissionItem called with _canEdit == false";
        return;
    }
    
145 146 147 148 149 150 151 152 153
    if (_missionItems->count() < 2 || index <= 0 || index >= _missionItems->count()) {
        return;
    }
    
    MissionItem item1 = *qobject_cast<MissionItem*>(_missionItems->get(index - 1));
    MissionItem item2 = *qobject_cast<MissionItem*>(_missionItems->get(index));
    
    _missionItems->removeAt(index - 1);
    _missionItems->removeAt(index - 1);
Don Gagne's avatar
Don Gagne committed
154
    
155 156
    _missionItems->insert(index - 1, new MissionItem(item2, _missionItems));
    _missionItems->insert(index, new MissionItem(item1, _missionItems));
Don Gagne's avatar
Don Gagne committed
157
    
Don Gagne's avatar
Don Gagne committed
158
    _recalcAll();
Don Gagne's avatar
Don Gagne committed
159 160
}

161
void MissionEditor::moveDown(int index)
Don Gagne's avatar
Don Gagne committed
162
{
Don Gagne's avatar
Don Gagne committed
163 164 165 166 167
    if (!_canEdit) {
        qWarning() << "addMissionItem called with _canEdit == false";
        return;
    }
    
168 169
    if (_missionItems->count() < 2 || index >= _missionItems->count() - 1) {
        return;
Don Gagne's avatar
Don Gagne committed
170
    }
171 172 173 174 175 176 177 178 179 180
    
    MissionItem item1 = *qobject_cast<MissionItem*>(_missionItems->get(index));
    MissionItem item2 = *qobject_cast<MissionItem*>(_missionItems->get(index + 1));
    
    _missionItems->removeAt(index);
    _missionItems->removeAt(index);
    
    _missionItems->insert(index, new MissionItem(item2, _missionItems));
    _missionItems->insert(index + 1, new MissionItem(item1, _missionItems));
    
Don Gagne's avatar
Don Gagne committed
181
    _recalcAll();
Don Gagne's avatar
Don Gagne committed
182
}
Don Gagne's avatar
Don Gagne committed
183 184 185 186 187 188 189 190 191 192

void MissionEditor::loadMissionFromFile(void)
{
    QString errorString;
    QString filename = QGCFileDialog::getOpenFileName(NULL, "Select Mission File to load");
    
    if (filename.isEmpty()) {
        return;
    }
    
193
    if (_missionItems) {
Don Gagne's avatar
Don Gagne committed
194
        _deinitAllMissionItems();
195 196 197 198
        _missionItems->deleteLater();
    }
    _missionItems = new QmlObjectListModel(this);

Don Gagne's avatar
Don Gagne committed
199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234
    _canEdit = true;
    
    QFile file(filename);
    
    if (!file.open(QIODevice::ReadOnly | QIODevice::Text)) {
        errorString = file.errorString();
    } else {
        QTextStream in(&file);
        
        const QStringList& version = in.readLine().split(" ");
        
        if (!(version.size() == 3 && version[0] == "QGC" && version[1] == "WPL" && version[2] == "120")) {
            errorString = "The mission file is not compatible with the current version of QGroundControl.";
        } else {
            while (!in.atEnd()) {
                MissionItem* item = new MissionItem();
                
                if (item->load(in)) {
                    _missionItems->append(item);
                    
                    if (!item->canEdit()) {
                        _canEdit = false;
                    }
                } else {
                    errorString = "The mission file is corrupted.";
                    break;
                }
            }
        }
        
    }
    
    if (!errorString.isEmpty()) {
        _missionItems->clear();
    }
    
Don Gagne's avatar
Don Gagne committed
235
    _initAllMissionItems();
Don Gagne's avatar
Don Gagne committed
236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259
}

void MissionEditor::saveMissionToFile(void)
{
    QString errorString;
    QString filename = QGCFileDialog::getSaveFileName(NULL, "Select file to save mission to");
    
    if (filename.isEmpty()) {
        return;
    }
    
    QFile file(filename);
    
    if (!file.open(QIODevice::WriteOnly | QIODevice::Text)) {
        errorString = file.errorString();
    } else {
        QTextStream out(&file);
        
        out << "QGC WPL 120\r\n";   // Version string
        
        for (int i=0; i<_missionItems->count(); i++) {
            qobject_cast<MissionItem*>(_missionItems->get(i))->save(out);
        }
    }
Don Gagne's avatar
Don Gagne committed
260 261
    
    _missionItems->setDirty(false);
Don Gagne's avatar
Don Gagne committed
262
}
263

Don Gagne's avatar
Don Gagne committed
264
void MissionEditor::_recalcWaypointLines(void)
265
{
Don Gagne's avatar
Don Gagne committed
266 267 268
    bool firstCoordinateItem = true;
    MissionItem* lastCoordinateItem = qobject_cast<MissionItem*>(_missionItems->get(0));
    
269
    _waypointLines.clear();
Don Gagne's avatar
Don Gagne committed
270
    
271
    for (int i=1; i<_missionItems->count(); i++) {
Don Gagne's avatar
Don Gagne committed
272
        MissionItem* item = qobject_cast<MissionItem*>(_missionItems->get(i));
273
        
Don Gagne's avatar
Don Gagne committed
274 275 276 277 278 279 280 281 282 283 284 285 286 287 288
        if (item->specifiesCoordinate()) {
            if (firstCoordinateItem) {
                if (item->command() == MavlinkQmlSingleton::MAV_CMD_NAV_TAKEOFF) {
                    // The first coordinate we hit is a takeoff command so link back to home position
                    _waypointLines.append(new CoordinateVector(qobject_cast<MissionItem*>(_missionItems->get(0))->coordinate(), item->coordinate()));
                } else {
                    // First coordiante is not a takeoff command, it does not link backwards to anything
                }
                firstCoordinateItem = false;
            } else {
                // Subsequent coordinate items link to last coordinate item
                _waypointLines.append(new CoordinateVector(lastCoordinateItem->coordinate(), item->coordinate()));
            }
            lastCoordinateItem = item;
        }
289
    }
Don Gagne's avatar
Don Gagne committed
290
    
291 292 293
    emit waypointLinesChanged();
}

Don Gagne's avatar
Don Gagne committed
294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 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 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386
// This will update the sequence numbers to be sequential starting from 0
void MissionEditor::_recalcSequence(void)
{
    MissionItem* currentParentItem = qobject_cast<MissionItem*>(_missionItems->get(0));
    
    currentParentItem->childItems()->clear();
    
    for (int i=0; i<_missionItems->count(); i++) {
        MissionItem* item = qobject_cast<MissionItem*>(_missionItems->get(i));
        
        // Setup ascending sequence numbers
        item->setSequenceNumber(i);
    }
}

// This will update the child item hierarchy
void MissionEditor::_recalcChildItems(void)
{
    MissionItem* currentParentItem = qobject_cast<MissionItem*>(_missionItems->get(0));
    
    currentParentItem->childItems()->clear();
    
    for (int i=1; i<_missionItems->count(); i++) {
        MissionItem* item = qobject_cast<MissionItem*>(_missionItems->get(i));
        
        // Set up non-coordinate item child hierarchy
        if (item->specifiesCoordinate()) {
            item->childItems()->clear();
            currentParentItem = item;
        } else {
            currentParentItem->childItems()->append(item);
        }
    }
}

void MissionEditor::_recalcAll(void)
{
    _recalcSequence();
    _recalcChildItems();
    _recalcWaypointLines();
}

/// Initializes a new set of mission items which may have come from the vehicle or have been loaded from a file
void MissionEditor::_initAllMissionItems(void)
{
    // Add the home position item to the front
    MissionItem* homeItem = new MissionItem(this);
    homeItem->setHomePositionSpecialCase(true);
    homeItem->setCommand(MavlinkQmlSingleton::MAV_CMD_NAV_WAYPOINT);
    _missionItems->insert(0, homeItem);
    
    for (int i=0; i<_missionItems->count(); i++) {
        _initMissionItem(qobject_cast<MissionItem*>(_missionItems->get(i)));
    }
    
    _recalcSequence();
    _recalcChildItems();
    _recalcWaypointLines();
    
    emit missionItemsChanged();
    emit canEditChanged(_canEdit);
    
    _missionItems->setDirty(false);
}

void MissionEditor::_deinitAllMissionItems(void)
{
    for (int i=0; i<_missionItems->count(); i++) {
        _deinitMissionItem(qobject_cast<MissionItem*>(_missionItems->get(i)));
    }
}

void MissionEditor::_initMissionItem(MissionItem* item)
{
    _missionItems->setDirty(false);
    
    connect(item, &MissionItem::commandChanged,     this, &MissionEditor::_itemCommandChanged);
    connect(item, &MissionItem::coordinateChanged,  this, &MissionEditor::_itemCoordinateChanged);
}

void MissionEditor::_deinitMissionItem(MissionItem* item)
{
    disconnect(item, &MissionItem::commandChanged,     this, &MissionEditor::_itemCommandChanged);
    disconnect(item, &MissionItem::coordinateChanged,  this, &MissionEditor::_itemCoordinateChanged);
}

void MissionEditor::_itemCoordinateChanged(const QGeoCoordinate& coordinate)
{
    Q_UNUSED(coordinate);
    _recalcWaypointLines();
}

void MissionEditor::_itemCommandChanged(MavlinkQmlSingleton::Qml_MAV_CMD command)
387
{
Don Gagne's avatar
Don Gagne committed
388 389 390
    Q_UNUSED(command);;
    _recalcChildItems();
    _recalcWaypointLines();
391
}