Skip to content

Commit

Permalink
handling done button functionality
Browse files Browse the repository at this point in the history
  • Loading branch information
VitorVieiraZ committed Oct 2, 2024
1 parent aa1f9ea commit 8a22005
Show file tree
Hide file tree
Showing 5 changed files with 79 additions and 49 deletions.
73 changes: 48 additions & 25 deletions app/maptools/measurementmaptool.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -53,51 +53,60 @@ 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<QgsPointXY> pointList;
for ( const QgsPoint &point : mPoints )
pointList.append( QgsPointXY( point.x(), point.y() ) );

QgsGeometry polygonGeometry = QgsGeometry::fromPolygonXY( QList<QList<QgsPointXY>>() << 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<QList<QgsPointXY>>() << 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()
{
mPoints.clear();

setPerimeter( 0.0 );
setArea( 0.0 );
setLengthWithGuideline( 0.0 );
setCanCloseShape( false );
setCloseShapeDone( false );
setMeasurementFinalized( false );

rebuildGeometry();
}
Expand Down Expand Up @@ -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 )
Expand Down
18 changes: 12 additions & 6 deletions app/maptools/measurementmaptool.h
Original file line number Diff line number Diff line change
Expand Up @@ -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 );
Expand All @@ -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.
Expand All @@ -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 );
Expand All @@ -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();
Expand All @@ -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
28 changes: 15 additions & 13 deletions app/qml/gps/MMMeasureDrawer.qml
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand All @@ -54,19 +55,19 @@ 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
leftMargin: __style.pageMargins + __style.safeAreaLeft
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 {
Expand All @@ -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()
}
}
Expand Down
3 changes: 1 addition & 2 deletions app/qml/main.qml
Original file line number Diff line number Diff line change
Expand Up @@ -777,8 +777,7 @@ ApplicationWindow {
id: finishMeasurementDialog

onFinishMeasurementRequested: {
measurePanelLoader.active = false
map.finishMeasure()
map.mapToolComponent.finalizeMeasurement( false )
}
}

Expand Down
6 changes: 3 additions & 3 deletions app/qml/map/MMMeasurementTools.qml
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down

0 comments on commit 8a22005

Please sign in to comment.