Skip to content

Commit

Permalink
Add processing's feature source parameter support
Browse files Browse the repository at this point in the history
  • Loading branch information
nirvn committed Sep 30, 2024
1 parent a81fb61 commit a190d0a
Show file tree
Hide file tree
Showing 4 changed files with 65 additions and 4 deletions.
32 changes: 29 additions & 3 deletions src/core/processing/processingalgorithmparametersmodel.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -140,7 +140,7 @@ void ProcessingAlgorithmParametersModelBase::rebuild()

if ( mAlgorithm )
{
const static QStringList sSupportedParameters = { QStringLiteral( "number" ), QStringLiteral( "distance" ), QStringLiteral( "enum" ), QStringLiteral( "boolean" ) };
const static QStringList sSupportedParameters = { QStringLiteral( "number" ), QStringLiteral( "distance" ), QStringLiteral( "enum" ), QStringLiteral( "boolean" ), QStringLiteral( "source" ) };
const QgsProcessingAlgorithm *algorithm = QgsApplication::instance()->processingRegistry()->algorithmById( mAlgorithmId );
for ( const QgsProcessingParameterDefinition *definition : algorithm->parameterDefinitions() )
{
Expand All @@ -151,8 +151,11 @@ void ProcessingAlgorithmParametersModelBase::rebuild()
mHasAdvancedParameters = true;
}

mParameters << definition;
mValues << definition->defaultValue();
if ( definition->type() == QStringLiteral( "source" ) && definition->name() != "INPUT" )
{
mParameters << definition;
mValues << definition->defaultValue();
}
}
}
}
Expand Down Expand Up @@ -283,6 +286,29 @@ QVariant ProcessingAlgorithmParametersModelBase::data( const QModelIndex &index,
const QgsProcessingParameterEnum *parameterEnum = dynamic_cast<const QgsProcessingParameterEnum *>( mParameters.at( index.row() ) );
configuration["options"] = parameterEnum->options();
}
else if ( mParameters.at( index.row() )->type() == QStringLiteral( "source" ) )
{
const QgsProcessingParameterFeatureSource *parameterFeatureSource = dynamic_cast<const QgsProcessingParameterFeatureSource *>( mParameters.at( index.row() ) );
const QMap<QString, QgsMapLayer *> mapLayers = QgsProject::instance()->mapLayers();
QVariantList supportedLayers;
QVariantMap supportedLayer;
supportedLayer["id"] = QString();
supportedLayer["name"] = QString();
supportedLayers << supportedLayer;
for ( auto it = mapLayers.begin(); it != mapLayers.end(); ++it )
{
if ( QgsVectorLayer *vectorLayer = dynamic_cast<QgsVectorLayer *>( it.value() ) )
{
if ( parameterFeatureSource->dataTypes().isEmpty() || parameterFeatureSource->dataTypes().contains( static_cast<int>( vectorLayer->geometryType() ) ) )
{
supportedLayer["id"] = vectorLayer->id();
supportedLayer["name"] = vectorLayer->name();
supportedLayers << supportedLayer;
}
}
}
configuration["layers"] = supportedLayers;
}
return configuration;
}

Expand Down
2 changes: 1 addition & 1 deletion src/core/processing/processingalgorithmsmodel.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -171,7 +171,7 @@ void ProcessingAlgorithmsModelBase::addProvider( QgsProcessingProvider *provider
break;
}

if ( parameter->type() == QStringLiteral( "source" ) && ( featureBasedAlgorithm && parameter->name() != featureBasedAlgorithm->inputParameterName() || parameter->name() != QStringLiteral( "INPUT" ) ) )
if ( parameter->type() == QStringLiteral( "source" ) && featureBasedAlgorithm && parameter->name() != featureBasedAlgorithm->inputParameterName() )
{
isSupported = false;
break;
Expand Down
34 changes: 34 additions & 0 deletions src/qml/processingparameterwidgets/source.qml
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
import QtQuick
import QtQuick.Controls
import Theme
import org.qfield
import org.qgis

ProcessingParameterWidgetBase {
id: sourceItem

height: childrenRect.height

Row {
anchors.left: parent.left
anchors.right: parent.right
anchors.top: parent.top
spacing: 5

QfComboBox {
id: sourceComboBox

width: parent.width

model: configuration["layers"]
textRole: "name"
valueRole: "id"

onCurrentValueChanged: {
if (currentValue !== value) {
valueChangeRequested(currentValue);
}
}
}
}
}
1 change: 1 addition & 0 deletions src/qml/qml.qrc
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@
<file>processingparameterwidgets/enum.qml</file>
<file>processingparameterwidgets/distance.qml</file>
<file>processingparameterwidgets/number.qml</file>
<file>processingparameterwidgets/source.qml</file>
<file>QFieldAudioRecorder.qml</file>
<file>QFieldCamera.qml</file>
<file>QFieldSettings.qml</file>
Expand Down

1 comment on commit a190d0a

@qfield-fairy
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please sign in to comment.