QGroundControl
Ground Control Station for MAVLink Drones
Loading...
Searching...
No Matches
PlanMasterController.cc
Go to the documentation of this file.
2
3#include <QtCore/QFileInfo>
4#include <QtCore/QJsonDocument>
5
6#include "AppMessages.h"
7#include "AppSettings.h"
8#include "GeoFenceManager.h"
9#include "JsonParsing.h"
10#include "KMLPlanDomDocument.h"
11#include "MissionManager.h"
12#include "MultiVehicleManager.h"
13#include "PlanCreator.h"
14#include "QGCCorePlugin.h"
15#include "QGCLoggingCategory.h"
16#include "QmlObjectListModel.h"
17#include "RallyPointManager.h"
18#include "SettingsManager.h"
19#include "Vehicle.h"
20#include "VehicleLinkManager.h"
21#include "VisualMissionItem.h"
22
23QGC_LOGGING_CATEGORY(PlanMasterControllerLog, "PlanManager.PlanMasterController")
24
26 : QObject (parent)
27 , _multiVehicleMgr (MultiVehicleManager::instance())
28 , _controllerVehicle (new Vehicle(Vehicle::MAV_AUTOPILOT_TRACK, Vehicle::MAV_TYPE_TRACK, this))
29 , _managerVehicle (_controllerVehicle)
30 , _missionController (this)
31 , _geoFenceController (this)
32 , _rallyPointController (this)
33{
34 _commonInit();
35}
36
37#ifdef QGC_UNITTEST_BUILD
38PlanMasterController::PlanMasterController(MAV_AUTOPILOT firmwareType, MAV_TYPE vehicleType, QObject* parent)
39 : QObject (parent)
40 , _multiVehicleMgr (MultiVehicleManager::instance())
41 , _controllerVehicle (new Vehicle(firmwareType, vehicleType))
42 , _managerVehicle (_controllerVehicle)
43 , _missionController (this)
44 , _geoFenceController (this)
45 , _rallyPointController (this)
46{
47 _commonInit();
48}
49#endif
50
51void PlanMasterController::_commonInit(void)
52{
53 connect(&_missionController, &MissionController::dirtyChanged, this, &PlanMasterController::_updateOverallDirty);
54 connect(&_geoFenceController, &GeoFenceController::dirtyChanged, this, &PlanMasterController::_updateOverallDirty);
55 connect(&_rallyPointController, &RallyPointController::dirtyChanged, this, &PlanMasterController::_updateOverallDirty);
56
60
61 connect(this, &PlanMasterController::containsItemsChanged, this, &PlanMasterController::_updateShowCreateFromTemplate);
62
66
67 // Offline vehicle can change firmware/vehicle type
68 connect(_controllerVehicle, &Vehicle::vehicleTypeChanged, this, &PlanMasterController::_updatePlanCreatorsList);
69}
70
71
76
78{
79 _missionController.start (_flyView);
80 _geoFenceController.start (_flyView);
81 _rallyPointController.start (_flyView);
82
83 _activeVehicleChanged(_multiVehicleMgr->activeVehicle());
84 connect(_multiVehicleMgr, &MultiVehicleManager::activeVehicleChanged, this, &PlanMasterController::_activeVehicleChanged);
85
86 _updatePlanCreatorsList();
87}
88
89void PlanMasterController::startStaticActiveVehicle(Vehicle* vehicle, bool deleteWhenSendCompleted)
90{
91 _flyView = true;
92 _deleteWhenSendCompleted = deleteWhenSendCompleted;
93 _missionController.start(_flyView);
94 _geoFenceController.start(_flyView);
95 _rallyPointController.start(_flyView);
96 _activeVehicleChanged(vehicle);
97}
98
99void PlanMasterController::_activeVehicleChanged(Vehicle* activeVehicle)
100{
101 if (_managerVehicle == activeVehicle) {
102 // We are already setup for this vehicle
103 return;
104 }
105
106 qCDebug(PlanMasterControllerLog) << "_activeVehicleChanged" << activeVehicle;
107
108 if (_managerVehicle) {
109 // Disconnect old vehicle. Be careful of wildcarding disconnect too much since _managerVehicle may equal _controllerVehicle
110 disconnect(_managerVehicle, &Vehicle::initialPlanRequestCompleteChanged, this, nullptr);
111 disconnect(_managerVehicle->missionManager(), nullptr, this, nullptr);
112 disconnect(_managerVehicle->geoFenceManager(), nullptr, this, nullptr);
113 disconnect(_managerVehicle->rallyPointManager(), nullptr, this, nullptr);
114
115 // Any in-flight transfer chain can never complete against the new vehicle's managers
116 _loadSequence = SyncSequence::Idle;
117 _sendSequence = SyncSequence::Idle;
118 }
119
120 bool newOffline = false;
121 if (activeVehicle == nullptr) {
122 // Since there is no longer an active vehicle we use the offline controller vehicle as the manager vehicle
123 _managerVehicle = _controllerVehicle;
124 newOffline = true;
125 } else {
126 newOffline = false;
127 _managerVehicle = activeVehicle;
128
129 // Update controllerVehicle to the currently connected vehicle
130 AppSettings* appSettings = SettingsManager::instance()->appSettings();
131 appSettings->offlineEditingFirmwareClass()->setRawValue(QGCMAVLink::firmwareClass(_managerVehicle->firmwareType()));
132 appSettings->offlineEditingVehicleClass()->setRawValue(QGCMAVLink::vehicleClass(_managerVehicle->vehicleType()));
133
134 // We use these signals to sequence upload and download to the multiple controller/managers
135 connect(_managerVehicle, &Vehicle::initialPlanRequestCompleteChanged, this, &PlanMasterController::_initialPlanRequestCompleteChanged);
136 connect(_managerVehicle->missionManager(), &MissionManager::newMissionItemsAvailable, this, &PlanMasterController::_loadMissionComplete);
137 connect(_managerVehicle->geoFenceManager(), &GeoFenceManager::loadComplete, this, &PlanMasterController::_loadGeoFenceComplete);
138 connect(_managerVehicle->rallyPointManager(), &RallyPointManager::loadComplete, this, &PlanMasterController::_loadRallyPointsComplete);
139 connect(_managerVehicle->missionManager(), &MissionManager::sendComplete, this, &PlanMasterController::_sendMissionComplete);
140 connect(_managerVehicle->geoFenceManager(), &GeoFenceManager::sendComplete, this, &PlanMasterController::_sendGeoFenceComplete);
141 connect(_managerVehicle->rallyPointManager(), &RallyPointManager::sendComplete, this, &PlanMasterController::_sendRallyPointsComplete);
142 }
143
144 _offline = newOffline;
145 emit offlineChanged(offline());
146 emit managerVehicleChanged(_managerVehicle);
147
148 if (_flyView) {
149 // We are in the Fly View
150 if (newOffline) {
151 // No active vehicle, clear mission
152 qCDebug(PlanMasterControllerLog) << "_activeVehicleChanged: Fly View - No active vehicle, clearing stale plan";
153 removeAll();
154 } else {
155 // Fly view has changed to a new active vehicle, update to show correct mission
156 qCDebug(PlanMasterControllerLog) << "_activeVehicleChanged: Fly View - New active vehicle, loading new plan from manager vehicle";
157 _showPlanFromManagerVehicle();
158 }
159 } else {
160 // We are in the Plan view.
161 if (containsItems()) {
162 // We have a plan which is from a different vehicle than the new active vehicle. By definition this plan requires and upload.
163 _setDirtyForUpload(true);
164
165 // The plan view has a stale plan in it
166 if (dirtyForSave()) {
167 // Plan is dirty, the user must decide what to do in all cases
168 qCDebug(PlanMasterControllerLog) << "_activeVehicleChanged: Plan View - Previous dirty plan exists, no new active vehicle, sending promptForPlanUsageOnVehicleChange signal";
170 } else {
171 // Plan is not dirty
172 if (newOffline) {
173 // The active vehicle went away with no new active vehicle
174 qCDebug(PlanMasterControllerLog) << "_activeVehicleChanged: Plan View - Previous clean plan exists, no new active vehicle, clear stale plan";
175 removeAll();
176 } else {
177 // We are transitioning from one active vehicle to another. Show the plan from the new vehicle.
178 qCDebug(PlanMasterControllerLog) << "_activeVehicleChanged: Plan View - Previous clean plan exists, new active vehicle, loading from new manager vehicle";
179 _showPlanFromManagerVehicle();
180 }
181 }
182 } else {
183 // There is no previous Plan in the view
184 _setDirtyStates(false, false);
185 if (newOffline) {
186 // Nothing special to do in this case
187 qCDebug(PlanMasterControllerLog) << "_activeVehicleChanged: Plan View - No previous plan, no longer connected to vehicle, nothing to do";
188 } else {
189 // Just show the plan from the new vehicle
190 qCDebug(PlanMasterControllerLog) << "_activeVehicleChanged: Plan View - No previous plan, new active vehicle, loading from new manager vehicle";
191 _showPlanFromManagerVehicle();
192 }
193 }
194 }
195
196 // Vehicle changed so we need to signal everything
201
202 _updatePlanCreatorsList();
203}
204
206{
207 SharedLinkInterfacePtr sharedLink = _managerVehicle->vehicleLinkManager()->primaryLink().lock();
208 if (sharedLink) {
209 if (sharedLink->linkConfiguration()->isHighLatency()) {
210 QGC::showAppMessage(tr("Download not supported on high latency links."));
211 return;
212 }
213 } else {
214 // Vehicle is shutting down
215 return;
216 }
217
218 if (offline()) {
219 qCCritical(PlanMasterControllerLog) << "PlanMasterController::loadFromVehicle called while offline";
220 } else if (_flyView) {
221 qCCritical(PlanMasterControllerLog) << "PlanMasterController::loadFromVehicle called from Fly view";
222 } else if (syncInProgress()) {
223 qCCritical(PlanMasterControllerLog) << "PlanMasterController::loadFromVehicle called while syncInProgress";
224 } else {
225 _loadSequence = SyncSequence::Mission;
226 qCDebug(PlanMasterControllerLog) << "PlanMasterController::loadFromVehicle calling _missionController.loadFromVehicle";
227 _missionController.loadFromVehicle();
228 }
229}
230
231
232void PlanMasterController::_loadMissionComplete(void)
233{
234 if (_loadSequence != SyncSequence::Mission) {
235 return;
236 }
237 _loadSequence = SyncSequence::GeoFence;
238 if (_geoFenceController.supported()) {
239 qCDebug(PlanMasterControllerLog) << "PlanMasterController::_loadMissionComplete calling _geoFenceController.loadFromVehicle";
240 _geoFenceController.loadFromVehicle();
241 } else {
242 qCDebug(PlanMasterControllerLog) << "PlanMasterController::_loadMissionComplete GeoFence not supported skipping";
243 _geoFenceController.removeAll();
244 _loadGeoFenceComplete();
245 }
246}
247
248void PlanMasterController::_loadGeoFenceComplete(void)
249{
250 if (_loadSequence != SyncSequence::GeoFence) {
251 return;
252 }
253 _loadSequence = SyncSequence::RallyPoints;
254 if (_rallyPointController.supported()) {
255 qCDebug(PlanMasterControllerLog) << "PlanMasterController::_loadGeoFenceComplete calling _rallyPointController.loadFromVehicle";
256 _rallyPointController.loadFromVehicle();
257 } else {
258 qCDebug(PlanMasterControllerLog) << "PlanMasterController::_loadGeoFenceComplete Rally Points not supported skipping";
259 _rallyPointController.removeAll();
260 _loadRallyPointsComplete();
261 }
262}
263
264void PlanMasterController::_loadRallyPointsComplete(void)
265{
266 if (_loadSequence != SyncSequence::RallyPoints) {
267 return;
268 }
269 _loadSequence = SyncSequence::Idle;
270 qCDebug(PlanMasterControllerLog) << "PlanMasterController::_loadRallyPointsComplete";
271 // A plan just downloaded from the vehicle reflects exactly what is on the vehicle.
272 // The user has made no edits, so it must not be dirty for save or upload. Any previous
273 // file association no longer describes the editor contents.
274 _clearCurrentPlanFile();
275 _setDirtyStates(false /* dirtyForSave */, false /* dirtyForUpload */);
276}
277
278void PlanMasterController::_sendMissionComplete(void)
279{
280 if (_sendSequence != SyncSequence::Mission) {
281 return;
282 }
283 _sendSequence = SyncSequence::GeoFence;
284 if (_geoFenceController.supported()) {
285 qCDebug(PlanMasterControllerLog) << "PlanMasterController::sendToVehicle start GeoFence sendToVehicle";
286 _geoFenceController.sendToVehicle();
287 } else {
288 qCDebug(PlanMasterControllerLog) << "PlanMasterController::sendToVehicle GeoFence not supported skipping";
289 _sendGeoFenceComplete();
290 }
291}
292
293void PlanMasterController::_sendGeoFenceComplete(void)
294{
295 if (_sendSequence != SyncSequence::GeoFence) {
296 return;
297 }
298 _sendSequence = SyncSequence::RallyPoints;
299 if (_rallyPointController.supported()) {
300 qCDebug(PlanMasterControllerLog) << "PlanMasterController::sendToVehicle start rally sendToVehicle";
301 _rallyPointController.sendToVehicle();
302 } else {
303 qCDebug(PlanMasterControllerLog) << "PlanMasterController::sendToVehicle Rally Points not support skipping";
304 _sendRallyPointsComplete();
305 }
306}
307
308void PlanMasterController::_sendRallyPointsComplete(void)
309{
310 if (_sendSequence != SyncSequence::RallyPoints) {
311 return;
312 }
313 _sendSequence = SyncSequence::Idle;
314 qCDebug(PlanMasterControllerLog) << "PlanMasterController::sendToVehicle Rally Point send complete";
315 _setDirtyForUpload(false);
316 if (_deleteWhenSendCompleted) {
317 this->deleteLater();
318 }
319}
320
322{
323 SharedLinkInterfacePtr sharedLink = _managerVehicle->vehicleLinkManager()->primaryLink().lock();
324 if (sharedLink) {
325 if (sharedLink->linkConfiguration()->isHighLatency()) {
326 QGC::showAppMessage(tr("Upload not supported on high latency links."));
327 return;
328 }
329 } else {
330 // Vehicle is shutting down
331 return;
332 }
333
334 if (offline()) {
335 qCCritical(PlanMasterControllerLog) << "PlanMasterController::sendToVehicle called while offline";
336 } else if (syncInProgress()) {
337 qCCritical(PlanMasterControllerLog) << "PlanMasterController::sendToVehicle called while syncInProgress";
338 } else {
339 qCDebug(PlanMasterControllerLog) << "PlanMasterController::sendToVehicle start mission sendToVehicle";
340 _sendSequence = SyncSequence::Mission;
341 _missionController.sendToVehicle();
342 }
343}
344
345void PlanMasterController::loadFromFile(const QString& filename)
346{
347 QString errorString;
348 QString errorMessage = tr("Error loading Plan file (%1). %2").arg(filename).arg("%1");
349
350 if (filename.isEmpty()) {
351 return;
352 }
353
354 QFileInfo fileInfo(filename);
355 QFile file(filename);
356
357 if (!file.open(QIODevice::ReadOnly | QIODevice::Text)) {
358 errorString = file.errorString() + QStringLiteral(" ") + filename;
359 QGC::showAppMessage(errorMessage.arg(errorString));
360 return;
361 }
362
363 bool success = false;
364 if (fileInfo.suffix() == AppSettings::waypointsFileExtension || fileInfo.suffix() == QStringLiteral("txt")) {
365 success = _missionController.loadTextFile(file, errorString);
366 } else {
367 success = _loadPlanJson(file.readAll(), errorString);
368 }
369 if (!success) {
370 QGC::showAppMessage(errorMessage.arg(errorString));
371 }
372
373 if (success){
374 _currentPlanFile = QString::asprintf("%s/%s.%s", fileInfo.path().toLocal8Bit().data(), fileInfo.completeBaseName().toLocal8Bit().data(), AppSettings::planFileExtension);
376 _setDirtyStates(false /* dirtyForSave */, true /* dirtyForUpload */);
377 } else {
378 _clearCurrentPlanFile();
379 }
380}
381
382bool PlanMasterController::_loadPlanJson(const QByteArray& bytes, QString& errorString)
383{
384 QJsonDocument jsonDoc;
385 if (!JsonParsing::isJsonFile(bytes, jsonDoc, errorString)) {
386 return false;
387 }
388
389 QJsonObject json = jsonDoc.object();
390 //-- Allow plugins to pre process the load
392
393 int version;
395 return false;
396 }
397
398 const QList<JsonParsing::KeyValidateInfo> rgKeyInfo = {
399 { kJsonMissionObjectKey, QJsonValue::Object, true },
400 { kJsonGeoFenceObjectKey, QJsonValue::Object, true },
401 { kJsonRallyPointsObjectKey, QJsonValue::Object, true },
402 };
403 if (!JsonParsing::validateKeys(json, rgKeyInfo, errorString)) {
404 return false;
405 }
406
407 if (!_missionController.load(json[kJsonMissionObjectKey].toObject(), errorString) ||
408 !_geoFenceController.load(json[kJsonGeoFenceObjectKey].toObject(), errorString) ||
409 !_rallyPointController.load(json[kJsonRallyPointsObjectKey].toObject(), errorString)) {
410 return false;
411 }
412
413 //-- Allow plugins to post process the load
415 return true;
416}
417
419{
420 QJsonObject planJson;
421 QGCCorePlugin::instance()->preSaveToJson(this, planJson);
422 QJsonObject missionJson;
423 QJsonObject fenceJson;
424 QJsonObject rallyJson;
426 //-- Allow plugin to preemptly add its own keys to mission
427 QGCCorePlugin::instance()->preSaveToMissionJson(this, missionJson);
428 _missionController.save(missionJson);
429 //-- Allow plugin to add its own keys to mission
431 _geoFenceController.save(fenceJson);
432 _rallyPointController.save(rallyJson);
433 planJson[kJsonMissionObjectKey] = missionJson;
434 planJson[kJsonGeoFenceObjectKey] = fenceJson;
435 planJson[kJsonRallyPointsObjectKey] = rallyJson;
436 QGCCorePlugin::instance()->postSaveToJson(this, planJson);
437 return QJsonDocument(planJson);
438}
439
440bool
442{
443 if (!_currentPlanFile.isEmpty()) {
444 const bool saveSuccess = saveToFile(_currentPlanFile);
445 return saveSuccess;
446 }
447
448 return false;
449}
450
451bool PlanMasterController::saveToFile(const QString& filename)
452{
453 if (filename.isEmpty()) {
454 return false;
455 }
456
457 QString planFilename = filename;
458 if (!QFileInfo(filename).fileName().contains(".")) {
459 planFilename += QString(".%1").arg(fileExtension());
460 }
461
462 QFile file(planFilename);
463
464 if (!file.open(QIODevice::WriteOnly | QIODevice::Text)) {
465 QGC::showAppMessage(tr("Plan save error %1 : %2").arg(filename).arg(file.errorString()));
466 return false;
467 } else {
468 const QByteArray saveBytes = saveToJson().toJson();
469 const qint64 bytesWritten = file.write(saveBytes);
470 if (bytesWritten != saveBytes.size()) {
471 QGC::showAppMessage(tr("Plan save error %1 : %2").arg(filename).arg(file.errorString()));
472 return false;
473 }
474 if(_currentPlanFile != planFilename) {
475 _currentPlanFile = planFilename;
477 }
478 _setDirtyForSave(false);
479 }
480
481 return true;
482}
483
484void PlanMasterController::saveToKml(const QString& filename)
485{
486 if (filename.isEmpty()) {
487 return;
488 }
489
490 QString kmlFilename = filename;
491 if (!QFileInfo(filename).fileName().contains(".")) {
492 kmlFilename += QString(".%1").arg(kmlFileExtension());
493 }
494
495 QFile file(kmlFilename);
496
497 if (!file.open(QIODevice::WriteOnly | QIODevice::Text)) {
498 QGC::showAppMessage(tr("KML save error %1 : %2").arg(filename).arg(file.errorString()));
499 } else {
500 KMLPlanDomDocument planKML;
501 _missionController.addMissionToKML(planKML);
502 QTextStream stream(&file);
503 stream << planKML.toString();
504 file.close();
505 }
506}
507
509{
510 _suppressOverallDirtyUpdate = true;
511 _missionController.removeAll();
512 _geoFenceController.removeAll();
513 _rallyPointController.removeAll();
514 _suppressOverallDirtyUpdate = false;
515
516 _setDirtyStates(false, false);
517 if (_offline) {
518 _clearCurrentPlanFile();
519 }
521}
522
524{
525 if (!offline()) {
526 _missionController.removeAllFromVehicle();
527 if (_geoFenceController.supported()) {
528 _geoFenceController.removeAllFromVehicle();
529 }
530 if (_rallyPointController.supported()) {
531 _rallyPointController.removeAllFromVehicle();
532 }
533 _setDirtyForUpload(false);
534 _clearCurrentPlanFile();
535 } else {
536 qCCritical(PlanMasterControllerLog) << "PlanMasterController::removeAllFromVehicle called while offline";
537 }
539}
540
542{
543 return _missionController.containsItems() || _geoFenceController.containsItems() || _rallyPointController.containsItems();
544}
545
546void PlanMasterController::_updateShowCreateFromTemplate(void)
547{
548 // When the plan becomes empty, always return to template-selection mode regardless
549 // of how the items were removed.
550 if (!containsItems() && _userSelectedManualCreation) {
551 _userSelectedManualCreation = false;
553 }
554 const bool show = showCreateFromTemplate();
555 if (show != _showCreateFromTemplate) {
556 _showCreateFromTemplate = show;
558 }
559}
560
565
567{
568 return _currentPlanFile.isEmpty() ? QString() : QFileInfo(_currentPlanFile).completeBaseName();
569}
570
571void PlanMasterController::_clearCurrentPlanFile()
572{
573 if (!_currentPlanFile.isEmpty()) {
574 _currentPlanFile.clear();
576 }
577}
578
583
585{
586 QStringList filters;
587
588 filters << tr("Supported types (*.%1 *.%2 *.%3)").arg(AppSettings::planFileExtension).arg(AppSettings::waypointsFileExtension).arg("txt") <<
589 tr("All Files (*)");
590 return filters;
591}
592
593
595{
596 QStringList filters;
597
598 filters << tr("Plan Files (*.%1)").arg(fileExtension()) << tr("All Files (*)");
599 return filters;
600}
601
602void PlanMasterController::sendPlanToVehicle(Vehicle* vehicle, const QString& filename)
603{
604 // Use a transient PlanMasterController to accomplish this
605 PlanMasterController* controller = new PlanMasterController();
606 controller->startStaticActiveVehicle(vehicle, true /* deleteWhenSendCompleted */);
607 controller->loadFromFile(filename);
608 controller->sendToVehicle();
609}
610
611void PlanMasterController::_showPlanFromManagerVehicle(void)
612{
613 if (!_managerVehicle->initialPlanRequestComplete()) {
614 // The sub-controllers pick up the editor contents as the initial download arrives
615 _loadSequence = SyncSequence::InitialPlanLoad;
616 return;
617 }
618
619 // The crazy if structure is to handle the load propagating by itself through the system
620 if (!_missionController.showPlanFromManagerVehicle()) {
621 if (!_geoFenceController.showPlanFromManagerVehicle()) {
622 _rallyPointController.showPlanFromManagerVehicle();
623 }
624 }
625
626 // The editor now shows the vehicle's plan: not dirty, and any previous file
627 // association no longer describes the contents
628 _clearCurrentPlanFile();
629 _setDirtyStates(false, false);
630}
631
632void PlanMasterController::_initialPlanRequestCompleteChanged(bool initialPlanRequestComplete)
633{
634 if (!initialPlanRequestComplete || _loadSequence != SyncSequence::InitialPlanLoad) {
635 return;
636 }
637 _loadSequence = SyncSequence::Idle;
638 qCDebug(PlanMasterControllerLog) << "_initialPlanRequestCompleteChanged: initial download complete";
639 // Import the manager data into the editor (non-empty sub-controllers reject the automatic
640 // manager updates during download) and scrub dirty/file association.
641 _showPlanFromManagerVehicle();
642}
643
645{
646 return _missionController.syncInProgress() ||
647 _geoFenceController.syncInProgress() ||
648 _rallyPointController.syncInProgress();
649}
650
652{
653 return _missionController.isEmpty() &&
654 _geoFenceController.isEmpty() &&
655 _rallyPointController.isEmpty();
656}
657
658void PlanMasterController::_updateOverallDirty(void)
659{
660 if (syncInProgress() || _suppressOverallDirtyUpdate) {
661 return;
662 }
663
664 const bool saveDirty = _missionController.dirty() || _geoFenceController.dirty() || _rallyPointController.dirty();
665 if (saveDirty) {
666 _setDirtyForSave(true);
667 }
668}
669
670void PlanMasterController::_setDirtyForSave(bool dirtyForSave)
671{
672 if (!dirtyForSave) {
673 _clearElementDirtyStates();
674 }
675 if (_dirtyForSave != dirtyForSave) {
676 _dirtyForSave = dirtyForSave;
677 emit dirtyForSaveChanged(_dirtyForSave);
678
679 if (_dirtyForSave) {
680 _setDirtyForUpload(true);
681 }
682 }
683}
684
685void PlanMasterController::_setDirtyForUpload(bool dirtyForUpload)
686{
687 if (_dirtyForUpload != dirtyForUpload) {
688 _dirtyForUpload = dirtyForUpload;
689 emit dirtyForUploadChanged(_dirtyForUpload);
690 }
691}
692
693void PlanMasterController::_setDirtyStates(bool dirtyForSave, bool dirtyForUpload)
694{
695 if (!dirtyForSave) {
696 _clearElementDirtyStates();
697 }
698
699 const bool saveChanged = (_dirtyForSave != dirtyForSave);
700 const bool uploadChanged = (_dirtyForUpload != dirtyForUpload);
701
702 _dirtyForSave = dirtyForSave;
703 _dirtyForUpload = dirtyForUpload;
704
705 if (saveChanged) {
706 emit dirtyForSaveChanged(_dirtyForSave);
707 }
708 if (uploadChanged) {
709 emit dirtyForUploadChanged(_dirtyForUpload);
710 }
711}
712
713// Element controllers only signal on a dirty transition, so they must be clean whenever the plan is clean for save
714void PlanMasterController::_clearElementDirtyStates()
715{
716 _suppressOverallDirtyUpdate = true;
717 _missionController.setDirty(false);
718 _geoFenceController.setDirty(false);
719 _rallyPointController.setDirty(false);
720 _suppressOverallDirtyUpdate = false;
721}
722
723void PlanMasterController::_updatePlanCreatorsList(void)
724{
725 if (_flyView) {
726 return;
727 }
728
729 const auto vehicleClass = _managerVehicle->vehicleClass();
730
731 // Only rebuild if the vehicle class actually changed
732 if (_planCreators && _planCreatorsVehicleClass == vehicleClass) {
733 return;
734 }
735
736 if (!_planCreators) {
737 _planCreators = new QmlObjectListModel(this);
738 } else {
739 _planCreators->clearAndDeleteContents();
740 }
741
742 _planCreatorsVehicleClass = vehicleClass;
743
744 // Allow custom builds to provide their own list of plan creators
745 const QList<PlanCreator*> creators = QGCCorePlugin::instance()->planCreators(this);
746
747 // Filter by vehicle class and add to the model
748 for (PlanCreator* creator : creators) {
749 if (creator->supportsVehicleClass(vehicleClass)) {
750 _planCreators->append(creator);
751 } else {
752 delete creator;
753 }
754 }
755
756 emit planCreatorsChanged(_planCreators);
757}
758
760{
761 const int missionState = _missionController.readyForSaveState();
762 if (missionState != VisualMissionItem::ReadyForSave) {
763 return missionState;
764 }
765
766 if (_rallyPointController.terrainQueryPending()) {
768 }
769
771}
772
774{
775 if (offline()) {
776 // There is no new vehicle so clear any previous plan
777 qCDebug(PlanMasterControllerLog) << "showPlanFromManagerVehicle: Plan View - No new vehicle, clear any previous plan";
778 removeAll();
779 } else {
780 // We have a new active vehicle, show the plan from that
781 qCDebug(PlanMasterControllerLog) << "showPlanFromManagerVehicle: Plan View - New vehicle available, show plan from new manager vehicle";
782 _showPlanFromManagerVehicle();
783 }
784}
785
787{
788 if (_userSelectedManualCreation != userSelectedManualCreation) {
789 _userSelectedManualCreation = userSelectedManualCreation;
791 // Update showCreateFromTemplate directly — do not go through _updateShowCreateFromTemplate,
792 // which would immediately auto-clear the flag if the plan happens to be empty right now.
793 const bool show = showCreateFromTemplate();
794 if (show != _showCreateFromTemplate) {
795 _showCreateFromTemplate = show;
797 }
798 }
799}
std::shared_ptr< LinkInterface > SharedLinkInterfacePtr
#define QGC_LOGGING_CATEGORY(name, categoryStr)
static constexpr const char * planFileExtension
static constexpr const char * kmlFileExtension
static constexpr const char * waypointsFileExtension
void loadComplete(void)
void sendComplete(bool error)
Used to convert a Plan to a KML document.
void activeVehicleChanged(Vehicle *activeVehicle)
void syncInProgressChanged(bool syncInProgress)
void dirtyChanged(bool dirty)
void sendComplete(bool error)
void newMissionItemsAvailable(bool removeAllRequested)
Q_INVOKABLE void removeAll(void)
Removes all from controller only, sync required to remove from vehicle.
PlanMasterController(QObject *parent=nullptr)
Q_INVOKABLE bool saveToCurrent()
QStringList saveNameFilters(void) const
void promptForPlanUsageOnVehicleChange(void)
void showCreateFromTemplateChanged()
Q_INVOKABLE void saveToKml(const QString &filename)
void setUserSelectedManualCreation(bool userSelectedManualCreation)
Q_INVOKABLE void showPlanFromManagerVehicle(void)
Replaces any current plan with the plan from the manager vehicle even if offline.
static constexpr int kPlanFileVersion
Q_INVOKABLE void start(void)
Should be called immediately upon Component.onCompleted.
QStringList loadNameFilters(void) const
void planCreatorsChanged(QmlObjectListModel *planCreators)
Q_INVOKABLE void loadFromVehicle(void)
void managerVehicleChanged(Vehicle *managerVehicle)
QString kmlFileExtension(void) const
void syncInProgressChanged(void)
Q_INVOKABLE void loadFromFile(const QString &filename)
Q_INVOKABLE void startStaticActiveVehicle(Vehicle *vehicle, bool deleteWhenSendCompleted=false)
bool dirtyForUpload(void) const
static constexpr const char * kJsonGeoFenceObjectKey
void dirtyForSaveChanged(bool dirtyForSave)
QString currentPlanFileName(void) const
void userSelectedManualCreationChanged()
bool syncInProgress(void) const
void currentPlanFileChanged(void)
bool showCreateFromTemplate(void) const
bool dirtyForSave(void) const
QString fileExtension(void) const
~PlanMasterController()
true: User is not using a template to create the plan
Q_INVOKABLE bool saveToFile(const QString &filename)
void dirtyForUploadChanged(bool dirtyForUpload)
static constexpr const char * kPlanFileType
static constexpr const char * kJsonMissionObjectKey
Q_INVOKABLE void removeAllFromVehicle(void)
Removes all from vehicle and controller.
static void sendPlanToVehicle(Vehicle *vehicle, const QString &filename)
Q_INVOKABLE int readyForSaveState(void) const
void offlineChanged(bool offlineEditing)
Q_INVOKABLE void sendToVehicle(void)
bool userSelectedManualCreation(void) const
static constexpr const char * kJsonRallyPointsObjectKey
virtual void postSaveToMissionJson(PlanMasterController *pController, QJsonObject &missionJson)
Allows custom builds to add custom items to the mission section of the plan file after the item is cr...
virtual void preLoadFromJson(PlanMasterController *pController, QJsonObject &json)
Allows custom builds to load custom items from the plan file before the document is parsed.
virtual void preSaveToMissionJson(PlanMasterController *pController, QJsonObject &missionJson)
Allows custom builds to add custom items to the mission section of the plan file before the item is c...
virtual void preSaveToJson(PlanMasterController *pController, QJsonObject &json)
Allows custom builds to add custom items to the plan file before the document is created.
virtual void postSaveToJson(PlanMasterController *pController, QJsonObject &json)
Allows custom builds to add custom items to the plan file after the document is created.
virtual QList< PlanCreator * > planCreators(PlanMasterController *planMasterController)
static QGCCorePlugin * instance()
virtual void postLoadFromJson(PlanMasterController *pController, QJsonObject &json)
Allows custom builds to load custom items from the plan file after the document is parsed.
void loadComplete(void)
void sendComplete(bool error)
static SettingsManager * instance()
AppSettings * appSettings() const
void initialPlanRequestCompleteChanged(bool initialPlanRequestComplete)
bool initialPlanRequestComplete() const
Definition Vehicle.h:738
void vehicleTypeChanged()
bool validateExternalQGCJsonFile(const QJsonObject &jsonObject, const QString &expectedFileType, int minSupportedVersion, int maxSupportedVersion, int &version, QString &errorString)
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.
bool isJsonFile(const QByteArray &bytes, QJsonDocument &jsonDoc, QString &errorString)
Determines whether an in-memory byte buffer contains parseable JSON content.
void saveQGCJsonFileHeader(QJsonObject &jsonObject, const QString &fileType, int version)
Saves the standard QGC file header (groundStation, fileType, version) into the json object.
void showAppMessage(const QString &message, const QString &title)
Modal application message. Queued if the UI isn't ready yet.
Definition AppMessages.cc:9