From ccf15a4ce82c440d846fb5051beb83c0da59dd2f Mon Sep 17 00:00:00 2001 From: Alexander Bruy Date: Mon, 10 Feb 2025 13:31:45 +0000 Subject: [PATCH] do not enable default Duplicate Feature and Digitize action for geometryless layers (refs #57916) --- python/PyQt6/core/auto_additions/qgis.py | 5 +++++ python/PyQt6/core/auto_generated/qgis.sip.in | 1 + python/core/auto_additions/qgis.py | 5 +++++ python/core/auto_generated/qgis.sip.in | 1 + src/app/qgisapp.cpp | 2 +- src/core/qgis.h | 1 + src/gui/actions/qgsmaplayeraction.cpp | 11 +++++++++++ 7 files changed, 25 insertions(+), 1 deletion(-) diff --git a/python/PyQt6/core/auto_additions/qgis.py b/python/PyQt6/core/auto_additions/qgis.py index 9c6af4a38d06..8bff45774c65 100644 --- a/python/PyQt6/core/auto_additions/qgis.py +++ b/python/PyQt6/core/auto_additions/qgis.py @@ -7663,6 +7663,7 @@ MapLayerActionTargets = Qgis # dirty hack since SIP seems to introduce the flags in module # monkey patching scoped based enum Qgis.MapLayerActionFlag.EnabledOnlyWhenEditable.__doc__ = "Action should be shown only for editable layers" +Qgis.MapLayerActionFlag.EnableOnlyWhenHasGeometry.__doc__ = "Action should be shown only for layers with geometry, \n.. versionadded:: 3.42" Qgis.MapLayerActionFlag.__doc__ = """Map layer action flags. Prior to QGIS 3.30 this was available as :py:class:`QgsMapLayerAction`.Flag @@ -7670,6 +7671,10 @@ .. versionadded:: 3.30 * ``EnabledOnlyWhenEditable``: Action should be shown only for editable layers +* ``EnableOnlyWhenHasGeometry``: Action should be shown only for layers with geometry, + + .. versionadded:: 3.42 + """ # -- diff --git a/python/PyQt6/core/auto_generated/qgis.sip.in b/python/PyQt6/core/auto_generated/qgis.sip.in index 5141594f875b..8fbfe474c91e 100644 --- a/python/PyQt6/core/auto_generated/qgis.sip.in +++ b/python/PyQt6/core/auto_generated/qgis.sip.in @@ -2427,6 +2427,7 @@ The development version enum class MapLayerActionFlag /BaseType=IntFlag/ { EnabledOnlyWhenEditable, + EnableOnlyWhenHasGeometry, }; typedef QFlags MapLayerActionFlags; diff --git a/python/core/auto_additions/qgis.py b/python/core/auto_additions/qgis.py index e2e8582f3bea..3358e5ed3f58 100644 --- a/python/core/auto_additions/qgis.py +++ b/python/core/auto_additions/qgis.py @@ -7588,6 +7588,7 @@ MapLayerActionTargets = Qgis # dirty hack since SIP seems to introduce the flags in module # monkey patching scoped based enum Qgis.MapLayerActionFlag.EnabledOnlyWhenEditable.__doc__ = "Action should be shown only for editable layers" +Qgis.MapLayerActionFlag.EnableOnlyWhenHasGeometry.__doc__ = "Action should be shown only for layers with geometry, \n.. versionadded:: 3.42" Qgis.MapLayerActionFlag.__doc__ = """Map layer action flags. Prior to QGIS 3.30 this was available as :py:class:`QgsMapLayerAction`.Flag @@ -7595,6 +7596,10 @@ .. versionadded:: 3.30 * ``EnabledOnlyWhenEditable``: Action should be shown only for editable layers +* ``EnableOnlyWhenHasGeometry``: Action should be shown only for layers with geometry, + + .. versionadded:: 3.42 + """ # -- diff --git a/python/core/auto_generated/qgis.sip.in b/python/core/auto_generated/qgis.sip.in index e11c4a2b9833..361d8ed208cb 100644 --- a/python/core/auto_generated/qgis.sip.in +++ b/python/core/auto_generated/qgis.sip.in @@ -2427,6 +2427,7 @@ The development version enum class MapLayerActionFlag { EnabledOnlyWhenEditable, + EnableOnlyWhenHasGeometry, }; typedef QFlags MapLayerActionFlags; diff --git a/src/app/qgisapp.cpp b/src/app/qgisapp.cpp index 5e908aefccd9..b8a9b7605bfa 100644 --- a/src/app/qgisapp.cpp +++ b/src/app/qgisapp.cpp @@ -9120,7 +9120,7 @@ void QgisApp::setupDuplicateFeaturesAction() duplicateFeatures( layer, feat ); } ); - mDuplicateFeatureDigitizeAction.reset( new QgsMapLayerAction( tr( "Duplicate Feature and Digitize" ), nullptr, Qgis::MapLayerActionTarget::SingleFeature, QgsApplication::getThemeIcon( QStringLiteral( "/mActionDuplicateFeatureDigitized.svg" ) ), Qgis::MapLayerActionFlag::EnabledOnlyWhenEditable ) ); + mDuplicateFeatureDigitizeAction.reset( new QgsMapLayerAction( tr( "Duplicate Feature and Digitize" ), nullptr, Qgis::MapLayerActionTarget::SingleFeature, QgsApplication::getThemeIcon( QStringLiteral( "/mActionDuplicateFeatureDigitized.svg" ) ), Qgis::MapLayerActionFlag::EnabledOnlyWhenEditable | Qgis::MapLayerActionFlag::EnableOnlyWhenHasGeometry ) ); QgsGui::mapLayerActionRegistry()->addMapLayerAction( mDuplicateFeatureDigitizeAction.get() ); connect( mDuplicateFeatureDigitizeAction.get(), &QgsMapLayerAction::triggeredForFeatureV2, this, [this]( QgsMapLayer *layer, const QgsFeature &feat, const QgsMapLayerActionContext & ) { diff --git a/src/core/qgis.h b/src/core/qgis.h index f79eb35d8589..0974f872dd44 100644 --- a/src/core/qgis.h +++ b/src/core/qgis.h @@ -4327,6 +4327,7 @@ class CORE_EXPORT Qgis enum class MapLayerActionFlag : int SIP_ENUM_BASETYPE( IntFlag ) { EnabledOnlyWhenEditable = 1 << 1, //!< Action should be shown only for editable layers + EnableOnlyWhenHasGeometry = 1 << 2, //!< Action should be shown only for layers with geometry, \since QGIS 3.42 }; Q_ENUM( MapLayerActionFlag ) diff --git a/src/gui/actions/qgsmaplayeraction.cpp b/src/gui/actions/qgsmaplayeraction.cpp index 63044c7d78de..c0cb4a46c0bf 100644 --- a/src/gui/actions/qgsmaplayeraction.cpp +++ b/src/gui/actions/qgsmaplayeraction.cpp @@ -75,6 +75,17 @@ bool QgsMapLayerAction::canRunUsingLayer( QgsMapLayer *layer, const QgsMapLayerA return false; } + if ( mFlags & Qgis::MapLayerActionFlag::EnableOnlyWhenHasGeometry ) + { + // action is only enabled for layers with geometry + if ( !layer ) + return false; + if ( layer->type() != Qgis::LayerType::Vector ) + return false; + if ( qobject_cast( layer )->wkbType() == Qgis::WkbType::NoGeometry ) + return false; + } + //check layer details if ( !mSingleLayer && !mSpecificLayerType ) {