20 #include <QMessageBox> 
   21 #include <QPushButton> 
   22 #include <QStandardItemModel> 
   23 #include <QToolButton> 
   24 #include <QItemSelectionModel> 
   29 #include "qgsprocessingmodelalgorithm.h" 
   43 QgsProcessingAggregatePanelWidget::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 )
 
   69 void 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();
 
   99 QVariant 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( 
"length" ), aggregate.field.length() );
 
  111     def.insert( QStringLiteral( 
"precision" ), aggregate.field.precision() );
 
  112     def.insert( QStringLiteral( 
"input" ), aggregate.source );
 
  113     def.insert( QStringLiteral( 
"aggregate" ), aggregate.aggregate );
 
  114     def.insert( QStringLiteral( 
"delimiter" ), aggregate.delimiter );
 
  115     results.append( def );
 
  120 void QgsProcessingAggregatePanelWidget::setValue( 
const QVariant &value )
 
  122   if ( value.type() != QVariant::List )
 
  125   QList< QgsAggregateMappingModel::Aggregate > aggregates;
 
  127   const QVariantList fields = value.toList();
 
  128   aggregates.reserve( fields.size() );
 
  129   for ( 
const QVariant &
field : fields )
 
  131     const QVariantMap map = 
field.toMap();
 
  132     QgsField f( map.value( QStringLiteral( 
"name" ) ).toString(),
 
  133                 static_cast< QVariant::Type 
>( map.value( QStringLiteral( 
"type" ), QVariant::Invalid ).toInt() ),
 
  134                 QVariant::typeToName( 
static_cast< QVariant::Type 
>( map.value( QStringLiteral( 
"type" ), QVariant::Invalid ).toInt() ) ),
 
  135                 map.value( QStringLiteral( 
"length" ), 0 ).toInt(),
 
  136                 map.value( QStringLiteral( 
"precision" ), 0 ).toInt() );
 
  141     aggregate.
source = map.value( QStringLiteral( 
"input" ) ).toString();
 
  142     aggregate.
aggregate = map.value( QStringLiteral( 
"aggregate" ) ).toString();
 
  143     aggregate.
delimiter = map.value( QStringLiteral( 
"delimiter" ) ).toString();
 
  145     aggregates.append( aggregate );
 
  148   mBlockChangedSignal = 
true;
 
  150   if ( aggregates.size() > 0 )
 
  151     mFieldsView->setMapping( aggregates );
 
  153   mBlockChangedSignal = 
false;
 
  160   mFieldsView->registerExpressionContextGenerator( generator );
 
  163 void QgsProcessingAggregatePanelWidget::loadFieldsFromLayer()
 
  167     mFieldsView->setSourceFields( mLayer->fields() );
 
  171 void QgsProcessingAggregatePanelWidget::addField()
 
  173   const int rowCount = mModel->rowCount();
 
  174   mModel->appendField( 
QgsField( QStringLiteral( 
"new_field" ) ) );
 
  175   QModelIndex index = mModel->index( rowCount, 0 );
 
  176   mFieldsView->selectionModel()->select(
 
  178     QItemSelectionModel::SelectionFlags(
 
  179       QItemSelectionModel::Clear |
 
  180       QItemSelectionModel::Select |
 
  181       QItemSelectionModel::Current |
 
  182       QItemSelectionModel::Rows ) );
 
  183   mFieldsView->scrollTo( index );
 
  186 void QgsProcessingAggregatePanelWidget::loadLayerFields()
 
  188   if ( 
QgsVectorLayer *vl = qobject_cast< QgsVectorLayer * >( mLayerCombo->currentLayer() ) )
 
  190     mFieldsView->setSourceFields( vl->fields() );
 
  201   QVBoxLayout *vlayout = 
new QVBoxLayout();
 
  202   vlayout->setContentsMargins( 0, 0, 0, 0 );
 
  204   vlayout->addWidget( 
new QLabel( tr( 
"Parent layer" ) ) );
 
  206   mParentLayerComboBox = 
new QComboBox();
 
  207   mParentLayerComboBox->addItem( tr( 
"None" ), QVariant() );
 
  209   QString initialParent;
 
  211     initialParent = aggregateParam->parentLayerParameterName();
 
  213   if ( 
auto *lModel = widgetContext.
model() )
 
  216     const QMap<QString, QgsProcessingModelParameter> components = lModel->parameterComponents();
 
  217     for ( 
auto it = components.constBegin(); it != components.constEnd(); ++it )
 
  221         mParentLayerComboBox-> addItem( definition->
description(), definition->
name() );
 
  222         if ( !initialParent.isEmpty() && initialParent == definition->
name() )
 
  224           mParentLayerComboBox->setCurrentIndex( mParentLayerComboBox->count() - 1 );
 
  229         mParentLayerComboBox-> addItem( definition->
description(), definition->
name() );
 
  230         if ( !initialParent.isEmpty() && initialParent == definition->
name() )
 
  232           mParentLayerComboBox->setCurrentIndex( mParentLayerComboBox->count() - 1 );
 
  238   if ( mParentLayerComboBox->count() == 1 && !initialParent.isEmpty() )
 
  241     mParentLayerComboBox->addItem( initialParent, initialParent );
 
  242     mParentLayerComboBox->setCurrentIndex( mParentLayerComboBox->count() - 1 );
 
  245   vlayout->addWidget( mParentLayerComboBox );
 
  246   setLayout( vlayout );
 
  249 QgsProcessingParameterDefinition *QgsProcessingAggregateParameterDefinitionWidget::createParameter( 
const QString &name, 
const QString &description, QgsProcessingParameterDefinition::Flags flags )
 const 
  251   auto param = std::make_unique< QgsProcessingParameterAggregate >( name, description, mParentLayerComboBox->currentData().toString() );
 
  252   param->setFlags( flags );
 
  253   return param.release();
 
  265 QString QgsProcessingAggregateWidgetWrapper::parameterType()
 const 
  272   return new QgsProcessingAggregateWidgetWrapper( parameter, type );
 
  275 QWidget *QgsProcessingAggregateWidgetWrapper::createWidget()
 
  277   mPanel = 
new QgsProcessingAggregatePanelWidget( 
nullptr );
 
  278   mPanel->setToolTip( parameterDefinition()->toolTip() );
 
  279   mPanel->registerExpressionContextGenerator( 
this );
 
  281   connect( mPanel, &QgsProcessingAggregatePanelWidget::changed, 
this, [ = ]
 
  283     emit widgetValueHasChanged( 
this );
 
  291   return new QgsProcessingAggregateParameterDefinitionWidget( context, widgetContext, definition, 
algorithm );
 
  294 void QgsProcessingAggregateWidgetWrapper::postInitialize( 
const QList<QgsAbstractProcessingParameterWidgetWrapper *> &wrappers )
 
  304         if ( wrapper->parameterDefinition()->name() == 
static_cast< const QgsProcessingParameterAggregate * 
>( parameterDefinition() )->parentLayerParameterName() )
 
  306           setParentLayerWrapperValue( wrapper );
 
  309             setParentLayerWrapperValue( wrapper );
 
  322 int QgsProcessingAggregateWidgetWrapper::stretch()
 const 
  331   std::unique_ptr< QgsProcessingContext > tmpContext;
 
  332   if ( mProcessingContextGenerator )
 
  333     context = mProcessingContextGenerator->processingContext();
 
  337     tmpContext = std::make_unique< QgsProcessingContext >();
 
  338     context = tmpContext.get();
 
  345       mPanel->setLayer( 
nullptr );
 
  351   std::unique_ptr< QgsMapLayer > ownedLayer( context->
takeResultLayer( layer->
id() ) );
 
  354     mParentLayer.reset( qobject_cast< QgsVectorLayer * >( ownedLayer.release() ) );
 
  355     layer = mParentLayer.get();
 
  363     mPanel->setLayer( layer );
 
  366 void QgsProcessingAggregateWidgetWrapper::setWidgetValue( 
const QVariant &value, 
QgsProcessingContext & )
 
  369     mPanel->setValue( value );
 
  372 QVariant QgsProcessingAggregateWidgetWrapper::widgetValue()
 const 
  374   return mPanel ? mPanel->value() : QVariant();
 
  377 QStringList QgsProcessingAggregateWidgetWrapper::compatibleParameterTypes()
 const 
  383 QStringList QgsProcessingAggregateWidgetWrapper::compatibleOutputTypes()
 const 
  385   return QStringList();
 
  388 QString QgsProcessingAggregateWidgetWrapper::modelerExpressionFormatString()
 const 
  390   return tr( 
"an array of map items, each containing a 'name', 'type', 'aggregate' and 'input' value (and optional 'length' and 'precision' values)." );
 
  393 const QgsVectorLayer *QgsProcessingAggregateWidgetWrapper::linkedVectorLayer()
 const 
  395   if ( mPanel && mPanel->layer() )
 
  396     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.
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)