ComplexMissionItem.cc 7.22 KB
Newer Older
1 2 3
/*===================================================================
QGroundControl Open Source Ground Control Station

4
(c) 2009, 2016 QGROUNDCONTROL PROJECT <http://www.qgroundcontrol.org>
5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23

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 "ComplexMissionItem.h"
24 25 26 27 28 29 30 31 32
#include "JsonHelper.h"
#include "MissionController.h"

const char* ComplexMissionItem::_jsonVersionKey =   "version";
const char* ComplexMissionItem::_jsonTypeKey =      "type";
const char* ComplexMissionItem::_jsonPolygonKey =   "polygon";
const char* ComplexMissionItem::_jsonIdKey =        "id";

const char* ComplexMissionItem::_complexType = "survey";
33 34

ComplexMissionItem::ComplexMissionItem(Vehicle* vehicle, QObject* parent)
35 36
    : VisualMissionItem(vehicle, parent)
    , _dirty(false)
37
{
38
    MissionItem missionItem;
39

40 41 42
    // FIXME: Bogus entries for testing
    _missionItems += new MissionItem(this);
    _missionItems += new MissionItem(this);
43 44 45
}

ComplexMissionItem::ComplexMissionItem(const ComplexMissionItem& other, QObject* parent)
46 47
    : VisualMissionItem(other, parent)
    , _dirty(false)
48 49 50 51 52 53 54 55 56 57 58 59 60 61
{

}

void ComplexMissionItem::clearPolygon(void)
{
    _polygonPath.clear();
    emit polygonPathChanged();
}

void ComplexMissionItem::addPolygonCoordinate(const QGeoCoordinate coordinate)
{
    _polygonPath << QVariant::fromValue(coordinate);
    emit polygonPathChanged();
62 63 64 65 66

    // FIXME: Hack, first polygon point sets entry coordinate
    if (_polygonPath.count() == 1) {
        setCoordinate(coordinate);
    }
67
}
68 69 70 71 72 73 74 75 76 77 78

int ComplexMissionItem::nextSequenceNumber(void) const
{
    return _sequenceNumber + _missionItems.count();
}

void ComplexMissionItem::setCoordinate(const QGeoCoordinate& coordinate)
{
    if (_coordinate != coordinate) {
        _coordinate = coordinate;
        emit coordinateChanged(_coordinate);
79 80 81 82
        _missionItems[0]->setCoordinate(coordinate);

        // FIXME: Hack
        _setExitCoordinate(coordinate);
83 84 85 86 87 88 89 90 91 92 93
    }
}

void ComplexMissionItem::setDirty(bool dirty)
{
   if (_dirty != dirty) {
       _dirty = dirty;
       emit dirtyChanged(_dirty);
   }
}

94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137
void ComplexMissionItem::save(QJsonObject& saveObject) const
{
    saveObject[_jsonVersionKey] =    1;
    saveObject[_jsonTypeKey] =       _complexType;
    saveObject[_jsonIdKey] =         sequenceNumber();

    // Polygon shape

    QJsonArray polygonArray;

    for (int i=0; i<_polygonPath.count(); i++) {
        const QVariant& polyVar = _polygonPath[i];

        QJsonValue jsonValue;
        JsonHelper::writeQGeoCoordinate(jsonValue, polyVar.value<QGeoCoordinate>(), false /* writeAltitude */);
        polygonArray += jsonValue;
    }

    saveObject[_jsonPolygonKey] = polygonArray;

    // Base mission items

    QJsonArray simpleItems;
    for (int i=0; i<_missionItems.count(); i++) {
        MissionItem* missionItem = _missionItems[i];

        QJsonObject jsonObject;
        missionItem->save(jsonObject);
        simpleItems.append(jsonObject);
    }
    saveObject[MissionController::jsonSimpleItemsKey] = simpleItems;
}

void ComplexMissionItem::setSequenceNumber(int sequenceNumber)
{
    VisualMissionItem::setSequenceNumber(sequenceNumber);

    // Update internal mission items to new numbering
    for (int i=0; i<_missionItems.count(); i++) {
        _missionItems[i]->setSequenceNumber(sequenceNumber++);
    }
}

void ComplexMissionItem::_clear(void)
138
{
139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 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
    // Clear old settings
    for (int i=0; i<_missionItems.count(); i++) {
        _missionItems[i]->deleteLater();
    }
    _missionItems.clear();
    _polygonPath.clear();
}


bool ComplexMissionItem::load(const QJsonObject& complexObject, QString& errorString)
{
    _clear();

    // Validate requires keys
    QStringList requiredKeys;
    requiredKeys << _jsonVersionKey << _jsonTypeKey << _jsonIdKey << _jsonPolygonKey << MissionController::jsonSimpleItemsKey;
    if (!JsonHelper::validateRequiredKeys(complexObject, requiredKeys, errorString)) {
        _clear();
        return false;
    }

    // Validate types
    QStringList keyList;
    QList<QJsonValue::Type> typeList;
    keyList << _jsonVersionKey << _jsonTypeKey << _jsonIdKey << _jsonPolygonKey << MissionController::jsonSimpleItemsKey;
    typeList << QJsonValue::Double << QJsonValue::String << QJsonValue::Double << QJsonValue::Array << QJsonValue::Array;
    if (!JsonHelper::validateKeyTypes(complexObject, keyList, typeList, errorString)) {
        _clear();
        return false;
    }

    // Version check
    if (complexObject[_jsonVersionKey].toInt() != 1) {
        errorString = tr("QGroundControl does not support this version of survey items");
        _clear();
        return false;
    }
    QString complexType = complexObject[_jsonTypeKey].toString();
    if (complexType != _complexType) {
        errorString = tr("QGroundControl does not support loading this complex mission item type: %1").arg(complexType);
        _clear();
        return false;
    }

    setSequenceNumber(complexObject[_jsonIdKey].toInt());

    // Polygon shape
    QJsonArray polygonArray(complexObject[_jsonPolygonKey].toArray());
    for (int i=0; i<polygonArray.count(); i++) {
        const QJsonValue& pointValue = polygonArray[i];

        QGeoCoordinate pointCoord;
        if (!JsonHelper::toQGeoCoordinate(pointValue, pointCoord, false /* altitudeRequired */, errorString)) {
            _clear();
            return false;
        }
        _polygonPath << QVariant::fromValue(pointCoord);
    }

    // Internal mission items
    QJsonArray itemArray(complexObject[MissionController::jsonSimpleItemsKey].toArray());
    for (int i=0; i<itemArray.count(); i++) {
        const QJsonValue& itemValue = itemArray[i];

        if (!itemValue.isObject()) {
            errorString = QStringLiteral("Mission item is not an object");
            _clear();
            return false;
        }

        MissionItem* item = new MissionItem(_vehicle, this);
        if (item->load(itemValue.toObject(), errorString)) {
            _missionItems.append(item);
        } else {
            _clear();
            return false;
        }
    }
217

218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237
    int itemCount = _missionItems.count();
    if (itemCount > 0) {
        setCoordinate(_missionItems[0]->coordinate());
        _setExitCoordinate(_missionItems[itemCount - 1]->coordinate());
    }

    return true;
}

void ComplexMissionItem::_setExitCoordinate(const QGeoCoordinate& coordinate)
{
    if (_exitCoordinate != coordinate) {
        _exitCoordinate = coordinate;
        emit exitCoordinateChanged(coordinate);

        int itemCount = _missionItems.count();
        if (itemCount > 0) {
            _missionItems[itemCount - 1]->setCoordinate(coordinate);
        }
    }
238
}