24#include <QItemSelectionModel>
28#include <QStandardItemModel>
32#include "moc_qgsprocessingaggregatewidgetwrapper.cpp"
34using namespace Qt::StringLiterals;
44QgsProcessingAggregatePanelWidget::QgsProcessingAggregatePanelWidget( QWidget *parent )
49 mModel = mFieldsView->model();
51 mLayerCombo->setAllowEmptyLayer(
true );
54 connect( mResetButton, &QPushButton::clicked,
this, &QgsProcessingAggregatePanelWidget::loadFieldsFromLayer );
55 connect( mAddButton, &QPushButton::clicked,
this, &QgsProcessingAggregatePanelWidget::addField );
59 connect( mLoadLayerFieldsButton, &QPushButton::clicked,
this, &QgsProcessingAggregatePanelWidget::loadLayerFields );
62 if ( !mBlockChangedSignal )
69void QgsProcessingAggregatePanelWidget::setLayer(
QgsVectorLayer *layer )
71 if ( layer == mLayer )
75 mFieldsView->setSourceLayer( mLayer );
76 if ( mModel->rowCount() == 0 )
78 loadFieldsFromLayer();
82 if ( mSkipConfirmDialog )
87 QMessageBox dlg(
this );
88 dlg.setText( tr(
"Do you want to reset the field mapping?" ) );
89 dlg.setStandardButtons(
90 QMessageBox::StandardButtons( QMessageBox::Yes | QMessageBox::No )
92 dlg.setDefaultButton( QMessageBox::No );
93 if ( dlg.exec() == QMessageBox::Yes )
95 loadFieldsFromLayer();
104QVariant QgsProcessingAggregatePanelWidget::value()
const
106 const QList<QgsAggregateMappingModel::Aggregate> mapping = mFieldsView->mapping();
108 QVariantList results;
109 results.reserve( mapping.size() );
113 def.insert( u
"name"_s, aggregate.field.name() );
114 def.insert( u
"type"_s,
static_cast<int>( aggregate.field.type() ) );
115 def.insert( u
"type_name"_s, aggregate.field.typeName() );
116 def.insert( u
"length"_s, aggregate.field.length() );
117 def.insert( u
"precision"_s, aggregate.field.precision() );
118 def.insert( u
"sub_type"_s,
static_cast<int>( aggregate.field.subType() ) );
119 def.insert( u
"input"_s, aggregate.source );
120 def.insert( u
"aggregate"_s, aggregate.aggregate );
121 def.insert( u
"delimiter"_s, aggregate.delimiter );
122 results.append( def );
127void QgsProcessingAggregatePanelWidget::setValue(
const QVariant &value )
129 if ( value.userType() != QMetaType::Type::QVariantList )
132 QList<QgsAggregateMappingModel::Aggregate> aggregates;
134 const QVariantList fields = value.toList();
135 aggregates.reserve( fields.size() );
136 for (
const QVariant &field : fields )
138 const QVariantMap map = field.toMap();
139 const QgsField f( map.value( u
"name"_s ).toString(),
static_cast<QMetaType::Type
>( map.value( u
"type"_s,
static_cast<int>( QMetaType::Type::UnknownType ) ).toInt() ), map.value( u
"type_name"_s, QVariant::typeToName(
static_cast<QMetaType::Type
>( map.value( u
"type"_s,
static_cast<int>( QMetaType::Type::UnknownType ) ).toInt() ) ) ).toString(), map.value( u
"length"_s, 0 ).toInt(), map.value( u
"precision"_s, 0 ).toInt(), QString(),
static_cast<QMetaType::Type
>( map.value( u
"sub_type"_s,
QgsVariantUtils::createNullVariant( QMetaType::Type::UnknownType ) ).toInt() ) );
144 aggregate.
source = map.value( u
"input"_s ).toString();
145 aggregate.
aggregate = map.value( u
"aggregate"_s ).toString();
146 aggregate.
delimiter = map.value( u
"delimiter"_s ).toString();
148 aggregates.append( aggregate );
151 mBlockChangedSignal =
true;
153 if ( aggregates.size() > 0 )
154 mFieldsView->setMapping( aggregates );
156 mBlockChangedSignal =
false;
163 mFieldsView->registerExpressionContextGenerator( generator );
166void QgsProcessingAggregatePanelWidget::loadFieldsFromLayer()
170 mFieldsView->setSourceFields( mLayer->fields() );
174void QgsProcessingAggregatePanelWidget::addField()
176 const int rowCount = mModel->rowCount();
177 mModel->appendField(
QgsField( u
"new_field"_s ) );
178 const QModelIndex index = mModel->index( rowCount, 0 );
179 mFieldsView->selectionModel()->select(
181 QItemSelectionModel::SelectionFlags(
182 QItemSelectionModel::Clear | QItemSelectionModel::Select | QItemSelectionModel::Current | QItemSelectionModel::Rows
185 mFieldsView->scrollTo( index );
188void QgsProcessingAggregatePanelWidget::loadLayerFields()
190 if (
QgsVectorLayer *vl = qobject_cast<QgsVectorLayer *>( mLayerCombo->currentLayer() ) )
192 mFieldsView->setSourceFields( vl->fields() );
203 QVBoxLayout *vlayout =
new QVBoxLayout();
204 vlayout->setContentsMargins( 0, 0, 0, 0 );
206 vlayout->addWidget(
new QLabel( tr(
"Parent layer" ) ) );
208 mParentLayerComboBox =
new QComboBox();
209 mParentLayerComboBox->addItem( tr(
"None" ), QVariant() );
211 QString initialParent;
213 initialParent = aggregateParam->parentLayerParameterName();
215 if (
auto *lModel = widgetContext.
model() )
218 const QMap<QString, QgsProcessingModelParameter> components = lModel->parameterComponents();
219 for (
auto it = components.constBegin(); it != components.constEnd(); ++it )
223 mParentLayerComboBox->addItem( definition->
description(), definition->
name() );
224 if ( !initialParent.isEmpty() && initialParent == definition->
name() )
226 mParentLayerComboBox->setCurrentIndex( mParentLayerComboBox->count() - 1 );
231 mParentLayerComboBox->addItem( definition->
description(), definition->
name() );
232 if ( !initialParent.isEmpty() && initialParent == definition->
name() )
234 mParentLayerComboBox->setCurrentIndex( mParentLayerComboBox->count() - 1 );
240 if ( mParentLayerComboBox->count() == 1 && !initialParent.isEmpty() )
243 mParentLayerComboBox->addItem( initialParent, initialParent );
244 mParentLayerComboBox->setCurrentIndex( mParentLayerComboBox->count() - 1 );
249 vlayout->addWidget( mParentLayerComboBox );
250 setLayout( vlayout );
255 auto param = std::make_unique<QgsProcessingParameterAggregate>( name, description, mParentLayerComboBox->currentData().toString() );
256 param->setFlags( flags );
257 return param.release();
269QString QgsProcessingAggregateWidgetWrapper::parameterType()
const
276 return new QgsProcessingAggregateWidgetWrapper( parameter, type );
279QWidget *QgsProcessingAggregateWidgetWrapper::createWidget()
281 mPanel =
new QgsProcessingAggregatePanelWidget(
nullptr );
282 mPanel->setToolTip( parameterDefinition()->toolTip() );
283 mPanel->registerExpressionContextGenerator(
this );
285 connect( mPanel, &QgsProcessingAggregatePanelWidget::changed,
this, [
this] {
286 emit widgetValueHasChanged(
this );
294 return new QgsProcessingAggregateParameterDefinitionWidget( context, widgetContext, definition,
algorithm );
297void QgsProcessingAggregateWidgetWrapper::postInitialize(
const QList<QgsAbstractProcessingParameterWidgetWrapper *> &wrappers )
307 if ( wrapper->parameterDefinition()->name() ==
static_cast<const QgsProcessingParameterAggregate *
>( parameterDefinition() )->parentLayerParameterName() )
309 setParentLayerWrapperValue( wrapper );
311 setParentLayerWrapperValue( wrapper );
324int QgsProcessingAggregateWidgetWrapper::stretch()
const
333 std::unique_ptr<QgsProcessingContext> tmpContext;
334 if ( mProcessingContextGenerator )
335 context = mProcessingContextGenerator->processingContext();
339 tmpContext = std::make_unique<QgsProcessingContext>();
340 context = tmpContext.get();
347 mPanel->setLayer(
nullptr );
353 std::unique_ptr<QgsMapLayer> ownedLayer( context->
takeResultLayer( layer->
id() ) );
356 mParentLayer.reset( qobject_cast<QgsVectorLayer *>( ownedLayer.release() ) );
357 layer = mParentLayer.get();
365 mPanel->setLayer( layer );
368void QgsProcessingAggregateWidgetWrapper::setWidgetValue(
const QVariant &value,
QgsProcessingContext & )
371 mPanel->setValue( value );
374QVariant QgsProcessingAggregateWidgetWrapper::widgetValue()
const
376 return mPanel ? mPanel->value() : QVariant();
379QString QgsProcessingAggregateWidgetWrapper::modelerExpressionFormatString()
const
381 return tr(
"an array of map items, each containing a 'name', 'type', 'aggregate' and 'input' value (and optional 'length' and 'precision' values)." );
384const QgsVectorLayer *QgsProcessingAggregateWidgetWrapper::linkedVectorLayer()
const
386 if ( mPanel && mPanel->layer() )
387 return mPanel->layer();
ProcessingMode
Types of modes which Processing widgets can be created for.
@ Batch
Batch processing mode.
@ Standard
Standard (single-run) algorithm mode.
QFlags< ProcessingParameterFlag > ProcessingParameterFlags
Flags which dictate the behavior of Processing parameters.
Abstract interface for generating an expression context.
Encapsulate a field in an attribute table or data source.
Abstract base class for processing algorithms.
Contains information about the context in which a processing algorithm is executed.
QgsMapLayer * takeResultLayer(const QString &id)
Takes the result map layer with matching id from the context and transfers ownership of it back to th...
A parameter for "aggregate" configurations, which consist of a definition of desired output fields,...
static QString typeName()
Returns the type name for the parameter class.
Base class for the definition of processing parameters.
QString description() const
Returns the description for the parameter.
QString name() const
Returns the name of the parameter.
An input feature source (such as vector layers) parameter for processing algorithms.
A vector layer (with or without geometry) parameter for processing algorithms.
Contains settings which reflect the context in which a Processing parameter widget is shown.
QgsProcessingModelAlgorithm * model() const
Returns the model which the parameter widget is associated with.
static QgsVectorLayer * parameterAsVectorLayer(const QgsProcessingParameterDefinition *definition, const QVariantMap ¶meters, QgsProcessingContext &context)
Evaluates the parameter with matching definition to a vector layer.
static QVariant createNullVariant(QMetaType::Type metaType)
Helper method to properly create a null QVariant from a metaType Returns the created QVariant.
Represents a vector layer which manages a vector based dataset.
As part of the API refactoring and improvements which landed in the Processing API was substantially reworked from the x version This was done in order to allow much of the underlying Processing framework to be ported into allowing algorithms to be written in pure substantial changes are required in order to port existing x Processing algorithms for QGIS x The most significant changes are outlined not GeoAlgorithm For algorithms which operate on features one by consider subclassing the QgsProcessingFeatureBasedAlgorithm class This class allows much of the boilerplate code for looping over features from a vector layer to be bypassed and instead requires implementation of a processFeature method Ensure that your algorithm(or algorithm 's parent class) implements the new pure virtual createInstance(self) call
The Aggregate struct holds information about an aggregate column.
QString source
The source expression used as the input for the aggregate calculation.
QString delimiter
Delimiter string.
QString aggregate
Aggregate name.
QgsField field
The field in its current status (it might have been renamed).