diff --git a/app/maptools/measurementmaptool.cpp b/app/maptools/measurementmaptool.cpp index 89c9b1fba..56eea147a 100644 --- a/app/maptools/measurementmaptool.cpp +++ b/app/maptools/measurementmaptool.cpp @@ -53,41 +53,49 @@ void MeasurementMapTool::updateDistance() void MeasurementMapTool::checkCanCloseShape() { - if ( mRecordedGeometry.isEmpty() || mPoints.count() < 3 || !mapSettings() ) + bool canFinalize = !mRecordedGeometry.isEmpty() && mapSettings() && mPoints.count() >= 2; + setCanFinalizeMeasurement( canFinalize ); + + if ( !canFinalize || mPoints.count() < 3 ) { setCanCloseShape( false ); return; } - else - { - QgsPoint firstPoint = mPoints.first(); - QPointF firstPointScreen = mapSettings()->coordinateToScreen( firstPoint ); - double distanceToFirstPoint = InputUtils::pixelDistanceBetween( mCrosshairPoint, firstPointScreen ); - setCanCloseShape( distanceToFirstPoint <= CLOSE_THRESHOLD ); - } + QgsPoint firstPoint = mPoints.first(); + QPointF firstPointScreen = mapSettings()->coordinateToScreen( firstPoint ); + double distanceToFirstPoint = InputUtils::pixelDistanceBetween( mCrosshairPoint, firstPointScreen ); + setCanCloseShape( distanceToFirstPoint <= CLOSE_THRESHOLD ); } -void MeasurementMapTool::closeShape() +void MeasurementMapTool::finalizeMeasurement( bool closeShapeClicked ) { - if ( mPoints.count() < 3 ) + if ( mPoints.count() < 2 ) return; QList pointList; for ( const QgsPoint &point : mPoints ) pointList.append( QgsPointXY( point.x(), point.y() ) ); - QgsGeometry polygonGeometry = QgsGeometry::fromPolygonXY( QList>() << pointList ); - setRecordedGeometry( polygonGeometry ); + QgsGeometry geometry; + double perimeter = 0.0; - double area = mDistanceArea.measureArea( polygonGeometry ); - setArea( area ); + if ( closeShapeClicked && mCanCloseShape && mPoints.count() >= 3 ) + { + geometry = QgsGeometry::fromPolygonXY( QList>() << pointList ); + perimeter = mDistanceArea.measurePerimeter( geometry ); + setArea( mDistanceArea.measureArea( geometry ) ); + setCanCloseShape( false ); + } + else + { + geometry = QgsGeometry::fromPolylineXY( pointList ); + perimeter = mDistanceArea.measureLength( geometry ); + } - double perimeter = mDistanceArea.measurePerimeter( polygonGeometry ); + setRecordedGeometry( geometry ); setPerimeter( perimeter ); - - setCanCloseShape( false ); - setCloseShapeDone( true ); + setMeasurementFinalized( true ); } void MeasurementMapTool::resetMeasurement() @@ -95,9 +103,10 @@ void MeasurementMapTool::resetMeasurement() mPoints.clear(); setPerimeter( 0.0 ); + setArea( 0.0 ); setLengthWithGuideline( 0.0 ); setCanCloseShape( false ); - setCloseShapeDone( false ); + setMeasurementFinalized( false ); rebuildGeometry(); } @@ -220,18 +229,32 @@ void MeasurementMapTool::setCanCloseShape( bool newCanCloseShape ) emit canCloseShapeChanged( mCanCloseShape ); } -bool MeasurementMapTool::closeShapeDone() const +bool MeasurementMapTool::canFinalizeMeasurement() const +{ + return mCanFinalizeMeasurement; +} + +void MeasurementMapTool::setCanFinalizeMeasurement( bool canFinalize ) +{ + if ( mCanFinalizeMeasurement != canFinalize ) + { + mCanFinalizeMeasurement = canFinalize; + emit canFinalizeMeasurementChanged( canFinalize ); + } +} + +bool MeasurementMapTool::measurementFinalized() const { - return mCloseShapeDone; + return mMeasurementFinalized; } -void MeasurementMapTool::setCloseShapeDone( bool newCloseShapeDone ) +void MeasurementMapTool::setMeasurementFinalized( bool newMeasurementFinalized ) { - if ( mCloseShapeDone == newCloseShapeDone ) + if ( mMeasurementFinalized == newMeasurementFinalized ) return; - mCloseShapeDone = newCloseShapeDone; - emit closeShapeDoneChanged( mCloseShapeDone ); + mMeasurementFinalized = newMeasurementFinalized; + emit measurementFinalizedChanged( mMeasurementFinalized ); } void MeasurementMapTool::setRecordedGeometry( const QgsGeometry &newRecordedGeometry ) diff --git a/app/maptools/measurementmaptool.h b/app/maptools/measurementmaptool.h index 3f87a0b67..0e07949af 100644 --- a/app/maptools/measurementmaptool.h +++ b/app/maptools/measurementmaptool.h @@ -36,7 +36,8 @@ class MeasurementMapTool : public AbstractMapTool Q_PROPERTY( bool canUndo READ canUndo WRITE setCanUndo NOTIFY canUndoChanged ) Q_PROPERTY( bool canCloseShape READ canCloseShape WRITE setCanCloseShape NOTIFY canCloseShapeChanged ) - Q_PROPERTY( bool closeShapeDone READ closeShapeDone WRITE setCloseShapeDone NOTIFY closeShapeDoneChanged ) + Q_PROPERTY( bool canFinalizeMeasurement READ canFinalizeMeasurement WRITE setCanFinalizeMeasurement NOTIFY canFinalizeMeasurementChanged ) + Q_PROPERTY( bool measurementFinalized READ measurementFinalized WRITE setMeasurementFinalized NOTIFY measurementFinalizedChanged ) public: explicit MeasurementMapTool( QObject *parent = nullptr ); @@ -58,7 +59,7 @@ class MeasurementMapTool : public AbstractMapTool * If there are at least 3 points, forms a polygon from recorded points so far. * Updates recorded geometry and calculates area and perimeter of formed polygon. */ - Q_INVOKABLE void closeShape(); + Q_INVOKABLE void finalizeMeasurement( bool closeShapeClicked ); /** * Repeats measurement process. @@ -85,8 +86,11 @@ class MeasurementMapTool : public AbstractMapTool bool canCloseShape() const; void setCanCloseShape( bool newCanCloseShape ); - bool closeShapeDone() const; - void setCloseShapeDone( bool newCloseShapeDone ); + bool canFinalizeMeasurement() const; + void setCanFinalizeMeasurement( bool canFinalize ); + + bool measurementFinalized() const; + void setMeasurementFinalized( bool newMeasurementFinalized ); const QgsGeometry &recordedGeometry() const; void setRecordedGeometry( const QgsGeometry &newRecordedGeometry ); @@ -102,10 +106,11 @@ class MeasurementMapTool : public AbstractMapTool void areaChanged( const double &area ); void canUndoChanged( bool canUndo ); void canCloseShapeChanged( bool canUndo ); - void closeShapeDoneChanged( bool canUndo ); + void measurementFinalizedChanged( bool measurementFinalized ); void recordedGeometryChanged( const QgsGeometry &recordedGeometry ); void existingVerticesChanged( const QgsGeometry &vertices ); void crosshairPointChanged( const QPointF &crosshairPoint ); + void canFinalizeMeasurementChanged( bool canFinalize ); protected: void rebuildGeometry(); @@ -125,7 +130,8 @@ class MeasurementMapTool : public AbstractMapTool double mArea = 0; bool mCanUndo = false; bool mCanCloseShape = false; - bool mCloseShapeDone = false; + bool mCanFinalizeMeasurement = false; + bool mMeasurementFinalized = false; }; #endif // MEASUREMENTMAPTOOL_H diff --git a/app/qml/gps/MMMeasureDrawer.qml b/app/qml/gps/MMMeasureDrawer.qml index 014a4b999..a655b7d79 100644 --- a/app/qml/gps/MMMeasureDrawer.qml +++ b/app/qml/gps/MMMeasureDrawer.qml @@ -27,11 +27,12 @@ MMDrawer { readonly property alias panelHeight: root.height property bool canCloseShape: mapCanvas.mapToolComponent?.mapTool?.canCloseShape ?? false - property bool closeShapeDone: mapCanvas.mapToolComponent?.mapTool?.closeShapeDone ?? false property bool canUndo: mapCanvas.mapToolComponent?.mapTool?.canUndo ?? false + property bool canFinalizeMeasurement: mapCanvas.mapToolComponent?.mapTool?.canFinalizeMeasurement ?? false + property bool measurementFinalized: mapCanvas.mapToolComponent?.mapTool?.measurementFinalized ?? false - property string perimeter: __inputUtils.formatDistanceInProjectUnit( mapCanvas.mapToolComponent?.mapTool?.perimeter ?? 0, 1, __activeProject.qgsProject ) - property string area: __inputUtils.formatAreaInProjectUnit( mapCanvas.mapToolComponent?.mapTool?.area ?? 0, 1, __activeProject.qgsProject ) + property string perimeter: mapCanvas.mapToolComponent?.mapTool?.perimeter ?? 0 + property string area: mapCanvas.mapToolComponent?.mapTool?.area ?? 0 signal measureFinished() signal measureDone() @@ -54,11 +55,11 @@ MMDrawer { drawerHeader.topLeftItemContent: MMButton { type: MMButton.Types.Primary - text: closeShapeDone ? qsTr( "Repeat" ) : qsTr( "Undo" ) - iconSourceLeft: closeShapeDone ? __style.syncIcon : __style.undoIcon + text: measurementFinalized ? qsTr( "Repeat" ) : qsTr( "Undo" ) + iconSourceLeft: measurementFinalized ? __style.syncIcon : __style.undoIcon bgndColor: __style.lightGreenColor size: MMButton.Sizes.Small - enabled: closeShapeDone || canUndo + enabled: measurementFinalized || canUndo anchors { left: parent.left @@ -66,7 +67,7 @@ MMDrawer { verticalCenter: parent.verticalCenter } - onClicked: closeShapeDone ? root.mapCanvas.mapToolComponent.repeatMeasure() : root.mapCanvas.mapToolComponent.mapTool.removePoint() + onClicked: measurementFinalized ? root.mapCanvas.mapToolComponent.repeatMeasure() : root.mapCanvas.mapToolComponent.mapTool.removePoint() } drawerContent: Column { @@ -81,35 +82,36 @@ MMDrawer { MMGpsComponents.MMGpsDataText{ width: ( parent.width + parent.spacing ) / 2 - title: closeShapeDone ? qsTr( "Perimeter" ) : qsTr( "Length" ) - value: root.perimeter + title: measurementFinalized ? qsTr( "Perimeter" ) : qsTr( "Length" ) + value: __inputUtils.formatDistanceInProjectUnit( root.perimeter, 1, __activeProject.qgsProject ) } MMGpsComponents.MMGpsDataText{ width: ( parent.width + parent.spacing ) / 2 title: qsTr( "Area" ) - value: root.area + value: __inputUtils.formatAreaInProjectUnit( root.area, 1, __activeProject.qgsProject ) alignmentRight: true - visible: closeShapeDone + visible: measurementFinalized && root.area > 0 } } Row { width: parent.width spacing: __style.margin12 - visible: !root.closeShapeDone + visible: !root.measurementFinalized MMButton { text: root.canCloseShape ? qsTr( "Close shape" ) : qsTr( "Add point" ) iconSourceLeft: canCloseShape ? __style.closeShapeIcon : __style.plusIcon - onClicked: canCloseShape ? root.mapCanvas.mapToolComponent.closeShape() : root.mapCanvas.mapToolComponent.mapTool.addPoint() + onClicked: canCloseShape ? root.mapCanvas.mapToolComponent.finalizeMeasurement( true ) : root.mapCanvas.mapToolComponent.mapTool.addPoint() } MMButton { type: MMButton.Types.Secondary text: qsTr( "Done" ) iconSourceLeft: __style.doneCircleIcon + enabled: root.canFinalizeMeasurement onClicked: root.measureDone() } } diff --git a/app/qml/main.qml b/app/qml/main.qml index 569c11de2..40ed9d314 100644 --- a/app/qml/main.qml +++ b/app/qml/main.qml @@ -777,8 +777,7 @@ ApplicationWindow { id: finishMeasurementDialog onFinishMeasurementRequested: { - measurePanelLoader.active = false - map.finishMeasure() + map.mapToolComponent.finalizeMeasurement( false ) } } diff --git a/app/qml/map/MMMeasurementTools.qml b/app/qml/map/MMMeasurementTools.qml index e3591d487..2ec5dd274 100644 --- a/app/qml/map/MMMeasurementTools.qml +++ b/app/qml/map/MMMeasurementTools.qml @@ -94,14 +94,14 @@ Item { text: __inputUtils.formatDistanceInProjectUnit( mapTool.lengthWithGuideline, __activeProject.qgsProject ) canCloseShape: mapTool.canCloseShape - onCloseShapeClicked: closeShape() + onCloseShapeClicked: finalizeMeasurement( true ) } - function closeShape() + function finalizeMeasurement( closeShapeClicked ) { guidelineController.allowed = false crosshair.visible = false - mapTool.closeShape() + mapTool.finalizeMeasurement( closeShapeClicked ) } function repeatMeasure()