Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
[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

(cherry picked from commit 69f73e3)
  • Loading branch information
nyalldawson committed Jul 27, 2020
1 parent a193785 commit 42c1703
Show file tree
Hide file tree
Showing 4 changed files with 56 additions and 1 deletion.
16 changes: 16 additions & 0 deletions python/gui/auto_generated/qgsfieldmappingwidget.sip.in
Expand Up @@ -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,
Expand Down
1 change: 1 addition & 0 deletions src/gui/processing/qgsprocessingfieldmapwidgetwrapper.cpp
Expand Up @@ -69,6 +69,7 @@ void QgsProcessingFieldMapPanelWidget::setLayer( QgsVectorLayer *layer )
return;

mLayer = layer;
mFieldsView->setSourceLayer( mLayer );
if ( mModel->rowCount() == 0 )
{
loadFieldsFromLayer();
Expand Down
20 changes: 19 additions & 1 deletion src/gui/qgsfieldmappingwidget.cpp
Expand Up @@ -18,6 +18,7 @@
#include "qgsfieldexpressionwidget.h"
#include "qgsexpression.h"
#include "qgsprocessingaggregatewidgets.h"
#include "qgsvectorlayer.h"

#include <QTableView>
#include <QVBoxLayout>
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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 );
Expand Down Expand Up @@ -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 ),
Expand Down
20 changes: 20 additions & 0 deletions src/gui/qgsfieldmappingwidget.h
Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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( );
Expand Down

0 comments on commit 42c1703

Please sign in to comment.