MissionEditor.cc 7.33 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 76
    _reSequence();
    
Don Gagne's avatar
Don Gagne committed
77
    emit missionItemsChanged();
Don Gagne's avatar
Don Gagne committed
78
    emit canEditChanged(_canEdit);
Don Gagne's avatar
Don Gagne committed
79 80 81 82 83 84 85
}

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

void MissionEditor::setMissionItems(void)
{
    Vehicle* activeVehicle = MultiVehicleManager::instance()->activeVehicle();
    
    if (activeVehicle) {
        activeVehicle->missionManager()->writeMissionItems(*_missionItems);
    }
}

101
int MissionEditor::addMissionItem(QGeoCoordinate coordinate)
Don Gagne's avatar
Don Gagne committed
102
{
Don Gagne's avatar
Don Gagne committed
103 104 105 106
    if (!_canEdit) {
        qWarning() << "addMissionItem called with _canEdit == false";
    }
    
107 108 109 110 111 112
    MissionItem * newItem = new MissionItem(this, _missionItems->count(), coordinate);
    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
113
    
114
    return _missionItems->count() - 1;
Don Gagne's avatar
Don Gagne committed
115 116
}

117
void MissionEditor::_reSequence(void)
Don Gagne's avatar
Don Gagne committed
118
{
119 120 121 122 123 124 125
    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
126 127 128 129 130
    if (!_canEdit) {
        qWarning() << "addMissionItem called with _canEdit == false";
        return;
    }
    
131 132 133 134 135 136
    _missionItems->removeAt(index);
    _reSequence();
}

void MissionEditor::moveUp(int index)
{
Don Gagne's avatar
Don Gagne committed
137 138 139 140 141
    if (!_canEdit) {
        qWarning() << "addMissionItem called with _canEdit == false";
        return;
    }
    
142 143 144 145 146 147 148 149 150
    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
151
    
152 153
    _missionItems->insert(index - 1, new MissionItem(item2, _missionItems));
    _missionItems->insert(index, new MissionItem(item1, _missionItems));
Don Gagne's avatar
Don Gagne committed
154
    
155
    _reSequence();
Don Gagne's avatar
Don Gagne committed
156 157
}

158
void MissionEditor::moveDown(int index)
Don Gagne's avatar
Don Gagne committed
159
{
Don Gagne's avatar
Don Gagne committed
160 161 162 163 164
    if (!_canEdit) {
        qWarning() << "addMissionItem called with _canEdit == false";
        return;
    }
    
165 166
    if (_missionItems->count() < 2 || index >= _missionItems->count() - 1) {
        return;
Don Gagne's avatar
Don Gagne committed
167
    }
168 169 170 171 172 173 174 175 176 177 178
    
    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
179
}
Don Gagne's avatar
Don Gagne committed
180 181 182 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 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252

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();
    }
    
    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);
        }
    }
}