ComplexMissionItem.cc 4.85 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

#include "ComplexMissionItem.h"
Don Gagne's avatar
Don Gagne committed
11
#include "QGCApplication.h"
12 13
#include "QGCCorePlugin.h"
#include "QGCOptions.h"
14
#include "PlanMasterController.h"
15 16
#include "FlightPathSegment.h"
#include "MissionController.h"
Don Gagne's avatar
Don Gagne committed
17

18
#include <QCborValue>
Don Gagne's avatar
Don Gagne committed
19
#include <QSettings>
20

Don Gagne's avatar
Don Gagne committed
21 22
const char* ComplexMissionItem::jsonComplexItemTypeKey = "complexItemType";

Don Gagne's avatar
Don Gagne committed
23 24
const char* ComplexMissionItem::_presetSettingsKey =        "_presets";

25 26
ComplexMissionItem::ComplexMissionItem(PlanMasterController* masterController, bool flyView, QObject* parent)
    : VisualMissionItem (masterController, flyView, parent)
27 28
    , _toolbox          (qgcApp()->toolbox())
    , _settingsManager  (_toolbox->settingsManager())
29
{
30

31 32
}

33 34
const ComplexMissionItem& ComplexMissionItem::operator=(const ComplexMissionItem& other)
{
35
    VisualMissionItem::operator=(other);
36 37 38

    return *this;
}
Don Gagne's avatar
Don Gagne committed
39 40 41 42 43 44 45 46 47 48 49 50 51 52 53

QStringList ComplexMissionItem::presetNames(void)
{
    QStringList names;

    QSettings settings;

    settings.beginGroup(presetsSettingsGroup());
    settings.beginGroup(_presetSettingsKey);
    return settings.childKeys();
}

void ComplexMissionItem::loadPreset(const QString& name)
{
    Q_UNUSED(name);
54
    qgcApp()->showAppMessage(tr("This Pattern does not support Presets."));
Don Gagne's avatar
Don Gagne committed
55 56 57 58 59
}

void ComplexMissionItem::savePreset(const QString& name)
{
    Q_UNUSED(name);
60
    qgcApp()->showAppMessage(tr("This Pattern does not support Presets."));
Don Gagne's avatar
Don Gagne committed
61 62
}

Don Gagne's avatar
Don Gagne committed
63
void ComplexMissionItem::deletePreset(const QString& name)
Don Gagne's avatar
Don Gagne committed
64
{
65
    if (qgcApp()->toolbox()->corePlugin()->options()->surveyBuiltInPresetNames().contains(name)) {
66
        qgcApp()->showAppMessage(tr("'%1' is a built-in preset which cannot be deleted.").arg(name));
67 68
        return;
    }
Don Gagne's avatar
Don Gagne committed
69

70
    QSettings settings;
Don Gagne's avatar
Don Gagne committed
71 72 73 74
    settings.beginGroup(presetsSettingsGroup());
    settings.beginGroup(_presetSettingsKey);
    settings.remove(name);
    emit presetNamesChanged();
Don Gagne's avatar
Don Gagne committed
75 76 77 78 79 80 81
}

void ComplexMissionItem::_savePresetJson(const QString& name, QJsonObject& presetObject)
{
    QSettings settings;
    settings.beginGroup(presetsSettingsGroup());
    settings.beginGroup(_presetSettingsKey);
82
    settings.setValue(name, QCborMap::fromJsonObject(presetObject).toCborValue().toByteArray());
83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100

    // Use this to save a survey preset as a JSON file to be included in the build
    // as a built-in survey preset that cannot be deleted.
    #if 0
    QString savePath = _settingsManager->appSettings()->missionSavePath();
    QDir saveDir(savePath);

    QString fileName = saveDir.absoluteFilePath(name);
    fileName.append(".json");
    QFile jsonFile(fileName);

    if (!jsonFile.open(QIODevice::WriteOnly)) {
        qDebug() << "Couldn't open .json file.";
    }

    qDebug() << "Saving survey preset to JSON";
    auto jsonDoc = QJsonDocument(jsonObj);
    jsonFile.write(jsonDoc.toJson());
101 102
    #endif

Don Gagne's avatar
Don Gagne committed
103 104 105 106 107 108 109 110
    emit presetNamesChanged();
}

QJsonObject ComplexMissionItem::_loadPresetJson(const QString& name)
{
    QSettings settings;
    settings.beginGroup(presetsSettingsGroup());
    settings.beginGroup(_presetSettingsKey);
111
    return QCborValue(settings.value(name).toByteArray()).toMap().toJsonObject();
Don Gagne's avatar
Don Gagne committed
112
}
113 114 115 116 117

void ComplexMissionItem::addKMLVisuals(KMLPlanDomDocument& /* domDocument */)
{
    // Default implementation has no visuals
}
118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148

void ComplexMissionItem::_appendFlightPathSegment(const QGeoCoordinate& coord1, double coord1AMSLAlt, const QGeoCoordinate& coord2, double coord2AMSLAlt)
{
    FlightPathSegment* segment = new FlightPathSegment(coord1, coord1AMSLAlt, coord2, coord2AMSLAlt, true /* queryTerrainData */, this /* parent */);

    connect(segment, &FlightPathSegment::terrainCollisionChanged,       this,               &ComplexMissionItem::_segmentTerrainCollisionChanged);
    connect(segment, &FlightPathSegment::terrainCollisionChanged,       _missionController, &MissionController::recalcTerrainProfile, Qt::QueuedConnection);
    connect(segment, &FlightPathSegment::amslTerrainHeightsChanged,     _missionController, &MissionController::recalcTerrainProfile, Qt::QueuedConnection);

    // Signals may have been emitted in contructor so we need to deal with that now since they were missed

    _flightPathSegments.append(segment);
    if (segment->terrainCollision()) {
        emit _segmentTerrainCollisionChanged(true);
    }

    if (segment->amslTerrainHeights().count()) {
        _missionController->recalcTerrainProfile();
    }
}

void ComplexMissionItem::_segmentTerrainCollisionChanged(bool terrainCollision)
{
    if (terrainCollision) {
        _cTerrainCollisionSegments++;
    } else {
        _cTerrainCollisionSegments--;
    }
    emit terrainCollisionChanged(_cTerrainCollisionSegments != 0);
}