17#include "moc_qgsprocessingaggregatewidgetwrapper.cpp"
23#include <QStandardItemModel>
25#include <QItemSelectionModel>
42QgsProcessingAggregatePanelWidget::QgsProcessingAggregatePanelWidget( QWidget *parent )
47 mModel = mFieldsView->model();
49 mLayerCombo->setAllowEmptyLayer(
true );
52 connect( mResetButton, &QPushButton::clicked,
this, &QgsProcessingAggregatePanelWidget::loadFieldsFromLayer );
53 connect( mAddButton, &QPushButton::clicked,
this, &QgsProcessingAggregatePanelWidget::addField );
57 connect( mLoadLayerFieldsButton, &QPushButton::clicked,
this, &QgsProcessingAggregatePanelWidget::loadLayerFields );
61 if ( !mBlockChangedSignal )
68void QgsProcessingAggregatePanelWidget::setLayer(
QgsVectorLayer *layer )
70 if ( layer == mLayer )
74 mFieldsView->setSourceLayer( mLayer );
75 if ( mModel->rowCount() == 0 )
77 loadFieldsFromLayer();
81 QMessageBox dlg(
this );
82 dlg.setText( tr(
"Do you want to reset the field mapping?" ) );
83 dlg.setStandardButtons(
84 QMessageBox::StandardButtons( QMessageBox::Yes |
86 dlg.setDefaultButton( QMessageBox::No );
87 if ( dlg.exec() == QMessageBox::Yes )
89 loadFieldsFromLayer();
98QVariant QgsProcessingAggregatePanelWidget::value()
const
100 const QList<QgsAggregateMappingModel::Aggregate> mapping = mFieldsView->mapping();
102 QVariantList results;
103 results.reserve( mapping.size() );
107 def.insert( QStringLiteral(
"name" ), aggregate.field.name() );
108 def.insert( QStringLiteral(
"type" ),
static_cast< int >( aggregate.field.type() ) );
109 def.insert( QStringLiteral(
"type_name" ), aggregate.field.typeName() );
110 def.insert( QStringLiteral(
"length" ), aggregate.field.length() );
111 def.insert( QStringLiteral(
"precision" ), aggregate.field.precision() );
112 def.insert( QStringLiteral(
"sub_type" ),
static_cast< int >( aggregate.field.subType() ) );
113 def.insert( QStringLiteral(
"input" ), aggregate.source );
114 def.insert( QStringLiteral(
"aggregate" ), aggregate.aggregate );
115 def.insert( QStringLiteral(
"delimiter" ), aggregate.delimiter );
116 results.append( def );
121void QgsProcessingAggregatePanelWidget::setValue(
const QVariant &value )
123 if ( value.userType() != QMetaType::Type::QVariantList )
126 QList< QgsAggregateMappingModel::Aggregate > aggregates;
128 const QVariantList fields = value.toList();
129 aggregates.reserve( fields.size() );
130 for (
const QVariant &field : fields )
132 const QVariantMap map = field.toMap();
133 const QgsField f( map.value( QStringLiteral(
"name" ) ).toString(),
134 static_cast< QMetaType::Type
>( map.value( QStringLiteral(
"type" ),
static_cast< int >( QMetaType::Type::UnknownType ) ).toInt() ),
135 map.value( QStringLiteral(
"type_name" ), QVariant::typeToName(
static_cast< QMetaType::Type
>( map.value( QStringLiteral(
"type" ),
static_cast< int >( QMetaType::Type::UnknownType ) ).toInt() ) ) ).toString(),
136 map.value( QStringLiteral(
"length" ), 0 ).toInt(),
137 map.value( QStringLiteral(
"precision" ), 0 ).toInt(),
144 aggregate.
source = map.value( QStringLiteral(
"input" ) ).toString();
145 aggregate.
aggregate = map.value( QStringLiteral(
"aggregate" ) ).toString();
146 aggregate.
delimiter = map.value( QStringLiteral(
"delimiter" ) ).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( QStringLiteral(
"new_field" ) ) );
178 const QModelIndex index = mModel->index( rowCount, 0 );
179 mFieldsView->selectionModel()->select(
181 QItemSelectionModel::SelectionFlags(
182 QItemSelectionModel::Clear |
183 QItemSelectionModel::Select |
184 QItemSelectionModel::Current |
185 QItemSelectionModel::Rows ) );
186 mFieldsView->scrollTo( index );
189void QgsProcessingAggregatePanelWidget::loadLayerFields()
191 if (
QgsVectorLayer *vl = qobject_cast< QgsVectorLayer * >( mLayerCombo->currentLayer() ) )
193 mFieldsView->setSourceFields( vl->fields() );
204 QVBoxLayout *vlayout =
new QVBoxLayout();
205 vlayout->setContentsMargins( 0, 0, 0, 0 );
207 vlayout->addWidget(
new QLabel( tr(
"Parent layer" ) ) );
209 mParentLayerComboBox =
new QComboBox();
210 mParentLayerComboBox->addItem( tr(
"None" ), QVariant() );
212 QString initialParent;
214 initialParent = aggregateParam->parentLayerParameterName();
216 if (
auto *lModel = widgetContext.
model() )
219 const QMap<QString, QgsProcessingModelParameter> components = lModel->parameterComponents();
220 for (
auto it = components.constBegin(); it != components.constEnd(); ++it )
224 mParentLayerComboBox-> addItem( definition->
description(), definition->
name() );
225 if ( !initialParent.isEmpty() && initialParent == definition->
name() )
227 mParentLayerComboBox->setCurrentIndex( mParentLayerComboBox->count() - 1 );
232 mParentLayerComboBox-> addItem( definition->
description(), definition->
name() );
233 if ( !initialParent.isEmpty() && initialParent == definition->
name() )
235 mParentLayerComboBox->setCurrentIndex( mParentLayerComboBox->count() - 1 );
241 if ( mParentLayerComboBox->count() == 1 && !initialParent.isEmpty() )
244 mParentLayerComboBox->addItem( initialParent, initialParent );
245 mParentLayerComboBox->setCurrentIndex( mParentLayerComboBox->count() - 1 );
248 vlayout->addWidget( mParentLayerComboBox );
249 setLayout( vlayout );
254 auto param = std::make_unique< QgsProcessingParameterAggregate >( name, description, mParentLayerComboBox->currentData().toString() );
255 param->setFlags( flags );
256 return param.release();
268QString QgsProcessingAggregateWidgetWrapper::parameterType()
const
275 return new QgsProcessingAggregateWidgetWrapper( parameter, type );
278QWidget *QgsProcessingAggregateWidgetWrapper::createWidget()
280 mPanel =
new QgsProcessingAggregatePanelWidget(
nullptr );
281 mPanel->setToolTip( parameterDefinition()->toolTip() );
282 mPanel->registerExpressionContextGenerator(
this );
284 connect( mPanel, &QgsProcessingAggregatePanelWidget::changed,
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 );
312 setParentLayerWrapperValue( wrapper );
325int QgsProcessingAggregateWidgetWrapper::stretch()
const
334 std::unique_ptr< QgsProcessingContext > tmpContext;
335 if ( mProcessingContextGenerator )
336 context = mProcessingContextGenerator->processingContext();
340 tmpContext = std::make_unique< QgsProcessingContext >();
341 context = tmpContext.get();
348 mPanel->setLayer(
nullptr );
354 std::unique_ptr< QgsMapLayer > ownedLayer( context->
takeResultLayer( layer->
id() ) );
357 mParentLayer.reset( qobject_cast< QgsVectorLayer * >( ownedLayer.release() ) );
358 layer = mParentLayer.get();
366 mPanel->setLayer( layer );
369void QgsProcessingAggregateWidgetWrapper::setWidgetValue(
const QVariant &value,
QgsProcessingContext & )
372 mPanel->setValue( value );
375QVariant QgsProcessingAggregateWidgetWrapper::widgetValue()
const
377 return mPanel ? mPanel->value() : QVariant();
380QStringList QgsProcessingAggregateWidgetWrapper::compatibleParameterTypes()
const
386QStringList QgsProcessingAggregateWidgetWrapper::compatibleOutputTypes()
const
388 return QStringList();
391QString QgsProcessingAggregateWidgetWrapper::modelerExpressionFormatString()
const
393 return tr(
"an array of map items, each containing a 'name', 'type', 'aggregate' and 'input' value (and optional 'length' and 'precision' values)." );
396const QgsVectorLayer *QgsProcessingAggregateWidgetWrapper::linkedVectorLayer()
const
398 if ( mPanel && mPanel->layer() )
399 return mPanel->layer();
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...
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.
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 data sets.
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)