13#include <QtCore/QJsonArray>
20 , _rallyPointManager (
masterController->managerVehicle()->rallyPointManager())
33 qCDebug(RallyPointControllerLog) <<
"start flyView" << flyView;
41void RallyPointController::_managerVehicleChanged(
Vehicle* managerVehicle)
43 if (_managerVehicle) {
44 _rallyPointManager->disconnect(
this);
45 _managerVehicle->disconnect(
this);
46 _managerVehicle =
nullptr;
47 _rallyPointManager =
nullptr;
50 _managerVehicle = managerVehicle;
51 if (!_managerVehicle) {
52 qWarning() <<
"RallyPointController::managerVehicleChanged managerVehicle=NULL";
63 Q_UNUSED(capabilityBits);
81 QList<JsonParsing::KeyValidateInfo> keyInfoList = {
83 { _jsonPointsKey, QJsonValue::Array,
true },
90 QString errorMessage = tr(
"Rally: %1");
93 if (version < _jsonOldestSupportedVersion || version > _jsonCurrentVersion) {
95 tr(
"Rally Points supports versions %1 to %2").arg(_jsonOldestSupportedVersion).arg(_jsonCurrentVersion);
100 auto loadPoint = [version](
const QJsonValue& jsonPoint, QGeoCoordinate& coordinate,
107 if (!jsonPoint.isObject()) {
108 error = tr(
"Each point must be an object");
111 const QJsonObject jsonPointObject = jsonPoint.toObject();
112 const QList<JsonParsing::KeyValidateInfo> pointKeyInfoList = {
113 {_jsonCoordinateKey, QJsonValue::Array,
true},
114 {_jsonFrameKey, QJsonValue::Double,
true},
118 coordinate, error)) {
122 const int frame = jsonPointObject[_jsonFrameKey].toInt(-1);
123 if (frame != MAV_FRAME_GLOBAL && frame != MAV_FRAME_GLOBAL_RELATIVE_ALT &&
124 frame != MAV_FRAME_GLOBAL_TERRAIN_ALT) {
125 error = tr(
"Unsupported frame %1").arg(frame);
132 QObjectList pointList;
133 const QJsonArray jsonPoints = json[_jsonPointsKey].toArray();
134 for (
const QJsonValue& jsonPoint : jsonPoints) {
135 QGeoCoordinate coordinate;
137 if (!loadPoint(jsonPoint, coordinate, altitudeFrame, errorStr)) {
138 qDeleteAll(pointList);
139 errorString = errorMessage.arg(errorStr);
142 pointList.append(
new RallyPoint(coordinate, altitudeFrame,
this));
144 _points.swapObjectList(pointList);
147 _setFirstPointCurrent();
157 for (
int i=0; i<_points.count(); i++) {
158 const RallyPoint* rallyPoint = qobject_cast<RallyPoint*>(_points[i]);
159 QJsonValue jsonCoordinate;
161 QJsonObject jsonPoint;
162 jsonPoint[_jsonCoordinateKey] = jsonCoordinate;
164 rgPoints.append(jsonPoint);
166 json[_jsonPointsKey] = QJsonValue(rgPoints);
171 _points.clearAndDeleteContents();
179 qCCritical(RallyPointControllerLog) <<
"RallyPointController::removeAllFromVehicle called while offline";
181 qCCritical(RallyPointControllerLog) <<
"RallyPointController::removeAllFromVehicle called while syncInProgress";
183 _rallyPointManager->removeAll();
190 qCCritical(RallyPointControllerLog) <<
"RallyPointController::loadFromVehicle called while offline";
192 qCCritical(RallyPointControllerLog) <<
"RallyPointController::loadFromVehicle called while syncInProgress";
194 _itemsRequested =
true;
195 _rallyPointManager->loadFromVehicle();
202 qCCritical(RallyPointControllerLog) <<
"RallyPointController::sendToVehicle called while offline";
204 qCCritical(RallyPointControllerLog) <<
"RallyPointController::sendToVehicle called while syncInProgress";
206 qCDebug(RallyPointControllerLog) <<
"RallyPointController::sendToVehicle";
208 QList<RallyPointData> rgPoints;
209 for (
int i=0; i<_points.count(); i++) {
210 const RallyPoint* rallyPoint = qobject_cast<RallyPoint*>(_points[i]);
213 _rallyPointManager->sendToVehicle(rgPoints);
219 return _rallyPointManager->inProgress();
224 if (
dirty != _dirty) {
227 _points.setDirty(
false);
233void RallyPointController::_pointsDirtyChanged(
bool dirty)
242 return _rallyPointManager->editorQml();
245void RallyPointController::_managerLoadComplete(
void)
253 QObjectList pointList;
258 _points.swapObjectList(pointList);
260 _setFirstPointCurrent();
263 _itemsRequested =
false;
266void RallyPointController::_managerSendComplete(
bool error)
274void RallyPointController::_managerRemoveAllComplete(
bool error)
292 }
else if (_points.count()) {
293 const RallyPoint* lastPoint = qobject_cast<RallyPoint*>(_points[_points.count() - 1]);
294 defaultAlt = lastPoint->
coordinate().altitude();
301 point.setAltitude(defaultAlt);
303 _points.append(newPoint);
315 for (
int i = 0; i < _points.count(); i++) {
316 const RallyPoint*
const rallyPoint = qobject_cast<const RallyPoint*>(_points[i]);
327 return _managerVehicle->capabilityBits() & MAV_PROTOCOL_CAPABILITY_MISSION_RALLY;
333 for (foundIndex=0; foundIndex<_points.count(); foundIndex++) {
334 if (_points[foundIndex] == rallyPoint) {
335 _points.removeOne(rallyPoint);
336 rallyPoint->deleteLater();
340 if (_points.count()) {
341 int newIndex = qMin(foundIndex, _points.count() - 1);
342 newIndex = qMax(newIndex, 0);
351 if (_currentRallyPoint != rallyPoint) {
352 _currentRallyPoint = rallyPoint;
357void RallyPointController::_setFirstPointCurrent(
void)
364 return _points.count() > 0;
369 qCDebug(RallyPointControllerLog) <<
"showPlanFromManagerVehicle _flyView" <<
_flyView;
371 qCCritical(RallyPointControllerLog) <<
"RallyPointController::showPlanFromManagerVehicle called while offline";
374 if (!_managerVehicle->initialPlanRequestComplete()) {
376 qCDebug(RallyPointControllerLog) <<
"showPlanFromManagerVehicle: !initialPlanRequestComplete, wait for signal";
380 qCDebug(RallyPointControllerLog) <<
"showPlanFromManagerVehicle: syncInProgress wait for signal";
383 qCDebug(RallyPointControllerLog) <<
"showPlanFromManagerVehicle: sync complete";
384 _itemsRequested =
true;
385 _managerLoadComplete();
393 return _points.count() == 0;
#define QGC_LOGGING_CATEGORY(name, categoryStr)
void dirtyChanged(bool dirty)
void countChanged(int count)
void supportedChanged(bool supported)
PlanElementController(PlanMasterController *masterController, QObject *parent=nullptr)
void syncInProgressChanged(bool syncInProgress)
PlanMasterController * masterController(void)
PlanMasterController * _masterController
virtual void start(bool flyView)
Should be called immediately upon Component.onCompleted.
void containsItemsChanged()
void dirtyChanged(bool dirty)
Master controller for mission, fence, rally.
void managerVehicleChanged(Vehicle *managerVehicle)
int count() const override final
void clearAndDeleteContents() override final
Clears the list and calls deleteLater on each entry.
bool containsItems(void) const final
void save(QJsonObject &json) final
RallyPointController(PlanMasterController *masterController, QObject *parent=nullptr)
Q_INVOKABLE void addPoint(QGeoCoordinate point)
bool supported(void) const final
true: controller is waiting for the current load to complete
void setDirty(bool dirty) final
QString editorQml(void) const
bool showPlanFromManagerVehicle(void) final
bool syncInProgress(void) const final
void setCurrentRallyPoint(QObject *rallyPoint)
bool dirty(void) const final
void start(bool flyView) final
Should be called immediately upon Component.onCompleted.
void removeAllFromVehicle(void) final
bool load(const QJsonObject &json, QString &errorString) final
Q_INVOKABLE void removePoint(QObject *rallyPoint)
void removeAll(void) final
Removes all from controller only.
bool terrainQueryPending(void) const
true: a rally point is still waiting on terrain data for its altitude
void currentRallyPointChanged(QObject *rallyPoint)
void loadFromVehicle(void) final
void sendToVehicle(void) final
void inProgressChanged(bool inProgress)
void sendComplete(bool error)
QList< RallyPointData > points(void) const
void removeAllComplete(bool error)
This class is used to encapsulate the QGeoCoordinate associated with a Rally Point into a QObject suc...
QGeoCoordinate coordinate(void) const
static double getDefaultFactAltitude()
static QGroundControlQmlGlobal::AltitudeFrame altitudeFrameFromMavFrame(MAV_FRAME mavFrame)
bool terrainQueryPending(void) const
true: a terrain answer is on its way and will change the altitude. A failed query is not pending.
Q_INVOKABLE void useTerrainAltitude(bool switchToAbsoluteOnFailure=false)
QGroundControlQmlGlobal::AltitudeFrame altitudeFrame(void) const
static MAV_FRAME mavFrame(QGroundControlQmlGlobal::AltitudeFrame altitudeFrame)
static SettingsManager * instance()
AppSettings * appSettings() const
void capabilityBitsChanged(uint64_t capabilityBits)
RallyPointManager * rallyPointManager()
bool loadGeoCoordinate(const QJsonValue &jsonValue, bool altitudeRequired, QGeoCoordinate &coordinate, QString &errorString)
void saveGeoCoordinate(const QGeoCoordinate &coordinate, bool writeAltitude, QJsonValue &jsonValue)
Saves a QGeoCoordinate as [lat, lon, alt] array (QGC plan format).
bool validateKeys(const QJsonObject &jsonObject, const QList< KeyValidateInfo > &keyInfo, QString &errorString)
Validates that all required keys are present and that listed keys have the expected type.
constexpr const char * jsonVersionKey
A rally point as exchanged with the vehicle.