Skip to content

Commit

Permalink
Default to current layer when appropriate
Browse files Browse the repository at this point in the history
  • Loading branch information
nyalldawson committed Mar 28, 2020
1 parent d40990e commit ef4344b
Show file tree
Hide file tree
Showing 10 changed files with 66 additions and 1 deletion.
Expand Up @@ -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

};
Expand Down
1 change: 1 addition & 0 deletions python/plugins/processing/gui/BatchPanel.py
Expand Up @@ -626,6 +626,7 @@ def setCellWrapper(self, row, column, wrapper, context):
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())
Expand Down
2 changes: 2 additions & 0 deletions python/plugins/processing/gui/ParametersPanel.py
Expand Up @@ -98,6 +98,8 @@ def initWidgets(self):
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())
Expand Down
2 changes: 1 addition & 1 deletion python/plugins/processing/gui/wrappers.py
Expand Up @@ -953,7 +953,7 @@ def createWidget(self):
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:
Expand Down
1 change: 1 addition & 0 deletions python/plugins/processing/modeler/ModelerDialog.py
Expand Up @@ -214,6 +214,7 @@ def create_widget_context(self):
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

Expand Down
2 changes: 2 additions & 0 deletions python/plugins/processing/modeler/ModelerGraphicItem.py
Expand Up @@ -55,6 +55,8 @@ def create_widget_context(self):
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

Expand Down
2 changes: 2 additions & 0 deletions python/plugins/processing/modeler/ModelerParametersDialog.py
Expand Up @@ -151,6 +151,8 @@ def setupUi(self):
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)

Expand Down
10 changes: 10 additions & 0 deletions src/gui/processing/qgsprocessingwidgetwrapper.cpp
Expand Up @@ -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;
Expand Down
18 changes: 18 additions & 0 deletions src/gui/processing/qgsprocessingwidgetwrapper.h
Expand Up @@ -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;
Expand All @@ -186,6 +202,8 @@ class GUI_EXPORT QgsProcessingParameterWidgetContext

QgsBrowserGuiModel *mBrowserModel = nullptr;

QgsMapLayer *mActiveLayer = nullptr;

};

#ifndef SIP_RUN
Expand Down
11 changes: 11 additions & 0 deletions src/gui/processing/qgsprocessingwidgetwrapperimpl.cpp
Expand Up @@ -4902,14 +4902,25 @@ QWidget *QgsProcessingMapLayerWidgetWrapper::createWidget()

emit widgetValueHasChanged( this );
} );

setWidgetContext( widgetContext() );
return mComboBox;
}

void QgsProcessingMapLayerWidgetWrapper::setWidgetContext( const QgsProcessingParameterWidgetContext &context )
{
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 )
Expand Down

0 comments on commit ef4344b

Please sign in to comment.