Skip to content

Commit

Permalink
Tweak model component clipboard logic -- only copy comments and outputs
Browse files Browse the repository at this point in the history
connected to algorithms when they have been explicitly selected by the user
  • Loading branch information
nyalldawson committed Apr 14, 2020
1 parent 47f96e2 commit d24ae53
Show file tree
Hide file tree
Showing 4 changed files with 96 additions and 5 deletions.
Expand Up @@ -520,6 +520,12 @@ Ownership of ``output`` is transferred to the item.

virtual bool canDeleteComponent();


QgsModelComponentGraphicItem *parentComponentItem() const;
%Docstring
Returns the parent model component item.
%End

protected:

virtual QColor fillColor( State state ) const;
Expand Down
5 changes: 5 additions & 0 deletions src/gui/processing/models/qgsmodelcomponentgraphicitem.cpp
Expand Up @@ -1421,5 +1421,10 @@ QgsProcessingModelComment *QgsModelCommentGraphicItem::modelComponent()
return nullptr;
}

QgsModelComponentGraphicItem *QgsModelCommentGraphicItem::parentComponentItem() const
{
return mParentItem;
}


///@endcond
6 changes: 6 additions & 0 deletions src/gui/processing/models/qgsmodelcomponentgraphicitem.h
Expand Up @@ -568,6 +568,12 @@ class GUI_EXPORT QgsModelCommentGraphicItem : public QgsModelComponentGraphicIte
~QgsModelCommentGraphicItem() override;
void contextMenuEvent( QGraphicsSceneContextMenuEvent *event ) override;
bool canDeleteComponent() override;

/**
* Returns the parent model component item.
*/
QgsModelComponentGraphicItem *parentComponentItem() const;

protected:

QColor fillColor( State state ) const override;
Expand Down
84 changes: 79 additions & 5 deletions src/gui/processing/models/qgsmodelgraphicsview.cpp
Expand Up @@ -486,13 +486,42 @@ void QgsModelGraphicsView::copyItems( const QList<QgsModelComponentGraphicItem *
QList< QVariant > paramComponents;
QList< QVariant > groupBoxComponents;
QList< QVariant > algComponents;

QList< QgsModelComponentGraphicItem * > selectedCommentParents;
QList< QgsProcessingModelOutput > selectedOutputs;
QList< QgsProcessingModelOutput > selectedOutputsComments;
for ( QgsModelComponentGraphicItem *item : items )
{
if ( const QgsModelCommentGraphicItem *commentItem = dynamic_cast< QgsModelCommentGraphicItem * >( item ) )
{
selectedCommentParents << commentItem->parentComponentItem();
if ( const QgsModelOutputGraphicItem *outputItem = dynamic_cast< QgsModelOutputGraphicItem * >( commentItem->parentComponentItem() ) )
{
selectedOutputsComments << *( static_cast< const QgsProcessingModelOutput *>( outputItem->component() ) );
}
}
else if ( const QgsModelOutputGraphicItem *outputItem = dynamic_cast< QgsModelOutputGraphicItem * >( item ) )
{
selectedOutputs << *( static_cast< const QgsProcessingModelOutput *>( outputItem->component() ) );
}
}

for ( QgsModelComponentGraphicItem *item : items )
{
if ( QgsProcessingModelParameter *param = dynamic_cast< QgsProcessingModelParameter * >( item->component() ) )
if ( const QgsProcessingModelParameter *param = dynamic_cast< QgsProcessingModelParameter * >( item->component() ) )
{
QgsProcessingModelParameter component = *param;

// was comment selected?
if ( !selectedCommentParents.contains( item ) )
{
// no, so drop comment
component.comment()->setDescription( QString() );
}

QVariantMap paramDef;
paramDef.insert( QStringLiteral( "component" ), param->toVariant() );
const QgsProcessingParameterDefinition *def = modelScene()->model()->parameterDefinition( param->parameterName() );
paramDef.insert( QStringLiteral( "component" ), component.toVariant() );
const QgsProcessingParameterDefinition *def = modelScene()->model()->parameterDefinition( component.parameterName() );
paramDef.insert( QStringLiteral( "definition" ), def->toVariantMap() );

paramComponents << paramDef;
Expand All @@ -501,9 +530,54 @@ void QgsModelGraphicsView::copyItems( const QList<QgsModelComponentGraphicItem *
{
groupBoxComponents << groupBox->toVariant();
}
else if ( QgsProcessingModelChildAlgorithm *alg = dynamic_cast< QgsProcessingModelChildAlgorithm * >( item->component() ) )
else if ( const QgsProcessingModelChildAlgorithm *alg = dynamic_cast< QgsProcessingModelChildAlgorithm * >( item->component() ) )
{
algComponents << alg->toVariant();
QgsProcessingModelChildAlgorithm childAlg = *alg;

// was comment selected?
if ( !selectedCommentParents.contains( item ) )
{
// no, so drop comment
childAlg.comment()->setDescription( QString() );
}

// don't copy outputs which weren't selected either
QMap<QString, QgsProcessingModelOutput> clipboardOutputs;
const QMap<QString, QgsProcessingModelOutput> existingOutputs = childAlg.modelOutputs();
for ( auto it = existingOutputs.constBegin(); it != existingOutputs.constEnd(); ++ it )
{
bool found = false;
for ( const QgsProcessingModelOutput &candidate : selectedOutputs )
{
if ( candidate.childId() == childAlg.childId() && candidate.name() == it.value().name() && candidate.childOutputName() == it.value().childOutputName() )
{
found = true;
break;
}
}
if ( found )
{
// should we also copy the comment?
bool commentFound = false;
for ( const QgsProcessingModelOutput &candidate : selectedOutputsComments )
{
if ( candidate.childId() == childAlg.childId() && candidate.name() == it.value().name() && candidate.childOutputName() == it.value().childOutputName() )
{
commentFound = true;
break;
}
}

QgsProcessingModelOutput output = it.value();
if ( !commentFound )
output.comment()->setDescription( QString() );

clipboardOutputs.insert( it.key(), output );
}
}
childAlg.setModelOutputs( clipboardOutputs );

algComponents << childAlg.toVariant();
}
}
QVariantMap components;
Expand Down

0 comments on commit d24ae53

Please sign in to comment.