Skip to content

Commit

Permalink
Add KML integration
Browse files Browse the repository at this point in the history
Signed-off-by: Patrick José Pereira <[email protected]>
  • Loading branch information
patrickelectric committed Sep 11, 2017
1 parent 74eead4 commit f55d443
Show file tree
Hide file tree
Showing 7 changed files with 111 additions and 1 deletion.
40 changes: 40 additions & 0 deletions src/MissionManager/MissionController.cc
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
****************************************************************************/


#include "MissionCommandUIInfo.h"
#include "MissionController.h"
#include "MultiVehicleManager.h"
#include "MissionManager.h"
Expand All @@ -25,6 +26,7 @@
#include "MissionSettingsItem.h"
#include "QGCQGeoCoordinate.h"
#include "PlanMasterController.h"
#include "KML.h"

#ifndef __mobile__
#include "MainWindow.h"
Expand Down Expand Up @@ -256,6 +258,44 @@ bool MissionController::_convertToMissionItems(QmlObjectListModel* visualMission
return endActionSet;
}

void MissionController::convertToKMLDocument(QDomDocument& document)
{
QJsonObject missionJson;
QmlObjectListModel* visualItems = new QmlObjectListModel();
QList<MissionItem*> missionItens;
QString error;
save(missionJson);
_loadItemsFromJson(missionJson, visualItems, error);
_convertToMissionItems(visualItems, missionItens, this);

float altitude = missionJson[_jsonPlannedHomePositionKey].toArray()[2].toDouble();

QString coord;
QStringList coords;
// Drop home position
bool dropPoint = true;
for(const auto& item : missionItens) {
if(dropPoint) {
dropPoint = false;
continue;
}
const MissionCommandUIInfo* uiInfo = \
qgcApp()->toolbox()->missionCommandTree()->getUIInfo(_controllerVehicle, item->command());

if (uiInfo && uiInfo->specifiesCoordinate() && !uiInfo->isStandaloneCoordinate()) {
coord = QString::number(item->param6()) \
+ "," \
+ QString::number(item->param5()) \
+ "," \
+ QString::number(item->param7() + altitude);
coords.append(coord);
}
}
Kml kml;
kml.points(coords);
kml.save(document);
}

void MissionController::sendItemsToVehicle(Vehicle* vehicle, QmlObjectListModel* visualMissionItems)
{
if (vehicle) {
Expand Down
4 changes: 4 additions & 0 deletions src/MissionManager/MissionController.h
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ class MissionSettingsItem;
class AppSettings;
class MissionManager;
class SimpleMissionItem;
class QDomDocument;

Q_DECLARE_LOGGING_CATEGORY(MissionControllerLog)

Expand Down Expand Up @@ -125,6 +126,9 @@ class MissionController : public PlanElementController
void managerVehicleChanged (Vehicle* managerVehicle) final;
bool showPlanFromManagerVehicle (void) final;

// Create KML file
void convertToKMLDocument(QDomDocument& document);

// Property accessors

QmlObjectListModel* visualItems (void) { return _visualItems; }
Expand Down
39 changes: 39 additions & 0 deletions src/MissionManager/PlanMasterController.cc
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,9 @@
#include "AppSettings.h"
#include "JsonHelper.h"
#include "MissionManager.h"
#include "KML.h"

#include <QDomDocument>
#include <QJsonDocument>
#include <QFileInfo>

Expand Down Expand Up @@ -372,6 +374,30 @@ void PlanMasterController::saveToFile(const QString& filename)
}
}

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();
}
}

void PlanMasterController::removeAll(void)
{
_missionController.removeAll();
Expand Down Expand Up @@ -417,6 +443,11 @@ QString PlanMasterController::fileExtension(void) const
return AppSettings::planFileExtension;
}

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

QStringList PlanMasterController::loadNameFilters(void) const
{
QStringList filters;
Expand All @@ -435,6 +466,14 @@ QStringList PlanMasterController::saveNameFilters(void) const
return filters;
}

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

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

void PlanMasterController::sendPlanToVehicle(Vehicle* vehicle, const QString& filename)
{
// Use a transient PlanMasterController to accomplish this
Expand Down
4 changes: 4 additions & 0 deletions src/MissionManager/PlanMasterController.h
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@ class PlanMasterController : public QObject
///< kml file extension for missions
Q_PROPERTY(QStringList loadNameFilters READ loadNameFilters CONSTANT) ///< File filter list loading plan files
Q_PROPERTY(QStringList saveNameFilters READ saveNameFilters CONSTANT) ///< File filter list saving plan files
Q_PROPERTY(QStringList saveKmlFilters READ saveKmlFilters CONSTANT) ///< File filter list saving KML files

/// Should be called immediately upon Component.onCompleted.
/// @param editMode true: controller being used in Plan view, false: controller being used in Fly view
Expand All @@ -60,6 +61,7 @@ class PlanMasterController : public QObject
Q_INVOKABLE void sendToVehicle(void);
Q_INVOKABLE void loadFromFile(const QString& filename);
Q_INVOKABLE void saveToFile(const QString& filename);
Q_INVOKABLE void saveToKml(const QString& filename);
Q_INVOKABLE void removeAll(void); ///< Removes all from controller only, synce required to remove from vehicle
Q_INVOKABLE void removeAllFromVehicle(void); ///< Removes all from vehicle and controller

Expand All @@ -73,8 +75,10 @@ class PlanMasterController : public QObject
bool dirty (void) const;
void setDirty (bool dirty);
QString fileExtension (void) const;
QString kmlFileExtension(void) const;
QStringList loadNameFilters (void) const;
QStringList saveNameFilters (void) const;
QStringList saveKmlFilters (void) const;

Vehicle* controllerVehicle(void) { return _controllerVehicle; }
Vehicle* managerVehicle(void) { return _managerVehicle; }
Expand Down
22 changes: 21 additions & 1 deletion src/PlanView/PlanView.qml
Original file line number Diff line number Diff line change
Expand Up @@ -169,6 +169,7 @@ QGCView {

function saveToSelectedFile() {
fileDialog.title = qsTr("Save Plan")
fileDialog.plan = true
fileDialog.selectExisting = false
fileDialog.nameFilters = masterController.saveNameFilters
fileDialog.openForSave()
Expand All @@ -177,6 +178,14 @@ QGCView {
function fitViewportToItems() {
mapFitFunctions.fitMapViewportToMissionItems()
}

function saveKmlToSelectedFile() {
fileDialog.title = qsTr("Save KML")
fileDialog.plan = false
fileDialog.selectExisting = false
fileDialog.nameFilters = masterController.saveKmlFilters
fileDialog.openForSave()
}
}

Connections {
Expand Down Expand Up @@ -228,12 +237,13 @@ QGCView {
QGCFileDialog {
id: fileDialog
qgcView: _qgcView
property var plan: true
folder: QGroundControl.settingsManager.appSettings.missionSavePath
fileExtension: QGroundControl.settingsManager.appSettings.planFileExtension
fileExtension2: QGroundControl.settingsManager.appSettings.missionFileExtension

onAcceptedForSave: {
masterController.saveToFile(file)
plan ? masterController.saveToFile(file) : masterController.saveToKml(file)
close()
}

Expand Down Expand Up @@ -792,6 +802,16 @@ QGCView {
_qgcView.showDialog(removeAllPromptDialog, qsTr("Remove all"), _qgcView.showDialogDefaultWidth, StandardButton.Yes | StandardButton.No)
}
}

QGCButton {
text: qsTr("Save KML...")
Layout.fillWidth: true
enabled: !masterController.syncInProgress
onClicked: {
dropPanel.hide()
masterController.saveKmlToSelectedFile()
}
}
}
}
}
Expand Down
1 change: 1 addition & 0 deletions src/Settings/AppSettings.cc
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@ const char* AppSettings::waypointsFileExtension = "waypoints";
const char* AppSettings::fenceFileExtension = "fence";
const char* AppSettings::rallyPointFileExtension = "rally";
const char* AppSettings::telemetryFileExtension = "tlog";
const char* AppSettings::kmlFileExtension = "kml";
const char* AppSettings::logFileExtension = "ulg";

const char* AppSettings::parameterDirectory = "Parameters";
Expand Down
2 changes: 2 additions & 0 deletions src/Settings/AppSettings.h
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,7 @@ class AppSettings : public SettingsGroup
Q_PROPERTY(QString waypointsFileExtension MEMBER waypointsFileExtension CONSTANT)
Q_PROPERTY(QString parameterFileExtension MEMBER parameterFileExtension CONSTANT)
Q_PROPERTY(QString telemetryFileExtension MEMBER telemetryFileExtension CONSTANT)
Q_PROPERTY(QString kmlFileExtension MEMBER kmlFileExtension CONSTANT)
Q_PROPERTY(QString logFileExtension MEMBER logFileExtension CONSTANT)

Fact* offlineEditingFirmwareType (void);
Expand Down Expand Up @@ -115,6 +116,7 @@ class AppSettings : public SettingsGroup
static const char* fenceFileExtension;
static const char* rallyPointFileExtension;
static const char* telemetryFileExtension;
static const char* kmlFileExtension;
static const char* logFileExtension;

// Child directories of savePath for specific file types
Expand Down

0 comments on commit f55d443

Please sign in to comment.