From cc0cfbe02be4f3f6d0801c33c1214329441c818d Mon Sep 17 00:00:00 2001 From: Nyall Dawson Date: Thu, 23 Jul 2020 09:26:47 +1000 Subject: [PATCH] [processing] Also fix expression preview results in Aggregate algorithm --- .../qgsprocessingaggregatewidgets.sip.in | 16 ++++++++++++++++ .../qgsprocessingaggregatewidgets.cpp | 15 ++++++++++++++- .../processing/qgsprocessingaggregatewidgets.h | 18 ++++++++++++++++++ .../qgsprocessingaggregatewidgetwrapper.cpp | 1 + src/gui/qgsfieldmappingwidget.cpp | 5 +++++ 5 files changed, 54 insertions(+), 1 deletion(-) diff --git a/python/gui/auto_generated/processing/qgsprocessingaggregatewidgets.sip.in b/python/gui/auto_generated/processing/qgsprocessingaggregatewidgets.sip.in index 67a08725bbf..1215a6b4164 100644 --- a/python/gui/auto_generated/processing/qgsprocessingaggregatewidgets.sip.in +++ b/python/gui/auto_generated/processing/qgsprocessingaggregatewidgets.sip.in @@ -166,6 +166,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 scrollTo( const QModelIndex &index ) const; diff --git a/src/gui/processing/qgsprocessingaggregatewidgets.cpp b/src/gui/processing/qgsprocessingaggregatewidgets.cpp index 729445b8461..18cb3244a46 100644 --- a/src/gui/processing/qgsprocessingaggregatewidgets.cpp +++ b/src/gui/processing/qgsprocessingaggregatewidgets.cpp @@ -257,6 +257,9 @@ bool QgsAggregateMappingModel::moveUpOrDown( const QModelIndex &index, bool up ) void QgsAggregateMappingModel::setSourceFields( const QgsFields &sourceFields ) { mSourceFields = sourceFields; + if ( mExpressionContextGenerator ) + mExpressionContextGenerator->setSourceFields( mSourceFields ); + QStringList usedFields; beginResetModel(); mMapping.clear(); @@ -356,7 +359,7 @@ QgsAggregateMappingWidget::QgsAggregateMappingWidget( QWidget *parent, mModel = new QgsAggregateMappingModel( sourceFields, this ); mTableView->setModel( mModel ); - mTableView->setItemDelegateForColumn( static_cast( QgsAggregateMappingModel::ColumnDataIndex::SourceExpression ), new QgsFieldMappingWidget::ExpressionDelegate( mTableView ) ); + mTableView->setItemDelegateForColumn( static_cast( QgsAggregateMappingModel::ColumnDataIndex::SourceExpression ), new QgsFieldMappingWidget::ExpressionDelegate( this ) ); mTableView->setItemDelegateForColumn( static_cast( QgsAggregateMappingModel::ColumnDataIndex::Aggregate ), new QgsAggregateMappingWidget::AggregateDelegate( mTableView ) ); mTableView->setItemDelegateForColumn( static_cast( QgsAggregateMappingModel::ColumnDataIndex::DestinationType ), new QgsFieldMappingWidget::TypeDelegate( mTableView ) ); updateColumns(); @@ -394,6 +397,16 @@ void QgsAggregateMappingWidget::setSourceFields( const QgsFields &sourceFields ) model()->setSourceFields( sourceFields ); } +void QgsAggregateMappingWidget::setSourceLayer( QgsVectorLayer *layer ) +{ + mSourceLayer = layer; +} + +QgsVectorLayer *QgsAggregateMappingWidget::sourceLayer() +{ + return mSourceLayer; +} + void QgsAggregateMappingWidget::scrollTo( const QModelIndex &index ) const { mTableView->scrollTo( index ); diff --git a/src/gui/processing/qgsprocessingaggregatewidgets.h b/src/gui/processing/qgsprocessingaggregatewidgets.h index 4d34d20a905..3e02426d083 100644 --- a/src/gui/processing/qgsprocessingaggregatewidgets.h +++ b/src/gui/processing/qgsprocessingaggregatewidgets.h @@ -18,6 +18,7 @@ #include #include +#include #include "qgsfields.h" #include "qgsexpressioncontextgenerator.h" @@ -175,6 +176,22 @@ class GUI_EXPORT QgsAggregateMappingWidget : 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(); + /** * Scroll the fields view to \a index */ @@ -211,6 +228,7 @@ class GUI_EXPORT QgsAggregateMappingWidget : public QgsPanelWidget QTableView *mTableView = nullptr; QAbstractTableModel *mModel = nullptr; + QPointer< QgsVectorLayer > mSourceLayer; void updateColumns(); //! Returns selected row indexes in ascending order std::list selectedRows( ); diff --git a/src/gui/processing/qgsprocessingaggregatewidgetwrapper.cpp b/src/gui/processing/qgsprocessingaggregatewidgetwrapper.cpp index 14afdc0f485..8c1a2bfb99c 100644 --- a/src/gui/processing/qgsprocessingaggregatewidgetwrapper.cpp +++ b/src/gui/processing/qgsprocessingaggregatewidgetwrapper.cpp @@ -71,6 +71,7 @@ void QgsProcessingAggregatePanelWidget::setLayer( QgsVectorLayer *layer ) return; mLayer = layer; + mFieldsView->setSourceLayer( mLayer ); if ( mModel->rowCount() == 0 ) { loadFieldsFromLayer(); diff --git a/src/gui/qgsfieldmappingwidget.cpp b/src/gui/qgsfieldmappingwidget.cpp index 7f3f77fe308..137d2d87dd0 100644 --- a/src/gui/qgsfieldmappingwidget.cpp +++ b/src/gui/qgsfieldmappingwidget.cpp @@ -274,6 +274,11 @@ QWidget *QgsFieldMappingWidget::ExpressionDelegate::createEditor( QWidget *paren if ( mappingWidget->sourceLayer() ) editor->setLayer( mappingWidget->sourceLayer() ); } + else if ( QgsAggregateMappingWidget *aggregateWidget = qobject_cast< QgsAggregateMappingWidget *>( ExpressionDelegate::parent() ) ) + { + if ( aggregateWidget->sourceLayer() ) + editor->setLayer( aggregateWidget->sourceLayer() ); + } editor->setField( index.model()->data( index, Qt::DisplayRole ).toString() ); connect( editor,