SimpleMissionItem.cc 31.1 KB
Newer Older
1 2 3 4 5 6 7 8
/****************************************************************************
 *
 *   (c) 2009-2016 QGROUNDCONTROL PROJECT <http://www.qgroundcontrol.org>
 *
 * QGroundControl is licensed according to the terms in the file
 * COPYING.md in the root of the source code directory.
 *
 ****************************************************************************/
9 10


11 12 13
#include <QStringList>
#include <QDebug>

14
#include "SimpleMissionItem.h"
15 16 17
#include "FirmwarePluginManager.h"
#include "QGCApplication.h"
#include "JsonHelper.h"
18 19
#include "MissionCommandTree.h"
#include "MissionCommandUIInfo.h"
20
#include "QGroundControlQmlGlobal.h"
21
#include "SettingsManager.h"
22

Don Gagne's avatar
Don Gagne committed
23 24 25 26 27 28
FactMetaData* SimpleMissionItem::_altitudeMetaData =        NULL;
FactMetaData* SimpleMissionItem::_commandMetaData =         NULL;
FactMetaData* SimpleMissionItem::_defaultParamMetaData =    NULL;
FactMetaData* SimpleMissionItem::_frameMetaData =           NULL;
FactMetaData* SimpleMissionItem::_latitudeMetaData =        NULL;
FactMetaData* SimpleMissionItem::_longitudeMetaData =       NULL;
29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48

struct EnumInfo_s {
    const char *    label;
    MAV_FRAME       frame;
};

static const struct EnumInfo_s _rgMavFrameInfo[] = {
{ "MAV_FRAME_GLOBAL",                   MAV_FRAME_GLOBAL },
{ "MAV_FRAME_LOCAL_NED",                MAV_FRAME_LOCAL_NED },
{ "MAV_FRAME_MISSION",                  MAV_FRAME_MISSION },
{ "MAV_FRAME_GLOBAL_RELATIVE_ALT",      MAV_FRAME_GLOBAL_RELATIVE_ALT },
{ "MAV_FRAME_LOCAL_ENU",                MAV_FRAME_LOCAL_ENU },
{ "MAV_FRAME_GLOBAL_INT",               MAV_FRAME_GLOBAL_INT },
{ "MAV_FRAME_GLOBAL_RELATIVE_ALT_INT",  MAV_FRAME_GLOBAL_RELATIVE_ALT_INT },
{ "MAV_FRAME_LOCAL_OFFSET_NED",         MAV_FRAME_LOCAL_OFFSET_NED },
{ "MAV_FRAME_BODY_NED",                 MAV_FRAME_BODY_NED },
{ "MAV_FRAME_BODY_OFFSET_NED",          MAV_FRAME_BODY_OFFSET_NED },
{ "MAV_FRAME_GLOBAL_TERRAIN_ALT",       MAV_FRAME_GLOBAL_TERRAIN_ALT },
{ "MAV_FRAME_GLOBAL_TERRAIN_ALT_INT",   MAV_FRAME_GLOBAL_TERRAIN_ALT_INT },
};
49 50

SimpleMissionItem::SimpleMissionItem(Vehicle* vehicle, QObject* parent)
51 52
    : VisualMissionItem(vehicle, parent)
    , _rawEdit(false)
53
    , _dirty(false)
54
    , _ignoreDirtyChangeSignals(false)
55
    , _speedSection(NULL)
56
    , _cameraSection(NULL)
57
    , _commandTree(qgcApp()->toolbox()->missionCommandTree())
58 59 60 61 62 63 64 65 66 67 68
    , _altitudeRelativeToHomeFact   (0, "Altitude is relative to home", FactMetaData::valueTypeUint32)
    , _supportedCommandFact         (0, "Command:",                     FactMetaData::valueTypeUint32)
    , _param1MetaData(FactMetaData::valueTypeDouble)
    , _param2MetaData(FactMetaData::valueTypeDouble)
    , _param3MetaData(FactMetaData::valueTypeDouble)
    , _param4MetaData(FactMetaData::valueTypeDouble)
    , _param5MetaData(FactMetaData::valueTypeDouble)
    , _param6MetaData(FactMetaData::valueTypeDouble)
    , _param7MetaData(FactMetaData::valueTypeDouble)
    , _syncingAltitudeRelativeToHomeAndFrame    (false)
    , _syncingHeadingDegreesAndParam4           (false)
69
{
Don Gagne's avatar
Don Gagne committed
70
    _editorQml = QStringLiteral("qrc:/qml/SimpleItemEditor.qml");
71

72
    _altitudeRelativeToHomeFact.setRawValue(true);
73

74 75
    _setupMetaData();
    _connectSignals();
76
    _updateOptionalSections();
77 78

    setDefaultsForCommand();
79
    _rebuildFacts();
80

DonLakeFlyer's avatar
DonLakeFlyer committed
81
    connect(&_missionItem, &MissionItem::specifiedFlightSpeedChanged, this, &SimpleMissionItem::specifiedFlightSpeedChanged);
82

83 84 85
    connect(this, &SimpleMissionItem::sequenceNumberChanged,        this, &SimpleMissionItem::lastSequenceNumberChanged);
    connect(this, &SimpleMissionItem::cameraSectionChanged,         this, &SimpleMissionItem::_setDirtyFromSignal);
    connect(this, &SimpleMissionItem::cameraSectionChanged,         this, &SimpleMissionItem::_updateLastSequenceNumber);
86 87
}

88 89 90 91 92
SimpleMissionItem::SimpleMissionItem(Vehicle* vehicle, const MissionItem& missionItem, QObject* parent)
    : VisualMissionItem(vehicle, parent)
    , _missionItem(missionItem)
    , _rawEdit(false)
    , _dirty(false)
93
    , _ignoreDirtyChangeSignals(false)
94
    , _speedSection(NULL)
95
    , _cameraSection(NULL)
96
    , _commandTree(qgcApp()->toolbox()->missionCommandTree())
97 98 99 100 101 102 103 104 105 106 107
    , _altitudeRelativeToHomeFact   (0, "Altitude is relative to home", FactMetaData::valueTypeUint32)
    , _supportedCommandFact         (0, "Command:",                     FactMetaData::valueTypeUint32)
    , _param1MetaData(FactMetaData::valueTypeDouble)
    , _param2MetaData(FactMetaData::valueTypeDouble)
    , _param3MetaData(FactMetaData::valueTypeDouble)
    , _param4MetaData(FactMetaData::valueTypeDouble)
    , _param5MetaData(FactMetaData::valueTypeDouble)
    , _param6MetaData(FactMetaData::valueTypeDouble)
    , _param7MetaData(FactMetaData::valueTypeDouble)
    , _syncingAltitudeRelativeToHomeAndFrame    (false)
    , _syncingHeadingDegreesAndParam4           (false)
108
{
Don Gagne's avatar
Don Gagne committed
109 110
    _editorQml = QStringLiteral("qrc:/qml/SimpleItemEditor.qml");

111
    _altitudeRelativeToHomeFact.setRawValue(true);
112
    _isCurrentItem = missionItem.isCurrentItem();
113 114 115

    _setupMetaData();
    _connectSignals();
116
    _updateOptionalSections();
117
    _syncFrameToAltitudeRelativeToHome();
118
    _rebuildFacts();
119 120 121
}

SimpleMissionItem::SimpleMissionItem(const SimpleMissionItem& other, QObject* parent)
122 123 124 125
    : VisualMissionItem(other, parent)
    , _missionItem(other._vehicle)
    , _rawEdit(false)
    , _dirty(false)
126
    , _ignoreDirtyChangeSignals(false)
127
    , _speedSection(NULL)
128
    , _cameraSection(NULL)
129
    , _commandTree(qgcApp()->toolbox()->missionCommandTree())
130 131 132 133 134 135 136 137
    , _altitudeRelativeToHomeFact   (0, "Altitude is relative to home", FactMetaData::valueTypeUint32)
    , _supportedCommandFact         (0, "Command:",                     FactMetaData::valueTypeUint32)
    , _param1MetaData(FactMetaData::valueTypeDouble)
    , _param2MetaData(FactMetaData::valueTypeDouble)
    , _param3MetaData(FactMetaData::valueTypeDouble)
    , _param4MetaData(FactMetaData::valueTypeDouble)
    , _syncingAltitudeRelativeToHomeAndFrame    (false)
    , _syncingHeadingDegreesAndParam4           (false)
138
{
Don Gagne's avatar
Don Gagne committed
139 140
    _editorQml = QStringLiteral("qrc:/qml/SimpleItemEditor.qml");

141 142
    _setupMetaData();
    _connectSignals();
143
    _updateOptionalSections();
144

145
    *this = other;
146 147

    _rebuildFacts();
148 149 150 151
}

const SimpleMissionItem& SimpleMissionItem::operator=(const SimpleMissionItem& other)
{
152
    VisualMissionItem::operator=(other);
153 154 155 156 157

    setRawEdit(other._rawEdit);
    setDirty(other._dirty);
    setHomePositionSpecialCase(other._homePositionSpecialCase);
    _syncFrameToAltitudeRelativeToHome();
158
    _rebuildFacts();
159 160 161

    return *this;
}
162 163 164 165 166 167 168 169 170 171 172 173 174 175 176

void SimpleMissionItem::_connectSignals(void)
{
    // Connect to change signals to track dirty state
    connect(&_missionItem._param1Fact,  &Fact::valueChanged,                    this, &SimpleMissionItem::_setDirtyFromSignal);
    connect(&_missionItem._param2Fact,  &Fact::valueChanged,                    this, &SimpleMissionItem::_setDirtyFromSignal);
    connect(&_missionItem._param3Fact,  &Fact::valueChanged,                    this, &SimpleMissionItem::_setDirtyFromSignal);
    connect(&_missionItem._param4Fact,  &Fact::valueChanged,                    this, &SimpleMissionItem::_setDirtyFromSignal);
    connect(&_missionItem._param5Fact,  &Fact::valueChanged,                    this, &SimpleMissionItem::_setDirtyFromSignal);
    connect(&_missionItem._param6Fact,  &Fact::valueChanged,                    this, &SimpleMissionItem::_setDirtyFromSignal);
    connect(&_missionItem._param7Fact,  &Fact::valueChanged,                    this, &SimpleMissionItem::_setDirtyFromSignal);
    connect(&_missionItem._frameFact,   &Fact::valueChanged,                    this, &SimpleMissionItem::_setDirtyFromSignal);
    connect(&_missionItem._commandFact, &Fact::valueChanged,                    this, &SimpleMissionItem::_setDirtyFromSignal);
    connect(&_missionItem,              &MissionItem::sequenceNumberChanged,    this, &SimpleMissionItem::_setDirtyFromSignal);

Ricardo de Almeida Gonzaga's avatar
Ricardo de Almeida Gonzaga committed
177
    // Values from these facts must propagate back and forth between the real object storage
178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194
    connect(&_altitudeRelativeToHomeFact,   &Fact::valueChanged,    this, &SimpleMissionItem::_syncAltitudeRelativeToHomeToFrame);
    connect(&_missionItem._frameFact,       &Fact::valueChanged,    this, &SimpleMissionItem::_syncFrameToAltitudeRelativeToHome);

    // These are coordinate parameters, they must emit coordinateChanged signal
    connect(&_missionItem._param5Fact, &Fact::valueChanged, this, &SimpleMissionItem::_sendCoordinateChanged);
    connect(&_missionItem._param6Fact, &Fact::valueChanged, this, &SimpleMissionItem::_sendCoordinateChanged);
    connect(&_missionItem._param7Fact, &Fact::valueChanged, this, &SimpleMissionItem::_sendCoordinateChanged);

    // The following changes may also change friendlyEditAllowed
    connect(&_missionItem._autoContinueFact,    &Fact::valueChanged, this, &SimpleMissionItem::_sendFriendlyEditAllowedChanged);
    connect(&_missionItem._commandFact,         &Fact::valueChanged, this, &SimpleMissionItem::_sendFriendlyEditAllowedChanged);
    connect(&_missionItem._frameFact,           &Fact::valueChanged, this, &SimpleMissionItem::_sendFriendlyEditAllowedChanged);

    // A command change triggers a number of other changes as well.
    connect(&_missionItem._commandFact, &Fact::valueChanged, this, &SimpleMissionItem::setDefaultsForCommand);
    connect(&_missionItem._commandFact, &Fact::valueChanged, this, &SimpleMissionItem::commandNameChanged);
    connect(&_missionItem._commandFact, &Fact::valueChanged, this, &SimpleMissionItem::commandDescriptionChanged);
195
    connect(&_missionItem._commandFact, &Fact::valueChanged, this, &SimpleMissionItem::abbreviationChanged);
196
    connect(&_missionItem._commandFact, &Fact::valueChanged, this, &SimpleMissionItem::specifiesCoordinateChanged);
197
    connect(&_missionItem._commandFact, &Fact::valueChanged, this, &SimpleMissionItem::specifiesAltitudeOnlyChanged);
198 199 200
    connect(&_missionItem._commandFact, &Fact::valueChanged, this, &SimpleMissionItem::isStandaloneCoordinateChanged);

    // Whenever these properties change the ui model changes as well
201 202
    connect(this, &SimpleMissionItem::commandChanged, this, &SimpleMissionItem::_rebuildFacts);
    connect(this, &SimpleMissionItem::rawEditChanged, this, &SimpleMissionItem::_rebuildFacts);
203 204 205 206

    // These fact signals must alway signal out through SimpleMissionItem signals
    connect(&_missionItem._commandFact,     &Fact::valueChanged, this, &SimpleMissionItem::_sendCommandChanged);
    connect(&_missionItem._frameFact,       &Fact::valueChanged, this, &SimpleMissionItem::_sendFrameChanged);
207

Ricardo de Almeida Gonzaga's avatar
Ricardo de Almeida Gonzaga committed
208
    // Sequence number is kept in mission iteem, so we need to propagate signal up as well
209
    connect(&_missionItem, &MissionItem::sequenceNumberChanged, this, &SimpleMissionItem::sequenceNumberChanged);
210 211 212 213 214 215 216 217 218
}

void SimpleMissionItem::_setupMetaData(void)
{
    QStringList enumStrings;
    QVariantList enumValues;

    if (!_altitudeMetaData) {
        _altitudeMetaData = new FactMetaData(FactMetaData::valueTypeDouble);
219
        _altitudeMetaData->setRawUnits("m");
220 221 222 223
        _altitudeMetaData->setDecimalPlaces(2);

        enumStrings.clear();
        enumValues.clear();
224 225 226 227
        MissionCommandTree* commandTree = qgcApp()->toolbox()->missionCommandTree();
        foreach (const MAV_CMD command, commandTree->allCommandIds()) {
            enumStrings.append(commandTree->rawName(command));
            enumValues.append(QVariant((int)command));
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 253 254 255 256 257 258 259 260 261 262 263
        }
        _commandMetaData = new FactMetaData(FactMetaData::valueTypeUint32);
        _commandMetaData->setEnumInfo(enumStrings, enumValues);

        _defaultParamMetaData = new FactMetaData(FactMetaData::valueTypeDouble);
        _defaultParamMetaData->setDecimalPlaces(7);

        enumStrings.clear();
        enumValues.clear();
        for (size_t i=0; i<sizeof(_rgMavFrameInfo)/sizeof(_rgMavFrameInfo[0]); i++) {
            const struct EnumInfo_s* mavFrameInfo = &_rgMavFrameInfo[i];

            enumStrings.append(mavFrameInfo->label);
            enumValues.append(QVariant(mavFrameInfo->frame));
        }
        _frameMetaData = new FactMetaData(FactMetaData::valueTypeUint32);
        _frameMetaData->setEnumInfo(enumStrings, enumValues);

        _latitudeMetaData = new FactMetaData(FactMetaData::valueTypeDouble);
        _latitudeMetaData->setRawUnits("deg");
        _latitudeMetaData->setDecimalPlaces(7);

        _longitudeMetaData = new FactMetaData(FactMetaData::valueTypeDouble);
        _longitudeMetaData->setRawUnits("deg");
        _longitudeMetaData->setDecimalPlaces(7);

    }

    _missionItem._commandFact.setMetaData(_commandMetaData);
    _missionItem._frameFact.setMetaData(_frameMetaData);
}

SimpleMissionItem::~SimpleMissionItem()
{    
}

264
void SimpleMissionItem::save(QJsonArray&  missionItems)
265
{
266 267 268 269 270 271 272 273 274 275 276
    QList<MissionItem*> items;

    appendMissionItems(items, this);

    for (int i=0; i<items.count(); i++) {
        MissionItem* item = items[i];
        QJsonObject saveObject;
        item->save(saveObject);
        missionItems.append(saveObject);
        item->deleteLater();
    }
277 278 279 280
}

bool SimpleMissionItem::load(QTextStream &loadStream)
{
281 282 283 284 285
    bool success;
    if ((success = _missionItem.load(loadStream))) {
        _updateOptionalSections();
    }
    return success;
286 287
}

Don Gagne's avatar
Don Gagne committed
288
bool SimpleMissionItem::load(const QJsonObject& json, int sequenceNumber, QString& errorString)
289
{
290 291 292 293 294
    bool success;
    if ((success = _missionItem.load(json, sequenceNumber, errorString))) {
        _updateOptionalSections();
    }
    return success;
295 296 297 298
}

bool SimpleMissionItem::isStandaloneCoordinate(void) const
{
299 300 301
    const MissionCommandUIInfo* uiInfo = _commandTree->getUIInfo(_vehicle, (MAV_CMD)command());
    if (uiInfo) {
        return uiInfo->isStandaloneCoordinate();
302 303 304 305 306 307 308
    } else {
        return false;
    }
}

bool SimpleMissionItem::specifiesCoordinate(void) const
{
309 310 311
    const MissionCommandUIInfo* uiInfo = _commandTree->getUIInfo(_vehicle, (MAV_CMD)command());
    if (uiInfo) {
        return uiInfo->specifiesCoordinate();
312 313 314 315 316
    } else {
        return false;
    }
}

317 318 319 320 321 322 323 324 325 326
bool SimpleMissionItem::specifiesAltitudeOnly(void) const
{
    const MissionCommandUIInfo* uiInfo = _commandTree->getUIInfo(_vehicle, (MAV_CMD)command());
    if (uiInfo) {
        return uiInfo->specifiesAltitudeOnly();
    } else {
        return false;
    }
}

327 328
QString SimpleMissionItem::commandDescription(void) const
{
329 330 331
    const MissionCommandUIInfo* uiInfo = _commandTree->getUIInfo(_vehicle, (MAV_CMD)command());
    if (uiInfo) {
        return uiInfo->description();
332 333 334 335 336 337 338 339
    } else {
        qWarning() << "Should not ask for command description on unknown command";
        return commandName();
    }
}

QString SimpleMissionItem::commandName(void) const
{
340 341 342
    const MissionCommandUIInfo* uiInfo = _commandTree->getUIInfo(_vehicle, (MAV_CMD)command());
    if (uiInfo) {
        return uiInfo->friendlyName();
343 344
    } else {
        qWarning() << "Request for command name on unknown command";
345
        return tr("Unknown: %1").arg(command());
346 347 348
    }
}

349 350 351
QString SimpleMissionItem::abbreviation() const
{
    if (homePosition())
352
        return tr("H");
353 354 355

    switch(command()) {
    case MavlinkQmlSingleton::MAV_CMD_NAV_TAKEOFF:
356
        return tr("Takeoff");
357
    case MavlinkQmlSingleton::MAV_CMD_NAV_LAND:
358
        return tr("Land");
359
    case MavlinkQmlSingleton::MAV_CMD_NAV_VTOL_TAKEOFF:
360
        return tr("VTOL Takeoff");
361
    case MavlinkQmlSingleton::MAV_CMD_NAV_VTOL_LAND:
362 363 364 365 366
        return tr("VTOL Land");
    case MavlinkQmlSingleton::MAV_CMD_DO_SET_ROI:
        return tr("ROI");
    default:
        return QString();
367 368 369
    }
}

370
void SimpleMissionItem::_rebuildTextFieldFacts(void)
371
{
372
    _textFieldFacts.clear();
373 374
    
    if (rawEdit()) {
375
        _missionItem._param1Fact._setName("Param1");
376
        _missionItem._param1Fact.setMetaData(_defaultParamMetaData);
377
        _textFieldFacts.append(&_missionItem._param1Fact);
378
        _missionItem._param2Fact._setName("Param2");
379
        _missionItem._param2Fact.setMetaData(_defaultParamMetaData);
380
        _textFieldFacts.append(&_missionItem._param2Fact);
381
        _missionItem._param3Fact._setName("Param3");
382
        _missionItem._param3Fact.setMetaData(_defaultParamMetaData);
383
        _textFieldFacts.append(&_missionItem._param3Fact);
384
        _missionItem._param4Fact._setName("Param4");
385
        _missionItem._param4Fact.setMetaData(_defaultParamMetaData);
386
        _textFieldFacts.append(&_missionItem._param4Fact);
387
        _missionItem._param5Fact._setName("Lat/X");
388
        _missionItem._param5Fact.setMetaData(_defaultParamMetaData);
389
        _textFieldFacts.append(&_missionItem._param5Fact);
390
        _missionItem._param6Fact._setName("Lon/Y");
391
        _missionItem._param6Fact.setMetaData(_defaultParamMetaData);
392
        _textFieldFacts.append(&_missionItem._param6Fact);
393
        _missionItem._param7Fact._setName("Alt/Z");
394
        _missionItem._param7Fact.setMetaData(_defaultParamMetaData);
395
        _textFieldFacts.append(&_missionItem._param7Fact);
396
    } else {
397 398
        _ignoreDirtyChangeSignals = true;

399 400 401 402 403 404 405 406 407 408
        MAV_CMD command;
        if (_homePositionSpecialCase) {
            command = MAV_CMD_NAV_LAST;
        } else {
            command = _missionItem.command();
        }

        Fact*           rgParamFacts[7] =       { &_missionItem._param1Fact, &_missionItem._param2Fact, &_missionItem._param3Fact, &_missionItem._param4Fact, &_missionItem._param5Fact, &_missionItem._param6Fact, &_missionItem._param7Fact };
        FactMetaData*   rgParamMetaData[7] =    { &_param1MetaData, &_param2MetaData, &_param3MetaData, &_param4MetaData, &_param5MetaData, &_param6MetaData, &_param7MetaData };

409 410
        const MissionCommandUIInfo* uiInfo = _commandTree->getUIInfo(_vehicle, command);

411
        for (int i=1; i<=7; i++) {
412
            const MissionCmdParamInfo* paramInfo = uiInfo->getParamInfo(i);
413

414
            if (paramInfo && paramInfo->enumStrings().count() == 0 && !paramInfo->nanUnchanged()) {
415 416 417 418 419 420 421
                Fact*               paramFact =     rgParamFacts[i-1];
                FactMetaData*       paramMetaData = rgParamMetaData[i-1];

                paramFact->_setName(paramInfo->label());
                paramMetaData->setDecimalPlaces(paramInfo->decimalPlaces());
                paramMetaData->setRawUnits(paramInfo->units());
                paramFact->setMetaData(paramMetaData);
422
                _textFieldFacts.append(paramFact);
423 424 425
            }
        }

426
        if (uiInfo->specifiesCoordinate() || uiInfo->specifiesAltitudeOnly()) {
427
            _missionItem._param7Fact._setName("Altitude");
428
            _missionItem._param7Fact.setMetaData(_altitudeMetaData);
429
            _textFieldFacts.append(&_missionItem._param7Fact);
430
        }
431 432

        _ignoreDirtyChangeSignals = false;
433 434 435
    }
}

436
void SimpleMissionItem::_rebuildNaNFacts(void)
437
{
438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457
    _nanFacts.clear();

    if (!rawEdit()) {
        _ignoreDirtyChangeSignals = true;

        MAV_CMD command;
        if (_homePositionSpecialCase) {
            command = MAV_CMD_NAV_LAST;
        } else {
            command = _missionItem.command();
        }

        Fact*           rgParamFacts[7] =       { &_missionItem._param1Fact, &_missionItem._param2Fact, &_missionItem._param3Fact, &_missionItem._param4Fact, &_missionItem._param5Fact, &_missionItem._param6Fact, &_missionItem._param7Fact };
        FactMetaData*   rgParamMetaData[7] =    { &_param1MetaData, &_param2MetaData, &_param3MetaData, &_param4MetaData, &_param5MetaData, &_param6MetaData, &_param7MetaData };

        const MissionCommandUIInfo* uiInfo = _commandTree->getUIInfo(_vehicle, command);

        for (int i=1; i<=7; i++) {
            const MissionCmdParamInfo* paramInfo = uiInfo->getParamInfo(i);

458 459 460 461 462 463 464
            if (paramInfo && paramInfo->nanUnchanged()) {
                // Show hide Heading field on waypoint based on vehicle yaw to next waypoint setting. This needs to come from the actual vehicle if it exists
                // and not _vehicle which is always offline.
                Vehicle* firmwareVehicle = qgcApp()->toolbox()->multiVehicleManager()->activeVehicle();
                if (!firmwareVehicle) {
                    firmwareVehicle = _vehicle;
                }
465
                bool hideWaypointHeading = (command == MAV_CMD_NAV_WAYPOINT || command == MAV_CMD_NAV_TAKEOFF) && (i == 4) && firmwareVehicle->firmwarePlugin()->vehicleYawsToNextWaypointInMission(firmwareVehicle);
466 467 468
                if (hideWaypointHeading) {
                    continue;
                }
469

470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487
                Fact*               paramFact =     rgParamFacts[i-1];
                FactMetaData*       paramMetaData = rgParamMetaData[i-1];

                paramFact->_setName(paramInfo->label());
                paramMetaData->setDecimalPlaces(paramInfo->decimalPlaces());
                paramMetaData->setRawUnits(paramInfo->units());
                paramFact->setMetaData(paramMetaData);
                _nanFacts.append(paramFact);
            }
        }

        _ignoreDirtyChangeSignals = false;
    }
}

void SimpleMissionItem::_rebuildCheckboxFacts(void)
{
    _checkboxFacts.clear();
488 489

    if (rawEdit()) {
490
        _checkboxFacts.append(&_missionItem._autoContinueFact);
491
    } else if ((specifiesCoordinate() || specifiesAltitudeOnly()) && !_homePositionSpecialCase) {
492
        _checkboxFacts.append(&_altitudeRelativeToHomeFact);
493 494 495
    }
}

496
void SimpleMissionItem::_rebuildComboBoxFacts(void)
497
{
498
    _comboboxFacts.clear();
499 500

    if (rawEdit()) {
501 502
        _comboboxFacts.append(&_missionItem._commandFact);
        _comboboxFacts.append(&_missionItem._frameFact);
503 504 505 506 507 508 509 510 511 512 513 514
    } else {
        Fact*           rgParamFacts[7] =       { &_missionItem._param1Fact, &_missionItem._param2Fact, &_missionItem._param3Fact, &_missionItem._param4Fact, &_missionItem._param5Fact, &_missionItem._param6Fact, &_missionItem._param7Fact };
        FactMetaData*   rgParamMetaData[7] =    { &_param1MetaData, &_param2MetaData, &_param3MetaData, &_param4MetaData, &_param5MetaData, &_param6MetaData, &_param7MetaData };

        MAV_CMD command;
        if (_homePositionSpecialCase) {
            command = MAV_CMD_NAV_LAST;
        } else {
            command = (MAV_CMD)this->command();
        }

        for (int i=1; i<=7; i++) {
515
            const MissionCmdParamInfo* paramInfo = _commandTree->getUIInfo(_vehicle, command)->getParamInfo(i);
516

517
            if (paramInfo && paramInfo->enumStrings().count() != 0) {
518 519 520 521 522 523 524 525
                Fact*               paramFact =     rgParamFacts[i-1];
                FactMetaData*       paramMetaData = rgParamMetaData[i-1];

                paramFact->_setName(paramInfo->label());
                paramMetaData->setDecimalPlaces(paramInfo->decimalPlaces());
                paramMetaData->setEnumInfo(paramInfo->enumStrings(), paramInfo->enumValues());
                paramMetaData->setRawUnits(paramInfo->units());
                paramFact->setMetaData(paramMetaData);
526
                _comboboxFacts.append(paramFact);
527 528 529
            }
        }
    }
530
}
531

532 533 534 535 536 537
void SimpleMissionItem::_rebuildFacts(void)
{
    _rebuildTextFieldFacts();
    _rebuildNaNFacts();
    _rebuildCheckboxFacts();
    _rebuildComboBoxFacts();
538 539 540 541
}

bool SimpleMissionItem::friendlyEditAllowed(void) const
{
542 543
    const MissionCommandUIInfo* uiInfo = _commandTree->getUIInfo(_vehicle, (MAV_CMD)command());
    if (uiInfo && uiInfo->friendlyEdit()) {
544 545 546 547
        if (!_missionItem.autoContinue()) {
            return false;
        }

548
        if (specifiesCoordinate() || specifiesAltitudeOnly()) {
549 550 551 552 553 554 555 556 557 558
            MAV_FRAME frame = _missionItem.frame();
            switch (frame) {
            case MAV_FRAME_GLOBAL:
            case MAV_FRAME_GLOBAL_RELATIVE_ALT:
                return true;
                break;

            default:
                return false;
            }
559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581
        }

        return true;
    }

    return false;
}

bool SimpleMissionItem::rawEdit(void) const
{
    return _rawEdit || !friendlyEditAllowed();
}

void SimpleMissionItem::setRawEdit(bool rawEdit)
{
    if (this->rawEdit() != rawEdit) {
        _rawEdit = rawEdit;
        emit rawEditChanged(this->rawEdit());
    }
}

void SimpleMissionItem::setDirty(bool dirty)
{
582
    if (!_homePositionSpecialCase || (_dirty != dirty)) {
583
        _dirty = dirty;
584 585 586 587
        if (!dirty) {
            _cameraSection->setDirty(false);
            _speedSection->setDirty(false);
        }
588
        emit dirtyChanged(dirty);
589 590 591 592 593
    }
}

void SimpleMissionItem::_setDirtyFromSignal(void)
{
594 595 596
    if (!_ignoreDirtyChangeSignals) {
        setDirty(true);
    }
597 598 599 600 601 602 603 604 605 606 607 608
}

void SimpleMissionItem::_sendCoordinateChanged(void)
{
    emit coordinateChanged(coordinate());
}

void SimpleMissionItem::_syncAltitudeRelativeToHomeToFrame(const QVariant& value)
{
    if (!_syncingAltitudeRelativeToHomeAndFrame) {
        _syncingAltitudeRelativeToHomeAndFrame = true;
        _missionItem.setFrame(value.toBool() ? MAV_FRAME_GLOBAL_RELATIVE_ALT : MAV_FRAME_GLOBAL);
609
        emit coordinateHasRelativeAltitudeChanged(value.toBool());
610 611 612 613 614 615 616 617 618
        _syncingAltitudeRelativeToHomeAndFrame = false;
    }
}

void SimpleMissionItem::_syncFrameToAltitudeRelativeToHome(void)
{
    if (!_syncingAltitudeRelativeToHomeAndFrame) {
        _syncingAltitudeRelativeToHomeAndFrame = true;
        _altitudeRelativeToHomeFact.setRawValue(relativeAltitude());
619
        emit coordinateHasRelativeAltitudeChanged(_altitudeRelativeToHomeFact.rawValue().toBool());
620 621 622 623 624 625 626
        _syncingAltitudeRelativeToHomeAndFrame = false;
    }
}

void SimpleMissionItem::setDefaultsForCommand(void)
{
    // We set these global defaults first, then if there are param defaults they will get reset
627
    _missionItem.setParam7(qgcApp()->toolbox()->settingsManager()->appSettings()->defaultMissionItemAltitude()->rawValue().toDouble());
628 629

    MAV_CMD command = (MAV_CMD)this->command();
630 631 632 633 634 635 636 637
    const MissionCommandUIInfo* uiInfo = _commandTree->getUIInfo(_vehicle, command);
    if (uiInfo) {
        for (int i=1; i<=7; i++) {
            const MissionCmdParamInfo* paramInfo = uiInfo->getParamInfo(i);
            if (paramInfo) {
                Fact* rgParamFacts[7] = { &_missionItem._param1Fact, &_missionItem._param2Fact, &_missionItem._param3Fact, &_missionItem._param4Fact, &_missionItem._param5Fact, &_missionItem._param6Fact, &_missionItem._param7Fact };
                rgParamFacts[paramInfo->param()-1]->setRawValue(paramInfo->defaultValue());
            }
638 639 640
        }
    }

641 642
    switch (command) {
    case MAV_CMD_NAV_WAYPOINT:
643 644 645
        // We default all acceptance radius to 0. This allows flight controller to be in control of
        // accept radius.
        _missionItem.setParam2(0);
646 647 648
        break;

    case MAV_CMD_NAV_LAND:
Don Gagne's avatar
Don Gagne committed
649
    case MAV_CMD_NAV_VTOL_LAND:
650 651
        _missionItem.setParam7(0);
        break;
Donald Gagne's avatar
Donald Gagne committed
652 653
    default:
        break;
654 655 656
    }

    _missionItem.setAutoContinue(true);
657
    _missionItem.setFrame((specifiesCoordinate() || specifiesAltitudeOnly()) ? MAV_FRAME_GLOBAL_RELATIVE_ALT : MAV_FRAME_MISSION);
658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677
    setRawEdit(false);
}

void SimpleMissionItem::_sendFrameChanged(void)
{
    emit frameChanged(_missionItem.frame());
}

void SimpleMissionItem::_sendCommandChanged(void)
{
    emit commandChanged(command());
}

void SimpleMissionItem::_sendFriendlyEditAllowedChanged(void)
{
    emit friendlyEditAllowedChanged(friendlyEditAllowed());
}

QString SimpleMissionItem::category(void) const
{
678
    return _commandTree->getUIInfo(_vehicle, (MAV_CMD)command())->category();
679 680 681 682 683 684
}

void SimpleMissionItem::setCommand(MavlinkQmlSingleton::Qml_MAV_CMD command)
{
    if ((MAV_CMD)command != _missionItem.command()) {
        _missionItem.setCommand((MAV_CMD)command);
685
        _updateOptionalSections();
686 687 688 689 690 691 692 693 694
    }
}

void SimpleMissionItem::setCoordinate(const QGeoCoordinate& coordinate)
{
    if (_missionItem.coordinate() != coordinate) {
        _missionItem.setCoordinate(coordinate);
    }
}
695 696 697

void SimpleMissionItem::setSequenceNumber(int sequenceNumber)
{
698 699 700 701 702 703
    if (_missionItem.sequenceNumber() != sequenceNumber) {
        _missionItem.setSequenceNumber(sequenceNumber);
        emit sequenceNumberChanged(sequenceNumber);
        // This is too likely to ignore
        emit abbreviationChanged();
    }
704
}
705

DonLakeFlyer's avatar
DonLakeFlyer committed
706
double SimpleMissionItem::specifiedFlightSpeed(void)
707
{
708 709 710 711 712
    if (_speedSection->specifyFlightSpeed()) {
        return _speedSection->flightSpeed()->rawValue().toDouble();
    } else {
        return missionItem().specifiedFlightSpeed();
    }
DonLakeFlyer's avatar
DonLakeFlyer committed
713 714 715 716 717
}

double SimpleMissionItem::specifiedGimbalYaw(void)
{
    return _cameraSection->available() ? _cameraSection->specifiedGimbalYaw() : missionItem().specifiedGimbalYaw();
718
}
719

720
bool SimpleMissionItem::scanForSections(QmlObjectListModel* visualItems, int scanIndex, Vehicle* vehicle)
721
{
722
    bool sectionFound = false;
723

724
    Q_UNUSED(vehicle);
725 726

    if (_cameraSection->available()) {
727 728 729 730
        sectionFound |= _cameraSection->scanForSection(visualItems, scanIndex);
    }
    if (_speedSection->available()) {
        sectionFound |= _speedSection->scanForSection(visualItems, scanIndex);
731
    }
732 733

    return sectionFound;
734 735
}

736
void SimpleMissionItem::_updateOptionalSections(void)
737
{
738
    // Remove previous sections
739 740 741 742
    if (_cameraSection) {
        _cameraSection->deleteLater();
        _cameraSection = NULL;
    }
743 744 745 746
    if (_speedSection) {
        _speedSection->deleteLater();
        _speedSection = NULL;
    }
747

748 749 750 751 752
    // Add new sections

    _cameraSection = new CameraSection(_vehicle, this);
    _speedSection = new SpeedSection(_vehicle, this);
    if ((MAV_CMD)command() == MAV_CMD_NAV_WAYPOINT) {
753
        _cameraSection->setAvailable(true);
754
        _speedSection->setAvailable(true);
755 756
    }

757 758
    connect(_cameraSection, &CameraSection::dirtyChanged,               this, &SimpleMissionItem::_sectionDirtyChanged);
    connect(_cameraSection, &CameraSection::itemCountChanged,           this, &SimpleMissionItem::_updateLastSequenceNumber);
DonLakeFlyer's avatar
DonLakeFlyer committed
759 760
    connect(_cameraSection, &CameraSection::availableChanged,           this, &SimpleMissionItem::specifiedGimbalYawChanged);
    connect(_cameraSection, &CameraSection::specifiedGimbalYawChanged,  this, &SimpleMissionItem::specifiedGimbalYawChanged);
761

762 763 764
    connect(_speedSection,  &SpeedSection::dirtyChanged,                this, &SimpleMissionItem::_sectionDirtyChanged);
    connect(_speedSection,  &SpeedSection::itemCountChanged,            this, &SimpleMissionItem::_updateLastSequenceNumber);
    connect(_speedSection,  &SpeedSection::specifiedFlightSpeedChanged, this, &SimpleMissionItem::specifiedFlightSpeedChanged);
765

766
    emit cameraSectionChanged(_cameraSection);
767
    emit speedSectionChanged(_speedSection);
768
    emit lastSequenceNumberChanged(lastSequenceNumber());
769 770 771 772
}

int SimpleMissionItem::lastSequenceNumber(void) const
{
773
    return sequenceNumber() + (_cameraSection ? _cameraSection->itemCount() : 0) + (_speedSection ? _speedSection->itemCount() : 0);
774 775 776 777 778 779 780
}

void SimpleMissionItem::_updateLastSequenceNumber(void)
{
    emit lastSequenceNumberChanged(lastSequenceNumber());
}

781
void SimpleMissionItem::_sectionDirtyChanged(bool dirty)
782 783 784 785 786 787 788 789 790 791 792 793 794
{
    if (dirty) {
        setDirty(true);
    }
}

void SimpleMissionItem::appendMissionItems(QList<MissionItem*>& items, QObject* missionItemParent)
{
    int seqNum = sequenceNumber();

    items.append(new MissionItem(missionItem(), missionItemParent));
    seqNum++;

795 796
    _cameraSection->appendSectionItems(items, missionItemParent, seqNum);
    _speedSection->appendSectionItems(items, missionItemParent, seqNum);
797
}
DonLakeFlyer's avatar
DonLakeFlyer committed
798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815

void SimpleMissionItem::applyNewAltitude(double newAltitude)
{
    MAV_CMD command = (MAV_CMD)this->command();
    const MissionCommandUIInfo* uiInfo = _commandTree->getUIInfo(_vehicle, command);

    if (uiInfo->specifiesCoordinate() || uiInfo->specifiesAltitudeOnly()) {
        switch ((MAV_CMD)this->command()) {
        case MAV_CMD_NAV_LAND:
        case MAV_CMD_NAV_VTOL_LAND:
            // Leave alone
            break;
        default:
            _missionItem.setParam7(newAltitude);
            break;
        }
    }
}