diff --git a/src/Wima/WimaArea.cc b/src/Wima/WimaArea.cc index bf1040c5fa819eb6db024d19f919c123715bb1fc..1398b8f65425dd8e798bc0ae46540a403dacd93d 100644 --- a/src/Wima/WimaArea.cc +++ b/src/Wima/WimaArea.cc @@ -499,7 +499,7 @@ bool WimaArea::loadFromJson(const QJsonObject &json, QString& errorString) _maxAltitude = json[maxAltitudeName].toDouble(); return true; } else { - errorString.append("Could not load Maximum Altitude value!\n"); + errorString.append(tr("Could not load Maximum Altitude value!\n")); return false; } } else { diff --git a/src/Wima/WimaController.cc b/src/Wima/WimaController.cc index 536d3a93b246adaa753065ce18063f9434fb8207..a9e363d67d27acac03a3c90e402783944a6883a3 100644 --- a/src/Wima/WimaController.cc +++ b/src/Wima/WimaController.cc @@ -225,7 +225,8 @@ bool WimaController::updateMission() } } - saveToFile("TestFile"); + //saveToFile("TestFile.wima"); + //loadFromFile("TestFile.wima"); return true; } @@ -262,9 +263,112 @@ void WimaController::saveToFile(const QString& filename) } } -void WimaController::loadFromFile() +bool WimaController::loadFromCurrent() { + return loadFromFile(_currentFile); +} + +bool WimaController::loadFromFile(const QString &filename) +{ + QString errorString; + QString errorMessage = tr("Error loading Plan file (%1). %2").arg(filename).arg("%1"); + + if (filename.isEmpty()) { + return false; + } + + QFileInfo fileInfo(filename); + QFile file(filename); + + if (!file.open(QIODevice::ReadOnly | QIODevice::Text)) { + errorString = file.errorString() + QStringLiteral(" ") + filename; + qgcApp()->showMessage(errorMessage.arg(errorString)); + return false; + } + + if(fileInfo.suffix() == wimaFileExtension) { + QJsonDocument jsonDoc; + QByteArray bytes = file.readAll(); + if (!JsonHelper::isJsonFile(bytes, jsonDoc, errorString)) { + qgcApp()->showMessage(errorMessage.arg(errorString)); + return false; + } + + QJsonObject json = jsonDoc.object(); + QJsonArray areaArray = json["AreaItems"].toArray(); + _visualItems->clear(); + + for( int i = 0; i < areaArray.size(); i++) { + QJsonObject jsonArea = areaArray[i].toObject(); + + if (jsonArea.contains(WimaArea::areaTypeName) && jsonArea[WimaArea::areaTypeName].isString()) { + if ( jsonArea[WimaArea::areaTypeName] == WimaArea::wimaAreaName ) { + WimaArea* area = new WimaArea(this); + bool success = area->loadFromJson(jsonArea, errorString); + + if ( !success ) { + qgcApp()->showMessage(errorMessage.arg(errorString)); + return false; + } + + _visualItems->append(area); + emit visualItemsChanged(); + } else if ( jsonArea[WimaArea::areaTypeName] == WimaGOperationArea::wimaGOperationAreaName) { + WimaGOperationArea* opArea = new WimaGOperationArea(this); + bool success = opArea->loadFromJson(jsonArea, errorString); + + if ( !success ) { + qgcApp()->showMessage(errorMessage.arg(errorString)); + return false; + } + + _visualItems->append(opArea); + emit visualItemsChanged(); + } else if ( jsonArea[WimaArea::areaTypeName] == WimaServiceArea::wimaServiceAreaName) { + WimaServiceArea* serArea = new WimaServiceArea(this); + bool success = serArea->loadFromJson(jsonArea, errorString); + + if ( !success ) { + qgcApp()->showMessage(errorMessage.arg(errorString)); + return false; + } + + _visualItems->append(serArea); + emit visualItemsChanged(); + } else if ( jsonArea[WimaArea::areaTypeName] == WimaVCorridor::wimaVCorridorName) { + WimaVCorridor* corridor = new WimaVCorridor(this); + bool success = corridor->loadFromJson(jsonArea, errorString); + + if ( !success ) { + qgcApp()->showMessage(errorMessage.arg(errorString)); + return false; + } + + _visualItems->append(corridor); + emit visualItemsChanged(); + } else { + errorString += QString(tr("%s not supported.\n").arg(WimaArea::areaTypeName)); + qgcApp()->showMessage(errorMessage.arg(errorString)); + return false; + } + } else { + errorString += QString(tr("Invalid or non existing entry for %s.\n").arg(WimaArea::areaTypeName)); + return false; + } + } + + _currentFile.sprintf("%s/%s.%s", fileInfo.path().toLocal8Bit().data(), fileInfo.completeBaseName().toLocal8Bit().data(), wimaFileExtension); + + emit currentFileChanged(); + + return true; + + } else { + errorString += QString(tr("File extension not supported.\n")); + qgcApp()->showMessage(errorMessage.arg(errorString)); + return false; + } } void WimaController::recalcVehicleCorridor() @@ -347,8 +451,11 @@ QJsonDocument WimaController::saveToJson() jsonArray.append(json); } + QJsonObject json; + json["AreaItems"] = jsonArray; + - return QJsonDocument(jsonArray); + return QJsonDocument(json); } diff --git a/src/Wima/WimaController.h b/src/Wima/WimaController.h index cdf961c5f491e7dadd2fbc2bbf58dd06cba400d1..587cc38ce4c89ea846fb258e58e6b06a20b4376b 100644 --- a/src/Wima/WimaController.h +++ b/src/Wima/WimaController.h @@ -15,6 +15,9 @@ #include "SimpleMissionItem.h" #include "MissionSettingsItem.h" +#include "JsonHelper.h" +#include "QGCApplication.h" + class WimaController : public QObject { @@ -27,9 +30,9 @@ public: Q_PROPERTY(MissionController* missionController READ missionController WRITE setMissionController NOTIFY missionControllerChanged) Q_PROPERTY(QmlObjectListModel* visualItems READ visualItems NOTIFY visualItemsChanged) Q_PROPERTY(int currentPolygonIndex READ currentPolygonIndex WRITE setCurrentPolygonIndex NOTIFY currentPolygonIndexChanged) - Q_PROPERTY(const QString currentFile READ currentFile NOTIFY currentFileChanged) + Q_PROPERTY(QString currentFile READ currentFile NOTIFY currentFileChanged) Q_PROPERTY(QStringList loadNameFilters READ loadNameFilters CONSTANT) - Q_PROPERTY(QString fileExtenstion READ fileExtenstion CONSTANT) + Q_PROPERTY(QString fileExtension READ fileExtension CONSTANT) // Property accessors @@ -37,9 +40,9 @@ public: MissionController* missionController (void) const { return _missionController; } QmlObjectListModel* visualItems (void) const { return _visualItems; } int currentPolygonIndex (void) const { return _currentPolygonIndex; } - const QString currentFile (void) const { return _currentFile; } + QString currentFile (void) const { return _currentFile; } QStringList loadNameFilters (void) const; - QString fileExtenstion (void) const { return wimaFileExtension; } + QString fileExtension (void) const { return wimaFileExtension; } @@ -66,7 +69,8 @@ public: Q_INVOKABLE void saveToCurrent(); Q_INVOKABLE void saveToFile(const QString& filename); - Q_INVOKABLE void loadFromFile(); + Q_INVOKABLE bool loadFromCurrent(); + Q_INVOKABLE bool loadFromFile(const QString& filename); Q_INVOKABLE void resetAllInteractive(void); Q_INVOKABLE void setInteractive(void); diff --git a/src/Wima/WimaGOperationArea.cc b/src/Wima/WimaGOperationArea.cc index 4949ad348e4494fa257547414f6fb5ae6cf73079..0351e8fa74dd366b820b4fca24cafe525106887e 100644 --- a/src/Wima/WimaGOperationArea.cc +++ b/src/Wima/WimaGOperationArea.cc @@ -91,28 +91,28 @@ bool WimaGOperationArea::loadFromJson(const QJsonObject &json, QString& errorStr if ( json.contains(bottomLayerAltitudeName) && json[bottomLayerAltitudeName].isDouble() ) { _bottomLayerAltitude.setRawValue(json[bottomLayerAltitudeName].toDouble()); } else { - errorString.append("Could not load Bottom Layer Altitude!\n"); + errorString.append(tr("Could not load Bottom Layer Altitude!\n")); retVal = false; } if ( json.contains(numberOfLayersName) && json[numberOfLayersName].isDouble() ) { _numberOfLayers.setRawValue(json[numberOfLayersName].toInt()); } else { - errorString.append("Could not load Number of Layers!\n"); + errorString.append(tr("Could not load Number of Layers!\n")); retVal = false; } if ( json.contains(layerDistanceName) && json[layerDistanceName].isDouble() ) { _layerDistance.setRawValue(json[layerDistanceName].toDouble()); } else { - errorString.append("Could not load Layer Distance!\n"); + errorString.append(tr("Could not load Layer Distance!\n")); retVal = false; } if ( json.contains(borderPolygonOffsetName) && json[borderPolygonOffsetName].isDouble() ) { _borderPolygonOffset.setRawValue(json[borderPolygonOffsetName].toDouble()); } else { - errorString.append("Could not load Border Polygon Offset!\n"); + errorString.append(tr("Could not load Border Polygon Offset!\n")); retVal = false; } diff --git a/src/WimaView/WimaView.qml b/src/WimaView/WimaView.qml index ef261adf6ac0ce1414d184ca4dbfea9a3d6f6efc..4e1b646f61d6c9f478db2f2164831d36c2d27fd5 100644 --- a/src/WimaView/WimaView.qml +++ b/src/WimaView/WimaView.qml @@ -58,6 +58,7 @@ QGCView { property var _planMasterController: masterController property var _missionController: _planMasterController.missionController property var _visualItems: _missionController.visualItems + property var _wimaVisualItems: _wimaController.visualItems property bool _lightWidgetBorders: editorMap.isSatelliteMap property bool _addWaypointOnClick: false property bool _addROIOnClick: false @@ -185,6 +186,14 @@ QGCView { wimaFileDialog.fileExtension = wimaController.fileExtension wimaFileDialog.openForLoad() } + + function saveToSelectedFile() { + wimaFileDialog.title = qsTr("Save to Wima File") + wimaFileDialog.selectExisting = false + wimaFileDialog.nameFilters = wimaController.loadNameFilters + wimaFileDialog.fileExtension = wimaController.fileExtension + wimaFileDialog.openForSave() + } } PlanMasterController { @@ -1032,12 +1041,13 @@ QGCView { QGCLabel { width: sendSaveGrid.width wrapMode: Text.WordWrap - text: masterController.dirty ? + text: "unsaved changes not yet implemented" + /*masterController.dirty ? (_activeVehicle ? qsTr("You have unsaved changes. You should upload to your vehicle, or save to a file:") : qsTr("You have unsaved changes.") ) : - qsTr("Plan File:") + qsTr("Plan File:")*/ } GridLayout { @@ -1048,9 +1058,9 @@ QGCView { columnSpacing: ScreenTools.defaultFontPixelWidth QGCButton { - text: qsTr("New...") + text: qsTr("New...ToDo") Layout.fillWidth: true - enabled: _visualItems.count > 1 + enabled: _wimaVisualItems.count > 1 onClicked: { dropPanel.hide() _qgcView.showDialog(removeAllPromptDialog, qsTr("New Plan"), _qgcView.showDialogDefaultWidth, StandardButton.Yes | StandardButton.No) @@ -1058,44 +1068,36 @@ QGCView { } QGCButton { - text: qsTr("Open...") + text: qsTr("Open...Testing") Layout.fillWidth: true - enabled: !masterController.syncInProgress + enabled: true//!masterController.syncInProgress onClicked: { dropPanel.hide() - if (masterController.dirty) { - _qgcView.showDialog(syncLoadFromFileOverwrite, columnHolder._overwriteText, _qgcView.showDialogDefaultWidth, StandardButton.Yes | StandardButton.Cancel) - } else { - masterController.loadFromSelectedFile() - } + wimaController.loadFromSelectedFile() } } QGCButton { - text: qsTr("Save") + text: qsTr("Save Testing") Layout.fillWidth: true - enabled: !masterController.syncInProgress && masterController.currentPlanFile !== "" + enabled: wimaController.currentFile !== "" onClicked: { dropPanel.hide() - if(masterController.currentPlanFile !== "") { - masterController.saveToCurrent() - } else { - masterController.saveToSelectedFile() - } + wimaController.saveToCurrent() } } QGCButton { text: qsTr("Save As...") Layout.fillWidth: true - enabled: !masterController.syncInProgress && _visualItems.count > 1 + enabled: _wimaVisualItems.count > 1 onClicked: { dropPanel.hide() masterController.saveToSelectedFile() } } - QGCButton { + /*QGCButton { text: qsTr("Save Mission Waypoints As KML...") Layout.columnSpan: 2 enabled: !masterController.syncInProgress && _visualItems.count > 1 @@ -1108,7 +1110,7 @@ QGCView { dropPanel.hide() masterController.saveKmlToSelectedFile() } - } + }*/ Rectangle { width: parent.width * 0.8 @@ -1121,7 +1123,7 @@ QGCView { } QGCButton { - text: qsTr("Upload") + text: qsTr("Upload ToDo") Layout.fillWidth: true enabled: !masterController.offline && !masterController.syncInProgress && _visualItems.count > 1 visible: !QGroundControl.corePlugin.options.disableVehicleConnection @@ -1132,7 +1134,7 @@ QGCView { } QGCButton { - text: qsTr("Download") + text: qsTr("Download ToDo") Layout.fillWidth: true enabled: !masterController.offline && !masterController.syncInProgress visible: !QGroundControl.corePlugin.options.disableVehicleConnection @@ -1147,7 +1149,7 @@ QGCView { } QGCButton { - text: qsTr("Clear Vehicle Mission") + text: qsTr("Clear Vehicle Mission ToDo") Layout.fillWidth: true Layout.columnSpan: 2 enabled: !masterController.offline && !masterController.syncInProgress