Skip to content
Permalink
Browse files

[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
  • Loading branch information
nyalldawson committed Jul 24, 2020
1 parent d255af7 commit 69f73e3a744c10a52b6cd05ada228e04945c7bd3
@@ -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,
@@ -69,6 +69,7 @@ void QgsProcessingFieldMapPanelWidget::setLayer( QgsVectorLayer *layer )
return;

mLayer = layer;
mFieldsView->setSourceLayer( mLayer );
if ( mModel->rowCount() == 0 )
{
loadFieldsFromLayer();
@@ -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 ),
@@ -19,13 +19,15 @@
#include <QWidget>
#include <QAbstractTableModel>
#include <QStyledItemDelegate>
#include <QPointer>

#include "qgis_gui.h"
#include "qgsfieldmappingmodel.h"
#include "qgspanelwidget.h"

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( );

0 comments on commit 69f73e3

Please sign in to comment.
You can’t perform that action at this time.