[processing] When running in toolbox mode, set the source layer for

the field expression widgets in the refactor fields widget so that
the expression editor can generate feature based previews

Fixes #37912
This commit is contained in:
Nyall Dawson 2020-07-23 09:10:07 +10:00
parent d255af792e
commit 69f73e3a74
4 changed files with 56 additions and 1 deletions

View File

@ -79,6 +79,22 @@ Returns the selection model
void setSourceFields( const QgsFields &sourceFields );
%Docstring
Set source fields of the underlying mapping model to ``sourceFields``
%End
void setSourceLayer( QgsVectorLayer *layer );
%Docstring
Sets a source ``layer`` to use when generating expression previews in the widget.
.. versionadded:: 3.16
%End
QgsVectorLayer *sourceLayer();
%Docstring
Returns the source layer for use when generating expression previews.
Returned value may be ``None``.
.. versionadded:: 3.16
%End
void setDestinationFields( const QgsFields &destinationFields,

View File

@ -69,6 +69,7 @@ void QgsProcessingFieldMapPanelWidget::setLayer( QgsVectorLayer *layer )
return;
mLayer = layer;
mFieldsView->setSourceLayer( mLayer );
if ( mModel->rowCount() == 0 )
{
loadFieldsFromLayer();

View File

@ -18,6 +18,7 @@
#include "qgsfieldexpressionwidget.h"
#include "qgsexpression.h"
#include "qgsprocessingaggregatewidgets.h"
#include "qgsvectorlayer.h"
#include <QTableView>
#include <QVBoxLayout>
@ -45,7 +46,7 @@ QgsFieldMappingWidget::QgsFieldMappingWidget( QWidget *parent,
#endif
mTableView->setModel( mModel );
mTableView->setItemDelegateForColumn( static_cast<int>( QgsFieldMappingModel::ColumnDataIndex::SourceExpression ), new ExpressionDelegate( mTableView ) );
mTableView->setItemDelegateForColumn( static_cast<int>( QgsFieldMappingModel::ColumnDataIndex::SourceExpression ), new ExpressionDelegate( this ) );
mTableView->setItemDelegateForColumn( static_cast<int>( QgsFieldMappingModel::ColumnDataIndex::DestinationType ), new TypeDelegate( mTableView ) );
updateColumns();
// Make sure columns are updated when rows are added
@ -98,6 +99,16 @@ void QgsFieldMappingWidget::setSourceFields( const QgsFields &sourceFields )
model()->setSourceFields( sourceFields );
}
void QgsFieldMappingWidget::setSourceLayer( QgsVectorLayer *layer )
{
mSourceLayer = layer;
}
QgsVectorLayer *QgsFieldMappingWidget::sourceLayer()
{
return mSourceLayer;
}
void QgsFieldMappingWidget::setDestinationFields( const QgsFields &destinationFields, const QMap<QString, QString> &expressions )
{
model()->setDestinationFields( destinationFields, expressions );
@ -257,6 +268,13 @@ QWidget *QgsFieldMappingWidget::ExpressionDelegate::createEditor( QWidget *paren
{
Q_ASSERT( false );
}
if ( QgsFieldMappingWidget *mappingWidget = qobject_cast< QgsFieldMappingWidget *>( ExpressionDelegate::parent() ) )
{
if ( mappingWidget->sourceLayer() )
editor->setLayer( mappingWidget->sourceLayer() );
}
editor->setField( index.model()->data( index, Qt::DisplayRole ).toString() );
connect( editor,
qgis::overload<const QString &, bool >::of( &QgsFieldExpressionWidget::fieldChanged ),

View File

@ -19,6 +19,7 @@
#include <QWidget>
#include <QAbstractTableModel>
#include <QStyledItemDelegate>
#include <QPointer>
#include "qgis_gui.h"
#include "qgsfieldmappingmodel.h"
@ -26,6 +27,7 @@
class QTableView;
class QItemSelectionModel;
class QgsVectorLayer;
/**
* \ingroup gui
@ -85,6 +87,22 @@ class GUI_EXPORT QgsFieldMappingWidget : public QgsPanelWidget
//! Set source fields of the underlying mapping model to \a sourceFields
void setSourceFields( const QgsFields &sourceFields );
/**
* Sets a source \a layer to use when generating expression previews in the widget.
*
* \since QGIS 3.16
*/
void setSourceLayer( QgsVectorLayer *layer );
/**
* Returns the source layer for use when generating expression previews.
*
* Returned value may be NULLPTR.
*
* \since QGIS 3.16
*/
QgsVectorLayer *sourceLayer();
/**
* Set destination fields to \a destinationFields in the underlying model,
* initial values for the expressions can be optionally specified through
@ -130,6 +148,8 @@ class GUI_EXPORT QgsFieldMappingWidget : public QgsPanelWidget
QTableView *mTableView = nullptr;
QAbstractTableModel *mModel = nullptr;
QPointer< QgsVectorLayer > mSourceLayer;
void updateColumns();
//! Returns selected row indexes in ascending order
std::list<int> selectedRows( );