Skip to content

Commit

Permalink
[processing] Add a stable way to get processing widgets to stretch ve…
Browse files Browse the repository at this point in the history
…rtically
  • Loading branch information
nyalldawson committed Jun 1, 2020
1 parent 146094f commit f0bb647
Show file tree
Hide file tree
Showing 10 changed files with 43 additions and 20 deletions.
Expand Up @@ -38,11 +38,11 @@ Constructor for QgsProcessingParametersWidget, for the specified ``algorithm``.

virtual void initWidgets();

void addParameterWidget( const QgsProcessingParameterDefinition *parameter, QWidget *widget /Transfer/ );
void addParameterWidget( const QgsProcessingParameterDefinition *parameter, QWidget *widget /Transfer/, int stretch = 0 );
void addParameterLabel( const QgsProcessingParameterDefinition *parameter, QWidget *label /Transfer/ );

void addOutputLabel( QWidget *label /Transfer/ );
void addOutputWidget( QWidget *widget /Transfer/ );
void addOutputWidget( QWidget *widget /Transfer/, int stretch = 0 );

void addExtraWidget( QWidget *widget /Transfer/ );

Expand Down
Expand Up @@ -352,6 +352,15 @@ to other parameter's values).
%Docstring
Called after all wrappers have been created within a particular dialog or context,
allowing the wrapper to connect to the wrappers of other, related parameters.
%End

virtual int stretch() const;
%Docstring
Returns the Qt layout "stretch" factor to use when adding this widget to a layout.

The default implementation returns 0.

.. versionadded:: 3.14
%End

virtual QgsExpressionContext createExpressionContext() const;
Expand Down
9 changes: 0 additions & 9 deletions python/plugins/processing/algs/qgis/ui/FieldsMappingPanel.py
Expand Up @@ -59,12 +59,3 @@ class FieldsMappingWidgetWrapper(WidgetWrapper):
def __init__(self, *args, **kwargs):
super(FieldsMappingWidgetWrapper, self).__init__(*args, **kwargs)
self._layer = None

def postInitialize(self, wrappers):

# remove exiting spacers to get FieldsMappingPanel fully expanded
if self.dialogType in (DIALOG_STANDARD, DIALOG_MODELER):
layout = self.widget.parent().layout()
spacer = layout.itemAt(layout.count() - 1)
if isinstance(spacer, QSpacerItem):
layout.removeItem(spacer)
6 changes: 4 additions & 2 deletions python/plugins/processing/gui/ParametersPanel.py
Expand Up @@ -112,8 +112,10 @@ def initWidgets(self):
# QgsAbstractProcessingParameterWidgetWrapper class
# TODO QGIS 4.0 - remove
is_python_wrapper = issubclass(wrapper.__class__, WidgetWrapper)
stretch = 0
if not is_python_wrapper:
widget = wrapper.createWrappedWidget(self.processing_context)
stretch = wrapper.stretch()
else:
widget = wrapper.widget

Expand Down Expand Up @@ -144,7 +146,7 @@ def initWidgets(self):
desc += self.tr(' [optional]')
widget.setText(desc)

self.addParameterWidget(param, widget)
self.addParameterWidget(param, widget, stretch)

for output in self.algorithm().destinationParameterDefinitions():
if output.flags() & QgsProcessingParameterDefinition.FlagHidden:
Expand All @@ -164,7 +166,7 @@ def initWidgets(self):
self.addOutputLabel(label)

widget = wrapper.createWrappedWidget(self.processing_context)
self.addOutputWidget(widget)
self.addOutputWidget(widget, wrapper.stretch())

# def skipOutputChanged(widget, checkbox, skipped):
# TODO
Expand Down
5 changes: 5 additions & 0 deletions src/gui/processing/qgsprocessingfieldmapwidgetwrapper.cpp
Expand Up @@ -238,6 +238,11 @@ void QgsProcessingFieldMapWidgetWrapper::postInitialize( const QList<QgsAbstract
}
}

int QgsProcessingFieldMapWidgetWrapper::stretch() const
{
return 1;
}

void QgsProcessingFieldMapWidgetWrapper::setParentLayerWrapperValue( const QgsAbstractProcessingParameterWidgetWrapper *parentWrapper )
{
// evaluate value to layer
Expand Down
2 changes: 2 additions & 0 deletions src/gui/processing/qgsprocessingfieldmapwidgetwrapper.h
Expand Up @@ -75,6 +75,8 @@ class GUI_EXPORT QgsProcessingFieldMapWidgetWrapper : public QgsAbstractProcessi
QWidget *createWidget() override SIP_FACTORY;

void postInitialize( const QList< QgsAbstractProcessingParameterWidgetWrapper * > &wrappers ) override;
int stretch() const override;

public slots:
void setParentLayerWrapperValue( const QgsAbstractProcessingParameterWidgetWrapper *parentWrapper );

Expand Down
10 changes: 5 additions & 5 deletions src/gui/processing/qgsprocessingparameterswidget.cpp
Expand Up @@ -50,12 +50,12 @@ void QgsProcessingParametersWidget::initWidgets()
}
}

void QgsProcessingParametersWidget::addParameterWidget( const QgsProcessingParameterDefinition *parameter, QWidget *widget )
void QgsProcessingParametersWidget::addParameterWidget( const QgsProcessingParameterDefinition *parameter, QWidget *widget, int stretch )
{
if ( parameter->flags() & QgsProcessingParameterDefinition::FlagAdvanced )
mAdvancedGroupLayout->addWidget( widget );
mAdvancedGroupLayout->addWidget( widget, stretch );
else
mScrollAreaLayout->insertWidget( mScrollAreaLayout->count() - 2, widget );
mScrollAreaLayout->insertWidget( mScrollAreaLayout->count() - 2, widget, stretch );
}

void QgsProcessingParametersWidget::addParameterLabel( const QgsProcessingParameterDefinition *parameter, QWidget *label )
Expand All @@ -71,9 +71,9 @@ void QgsProcessingParametersWidget::addOutputLabel( QWidget *label )
mScrollAreaLayout->insertWidget( mScrollAreaLayout->count() - 1, label );
}

void QgsProcessingParametersWidget::addOutputWidget( QWidget *widget )
void QgsProcessingParametersWidget::addOutputWidget( QWidget *widget, int stretch )
{
mScrollAreaLayout->insertWidget( mScrollAreaLayout->count() - 1, widget );
mScrollAreaLayout->insertWidget( mScrollAreaLayout->count() - 1, widget, stretch );
}

void QgsProcessingParametersWidget::addExtraWidget( QWidget *widget )
Expand Down
4 changes: 2 additions & 2 deletions src/gui/processing/qgsprocessingparameterswidget.h
Expand Up @@ -50,11 +50,11 @@ class GUI_EXPORT QgsProcessingParametersWidget : public QgsPanelWidget, public Q

virtual void initWidgets();

void addParameterWidget( const QgsProcessingParameterDefinition *parameter, QWidget *widget SIP_TRANSFER );
void addParameterWidget( const QgsProcessingParameterDefinition *parameter, QWidget *widget SIP_TRANSFER, int stretch = 0 );
void addParameterLabel( const QgsProcessingParameterDefinition *parameter, QWidget *label SIP_TRANSFER );

void addOutputLabel( QWidget *label SIP_TRANSFER );
void addOutputWidget( QWidget *widget SIP_TRANSFER );
void addOutputWidget( QWidget *widget SIP_TRANSFER, int stretch = 0 );

void addExtraWidget( QWidget *widget SIP_TRANSFER );

Expand Down
5 changes: 5 additions & 0 deletions src/gui/processing/qgsprocessingwidgetwrapper.cpp
Expand Up @@ -277,6 +277,11 @@ void QgsAbstractProcessingParameterWidgetWrapper::postInitialize( const QList<Qg
}
}

int QgsAbstractProcessingParameterWidgetWrapper::stretch() const
{
return 0;
}

QgsExpressionContext QgsAbstractProcessingParameterWidgetWrapper::createExpressionContext() const
{
QgsExpressionContext context = QgsProcessingGuiUtils::createExpressionContext( mProcessingContextGenerator, mWidgetContext, mParameterDefinition ? mParameterDefinition->algorithm() : nullptr, linkedVectorLayer() );
Expand Down
9 changes: 9 additions & 0 deletions src/gui/processing/qgsprocessingwidgetwrapper.h
Expand Up @@ -392,6 +392,15 @@ class GUI_EXPORT QgsAbstractProcessingParameterWidgetWrapper : public QObject, p
*/
virtual void postInitialize( const QList< QgsAbstractProcessingParameterWidgetWrapper * > &wrappers );

/**
* Returns the Qt layout "stretch" factor to use when adding this widget to a layout.
*
* The default implementation returns 0.
*
* \since QGIS 3.14
*/
virtual int stretch() const;

QgsExpressionContext createExpressionContext() const override;

/**
Expand Down

0 comments on commit f0bb647

Please sign in to comment.