MissionEditor.cc 7.53 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"
Don Gagne's avatar
Don Gagne committed
29 30 31 32 33 34 35 36 37

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

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

MissionEditor::MissionEditor(QWidget *parent)
    : QGCQmlWidgetHolder(parent)
Don Gagne's avatar
Don Gagne committed
38
    , _missionItems(NULL)
Don Gagne's avatar
Don Gagne committed
39
    , _canEdit(true)
Don Gagne's avatar
Don Gagne committed
40 41 42 43 44 45 46
{
    // Get rid of layout default margins
    QLayout* pl = layout();
    if(pl) {
        pl->setContentsMargins(0,0,0,0);
    }
    
Don Gagne's avatar
Don Gagne committed
47 48 49 50 51 52 53 54 55
    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
56 57 58 59 60 61 62 63 64
    setContextPropertyObject("controller", this);

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

MissionEditor::~MissionEditor()
{
}

Don Gagne's avatar
Don Gagne committed
65 66 67 68 69 70
void MissionEditor::_newMissionItemsAvailable(void)
{
    if (_missionItems) {
        _missionItems->deleteLater();
    }
    
Don Gagne's avatar
Don Gagne committed
71 72 73 74
    MissionManager* missionManager = MultiVehicleManager::instance()->activeVehicle()->missionManager();
    
    _canEdit = missionManager->canEdit();
    _missionItems = missionManager->copyMissionItems();
75
    _reSequence();
Don Gagne's avatar
Don Gagne committed
76
    _missionItems->setDirty(false);
77
    
Don Gagne's avatar
Don Gagne committed
78
    emit missionItemsChanged();
Don Gagne's avatar
Don Gagne committed
79
    emit canEditChanged(_canEdit);
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 95 96 97 98
        activeVehicle->missionManager()->requestMissionItems();
    }
}

void MissionEditor::setMissionItems(void)
{
    Vehicle* activeVehicle = MultiVehicleManager::instance()->activeVehicle();
    
    if (activeVehicle) {
        activeVehicle->missionManager()->writeMissionItems(*_missionItems);
Don Gagne's avatar
Don Gagne committed
99
        _missionItems->setDirty(false);
Don Gagne's avatar
Don Gagne committed
100 101 102
    }
}

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

120
void MissionEditor::_reSequence(void)
Don Gagne's avatar
Don Gagne committed
121
{
122 123 124 125 126 127 128
    for (int i=0; i<_missionItems->count(); i++) {
        qobject_cast<MissionItem*>(_missionItems->get(i))->setSequenceNumber(i);
    }
}

void MissionEditor::removeMissionItem(int index)
{
Don Gagne's avatar
Don Gagne committed
129 130 131 132 133
    if (!_canEdit) {
        qWarning() << "addMissionItem called with _canEdit == false";
        return;
    }
    
134 135 136 137 138 139
    _missionItems->removeAt(index);
    _reSequence();
}

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
    
158
    _reSequence();
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 181
    
    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));
    
    _reSequence();
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 193 194 195 196 197 198 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

void MissionEditor::loadMissionFromFile(void)
{
    QString errorString;
    QString filename = QGCFileDialog::getOpenFileName(NULL, "Select Mission File to load");
    
    if (filename.isEmpty()) {
        return;
    }
    
    _missionItems->clear();
    _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
230
    _missionItems->setDirty(false);
Don Gagne's avatar
Don Gagne committed
231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255
    emit canEditChanged(_canEdit);
}

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
256 257
    
    _missionItems->setDirty(false);
Don Gagne's avatar
Don Gagne committed
258
}