PlanMasterController.cc 22.6 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11
/****************************************************************************
 *
 *   (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.
 *
 ****************************************************************************/

#include "PlanMasterController.h"
#include "QGCApplication.h"
12
#include "QGCCorePlugin.h"
13
#include "MultiVehicleManager.h"
14
#include "SettingsManager.h"
15 16
#include "AppSettings.h"
#include "JsonHelper.h"
17
#include "MissionManager.h"
18
#include "KML.h"
19 20 21
#if defined(QGC_AIRMAP_ENABLED)
#include "AirspaceFlightPlanProvider.h"
#endif
22

23
#include <QDomDocument>
24
#include <QJsonDocument>
Don Gagne's avatar
Don Gagne committed
25
#include <QFileInfo>
26

DonLakeFlyer's avatar
DonLakeFlyer committed
27 28
QGC_LOGGING_CATEGORY(PlanMasterControllerLog, "PlanMasterControllerLog")

29 30 31 32 33
const int   PlanMasterController::kPlanFileVersion =            1;
const char* PlanMasterController::kPlanFileType =               "Plan";
const char* PlanMasterController::kJsonMissionObjectKey =       "mission";
const char* PlanMasterController::kJsonGeoFenceObjectKey =      "geoFence";
const char* PlanMasterController::kJsonRallyPointsObjectKey =   "rallyPoints";
34 35

PlanMasterController::PlanMasterController(QObject* parent)
36 37 38
    : QObject                   (parent)
    , _multiVehicleMgr          (qgcApp()->toolbox()->multiVehicleManager())
    , _controllerVehicle        (new Vehicle(
39 40 41
        static_cast<MAV_AUTOPILOT>(qgcApp()->toolbox()->settingsManager()->appSettings()->offlineEditingFirmwareType()->rawValue().toInt()),
        static_cast<MAV_TYPE>(qgcApp()->toolbox()->settingsManager()->appSettings()->offlineEditingVehicleType()->rawValue().toInt()),
        qgcApp()->toolbox()->firmwarePluginManager()))
42 43 44 45 46 47 48 49 50 51 52
    , _managerVehicle           (_controllerVehicle)
    , _flyView                  (true)
    , _offline                  (true)
    , _missionController        (this)
    , _geoFenceController       (this)
    , _rallyPointController     (this)
    , _loadGeoFence             (false)
    , _loadRallyPoints          (false)
    , _sendGeoFence             (false)
    , _sendRallyPoints          (false)
    , _deleteWhenSendCompleted  (false)
53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71
{
    connect(&_missionController,    &MissionController::dirtyChanged,       this, &PlanMasterController::dirtyChanged);
    connect(&_geoFenceController,   &GeoFenceController::dirtyChanged,      this, &PlanMasterController::dirtyChanged);
    connect(&_rallyPointController, &RallyPointController::dirtyChanged,    this, &PlanMasterController::dirtyChanged);

    connect(&_missionController,    &MissionController::containsItemsChanged,       this, &PlanMasterController::containsItemsChanged);
    connect(&_geoFenceController,   &GeoFenceController::containsItemsChanged,      this, &PlanMasterController::containsItemsChanged);
    connect(&_rallyPointController, &RallyPointController::containsItemsChanged,    this, &PlanMasterController::containsItemsChanged);

    connect(&_missionController,    &MissionController::syncInProgressChanged,      this, &PlanMasterController::syncInProgressChanged);
    connect(&_geoFenceController,   &GeoFenceController::syncInProgressChanged,     this, &PlanMasterController::syncInProgressChanged);
    connect(&_rallyPointController, &RallyPointController::syncInProgressChanged,   this, &PlanMasterController::syncInProgressChanged);
}

PlanMasterController::~PlanMasterController()
{

}

72
void PlanMasterController::start(bool flyView)
73
{
74 75 76 77
    _flyView = flyView;
    _missionController.start(_flyView);
    _geoFenceController.start(_flyView);
    _rallyPointController.start(_flyView);
78 79 80

    connect(_multiVehicleMgr, &MultiVehicleManager::activeVehicleChanged, this, &PlanMasterController::_activeVehicleChanged);
    _activeVehicleChanged(_multiVehicleMgr->activeVehicle());
81 82 83

#if defined(QGC_AIRMAP_ENABLED)
    //-- This assumes there is one single instance of PlanMasterController in edit mode.
84
    if(!flyView) {
85 86
        // Wait for signal confirming AirMap client connection before starting flight planning
        connect(qgcApp()->toolbox()->airspaceManager(), &AirspaceManager::connectStatusChanged, this, &PlanMasterController::_startFlightPlanning);
87 88
    }
#endif
89 90
}

91
void PlanMasterController::startStaticActiveVehicle(Vehicle* vehicle, bool deleteWhenSendCompleted)
92
{
93
    _flyView = true;
94
    _deleteWhenSendCompleted = deleteWhenSendCompleted;
95 96 97
    _missionController.start(_flyView);
    _geoFenceController.start(_flyView);
    _rallyPointController.start(_flyView);
98 99 100 101 102
    _activeVehicleChanged(vehicle);
}

void PlanMasterController::_activeVehicleChanged(Vehicle* activeVehicle)
{
103 104 105
    if (_managerVehicle == activeVehicle) {
        // We are already setup for this vehicle
        return;
106 107
    }

DonLakeFlyer's avatar
DonLakeFlyer committed
108 109
    qCDebug(PlanMasterControllerLog) << "_activeVehicleChanged" << activeVehicle;

110 111 112 113 114 115 116 117 118 119
    if (_managerVehicle) {
        // Disconnect old vehicle
        disconnect(_managerVehicle->missionManager(),       &MissionManager::newMissionItemsAvailable,  this, &PlanMasterController::_loadMissionComplete);
        disconnect(_managerVehicle->geoFenceManager(),      &GeoFenceManager::loadComplete,             this, &PlanMasterController::_loadGeoFenceComplete);
        disconnect(_managerVehicle->rallyPointManager(),    &RallyPointManager::loadComplete,           this, &PlanMasterController::_loadRallyPointsComplete);
        disconnect(_managerVehicle->missionManager(),       &MissionManager::sendComplete,              this, &PlanMasterController::_sendMissionComplete);
        disconnect(_managerVehicle->geoFenceManager(),      &GeoFenceManager::sendComplete,             this, &PlanMasterController::_sendGeoFenceComplete);
        disconnect(_managerVehicle->rallyPointManager(),    &RallyPointManager::sendComplete,           this, &PlanMasterController::_sendRallyPointsComplete);
    }

120
    bool newOffline = false;
121
    if (activeVehicle == nullptr) {
122 123 124
        // Since there is no longer an active vehicle we use the offline controller vehicle as the manager vehicle
        _managerVehicle = _controllerVehicle;
        newOffline = true;
125
    } else {
126
        newOffline = false;
127 128 129 130 131 132 133 134
        _managerVehicle = activeVehicle;

        // Update controllerVehicle to the currently connected vehicle
        AppSettings* appSettings = qgcApp()->toolbox()->settingsManager()->appSettings();
        appSettings->offlineEditingFirmwareType()->setRawValue(AppSettings::offlineEditingFirmwareTypeFromFirmwareType(_managerVehicle->firmwareType()));
        appSettings->offlineEditingVehicleType()->setRawValue(AppSettings::offlineEditingVehicleTypeFromVehicleType(_managerVehicle->vehicleType()));

        // We use these signals to sequence upload and download to the multiple controller/managers
DonLakeFlyer's avatar
DonLakeFlyer committed
135 136 137 138 139 140
        connect(_managerVehicle->missionManager(),      &MissionManager::newMissionItemsAvailable,  this, &PlanMasterController::_loadMissionComplete);
        connect(_managerVehicle->geoFenceManager(),     &GeoFenceManager::loadComplete,             this, &PlanMasterController::_loadGeoFenceComplete);
        connect(_managerVehicle->rallyPointManager(),   &RallyPointManager::loadComplete,           this, &PlanMasterController::_loadRallyPointsComplete);
        connect(_managerVehicle->missionManager(),      &MissionManager::sendComplete,              this, &PlanMasterController::_sendMissionComplete);
        connect(_managerVehicle->geoFenceManager(),     &GeoFenceManager::sendComplete,             this, &PlanMasterController::_sendGeoFenceComplete);
        connect(_managerVehicle->rallyPointManager(),   &RallyPointManager::sendComplete,           this, &PlanMasterController::_sendRallyPointsComplete);
141
    }
142

143 144 145
    _missionController.managerVehicleChanged(_managerVehicle);
    _geoFenceController.managerVehicleChanged(_managerVehicle);
    _rallyPointController.managerVehicleChanged(_managerVehicle);
146

147 148 149 150 151 152 153
    // Vehicle changed so we need to signal everything
    _offline = newOffline;
    emit containsItemsChanged(containsItems());
    emit syncInProgressChanged();
    emit dirtyChanged(dirty());
    emit offlineChanged(offline());

154
    if (!_flyView) {
DonLakeFlyer's avatar
DonLakeFlyer committed
155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173
        if (!offline()) {
            // We are in Plan view and we have a newly connected vehicle:
            //  - If there is no plan available in Plan view show the one from the vehicle
            //  - Otherwise leave the current plan alone
            if (!containsItems()) {
                qCDebug(PlanMasterControllerLog) << "_activeVehicleChanged: Plan view is empty so loading from manager";
                _showPlanFromManagerVehicle();
            }
        }
    } else {
        if (offline()) {
            // No more active vehicle, clear mission
            qCDebug(PlanMasterControllerLog) << "_activeVehicleChanged: Fly view is offline clearing plan";
            removeAll();
        } else {
            // Fly view has changed to a new active vehicle, update to show correct mission
            qCDebug(PlanMasterControllerLog) << "_activeVehicleChanged: Fly view is online so loading from manager";
            _showPlanFromManagerVehicle();
        }
174
    }
175 176 177 178
}

void PlanMasterController::loadFromVehicle(void)
{
179 180 181 182 183
    if (_managerVehicle->highLatencyLink()) {
        qgcApp()->showMessage(tr("Download not supported on high latency links."));
        return;
    }

DonLakeFlyer's avatar
DonLakeFlyer committed
184 185
    if (offline()) {
        qCWarning(PlanMasterControllerLog) << "PlanMasterController::loadFromVehicle called while offline";
186
    } else if (_flyView) {
DonLakeFlyer's avatar
DonLakeFlyer committed
187 188 189 190
        qCWarning(PlanMasterControllerLog) << "PlanMasterController::loadFromVehicle called from Fly view";
    } else if (syncInProgress()) {
        qCWarning(PlanMasterControllerLog) << "PlanMasterController::loadFromVehicle called while syncInProgress";
    } else {
191
        _loadGeoFence = true;
192
        qCDebug(PlanMasterControllerLog) << "PlanMasterController::loadFromVehicle calling _missionController.loadFromVehicle";
193 194 195
        _missionController.loadFromVehicle();
        setDirty(false);
    }
196 197
}

198

DonLakeFlyer's avatar
DonLakeFlyer committed
199
void PlanMasterController::_loadMissionComplete(void)
200
{
201
    if (!_flyView && _loadGeoFence) {
202 203
        _loadGeoFence = false;
        _loadRallyPoints = true;
204
        if (_geoFenceController.supported()) {
205
            qCDebug(PlanMasterControllerLog) << "PlanMasterController::_loadMissionComplete calling _geoFenceController.loadFromVehicle";
206 207 208 209 210 211
            _geoFenceController.loadFromVehicle();
        } else {
            qCDebug(PlanMasterControllerLog) << "PlanMasterController::_loadMissionComplete GeoFence not supported skipping";
            _geoFenceController.removeAll();
            _loadGeoFenceComplete();
        }
212
        setDirty(false);
DonLakeFlyer's avatar
DonLakeFlyer committed
213 214 215 216 217
    }
}

void PlanMasterController::_loadGeoFenceComplete(void)
{
218
    if (!_flyView && _loadRallyPoints) {
DonLakeFlyer's avatar
DonLakeFlyer committed
219
        _loadRallyPoints = false;
220
        if (_rallyPointController.supported()) {
221
            qCDebug(PlanMasterControllerLog) << "PlanMasterController::_loadGeoFenceComplete calling _rallyPointController.loadFromVehicle";
222 223 224 225 226 227
            _rallyPointController.loadFromVehicle();
        } else {
            qCDebug(PlanMasterControllerLog) << "PlanMasterController::_loadMissionComplete Rally Points not supported skipping";
            _rallyPointController.removeAll();
            _loadRallyPointsComplete();
        }
DonLakeFlyer's avatar
DonLakeFlyer committed
228 229 230 231 232 233
        setDirty(false);
    }
}

void PlanMasterController::_loadRallyPointsComplete(void)
{
234
    qCDebug(PlanMasterControllerLog) << "PlanMasterController::_loadRallyPointsComplete";
DonLakeFlyer's avatar
DonLakeFlyer committed
235 236 237 238
}

void PlanMasterController::_sendMissionComplete(void)
{
239
    if (_sendGeoFence) {
240 241
        _sendGeoFence = false;
        _sendRallyPoints = true;
242 243 244 245 246 247 248
        if (_geoFenceController.supported()) {
            qCDebug(PlanMasterControllerLog) << "PlanMasterController::sendToVehicle start GeoFence sendToVehicle";
            _geoFenceController.sendToVehicle();
        } else {
            qCDebug(PlanMasterControllerLog) << "PlanMasterController::sendToVehicle GeoFence not supported skipping";
            _sendGeoFenceComplete();
        }
249 250 251 252
        setDirty(false);
    }
}

DonLakeFlyer's avatar
DonLakeFlyer committed
253
void PlanMasterController::_sendGeoFenceComplete(void)
254
{
255
    if (_sendRallyPoints) {
256
        _sendRallyPoints = false;
257 258 259 260 261 262 263
        if (_rallyPointController.supported()) {
            qCDebug(PlanMasterControllerLog) << "PlanMasterController::sendToVehicle start rally sendToVehicle";
            _rallyPointController.sendToVehicle();
        } else {
            qCDebug(PlanMasterControllerLog) << "PlanMasterController::sendToVehicle Rally Points not support skipping";
            _sendRallyPointsComplete();
        }
264 265 266
    }
}

DonLakeFlyer's avatar
DonLakeFlyer committed
267 268
void PlanMasterController::_sendRallyPointsComplete(void)
{
269
    qCDebug(PlanMasterControllerLog) << "PlanMasterController::sendToVehicle Rally Point send complete";
270 271 272
    if (_deleteWhenSendCompleted) {
        this->deleteLater();
    }
DonLakeFlyer's avatar
DonLakeFlyer committed
273 274
}

275
#if defined(QGC_AIRMAP_ENABLED)
276
void PlanMasterController::_startFlightPlanning(void) {
277 278 279 280
    if (qgcApp()->toolbox()->airspaceManager()->connected()) {
        qCDebug(PlanMasterControllerLog) << "PlanMasterController::_startFlightPlanning client connected, start flight planning";
        qgcApp()->toolbox()->airspaceManager()->flightPlan()->startFlightPlanning(this);
    }
281
}
282
#endif
283

284 285
void PlanMasterController::sendToVehicle(void)
{
286 287 288 289 290
    if (_managerVehicle->highLatencyLink()) {
        qgcApp()->showMessage(tr("Upload not supported on high latency links."));
        return;
    }

DonLakeFlyer's avatar
DonLakeFlyer committed
291 292 293 294 295 296
    if (offline()) {
        qCWarning(PlanMasterControllerLog) << "PlanMasterController::sendToVehicle called while offline";
    } else if (syncInProgress()) {
        qCWarning(PlanMasterControllerLog) << "PlanMasterController::sendToVehicle called while syncInProgress";
    } else {
        qCDebug(PlanMasterControllerLog) << "PlanMasterController::sendToVehicle start mission sendToVehicle";
297 298 299 300
        _sendGeoFence = true;
        _missionController.sendToVehicle();
        setDirty(false);
    }
301 302 303 304 305
}

void PlanMasterController::loadFromFile(const QString& filename)
{
    QString errorString;
306
    QString errorMessage = tr("Error loading Plan file (%1). %2").arg(filename).arg("%1");
307 308 309 310 311

    if (filename.isEmpty()) {
        return;
    }

312
    QFileInfo fileInfo(filename);
313 314 315 316
    QFile file(filename);

    if (!file.open(QIODevice::ReadOnly | QIODevice::Text)) {
        errorString = file.errorString() + QStringLiteral(" ") + filename;
317
        qgcApp()->showMessage(errorMessage.arg(errorString));
318 319 320
        return;
    }

321 322
    bool success = false;
    if(fileInfo.suffix() == AppSettings::planFileExtension) {
323 324 325 326 327 328 329 330 331
        QJsonDocument   jsonDoc;
        QByteArray      bytes = file.readAll();

        if (!JsonHelper::isJsonFile(bytes, jsonDoc, errorString)) {
            qgcApp()->showMessage(errorMessage.arg(errorString));
            return;
        }

        QJsonObject json = jsonDoc.object();
332 333 334 335 336
        //-- Allow plugins to pre process the load
        qgcApp()->toolbox()->corePlugin()->preLoadFromJson(this, json);

        int version;
        if (!JsonHelper::validateQGCJsonFile(json, kPlanFileType, kPlanFileVersion, kPlanFileVersion, version, errorString)) {
337 338 339 340 341
            qgcApp()->showMessage(errorMessage.arg(errorString));
            return;
        }

        QList<JsonHelper::KeyValidateInfo> rgKeyInfo = {
342 343 344
            { kJsonMissionObjectKey,        QJsonValue::Object, true },
            { kJsonGeoFenceObjectKey,       QJsonValue::Object, true },
            { kJsonRallyPointsObjectKey,    QJsonValue::Object, true },
345 346 347 348 349 350
        };
        if (!JsonHelper::validateKeys(json, rgKeyInfo, errorString)) {
            qgcApp()->showMessage(errorMessage.arg(errorString));
            return;
        }

351 352 353
        if (!_missionController.load(json[kJsonMissionObjectKey].toObject(), errorString) ||
                !_geoFenceController.load(json[kJsonGeoFenceObjectKey].toObject(), errorString) ||
                !_rallyPointController.load(json[kJsonRallyPointsObjectKey].toObject(), errorString)) {
354
            qgcApp()->showMessage(errorMessage.arg(errorString));
355
        } else {
356 357
            //-- Allow plugins to post process the load
            qgcApp()->toolbox()->corePlugin()->postLoadFromJson(this, json);
358
            success = true;
359
        }
360
    } else if (fileInfo.suffix() == AppSettings::missionFileExtension) {
361 362
        if (!_missionController.loadJsonFile(file, errorString)) {
            qgcApp()->showMessage(errorMessage.arg(errorString));
363 364
        } else {
            success = true;
365
        }
366
    } else if (fileInfo.suffix() == AppSettings::waypointsFileExtension || fileInfo.suffix() == QStringLiteral("txt")) {
367 368
        if (!_missionController.loadTextFile(file, errorString)) {
            qgcApp()->showMessage(errorMessage.arg(errorString));
369 370
        } else {
            success = true;
371
        }
372 373 374 375 376 377 378 379
    } else {
        //-- TODO: What then?
    }

    if(success){
        _currentPlanFile.sprintf("%s/%s.%s", fileInfo.path().toLocal8Bit().data(), fileInfo.completeBaseName().toLocal8Bit().data(), AppSettings::planFileExtension);
    } else {
        _currentPlanFile.clear();
380
    }
381
    emit currentPlanFileChanged();
382

383
    if (!offline()) {
384
        setDirty(true);
385 386 387
    }
}

Gus Grubba's avatar
Gus Grubba committed
388 389 390
QJsonDocument PlanMasterController::saveToJson()
{
    QJsonObject planJson;
391
    qgcApp()->toolbox()->corePlugin()->preSaveToJson(this, planJson);
Gus Grubba's avatar
Gus Grubba committed
392 393 394
    QJsonObject missionJson;
    QJsonObject fenceJson;
    QJsonObject rallyJson;
395 396 397
    JsonHelper::saveQGCJsonFileHeader(planJson, kPlanFileType, kPlanFileVersion);
    //-- Allow plugin to preemptly add its own keys to mission
    qgcApp()->toolbox()->corePlugin()->preSaveToMissionJson(this, missionJson);
Gus Grubba's avatar
Gus Grubba committed
398
    _missionController.save(missionJson);
399 400
    //-- Allow plugin to add its own keys to mission
    qgcApp()->toolbox()->corePlugin()->postSaveToMissionJson(this, missionJson);
Gus Grubba's avatar
Gus Grubba committed
401 402
    _geoFenceController.save(fenceJson);
    _rallyPointController.save(rallyJson);
403 404 405 406
    planJson[kJsonMissionObjectKey] = missionJson;
    planJson[kJsonGeoFenceObjectKey] = fenceJson;
    planJson[kJsonRallyPointsObjectKey] = rallyJson;
    qgcApp()->toolbox()->corePlugin()->postSaveToJson(this, planJson);
Gus Grubba's avatar
Gus Grubba committed
407 408 409
    return QJsonDocument(planJson);
}

410 411 412 413 414 415 416 417
void
PlanMasterController::saveToCurrent()
{
    if(!_currentPlanFile.isEmpty()) {
        saveToFile(_currentPlanFile);
    }
}

418 419 420 421 422 423 424 425 426 427 428 429 430 431 432
void PlanMasterController::saveToFile(const QString& filename)
{
    if (filename.isEmpty()) {
        return;
    }

    QString planFilename = filename;
    if (!QFileInfo(filename).fileName().contains(".")) {
        planFilename += QString(".%1").arg(fileExtension());
    }

    QFile file(planFilename);

    if (!file.open(QIODevice::WriteOnly | QIODevice::Text)) {
        qgcApp()->showMessage(tr("Plan save error %1 : %2").arg(filename).arg(file.errorString()));
433 434
        _currentPlanFile.clear();
        emit currentPlanFileChanged();
435
    } else {
Gus Grubba's avatar
Gus Grubba committed
436
        QJsonDocument saveDoc = saveToJson();
437
        file.write(saveDoc.toJson());
438 439 440 441
        if(_currentPlanFile != planFilename) {
            _currentPlanFile = planFilename;
            emit currentPlanFileChanged();
        }
442 443
    }

444 445
    // Only clear dirty bit if we are offline
    if (offline()) {
446 447 448 449
        setDirty(false);
    }
}

450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473
void PlanMasterController::saveToKml(const QString& filename)
{
    if (filename.isEmpty()) {
        return;
    }

    QString kmlFilename = filename;
    if (!QFileInfo(filename).fileName().contains(".")) {
        kmlFilename += QString(".%1").arg(kmlFileExtension());
    }

    QFile file(kmlFilename);

    if (!file.open(QIODevice::WriteOnly | QIODevice::Text)) {
        qgcApp()->showMessage(tr("KML save error %1 : %2").arg(filename).arg(file.errorString()));
    } else {
        QDomDocument domDocument;
        _missionController.convertToKMLDocument(domDocument);
        QTextStream stream(&file);
        stream << domDocument.toString();
        file.close();
    }
}

474 475
void PlanMasterController::removeAll(void)
{
476 477 478
    _missionController.removeAll();
    _geoFenceController.removeAll();
    _rallyPointController.removeAll();
479 480 481 482
    if (_offline) {
        _missionController.setDirty(false);
        _geoFenceController.setDirty(false);
        _rallyPointController.setDirty(false);
483 484
        _currentPlanFile.clear();
        emit currentPlanFileChanged();
485
    }
486 487 488 489
}

void PlanMasterController::removeAllFromVehicle(void)
{
490 491
    if (!offline()) {
        _missionController.removeAllFromVehicle();
492 493 494 495 496 497
        if (_geoFenceController.supported()) {
            _geoFenceController.removeAllFromVehicle();
        }
        if (_rallyPointController.supported()) {
            _rallyPointController.removeAllFromVehicle();
        }
498
        setDirty(false);
499 500 501
    } else {
        qWarning() << "PlanMasterController::removeAllFromVehicle called while offline";
    }
502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525
}

bool PlanMasterController::containsItems(void) const
{
    return _missionController.containsItems() || _geoFenceController.containsItems() || _rallyPointController.containsItems();
}

bool PlanMasterController::dirty(void) const
{
    return _missionController.dirty() || _geoFenceController.dirty() || _rallyPointController.dirty();
}

void PlanMasterController::setDirty(bool dirty)
{
    _missionController.setDirty(dirty);
    _geoFenceController.setDirty(dirty);
    _rallyPointController.setDirty(dirty);
}

QString PlanMasterController::fileExtension(void) const
{
    return AppSettings::planFileExtension;
}

526 527 528 529 530
QString PlanMasterController::kmlFileExtension(void) const
{
    return AppSettings::kmlFileExtension;
}

531 532 533 534
QStringList PlanMasterController::loadNameFilters(void) const
{
    QStringList filters;

Don Gagne's avatar
Don Gagne committed
535
    filters << tr("Supported types (*.%1 *.%2 *.%3 *.%4)").arg(AppSettings::planFileExtension).arg(AppSettings::missionFileExtension).arg(AppSettings::waypointsFileExtension).arg("txt") <<
536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552
               tr("All Files (*.*)");
    return filters;
}


QStringList PlanMasterController::saveNameFilters(void) const
{
    QStringList filters;

    filters << tr("Plan Files (*.%1)").arg(fileExtension()) << tr("All Files (*.*)");
    return filters;
}

void PlanMasterController::sendPlanToVehicle(Vehicle* vehicle, const QString& filename)
{
    // Use a transient PlanMasterController to accomplish this
    PlanMasterController* controller = new PlanMasterController();
553
    controller->startStaticActiveVehicle(vehicle, true /* deleteWhenSendCompleted */);
554
    controller->loadFromFile(filename);
555
    controller->sendToVehicle();
556
}
DonLakeFlyer's avatar
DonLakeFlyer committed
557 558 559

void PlanMasterController::_showPlanFromManagerVehicle(void)
{
560
    if (!_managerVehicle->initialPlanRequestComplete() && !syncInProgress()) {
561 562 563 564
        // Something went wrong with initial load. All controllers are idle, so just force it off
        _managerVehicle->forceInitialPlanRequestComplete();
    }

565
    // The crazy if structure is to handle the load propagating by itself through the system
DonLakeFlyer's avatar
DonLakeFlyer committed
566 567 568 569 570 571
    if (!_missionController.showPlanFromManagerVehicle()) {
        if (!_geoFenceController.showPlanFromManagerVehicle()) {
            _rallyPointController.showPlanFromManagerVehicle();
        }
    }
}
572 573 574 575 576 577 578

bool PlanMasterController::syncInProgress(void) const
{
    return _missionController.syncInProgress() ||
            _geoFenceController.syncInProgress() ||
            _rallyPointController.syncInProgress();
}