From a0a780fd8ec520be0dad6153326b7a1ab76111c6 Mon Sep 17 00:00:00 2001 From: Jan Caha Date: Fri, 20 Sep 2024 06:39:49 +0200 Subject: [PATCH 1/7] add message to QGIS status bar on selection of vertices --- src/app/mesh/qgsmaptooleditmeshframe.cpp | 41 ++++++++++++++++++++++++ src/app/mesh/qgsmaptooleditmeshframe.h | 1 + 2 files changed, 42 insertions(+) diff --git a/src/app/mesh/qgsmaptooleditmeshframe.cpp b/src/app/mesh/qgsmaptooleditmeshframe.cpp index b6677813ffb1..59efef781049 100644 --- a/src/app/mesh/qgsmaptooleditmeshframe.cpp +++ b/src/app/mesh/qgsmaptooleditmeshframe.cpp @@ -16,10 +16,12 @@ #include "qgsmaptooleditmeshframe.h" #include +#include #include "qgis.h" #include "qgisapp.h" #include "qgsapplication.h" +#include "qgsstatusbar.h" #include "qgsadvanceddigitizingdockwidget.h" #include "qgsdoublespinbox.h" @@ -331,6 +333,8 @@ QgsMapToolEditMeshFrame::QgsMapToolEditMeshFrame( QgsMapCanvas *canvas ) createZValueWidget(); } ); + connect( this, &QgsMapToolEditMeshFrame::selectionChange, this, &QgsMapToolEditMeshFrame::updateStatusBarMessage ); + setAutoSnapEnabled( true ); } @@ -2841,3 +2845,40 @@ void QgsMapToolEditMeshFrame::showSelectByExpressionDialog() connect( dialog, &QgsMeshSelectByExpressionDialog::select, this, &QgsMapToolEditMeshFrame::selectByExpression ); connect( dialog, &QgsMeshSelectByExpressionDialog::zoomToSelected, this, &QgsMapToolEditMeshFrame::onZoomToSelected ); } + +void QgsMapToolEditMeshFrame::updateStatusBarMessage() +{ + if ( ! mSelectedVertices.isEmpty() ) + { + QString message; + if ( mSelectedVertices.count() == 1 ) + { + const QgsMesh &mesh = *mCurrentLayer->nativeMesh(); + int vertexId = mSelectedVertices.keys()[0]; + QgsMeshVertex vertex = mesh.vertex( vertexId ); + + message = tr( "Selected mesh vertex ID: %1 at x: %2 y: %3." ).arg( vertexId ).arg( QLocale().toString( vertex.x(), 'f' ) ).arg( QLocale().toString( vertex.y(), 'f' ) ); + } + else if ( mSelectedVertices.count() == 2 ) + { + const QgsMesh &mesh = *mCurrentLayer->nativeMesh(); + int vertexId1 = mSelectedVertices.keys()[0]; + int vertexId2 = mSelectedVertices.keys()[1]; + QgsMeshVertex vertex1 = mesh.vertex( vertexId1 ); + QgsMeshVertex vertex2 = mesh.vertex( vertexId2 ); + double distance = vertex1.distance( vertex2 ); + + message = tr( "Selected mesh vertices IDs: %1 and %2 with distance %3." ).arg( vertexId1 ).arg( vertexId2 ).arg( QLocale().toString( distance, 'f' ) ); + } + else if ( mSelectedVertices.count() > 2 ) + { + message = tr( "Selected %1 mesh vertices." ).arg( mSelectedVertices.count() ); + } + + QgisApp::instance()->statusBarIface()->showMessage( message ); + } + else + { + QgisApp::instance()->statusBarIface()->clearMessage(); + } +} diff --git a/src/app/mesh/qgsmaptooleditmeshframe.h b/src/app/mesh/qgsmaptooleditmeshframe.h index 850b2a876bb5..e6ca4ca9a12b 100644 --- a/src/app/mesh/qgsmaptooleditmeshframe.h +++ b/src/app/mesh/qgsmaptooleditmeshframe.h @@ -166,6 +166,7 @@ class APP_EXPORT QgsMapToolEditMeshFrame : public QgsMapToolAdvancedDigitizing void onZoomToSelected(); void reindexMesh(); void onUndoRedo(); + void updateStatusBarMessage(); private: From f71e95c92002afdfef7cf402a90f82dad18222fa Mon Sep 17 00:00:00 2001 From: Jan Caha Date: Fri, 20 Sep 2024 11:18:40 +0200 Subject: [PATCH 2/7] fix review suggestions --- src/app/mesh/qgsmaptooleditmeshframe.cpp | 37 ++++++++++++++++++------ 1 file changed, 28 insertions(+), 9 deletions(-) diff --git a/src/app/mesh/qgsmaptooleditmeshframe.cpp b/src/app/mesh/qgsmaptooleditmeshframe.cpp index 59efef781049..8b7ab1bf3d9b 100644 --- a/src/app/mesh/qgsmaptooleditmeshframe.cpp +++ b/src/app/mesh/qgsmaptooleditmeshframe.cpp @@ -47,6 +47,7 @@ #include "qgsmeshselectbyexpressiondialog.h" #include "qgsmaptoolidentify.h" #include "qgsidentifymenu.h" +#include "qgsdistancearea.h" // @@ -2854,21 +2855,39 @@ void QgsMapToolEditMeshFrame::updateStatusBarMessage() if ( mSelectedVertices.count() == 1 ) { const QgsMesh &mesh = *mCurrentLayer->nativeMesh(); - int vertexId = mSelectedVertices.keys()[0]; - QgsMeshVertex vertex = mesh.vertex( vertexId ); + const int vertexId = mSelectedVertices.firstKey(); + const QgsMeshVertex vertex = mesh.vertex( vertexId ); - message = tr( "Selected mesh vertex ID: %1 at x: %2 y: %3." ).arg( vertexId ).arg( QLocale().toString( vertex.x(), 'f' ) ).arg( QLocale().toString( vertex.y(), 'f' ) ); + message = tr( "Selected mesh vertex ID: %1 at x: %2 y: %3 z: %4." ).arg( vertexId ).arg( QLocale().toString( vertex.x(), 'f' ) ).arg( QLocale().toString( vertex.y(), 'f' ) ).arg( QLocale().toString( vertex.z(), 'f' ) ); } else if ( mSelectedVertices.count() == 2 ) { const QgsMesh &mesh = *mCurrentLayer->nativeMesh(); - int vertexId1 = mSelectedVertices.keys()[0]; - int vertexId2 = mSelectedVertices.keys()[1]; - QgsMeshVertex vertex1 = mesh.vertex( vertexId1 ); - QgsMeshVertex vertex2 = mesh.vertex( vertexId2 ); - double distance = vertex1.distance( vertex2 ); + const int vertexId1 = mSelectedVertices.firstKey(); + const int vertexId2 = mSelectedVertices.lastKey(); + const QgsMeshVertex vertex1 = mesh.vertex( vertexId1 ); + const QgsMeshVertex vertex2 = mesh.vertex( vertexId2 ); + + QString formattedDistance; + double distance; + // if crs is valid calculate using QgsDistanceArea otherwise calculate just as distance + if ( mCurrentLayer->crs().isValid() ) + { + QgsDistanceArea distArea = QgsDistanceArea(); + distArea.setSourceCrs( mCurrentLayer->crs(), QgsProject::instance()->transformContext() ); + distArea.setEllipsoid( QgsProject::instance()->ellipsoid() ); + distance = distArea.measureLine( QgsPointXY( vertex1 ), QgsPointXY( vertex2 ) ); + formattedDistance = distArea.formatDistance( distance, 6, mCurrentLayer->crs().mapUnits() ); + } + else + { + distance = vertex1.distance( vertex2 ); + formattedDistance = QLocale().toString( distance, 'f' ); + } + + const double zDiff = vertex2.z() - vertex1.z(); - message = tr( "Selected mesh vertices IDs: %1 and %2 with distance %3." ).arg( vertexId1 ).arg( vertexId2 ).arg( QLocale().toString( distance, 'f' ) ); + message = tr( "Selected mesh vertices IDs: %1 and %2 with distance %3 and dZ %4." ).arg( vertexId1 ).arg( vertexId2 ).arg( formattedDistance ).arg( QLocale().toString( zDiff, 'f' ) ); } else if ( mSelectedVertices.count() > 2 ) { From b0769f5f0e204420b8ae7d24ec19f637c257b563 Mon Sep 17 00:00:00 2001 From: Jan Caha Date: Fri, 20 Sep 2024 13:50:26 +0200 Subject: [PATCH 3/7] Update src/app/mesh/qgsmaptooleditmeshframe.cpp use project distance units Co-authored-by: Stefanos Natsis --- src/app/mesh/qgsmaptooleditmeshframe.cpp | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/src/app/mesh/qgsmaptooleditmeshframe.cpp b/src/app/mesh/qgsmaptooleditmeshframe.cpp index 8b7ab1bf3d9b..b5fae9263438 100644 --- a/src/app/mesh/qgsmaptooleditmeshframe.cpp +++ b/src/app/mesh/qgsmaptooleditmeshframe.cpp @@ -2877,7 +2877,8 @@ void QgsMapToolEditMeshFrame::updateStatusBarMessage() distArea.setSourceCrs( mCurrentLayer->crs(), QgsProject::instance()->transformContext() ); distArea.setEllipsoid( QgsProject::instance()->ellipsoid() ); distance = distArea.measureLine( QgsPointXY( vertex1 ), QgsPointXY( vertex2 ) ); - formattedDistance = distArea.formatDistance( distance, 6, mCurrentLayer->crs().mapUnits() ); + distance = distArea.convertLengthMeasurement( distance, QgsProject::instance()->distanceUnits() ); + formattedDistance = distArea.formatDistance( distance, 6, QgsProject::instance()->distanceUnits() ); } else { From f7afcd7c13ef1785d5045ba337237d11a81f83ad Mon Sep 17 00:00:00 2001 From: Jan Caha Date: Fri, 20 Sep 2024 13:50:38 +0200 Subject: [PATCH 4/7] Update src/app/mesh/qgsmaptooleditmeshframe.h make const Co-authored-by: Stefanos Natsis --- src/app/mesh/qgsmaptooleditmeshframe.h | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/app/mesh/qgsmaptooleditmeshframe.h b/src/app/mesh/qgsmaptooleditmeshframe.h index e6ca4ca9a12b..7e6a66a77b25 100644 --- a/src/app/mesh/qgsmaptooleditmeshframe.h +++ b/src/app/mesh/qgsmaptooleditmeshframe.h @@ -166,7 +166,7 @@ class APP_EXPORT QgsMapToolEditMeshFrame : public QgsMapToolAdvancedDigitizing void onZoomToSelected(); void reindexMesh(); void onUndoRedo(); - void updateStatusBarMessage(); + void updateStatusBarMessage() const; private: From 7b40a0d10b82dc68388b58eca4d8948e2d00fcd1 Mon Sep 17 00:00:00 2001 From: Jan Caha Date: Sun, 22 Sep 2024 18:34:50 +0200 Subject: [PATCH 5/7] Update src/app/mesh/qgsmaptooleditmeshframe.cpp cons function Co-authored-by: Stefanos Natsis --- src/app/mesh/qgsmaptooleditmeshframe.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/app/mesh/qgsmaptooleditmeshframe.cpp b/src/app/mesh/qgsmaptooleditmeshframe.cpp index b5fae9263438..774e1778e853 100644 --- a/src/app/mesh/qgsmaptooleditmeshframe.cpp +++ b/src/app/mesh/qgsmaptooleditmeshframe.cpp @@ -2847,7 +2847,7 @@ void QgsMapToolEditMeshFrame::showSelectByExpressionDialog() connect( dialog, &QgsMeshSelectByExpressionDialog::zoomToSelected, this, &QgsMapToolEditMeshFrame::onZoomToSelected ); } -void QgsMapToolEditMeshFrame::updateStatusBarMessage() +void QgsMapToolEditMeshFrame::updateStatusBarMessage() const { if ( ! mSelectedVertices.isEmpty() ) { From a9b48f9e1e81f6aa5eb9d0e2926d0c43123e596d Mon Sep 17 00:00:00 2001 From: Jan Caha Date: Mon, 23 Sep 2024 14:57:35 +0200 Subject: [PATCH 6/7] add try to catch exception with measureLine --- src/app/mesh/qgsmaptooleditmeshframe.cpp | 16 ++++++++++++---- 1 file changed, 12 insertions(+), 4 deletions(-) diff --git a/src/app/mesh/qgsmaptooleditmeshframe.cpp b/src/app/mesh/qgsmaptooleditmeshframe.cpp index 774e1778e853..2815f199e21f 100644 --- a/src/app/mesh/qgsmaptooleditmeshframe.cpp +++ b/src/app/mesh/qgsmaptooleditmeshframe.cpp @@ -2870,17 +2870,25 @@ void QgsMapToolEditMeshFrame::updateStatusBarMessage() const QString formattedDistance; double distance; + bool distanceIsValid = false; + // if crs is valid calculate using QgsDistanceArea otherwise calculate just as distance if ( mCurrentLayer->crs().isValid() ) { QgsDistanceArea distArea = QgsDistanceArea(); distArea.setSourceCrs( mCurrentLayer->crs(), QgsProject::instance()->transformContext() ); distArea.setEllipsoid( QgsProject::instance()->ellipsoid() ); - distance = distArea.measureLine( QgsPointXY( vertex1 ), QgsPointXY( vertex2 ) ); - distance = distArea.convertLengthMeasurement( distance, QgsProject::instance()->distanceUnits() ); - formattedDistance = distArea.formatDistance( distance, 6, QgsProject::instance()->distanceUnits() ); + try + { + distance = distArea.measureLine( QgsPointXY( vertex1 ), QgsPointXY( vertex2 ) ); + distance = distArea.convertLengthMeasurement( distance, QgsProject::instance()->distanceUnits() ); + formattedDistance = distArea.formatDistance( distance, 6, QgsProject::instance()->distanceUnits() ); + distanceIsValid = true; + } + catch ( QgsCsException & ) {} } - else + + if ( ! distanceIsValid ) { distance = vertex1.distance( vertex2 ); formattedDistance = QLocale().toString( distance, 'f' ); From 7e0abb459490e379167139e70c4bc2822baad5dd Mon Sep 17 00:00:00 2001 From: Jan Caha Date: Thu, 26 Sep 2024 09:50:32 +0200 Subject: [PATCH 7/7] remove unnecessary bool --- src/app/mesh/qgsmaptooleditmeshframe.cpp | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) diff --git a/src/app/mesh/qgsmaptooleditmeshframe.cpp b/src/app/mesh/qgsmaptooleditmeshframe.cpp index 2815f199e21f..3d6a27c145c9 100644 --- a/src/app/mesh/qgsmaptooleditmeshframe.cpp +++ b/src/app/mesh/qgsmaptooleditmeshframe.cpp @@ -2870,7 +2870,6 @@ void QgsMapToolEditMeshFrame::updateStatusBarMessage() const QString formattedDistance; double distance; - bool distanceIsValid = false; // if crs is valid calculate using QgsDistanceArea otherwise calculate just as distance if ( mCurrentLayer->crs().isValid() ) @@ -2883,12 +2882,11 @@ void QgsMapToolEditMeshFrame::updateStatusBarMessage() const distance = distArea.measureLine( QgsPointXY( vertex1 ), QgsPointXY( vertex2 ) ); distance = distArea.convertLengthMeasurement( distance, QgsProject::instance()->distanceUnits() ); formattedDistance = distArea.formatDistance( distance, 6, QgsProject::instance()->distanceUnits() ); - distanceIsValid = true; } catch ( QgsCsException & ) {} } - if ( ! distanceIsValid ) + if ( formattedDistance.isEmpty() ) { distance = vertex1.distance( vertex2 ); formattedDistance = QLocale().toString( distance, 'f' );