VisualMissionItem.cc 7.72 KB
Newer Older
1 2
/****************************************************************************
 *
Gus Grubba's avatar
Gus Grubba committed
3
 * (c) 2009-2020 QGROUNDCONTROL PROJECT <http://www.qgroundcontrol.org>
4 5 6 7 8
 *
 * 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 14 15 16 17


#include <QStringList>
#include <QDebug>

#include "VisualMissionItem.h"
#include "FirmwarePluginManager.h"
#include "QGCApplication.h"
#include "JsonHelper.h"
18
#include "TerrainQuery.h"
19
#include "TakeoffMissionItem.h"
20
#include "PlanMasterController.h"
21

Don Gagne's avatar
Don Gagne committed
22 23 24 25
const char* VisualMissionItem::jsonTypeKey =                "type";
const char* VisualMissionItem::jsonTypeSimpleItemValue =    "SimpleItem";
const char* VisualMissionItem::jsonTypeComplexItemValue =   "ComplexItem";

26 27 28 29
VisualMissionItem::VisualMissionItem(PlanMasterController* masterController, bool flyView, QObject* parent)
    : QObject           (parent)
    , _flyView          (flyView)
    , _masterController (masterController)
30
    , _missionController(masterController->missionController())
31
    , _controllerVehicle(masterController->controllerVehicle())
32
{
33
    _commonInit();
34 35
}

36
VisualMissionItem::VisualMissionItem(const VisualMissionItem& other, bool flyView, QObject* parent)
37
    : QObject                   (parent)
38
    , _flyView                  (flyView)
39 40
{
    *this = other;
41

42 43 44 45 46
    _commonInit();
}

void VisualMissionItem::_commonInit(void)
{
47
    // Don't get terrain altitude information for submarines or boats
48 49
    Vehicle* controllerVehicle = _masterController->controllerVehicle();
    if (controllerVehicle->vehicleType() != MAV_TYPE_SUBMARINE && controllerVehicle->vehicleType() != MAV_TYPE_SURFACE_BOAT) {
50 51 52 53
        _updateTerrainTimer.setInterval(500);
        _updateTerrainTimer.setSingleShot(true);
        connect(&_updateTerrainTimer, &QTimer::timeout, this, &VisualMissionItem::_reallyUpdateTerrainAltitude);

54 55
        connect(this, &VisualMissionItem::coordinateChanged, this, &VisualMissionItem::_updateTerrainAltitude);
    }
56 57 58 59
}

const VisualMissionItem& VisualMissionItem::operator=(const VisualMissionItem& other)
{
60 61
    _masterController = other._masterController;
    _controllerVehicle = other._controllerVehicle;
62 63 64

    setIsCurrentItem(other._isCurrentItem);
    setDirty(other._dirty);
65
    _homePositionSpecialCase = other._homePositionSpecialCase;
66
    _terrainAltitude = other._terrainAltitude;
67 68
    setAltDifference(other._altDifference);
    setAltPercent(other._altPercent);
69
    setTerrainPercent(other._terrainPercent);
70 71
    setAzimuth(other._azimuth);
    setDistance(other._distance);
72
    setDistanceFromStart(other._distance);
73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88

    return *this;
}

VisualMissionItem::~VisualMissionItem()
{    
}

void VisualMissionItem::setIsCurrentItem(bool isCurrentItem)
{
    if (_isCurrentItem != isCurrentItem) {
        _isCurrentItem = isCurrentItem;
        emit isCurrentItemChanged(isCurrentItem);
    }
}

89 90 91 92 93 94 95 96
void VisualMissionItem::setHasCurrentChildItem(bool hasCurrentChildItem)
{
    if (_hasCurrentChildItem != hasCurrentChildItem) {
        _hasCurrentChildItem = hasCurrentChildItem;
        emit hasCurrentChildItemChanged(hasCurrentChildItem);
    }
}

97 98
void VisualMissionItem::setDistance(double distance)
{
99 100 101 102
    if (!qFuzzyCompare(_distance, distance)) {
        _distance = distance;
        emit distanceChanged(_distance);
    }
103 104
}

105 106 107 108 109 110 111 112
void VisualMissionItem::setDistanceFromStart(double distanceFromStart)
{
    if (!qFuzzyCompare(_distanceFromStart, distanceFromStart)) {
        _distanceFromStart = distanceFromStart;
        emit distanceFromStartChanged(_distanceFromStart);
    }
}

113 114
void VisualMissionItem::setAltDifference(double altDifference)
{
115 116 117 118
    if (!qFuzzyCompare(_altDifference, altDifference)) {
        _altDifference = altDifference;
        emit altDifferenceChanged(_altDifference);
    }
119 120 121 122
}

void VisualMissionItem::setAltPercent(double altPercent)
{
123 124 125 126
    if (!qFuzzyCompare(_altPercent, altPercent)) {
        _altPercent = altPercent;
        emit altPercentChanged(_altPercent);
    }
127 128
}

129 130 131 132 133 134 135 136
void VisualMissionItem::setTerrainPercent(double terrainPercent)
{
    if (!qFuzzyCompare(_terrainPercent, terrainPercent)) {
        _terrainPercent = terrainPercent;
        emit terrainPercentChanged(terrainPercent);
    }
}

137 138 139 140 141 142 143 144
void VisualMissionItem::setTerrainCollision(bool terrainCollision)
{
    if (terrainCollision != _terrainCollision) {
        _terrainCollision = terrainCollision;
        emit terrainCollisionChanged(terrainCollision);
    }
}

145 146
void VisualMissionItem::setAzimuth(double azimuth)
{
147 148 149 150
    if (!qFuzzyCompare(_azimuth, azimuth)) {
        _azimuth = azimuth;
        emit azimuthChanged(_azimuth);
    }
151
}
152

DonLakeFlyer's avatar
DonLakeFlyer committed
153 154
void VisualMissionItem::setMissionFlightStatus(MissionController::MissionFlightStatus_t& missionFlightStatus)
{
155
    if (qIsNaN(missionFlightStatus.gimbalYaw) && qIsNaN(_missionGimbalYaw)) {
DonLakeFlyer's avatar
DonLakeFlyer committed
156 157
        return;
    }
158 159
    if (!qFuzzyCompare(missionFlightStatus.gimbalYaw, _missionGimbalYaw)) {
        _missionGimbalYaw = missionFlightStatus.gimbalYaw;
DonLakeFlyer's avatar
DonLakeFlyer committed
160 161 162 163 164 165 166 167 168 169 170
        emit missionGimbalYawChanged(_missionGimbalYaw);
    }
}

void VisualMissionItem::setMissionVehicleYaw(double vehicleYaw)
{
    if (!qFuzzyCompare(_missionVehicleYaw, vehicleYaw)) {
        _missionVehicleYaw = vehicleYaw;
        emit missionVehicleYawChanged(_missionVehicleYaw);
    }
}
171 172 173

void VisualMissionItem::_updateTerrainAltitude(void)
{
174 175 176 177
    if (coordinate().latitude() == 0 && coordinate().longitude() == 0) {
        // This is an intermediate state we don't react to
        return;
    }
178 179 180 181

    _terrainAltitude = qQNaN();
    emit terrainAltitudeChanged(qQNaN());

182
    if (!_flyView && specifiesCoordinate() && coordinate().isValid()) {
Alexey's avatar
Alexey committed
183 184
        // We use a timer so that any additional requests before the timer fires result in only a single request
        _updateTerrainTimer.start();
185 186 187 188 189 190
    }
}

void VisualMissionItem::_reallyUpdateTerrainAltitude(void)
{
    QGeoCoordinate coord = coordinate();
191
    if (specifiesCoordinate() && coord.isValid() && (qIsNaN(_terrainAltitude) || !qFuzzyCompare(_lastLatTerrainQuery, coord.latitude()) || qFuzzyCompare(_lastLonTerrainQuery, coord.longitude()))) {
192 193
        _lastLatTerrainQuery = coord.latitude();
        _lastLonTerrainQuery = coord.longitude();
194 195 196 197 198 199
        if (_currentTerrainAtCoordinateQuery) {
            disconnect(_currentTerrainAtCoordinateQuery, &TerrainAtCoordinateQuery::terrainDataReceived, this, &VisualMissionItem::_terrainDataReceived);
            _currentTerrainAtCoordinateQuery = nullptr;
        }
        _currentTerrainAtCoordinateQuery = new TerrainAtCoordinateQuery(true /* autoDelet */);
        connect(_currentTerrainAtCoordinateQuery, &TerrainAtCoordinateQuery::terrainDataReceived, this, &VisualMissionItem::_terrainDataReceived);
200 201
        QList<QGeoCoordinate> rgCoord;
        rgCoord.append(coordinate());
202
        _currentTerrainAtCoordinateQuery->requestData(rgCoord);
203 204 205
    }
}

206
void VisualMissionItem::_terrainDataReceived(bool success, QList<double> heights)
207
{
Don Gagne's avatar
Don Gagne committed
208 209
    _terrainAltitude = success ? heights[0] : qQNaN();
    emit terrainAltitudeChanged(_terrainAltitude);
210
    _currentTerrainAtCoordinateQuery = nullptr;
211
}
212 213 214 215 216 217 218 219 220

void VisualMissionItem::_setBoundingCube(QGCGeoBoundingCube bc)
{
    if (bc != _boundingCube) {
        _boundingCube = bc;
        emit boundingCubeChanged();
    }
}

221 222 223 224 225 226 227
void VisualMissionItem::setWizardMode(bool wizardMode)
{
    if (wizardMode != _wizardMode) {
        _wizardMode = wizardMode;
        emit wizardModeChanged(_wizardMode);
    }
}
228 229 230 231 232 233 234 235

void VisualMissionItem::setParentItem(VisualMissionItem* parentItem)
{
    if (_parentItem != parentItem) {
        _parentItem = parentItem;
        emit parentItemChanged(parentItem);
    }
}
236 237 238 239 240 241 242 243 244 245

void VisualMissionItem::_amslEntryAltChanged(void)
{
    emit amslEntryAltChanged(amslEntryAlt());
}

void VisualMissionItem::_amslExitAltChanged(void)
{
    emit amslExitAltChanged(amslExitAlt());
}