diff --git a/python/gui/auto_generated/processing/qgsprocessingwidgetwrapper.sip.in b/python/gui/auto_generated/processing/qgsprocessingwidgetwrapper.sip.in index 2c7702a6900..a43e9a14c1a 100644 --- a/python/gui/auto_generated/processing/qgsprocessingwidgetwrapper.sip.in +++ b/python/gui/auto_generated/processing/qgsprocessingwidgetwrapper.sip.in @@ -160,6 +160,24 @@ Sets the child algorithm ``id`` within the model which the parameter widget is a .. seealso:: :py:func:`modelChildAlgorithmId` .. seealso:: :py:func:`setModel` +%End + + QgsMapLayer *activeLayer() const; +%Docstring +Returns the current active layer. + +.. seealso:: :py:func:`setActiveLayer` + +.. versionadded:: 3.14 +%End + + void setActiveLayer( QgsMapLayer *layer ); +%Docstring +Sets the current active ``layer``. + +.. seealso:: :py:func:`activeLayer` + +.. versionadded:: 3.14 %End }; diff --git a/python/plugins/processing/gui/BatchPanel.py b/python/plugins/processing/gui/BatchPanel.py index 3132321d841..c84fd6b7849 100644 --- a/python/plugins/processing/gui/BatchPanel.py +++ b/python/plugins/processing/gui/BatchPanel.py @@ -626,6 +626,7 @@ class BatchPanel(QgsPanelWidget, WIDGET): widget_context = QgsProcessingParameterWidgetContext() widget_context.setProject(QgsProject.instance()) if iface is not None: + widget_context.setActiveLayer(iface.activeLayer()) widget_context.setMapCanvas(iface.mapCanvas()) widget_context.setMessageBar(self.parent.messageBar()) diff --git a/python/plugins/processing/gui/ParametersPanel.py b/python/plugins/processing/gui/ParametersPanel.py index 09c76f183bd..ca870928ac0 100644 --- a/python/plugins/processing/gui/ParametersPanel.py +++ b/python/plugins/processing/gui/ParametersPanel.py @@ -98,6 +98,8 @@ class ParametersPanel(QgsProcessingParametersWidget): if iface is not None: widget_context.setMapCanvas(iface.mapCanvas()) widget_context.setBrowserModel(iface.browserModel()) + widget_context.setActiveLayer(iface.activeLayer()) + widget_context.setMessageBar(self.parent().messageBar()) if isinstance(self.algorithm(), QgsProcessingModelAlgorithm): widget_context.setModel(self.algorithm()) diff --git a/python/plugins/processing/gui/wrappers.py b/python/plugins/processing/gui/wrappers.py index 309b995adaf..9691e78a060 100755 --- a/python/plugins/processing/gui/wrappers.py +++ b/python/plugins/processing/gui/wrappers.py @@ -953,7 +953,7 @@ class MapLayerWidgetWrapper(WidgetWrapper): self.combo.setValue(self.parameterDefinition().defaultValue(), self.context) else: if self.parameterDefinition().defaultValue(): - self.combo.setvalue(self.parameterDefinition().defaultValue(), self.context) + self.combo.setValue(self.parameterDefinition().defaultValue(), self.context) else: self.combo.setLayer(iface.activeLayer()) except: diff --git a/python/plugins/processing/modeler/ModelerDialog.py b/python/plugins/processing/modeler/ModelerDialog.py index 3f787e85a96..dc15c3356a9 100644 --- a/python/plugins/processing/modeler/ModelerDialog.py +++ b/python/plugins/processing/modeler/ModelerDialog.py @@ -214,6 +214,7 @@ class ModelerDialog(QgsModelDesignerDialog): widget_context.setProject(QgsProject.instance()) if iface is not None: widget_context.setMapCanvas(iface.mapCanvas()) + widget_context.setActiveLayer(iface.activeLayer()) widget_context.setModel(self.model()) return widget_context diff --git a/python/plugins/processing/modeler/ModelerGraphicItem.py b/python/plugins/processing/modeler/ModelerGraphicItem.py index 9db0500875e..82a4480bb91 100644 --- a/python/plugins/processing/modeler/ModelerGraphicItem.py +++ b/python/plugins/processing/modeler/ModelerGraphicItem.py @@ -55,6 +55,8 @@ class ModelerInputGraphicItem(QgsModelParameterGraphicItem): widget_context.setProject(QgsProject.instance()) if iface is not None: widget_context.setMapCanvas(iface.mapCanvas()) + widget_context.setActiveLayer(iface.activeLayer()) + widget_context.setModel(self.model()) return widget_context diff --git a/python/plugins/processing/modeler/ModelerParametersDialog.py b/python/plugins/processing/modeler/ModelerParametersDialog.py index 0bdc8755b05..9d2ed9531b4 100644 --- a/python/plugins/processing/modeler/ModelerParametersDialog.py +++ b/python/plugins/processing/modeler/ModelerParametersDialog.py @@ -151,6 +151,8 @@ class ModelerParametersDialog(QDialog): widget_context.setProject(QgsProject.instance()) if iface is not None: widget_context.setMapCanvas(iface.mapCanvas()) + widget_context.setActiveLayer(iface.activeLayer()) + widget_context.setModel(self.model) widget_context.setModelChildAlgorithmId(self.childId) diff --git a/src/gui/processing/qgsprocessingwidgetwrapper.cpp b/src/gui/processing/qgsprocessingwidgetwrapper.cpp index 929609e0f34..c6f2aee0ff3 100644 --- a/src/gui/processing/qgsprocessingwidgetwrapper.cpp +++ b/src/gui/processing/qgsprocessingwidgetwrapper.cpp @@ -81,6 +81,16 @@ void QgsProcessingParameterWidgetContext::setModelChildAlgorithmId( const QStrin mModelChildAlgorithmId = modelChildAlgorithmId; } +QgsMapLayer *QgsProcessingParameterWidgetContext::activeLayer() const +{ + return mActiveLayer; +} + +void QgsProcessingParameterWidgetContext::setActiveLayer( QgsMapLayer *activeLayer ) +{ + mActiveLayer = activeLayer; +} + QgsProcessingModelAlgorithm *QgsProcessingParameterWidgetContext::model() const { return mModel; diff --git a/src/gui/processing/qgsprocessingwidgetwrapper.h b/src/gui/processing/qgsprocessingwidgetwrapper.h index b7db3aab3a7..57650d8419b 100644 --- a/src/gui/processing/qgsprocessingwidgetwrapper.h +++ b/src/gui/processing/qgsprocessingwidgetwrapper.h @@ -172,6 +172,22 @@ class GUI_EXPORT QgsProcessingParameterWidgetContext */ void setModelChildAlgorithmId( const QString &id ); + /** + * Returns the current active layer. + * + * \see setActiveLayer() + * \since QGIS 3.14 + */ + QgsMapLayer *activeLayer() const; + + /** + * Sets the current active \a layer. + * + * \see activeLayer() + * \since QGIS 3.14 + */ + void setActiveLayer( QgsMapLayer *layer ); + private: QgsProcessingModelAlgorithm *mModel = nullptr; @@ -186,6 +202,8 @@ class GUI_EXPORT QgsProcessingParameterWidgetContext QgsBrowserGuiModel *mBrowserModel = nullptr; + QgsMapLayer *mActiveLayer = nullptr; + }; #ifndef SIP_RUN diff --git a/src/gui/processing/qgsprocessingwidgetwrapperimpl.cpp b/src/gui/processing/qgsprocessingwidgetwrapperimpl.cpp index 5e5383ea9dd..282877461f3 100644 --- a/src/gui/processing/qgsprocessingwidgetwrapperimpl.cpp +++ b/src/gui/processing/qgsprocessingwidgetwrapperimpl.cpp @@ -4902,6 +4902,8 @@ QWidget *QgsProcessingMapLayerWidgetWrapper::createWidget() emit widgetValueHasChanged( this ); } ); + + setWidgetContext( widgetContext() ); return mComboBox; } @@ -4909,7 +4911,16 @@ void QgsProcessingMapLayerWidgetWrapper::setWidgetContext( const QgsProcessingPa { QgsAbstractProcessingParameterWidgetWrapper::setWidgetContext( context ); if ( mComboBox ) + { mComboBox->setWidgetContext( context ); + + if ( !( parameterDefinition()->flags() & QgsProcessingParameterDefinition::FlagOptional ) ) + { + // non optional parameter -- if no default value set, default to active layer + if ( !parameterDefinition()->defaultValue().isValid() ) + mComboBox->setLayer( context.activeLayer() ); + } + } } void QgsProcessingMapLayerWidgetWrapper::setWidgetValue( const QVariant &value, QgsProcessingContext &context )