@@ -318,27 +318,60 @@ QVariantMap QgsProcessingModelAlgorithm::parametersForChildAlgorithm( const Chil
318318 }
319319 else
320320 {
321+ const QgsProcessingDestinationParameter *destParam = static_cast < const QgsProcessingDestinationParameter * >( def );
321322 // is destination linked to one of the final outputs from this model?
322- if ( child.modelOutputs ().contains ( def ->name () ) )
323+ if ( child.modelOutputs ().contains ( destParam ->name () ) )
323324 {
324- QString outputName = child.modelOutputs ().value ( def ->name () ).outputName ();
325+ QString outputName = child.modelOutputs ().value ( destParam ->name () ).outputName ();
325326 QString paramName = child.childId () + ' :' + outputName;
326327 if ( modelParameters.contains ( paramName ) )
327- childParams.insert ( def ->name (), modelParameters.value ( paramName ) );
328+ childParams.insert ( destParam ->name (), modelParameters.value ( paramName ) );
328329 }
329330 else
330331 {
331332 // output is temporary
332- // TODO - skip if this output is optional and not used elsewhere in the model
333- childParams.insert ( def->name (), " memory:" );
334333
334+ // check whether it's optional, and if so - is it required?
335+ bool required = true ;
336+ if ( destParam->flags () & QgsProcessingParameterDefinition::FlagOptional )
337+ {
338+ required = childOutputIsRequired ( child.childId (), destParam->name () );
339+ }
335340
341+ // not optional, or required elsewhere in model
342+ if ( required )
343+ childParams.insert ( destParam->name (), destParam->generateTemporaryDestination () );
336344 }
337345 }
338346 }
339347 return childParams;
340348}
341349
350+ bool QgsProcessingModelAlgorithm::childOutputIsRequired ( const QString &childId, const QString &outputName ) const
351+ {
352+ // look through all child algs
353+ QMap< QString, ChildAlgorithm >::const_iterator childIt = mChildAlgorithms .constBegin ();
354+ for ( ; childIt != mChildAlgorithms .constEnd (); ++childIt )
355+ {
356+ if ( childIt->childId () == childId || !childIt->isActive () )
357+ continue ;
358+
359+ // look through all sources for child
360+ QMap<QString, QgsProcessingModelAlgorithm::ChildParameterSource> candidateChildParams = childIt->parameterSources ();
361+ QMap<QString, QgsProcessingModelAlgorithm::ChildParameterSource>::const_iterator childParamIt = candidateChildParams.constBegin ();
362+ for ( ; childParamIt != candidateChildParams.constEnd (); ++childParamIt )
363+ {
364+ if ( childParamIt->source () == ChildParameterSource::ChildOutput
365+ && childParamIt->outputChildId () == childId
366+ && childParamIt->outputName () == outputName )
367+ {
368+ return true ;
369+ }
370+ }
371+ }
372+ return false ;
373+ }
374+
342375QVariantMap QgsProcessingModelAlgorithm::processAlgorithm ( const QVariantMap ¶meters, QgsProcessingContext &context, QgsProcessingFeedback *feedback ) const
343376{
344377 QSet< QString > toExecute;
0 commit comments