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( QMessageBox::StandardButtons( QMessageBox::Yes | QMessageBox::No ) );
90 dlg.setDefaultButton( QMessageBox::No );
91 if ( dlg.exec() == QMessageBox::Yes )
93 loadFieldsFromLayer();
102QVariant QgsProcessingAggregatePanelWidget::value()
const
104 const QList<QgsAggregateMappingModel::Aggregate> mapping = mFieldsView->mapping();
106 QVariantList results;
107 results.reserve( mapping.size() );
111 def.insert( u
"name"_s, aggregate.field.name() );
112 def.insert( u
"type"_s,
static_cast<int>( aggregate.field.type() ) );
113 def.insert( u
"type_name"_s, aggregate.field.typeName() );
114 def.insert( u
"length"_s, aggregate.field.length() );
115 def.insert( u
"precision"_s, aggregate.field.precision() );
116 def.insert( u
"sub_type"_s,
static_cast<int>( aggregate.field.subType() ) );
117 def.insert( u
"input"_s, aggregate.source );
118 def.insert( u
"aggregate"_s, aggregate.aggregate );
119 def.insert( u
"delimiter"_s, aggregate.delimiter );
120 results.append( def );
125void QgsProcessingAggregatePanelWidget::setValue(
const QVariant &value )
127 if ( value.userType() != QMetaType::Type::QVariantList )
130 QList<QgsAggregateMappingModel::Aggregate> aggregates;
132 const QVariantList fields = value.toList();
133 aggregates.reserve( fields.size() );
134 for (
const QVariant &field : fields )
136 const QVariantMap map = field.toMap();
138 f( map.value( u
"name"_s ).toString(),
139 static_cast<QMetaType::Type
>( map.value( u
"type"_s,
static_cast<int>( QMetaType::Type::UnknownType ) ).toInt() ),
140 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(),
141 map.value( u
"length"_s, 0 ).toInt(),
142 map.value( u
"precision"_s, 0 ).toInt(),
149 aggregate.
source = map.value( u
"input"_s ).toString();
150 aggregate.
aggregate = map.value( u
"aggregate"_s ).toString();
151 aggregate.
delimiter = map.value( u
"delimiter"_s ).toString();
153 aggregates.append( aggregate );
156 mBlockChangedSignal =
true;
158 if ( aggregates.size() > 0 )
159 mFieldsView->setMapping( aggregates );
161 mBlockChangedSignal =
false;
168 mFieldsView->registerExpressionContextGenerator( generator );
171void QgsProcessingAggregatePanelWidget::loadFieldsFromLayer()
175 mFieldsView->setSourceFields( mLayer->fields() );
179void QgsProcessingAggregatePanelWidget::addField()
181 const int rowCount = mModel->rowCount();
182 mModel->appendField(
QgsField( u
"new_field"_s ) );
183 const QModelIndex index = mModel->index( rowCount, 0 );
184 mFieldsView->selectionModel()->select( index, QItemSelectionModel::SelectionFlags( 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() );
200QgsProcessingAggregateParameterDefinitionWidget::QgsProcessingAggregateParameterDefinitionWidget(
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 );
251 vlayout->addWidget( mParentLayerComboBox );
252 setLayout( vlayout );
257 auto param = std::make_unique<QgsProcessingParameterAggregate>( name, description, mParentLayerComboBox->currentData().toString() );
258 param->setFlags( flags );
259 return param.release();
270QString QgsProcessingAggregateWidgetWrapper::parameterType()
const
277 return new QgsProcessingAggregateWidgetWrapper( parameter, type );
280QWidget *QgsProcessingAggregateWidgetWrapper::createWidget()
282 mPanel =
new QgsProcessingAggregatePanelWidget(
nullptr );
283 mPanel->setToolTip( parameterDefinition()->toolTip() );
284 mPanel->registerExpressionContextGenerator(
this );
286 connect( mPanel, &QgsProcessingAggregatePanelWidget::changed,
this, [
this] { 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 );
323int QgsProcessingAggregateWidgetWrapper::stretch()
const
332 std::unique_ptr<QgsProcessingContext> tmpContext;
333 if ( mProcessingContextGenerator )
334 context = mProcessingContextGenerator->processingContext();
338 tmpContext = std::make_unique<QgsProcessingContext>();
339 context = tmpContext.get();
346 mPanel->setLayer(
nullptr );
352 std::unique_ptr<QgsMapLayer> ownedLayer( context->
takeResultLayer( layer->
id() ) );
355 mParentLayer.reset( qobject_cast<QgsVectorLayer *>( ownedLayer.release() ) );
356 layer = mParentLayer.get();
364 mPanel->setLayer( layer );
367void QgsProcessingAggregateWidgetWrapper::setWidgetValue(
const QVariant &value,
QgsProcessingContext & )
370 mPanel->setValue( value );
373QVariant QgsProcessingAggregateWidgetWrapper::widgetValue()
const
375 return mPanel ? mPanel->value() : QVariant();
378QString QgsProcessingAggregateWidgetWrapper::modelerExpressionFormatString()
const
380 return tr(
"an array of map items, each containing a 'name', 'type', 'aggregate' and 'input' value (and optional 'length' and 'precision' values)." );
383const QgsVectorLayer *QgsProcessingAggregateWidgetWrapper::linkedVectorLayer()
const
385 if ( mPanel && mPanel->layer() )
386 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).