22#include <QStandardItemModel>
24#include <QItemSelectionModel>
43QgsProcessingAggregatePanelWidget::QgsProcessingAggregatePanelWidget( QWidget *parent )
48 mModel = mFieldsView->model();
50 mLayerCombo->setAllowEmptyLayer(
true );
53 connect( mResetButton, &QPushButton::clicked,
this, &QgsProcessingAggregatePanelWidget::loadFieldsFromLayer );
54 connect( mAddButton, &QPushButton::clicked,
this, &QgsProcessingAggregatePanelWidget::addField );
58 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 QMessageBox dlg(
this );
83 dlg.setText( tr(
"Do you want to reset the field mapping?" ) );
84 dlg.setStandardButtons(
85 QMessageBox::StandardButtons( QMessageBox::Yes |
87 dlg.setDefaultButton( QMessageBox::No );
88 if ( dlg.exec() == QMessageBox::Yes )
90 loadFieldsFromLayer();
99QVariant QgsProcessingAggregatePanelWidget::value()
const
101 const QList<QgsAggregateMappingModel::Aggregate> mapping = mFieldsView->mapping();
103 QVariantList results;
104 results.reserve( mapping.size() );
108 def.insert( QStringLiteral(
"name" ), aggregate.field.name() );
109 def.insert( QStringLiteral(
"type" ),
static_cast< int >( aggregate.field.type() ) );
110 def.insert( QStringLiteral(
"type_name" ), aggregate.field.typeName() );
111 def.insert( QStringLiteral(
"length" ), aggregate.field.length() );
112 def.insert( QStringLiteral(
"precision" ), aggregate.field.precision() );
113 def.insert( QStringLiteral(
"sub_type" ),
static_cast< int >( aggregate.field.subType() ) );
114 def.insert( QStringLiteral(
"input" ), aggregate.source );
115 def.insert( QStringLiteral(
"aggregate" ), aggregate.aggregate );
116 def.insert( QStringLiteral(
"delimiter" ), aggregate.delimiter );
117 results.append( def );
122void QgsProcessingAggregatePanelWidget::setValue(
const QVariant &value )
124 if ( value.type() != QVariant::List )
127 QList< QgsAggregateMappingModel::Aggregate > aggregates;
129 const QVariantList fields = value.toList();
130 aggregates.reserve( fields.size() );
131 for (
const QVariant &
field : fields )
133 const QVariantMap map =
field.toMap();
134 const QgsField f( map.value( QStringLiteral(
"name" ) ).toString(),
135 static_cast< QVariant::Type
>( map.value( QStringLiteral(
"type" ), QVariant::Invalid ).toInt() ),
136 map.value( QStringLiteral(
"type_name" ), QVariant::typeToName(
static_cast< QVariant::Type
>( map.value( QStringLiteral(
"type" ), QVariant::Invalid ).toInt() ) ) ).toString(),
137 map.value( QStringLiteral(
"length" ), 0 ).toInt(),
138 map.value( QStringLiteral(
"precision" ), 0 ).toInt(),
140 static_cast< QVariant::Type
>( map.value( QStringLiteral(
"sub_type" ), QVariant::Invalid ).toInt() ) );
145 aggregate.
source = map.value( QStringLiteral(
"input" ) ).toString();
146 aggregate.
aggregate = map.value( QStringLiteral(
"aggregate" ) ).toString();
147 aggregate.
delimiter = map.value( QStringLiteral(
"delimiter" ) ).toString();
149 aggregates.append( aggregate );
152 mBlockChangedSignal =
true;
154 if ( aggregates.size() > 0 )
155 mFieldsView->setMapping( aggregates );
157 mBlockChangedSignal =
false;
164 mFieldsView->registerExpressionContextGenerator( generator );
167void QgsProcessingAggregatePanelWidget::loadFieldsFromLayer()
171 mFieldsView->setSourceFields( mLayer->fields() );
175void QgsProcessingAggregatePanelWidget::addField()
177 const int rowCount = mModel->rowCount();
178 mModel->appendField(
QgsField( QStringLiteral(
"new_field" ) ) );
179 const QModelIndex index = mModel->index( rowCount, 0 );
180 mFieldsView->selectionModel()->select(
182 QItemSelectionModel::SelectionFlags(
183 QItemSelectionModel::Clear |
184 QItemSelectionModel::Select |
185 QItemSelectionModel::Current |
186 QItemSelectionModel::Rows ) );
187 mFieldsView->scrollTo( index );
190void QgsProcessingAggregatePanelWidget::loadLayerFields()
192 if (
QgsVectorLayer *vl = qobject_cast< QgsVectorLayer * >( mLayerCombo->currentLayer() ) )
194 mFieldsView->setSourceFields( vl->fields() );
205 QVBoxLayout *vlayout =
new QVBoxLayout();
206 vlayout->setContentsMargins( 0, 0, 0, 0 );
208 vlayout->addWidget(
new QLabel( tr(
"Parent layer" ) ) );
210 mParentLayerComboBox =
new QComboBox();
211 mParentLayerComboBox->addItem( tr(
"None" ), QVariant() );
213 QString initialParent;
215 initialParent = aggregateParam->parentLayerParameterName();
217 if (
auto *lModel = widgetContext.
model() )
220 const QMap<QString, QgsProcessingModelParameter> components = lModel->parameterComponents();
221 for (
auto it = components.constBegin(); it != components.constEnd(); ++it )
225 mParentLayerComboBox-> addItem( definition->
description(), definition->
name() );
226 if ( !initialParent.isEmpty() && initialParent == definition->
name() )
228 mParentLayerComboBox->setCurrentIndex( mParentLayerComboBox->count() - 1 );
233 mParentLayerComboBox-> addItem( definition->
description(), definition->
name() );
234 if ( !initialParent.isEmpty() && initialParent == definition->
name() )
236 mParentLayerComboBox->setCurrentIndex( mParentLayerComboBox->count() - 1 );
242 if ( mParentLayerComboBox->count() == 1 && !initialParent.isEmpty() )
245 mParentLayerComboBox->addItem( initialParent, initialParent );
246 mParentLayerComboBox->setCurrentIndex( mParentLayerComboBox->count() - 1 );
249 vlayout->addWidget( mParentLayerComboBox );
250 setLayout( vlayout );
253QgsProcessingParameterDefinition *QgsProcessingAggregateParameterDefinitionWidget::createParameter(
const QString &name,
const QString &description, QgsProcessingParameterDefinition::Flags flags )
const
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, [ = ]
287 emit widgetValueHasChanged(
this );
295 return new QgsProcessingAggregateParameterDefinitionWidget( context, widgetContext, definition,
algorithm );
298void QgsProcessingAggregateWidgetWrapper::postInitialize(
const QList<QgsAbstractProcessingParameterWidgetWrapper *> &wrappers )
308 if ( wrapper->parameterDefinition()->name() ==
static_cast< const QgsProcessingParameterAggregate *
>( parameterDefinition() )->parentLayerParameterName() )
310 setParentLayerWrapperValue( wrapper );
313 setParentLayerWrapperValue( wrapper );
326int QgsProcessingAggregateWidgetWrapper::stretch()
const
335 std::unique_ptr< QgsProcessingContext > tmpContext;
336 if ( mProcessingContextGenerator )
337 context = mProcessingContextGenerator->processingContext();
341 tmpContext = std::make_unique< QgsProcessingContext >();
342 context = tmpContext.get();
349 mPanel->setLayer(
nullptr );
355 std::unique_ptr< QgsMapLayer > ownedLayer( context->
takeResultLayer( layer->
id() ) );
358 mParentLayer.reset( qobject_cast< QgsVectorLayer * >( ownedLayer.release() ) );
359 layer = mParentLayer.get();
367 mPanel->setLayer( layer );
370void QgsProcessingAggregateWidgetWrapper::setWidgetValue(
const QVariant &value,
QgsProcessingContext & )
373 mPanel->setValue( value );
376QVariant QgsProcessingAggregateWidgetWrapper::widgetValue()
const
378 return mPanel ? mPanel->value() : QVariant();
381QStringList QgsProcessingAggregateWidgetWrapper::compatibleParameterTypes()
const
387QStringList QgsProcessingAggregateWidgetWrapper::compatibleOutputTypes()
const
389 return QStringList();
392QString QgsProcessingAggregateWidgetWrapper::modelerExpressionFormatString()
const
394 return tr(
"an array of map items, each containing a 'name', 'type', 'aggregate' and 'input' value (and optional 'length' and 'precision' values)." );
397const QgsVectorLayer *QgsProcessingAggregateWidgetWrapper::linkedVectorLayer()
const
399 if ( mPanel && mPanel->layer() )
400 return mPanel->layer();
Abstract interface for generating an expression context.
Encapsulate a field in an attribute table or data source.
QString id() const
Returns the layer's unique ID, which is used to access this layer from QgsProject.
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...
WidgetType
Types of dialogs which Processing widgets can be created for.
@ Standard
Standard algorithm dialog.
@ Batch
Batch processing dialog.
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.
Represents a vector layer which manages a vector based data sets.
@ VectorLayer
Vector layer.
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)