From 2c5f081c42094c596fedf3aa83b9ad0be1d2cbb4 Mon Sep 17 00:00:00 2001 From: Don Gagne Date: Tue, 12 Jul 2016 12:33:11 -0700 Subject: [PATCH 1/3] FlightMap supports generic polygon drawing tool --- src/FlightMap/FlightMap.qml | 148 +++++++++++++++++++++++++++++ src/QmlControls/QGroundControlQmlGlobal.cc | 8 ++ src/QmlControls/QGroundControlQmlGlobal.h | 2 + 3 files changed, 158 insertions(+) diff --git a/src/FlightMap/FlightMap.qml b/src/FlightMap/FlightMap.qml index 3cccb57..bcce2ad 100644 --- a/src/FlightMap/FlightMap.qml +++ b/src/FlightMap/FlightMap.qml @@ -128,4 +128,152 @@ Map { label: "Q" } } + + //---- Polygon drawing code + + // + // Usage: + // + // Connections { + // target: map.polygonDraw + // + // onPolygonStarted: { + // // Polygon creation has started + // } + // + // onPolygonFinished: { + // // Polygon capture complete, coordinates signal variable contains the polygon points + // } + // } + // + // map.polygonDraqw.startPolgyon() - begin capturing a new polygon + // map.polygonDraqw.endPolygon() - end capture (right-click will also end capture) + + // Not sure why this is needed, but trying to reference polygonDrawer directly from other code doesn't work + property alias polygonDraw: polygonDrawer + + QGCLabel { + id: polygonHelp + anchors.topMargin: parent.height - ScreenTools.availableHeight + anchors.top: parent.top + anchors.left: parent.left + anchors.right: parent.right + horizontalAlignment: Text.AlignHCenter + text: qsTr("Click to add point %1").arg(ScreenTools.isMobile || !polygonDrawer.polygonReady ? "" : qsTr("- Right Click to end polygon")) + visible: polygonDrawer.drawingPolygon + } + + MouseArea { + id: polygonDrawer + anchors.fill: parent + acceptedButtons: Qt.LeftButton | Qt.RightButton + visible: drawingPolygon + z: 1000 // Hack to fix MouseArea layering for now + + property alias drawingPolygon: polygonDrawer.hoverEnabled + property bool polygonReady: polygonDrawerPolygon.path.length > 3 ///< true: enough points have been captured to create a closed polygon + + /// New polygon capture has started + signal polygonStarted + + /// Polygon capture is complete + /// @param coordinates Map coordinates for the polygon points + signal polygonFinished(var coordinates) + + /// Begin capturing a new polygon + /// polygonStarted will be signalled + function startPolygon() { + polygonDrawer.drawingPolygon = true + polygonDrawer._clearPolygon() + polygonDrawer.polygonStarted() + } + + /// Finish capturing the polygon + /// polygonFinished will be signalled + /// @return true: polygon completed, false: not enough points to complete polygon + function finishPolygon() { + if (!polygonDrawer.polygonReady) { + return false + } + + var polygonPath = polygonDrawerPolygon.path + polygonPath.pop() // get rid of drag coordinate + polygonDrawer._clearPolygon() + polygonDrawer.drawingPolygon = false + polygonDrawer.polygonFinished(polygonPath) + return true + } + + function _clearPolygon() { + // Simpler methods to clear the path simply don't work due to bugs. This craziness does. + var bogusCoord = _map.toCoordinate(Qt.point(height/2, width/2)) + polygonDrawerPolygon.path = [ bogusCoord, bogusCoord ] + polygonDrawerNextPoint.path = [ bogusCoord, bogusCoord ] + polygonDrawerPolygon.path = [ ] + polygonDrawerNextPoint.path = [ ] + } + + onClicked: { + if (mouse.button == Qt.LeftButton) { + if (polygonDrawerPolygon.path.length > 2) { + // Make sure the new line doesn't intersect the existing polygon + var lastSegment = polygonDrawerPolygon.path.length - 2 + var newLineA = _map.fromCoordinate(polygonDrawerPolygon.path[lastSegment], false /* clipToViewPort */) + var newLineB = _map.fromCoordinate(polygonDrawerPolygon.path[lastSegment+1], false /* clipToViewPort */) + for (var i=0; isetFilterRulesFromSettings(QString()); } + Q_INVOKABLE bool linesIntersect(QPointF xLine1, QPointF yLine1, QPointF xLine2, QPointF yLine2); + // Property accesors FlightMapSettings* flightMapSettings () { return _flightMapSettings; } From b25e32e3b95d42c91bbf61bfcc033b1116666cad Mon Sep 17 00:00:00 2001 From: Don Gagne Date: Tue, 12 Jul 2016 12:33:19 -0700 Subject: [PATCH 2/3] Use new polygon drawing tool --- src/MissionEditor/SurveyItemEditor.qml | 38 +++++++++++++++++++--------------- 1 file changed, 21 insertions(+), 17 deletions(-) diff --git a/src/MissionEditor/SurveyItemEditor.qml b/src/MissionEditor/SurveyItemEditor.qml index 61cd8d9..f3ee5c8 100644 --- a/src/MissionEditor/SurveyItemEditor.qml +++ b/src/MissionEditor/SurveyItemEditor.qml @@ -19,8 +19,7 @@ Rectangle { //property real availableWidth ///< Width for control //property var missionItem ///< Mission Item for editor - property bool _addPointsMode: false - property real _margin: ScreenTools.defaultFontPixelWidth / 2 + property real _margin: ScreenTools.defaultFontPixelWidth / 2 QGCPalette { id: qgcPal; colorGroupEnabled: true } @@ -32,16 +31,6 @@ Rectangle { anchors.right: parent.right spacing: _margin - Connections { - target: editorMap - - onMapClicked: { - if (_addPointsMode) { - missionItem.addPolygonCoordinate(coordinate) - } - } - } - QGCLabel { text: qsTr("Fly a grid pattern over a defined area.") wrapMode: Text.WordWrap @@ -108,14 +97,29 @@ Rectangle { } } + Connections { + target: editorMap.polygonDraw + + onPolygonStarted: { + missionItem.clearPolygon() + } + + onPolygonFinished: { + for (var i=0; i Date: Tue, 12 Jul 2016 18:28:14 -0700 Subject: [PATCH 3/3] Add missing headers --- src/QmlControls/QGroundControlQmlGlobal.cc | 2 ++ 1 file changed, 2 insertions(+) diff --git a/src/QmlControls/QGroundControlQmlGlobal.cc b/src/QmlControls/QGroundControlQmlGlobal.cc index d95697b..24e3753 100644 --- a/src/QmlControls/QGroundControlQmlGlobal.cc +++ b/src/QmlControls/QGroundControlQmlGlobal.cc @@ -14,6 +14,8 @@ #include "QGroundControlQmlGlobal.h" #include +#include +#include static const char* kQmlGlobalKeyName = "QGCQml";