17#include "moc_qgsmodelgraphicsscene.cpp"
27#include <QGraphicsSceneMouseEvent>
32QgsModelGraphicsScene::QgsModelGraphicsScene( QObject *parent )
33 : QGraphicsScene( parent )
35 setItemIndexMethod( QGraphicsScene::NoIndex );
38QgsProcessingModelAlgorithm *QgsModelGraphicsScene::model()
43void QgsModelGraphicsScene::setModel( QgsProcessingModelAlgorithm *model )
48void QgsModelGraphicsScene::setFlag( QgsModelGraphicsScene::Flag flag,
bool on )
56void QgsModelGraphicsScene::mousePressEvent( QGraphicsSceneMouseEvent *event )
58 if ( event->button() != Qt::LeftButton )
60 QGraphicsScene::mousePressEvent( event );
63QgsModelComponentGraphicItem *QgsModelGraphicsScene::createParameterGraphicItem( QgsProcessingModelAlgorithm *model, QgsProcessingModelParameter *param )
const
65 return new QgsModelParameterGraphicItem( param, model,
nullptr );
68QgsModelChildAlgorithmGraphicItem *QgsModelGraphicsScene::createChildAlgGraphicItem( QgsProcessingModelAlgorithm *model, QgsProcessingModelChildAlgorithm *child )
const
70 return new QgsModelChildAlgorithmGraphicItem( child, model,
nullptr );
73QgsModelComponentGraphicItem *QgsModelGraphicsScene::createOutputGraphicItem( QgsProcessingModelAlgorithm *model, QgsProcessingModelOutput *output )
const
75 return new QgsModelOutputGraphicItem( output, model,
nullptr );
78QgsModelComponentGraphicItem *QgsModelGraphicsScene::createCommentGraphicItem( QgsProcessingModelAlgorithm *model, QgsProcessingModelComment *comment, QgsModelComponentGraphicItem *parentItem )
const
80 return new QgsModelCommentGraphicItem( comment, parentItem, model,
nullptr );
83QgsModelComponentGraphicItem *QgsModelGraphicsScene::createGroupBoxGraphicItem( QgsProcessingModelAlgorithm *model, QgsProcessingModelGroupBox *box )
const
85 return new QgsModelGroupBoxGraphicItem( box, model,
nullptr );
88void QgsModelGraphicsScene::createItems( QgsProcessingModelAlgorithm *model,
QgsProcessingContext &context )
91 const QList<QgsProcessingModelGroupBox> boxes = model->groupBoxes();
92 mGroupBoxItems.clear();
93 for (
const QgsProcessingModelGroupBox &box : boxes )
95 QgsModelComponentGraphicItem *item = createGroupBoxGraphicItem( model, box.clone() );
97 item->setPos( box.position().x(), box.position().y() );
98 mGroupBoxItems.insert( box.uuid(), item );
99 connect( item, &QgsModelComponentGraphicItem::requestModelRepaint,
this, &QgsModelGraphicsScene::rebuildRequired );
100 connect( item, &QgsModelComponentGraphicItem::changed,
this, &QgsModelGraphicsScene::componentChanged );
101 connect( item, &QgsModelComponentGraphicItem::aboutToChange,
this, &QgsModelGraphicsScene::componentAboutToChange );
105 const QMap<QString, QgsProcessingModelParameter> params = model->parameterComponents();
106 for (
auto it = params.constBegin(); it != params.constEnd(); ++it )
108 QgsModelComponentGraphicItem *item = createParameterGraphicItem( model, it.value().clone() );
110 item->setPos( it.value().position().x(), it.value().position().y() );
111 mParameterItems.insert( it.value().parameterName(), item );
112 connect( item, &QgsModelComponentGraphicItem::requestModelRepaint,
this, &QgsModelGraphicsScene::rebuildRequired );
113 connect( item, &QgsModelComponentGraphicItem::changed,
this, &QgsModelGraphicsScene::componentChanged );
114 connect( item, &QgsModelComponentGraphicItem::aboutToChange,
this, &QgsModelGraphicsScene::componentAboutToChange );
116 addCommentItemForComponent( model, it.value(), item );
120 for (
auto it = params.constBegin(); it != params.constEnd(); ++it )
124 for (
const QString &otherName : parameterLinks )
126 if ( mParameterItems.contains( it.key() ) && mParameterItems.contains( otherName ) )
128 std::unique_ptr< QgsModelArrowItem > arrow = std::make_unique< QgsModelArrowItem >( mParameterItems.value( otherName ), QgsModelArrowItem::Marker::Circle, mParameterItems.value( it.key() ), QgsModelArrowItem::Marker::ArrowHead );
129 arrow->setPenStyle( Qt::DotLine );
130 addItem( arrow.release() );
136 const QMap<QString, QgsProcessingModelChildAlgorithm> childAlgs = model->childAlgorithms();
137 for (
auto it = childAlgs.constBegin(); it != childAlgs.constEnd(); ++it )
139 QgsModelChildAlgorithmGraphicItem *item = createChildAlgGraphicItem( model, it.value().clone() );
141 item->setPos( it.value().position().x(), it.value().position().y() );
143 const QString childId = it.value().childId();
144 item->setResults( mLastResult.childResults().value( childId ) );
145 mChildAlgorithmItems.insert( childId, item );
146 connect( item, &QgsModelComponentGraphicItem::requestModelRepaint,
this, &QgsModelGraphicsScene::rebuildRequired );
147 connect( item, &QgsModelComponentGraphicItem::changed,
this, &QgsModelGraphicsScene::componentChanged );
148 connect( item, &QgsModelComponentGraphicItem::aboutToChange,
this, &QgsModelGraphicsScene::componentAboutToChange );
149 connect( item, &QgsModelChildAlgorithmGraphicItem::runFromHere,
this, [
this, childId]
151 emit runFromChild( childId );
153 connect( item, &QgsModelChildAlgorithmGraphicItem::runSelected,
this, &QgsModelGraphicsScene::runSelected );
154 connect( item, &QgsModelChildAlgorithmGraphicItem::showPreviousResults,
this, [
this, childId]
156 emit showChildAlgorithmOutputs( childId );
158 connect( item, &QgsModelChildAlgorithmGraphicItem::showLog,
this, [
this, childId]
160 emit showChildAlgorithmLog( childId );
163 addCommentItemForComponent( model, it.value(), item );
167 for (
auto it = childAlgs.constBegin(); it != childAlgs.constEnd(); ++it )
171 if ( !it.value().algorithm() )
179 QList< QgsProcessingModelChildParameterSource > sources;
180 if ( it.value().parameterSources().contains( parameter->name() ) )
181 sources = it.value().parameterSources()[parameter->name()];
182 for (
const QgsProcessingModelChildParameterSource &source : std::as_const( sources ) )
184 const QList< LinkSource > sourceItems = linkSourcesForParameterValue( model, QVariant::fromValue( source ), it.value().childId(), context );
185 for (
const LinkSource &link : sourceItems )
189 QgsModelArrowItem *arrow =
nullptr;
190 if ( link.linkIndex == -1 )
191 arrow =
new QgsModelArrowItem( link.item, QgsModelArrowItem::Marker::Circle, mChildAlgorithmItems.value( it.value().childId() ), parameter->isDestination() ? Qt::BottomEdge : Qt::TopEdge, parameter->isDestination() ? bottomIdx : topIdx, QgsModelArrowItem::Marker::Circle );
193 arrow =
new QgsModelArrowItem( link.item, link.edge, link.linkIndex,
true, QgsModelArrowItem::Marker::Circle,
194 mChildAlgorithmItems.value( it.value().childId() ),
195 parameter->isDestination() ? Qt::BottomEdge : Qt::TopEdge,
196 parameter->isDestination() ? bottomIdx : topIdx,
198 QgsModelArrowItem::Marker::Circle );
202 if ( parameter->isDestination() )
208 const QList< QgsProcessingModelChildDependency > dependencies = it.value().dependencies();
209 for (
const QgsProcessingModelChildDependency &depend : dependencies )
211 if ( depend.conditionalBranch.isEmpty() || !model->childAlgorithm( depend.childId ).algorithm() )
213 addItem(
new QgsModelArrowItem( mChildAlgorithmItems.value( depend.childId ), QgsModelArrowItem::Marker::Circle, mChildAlgorithmItems.value( it.value().childId() ), QgsModelArrowItem::Marker::ArrowHead ) );
223 if ( output->name() == depend.conditionalBranch )
231 addItem(
new QgsModelArrowItem( mChildAlgorithmItems.value( depend.childId ), Qt::BottomEdge, i, QgsModelArrowItem::Marker::Circle, mChildAlgorithmItems.value( it.value().childId() ), QgsModelArrowItem::Marker::ArrowHead ) );
237 for (
auto it = childAlgs.constBegin(); it != childAlgs.constEnd(); ++it )
239 const QMap<QString, QgsProcessingModelOutput> outputs = it.value().modelOutputs();
240 QMap< QString, QgsModelComponentGraphicItem * > outputItems;
244 QgsProcessingModelComponent *algItem = mChildAlgorithmItems[it.value().childId()]->component();
245 const double outputOffsetX = algItem->size().width();
246 double outputOffsetY = 1.5 * algItem->size().height();
248 for (
auto outputIt = outputs.constBegin(); outputIt != outputs.constEnd(); ++outputIt )
250 QgsModelComponentGraphicItem *item = createOutputGraphicItem( model, outputIt.value().clone() );
252 connect( item, &QgsModelComponentGraphicItem::requestModelRepaint,
this, &QgsModelGraphicsScene::rebuildRequired );
253 connect( item, &QgsModelComponentGraphicItem::changed,
this, &QgsModelGraphicsScene::componentChanged );
254 connect( item, &QgsModelComponentGraphicItem::aboutToChange,
this, &QgsModelGraphicsScene::componentAboutToChange );
259 QPointF pos = outputIt.value().position();
262 pos = algItem->position() + QPointF( outputOffsetX, outputOffsetY );
263 outputOffsetY += 1.5 * outputIt.value().size().height();
268 if ( it.value().algorithm() )
273 if ( childAlgOutput->name() == outputIt.value().childOutputName() )
283 item->component()->setPosition( pos );
284 outputItems.insert( outputIt.key(), item );
285 addItem(
new QgsModelArrowItem( mChildAlgorithmItems[it.value().childId()], Qt::BottomEdge, idx, QgsModelArrowItem::Marker::Circle, item, QgsModelArrowItem::Marker::Circle ) );
287 addCommentItemForComponent( model, outputIt.value(), item );
289 mOutputItems.insert( it.value().childId(), outputItems );
293QList<QgsModelComponentGraphicItem *> QgsModelGraphicsScene::selectedComponentItems()
295 QList<QgsModelComponentGraphicItem *> componentItemList;
297 const QList<QGraphicsItem *> graphicsItemList = selectedItems();
298 for ( QGraphicsItem *item : graphicsItemList )
300 if ( QgsModelComponentGraphicItem *componentItem =
dynamic_cast<QgsModelComponentGraphicItem *
>( item ) )
302 componentItemList.push_back( componentItem );
306 return componentItemList;
309QgsModelComponentGraphicItem *QgsModelGraphicsScene::componentItemAt( QPointF position )
const
312 const QList<QGraphicsItem *> itemList = items( position, Qt::IntersectsItemShape, Qt::DescendingOrder );
314 for ( QGraphicsItem *graphicsItem : itemList )
316 if ( QgsModelComponentGraphicItem *componentItem =
dynamic_cast<QgsModelComponentGraphicItem *
>( graphicsItem ) )
318 return componentItem;
324QgsModelComponentGraphicItem *QgsModelGraphicsScene::groupBoxItem(
const QString &uuid )
326 return mGroupBoxItems.value( uuid );
329void QgsModelGraphicsScene::selectAll()
332 QgsModelComponentGraphicItem *focusedItem =
nullptr;
333 const QList<QGraphicsItem *> itemList = items();
334 for ( QGraphicsItem *graphicsItem : itemList )
336 if ( QgsModelComponentGraphicItem *componentItem =
dynamic_cast<QgsModelComponentGraphicItem *
>( graphicsItem ) )
338 componentItem->setSelected(
true );
340 focusedItem = componentItem;
343 emit selectedItemChanged( focusedItem );
346void QgsModelGraphicsScene::deselectAll()
351 const QList<QGraphicsItem *> selectedItemList = selectedItems();
352 for ( QGraphicsItem *item : selectedItemList )
354 if ( QgsModelComponentGraphicItem *componentItem =
dynamic_cast<QgsModelComponentGraphicItem *
>( item ) )
356 componentItem->setSelected(
false );
359 emit selectedItemChanged(
nullptr );
362void QgsModelGraphicsScene::setSelectedItem( QgsModelComponentGraphicItem *item )
367 item->setSelected(
true );
369 emit selectedItemChanged( item );
374 mLastResult = result;
377 for (
auto it = childResults.constBegin(); it != childResults.constEnd(); ++it )
379 if ( QgsModelChildAlgorithmGraphicItem *item = mChildAlgorithmItems.value( it.key() ) )
381 item->setResults( it.value() );
386QList<QgsModelGraphicsScene::LinkSource> QgsModelGraphicsScene::linkSourcesForParameterValue( QgsProcessingModelAlgorithm *model,
const QVariant &value,
const QString &childId,
QgsProcessingContext &context )
const
388 QList<QgsModelGraphicsScene::LinkSource> res;
389 if ( value.userType() == QMetaType::Type::QVariantList )
391 const QVariantList list = value.toList();
392 for (
const QVariant &v : list )
393 res.append( linkSourcesForParameterValue( model, v, childId, context ) );
395 else if ( value.userType() == QMetaType::Type::QStringList )
397 const QStringList list = value.toStringList();
398 for (
const QString &v : list )
399 res.append( linkSourcesForParameterValue( model, v, childId, context ) );
401 else if ( value.userType() == qMetaTypeId<QgsProcessingModelChildParameterSource>() )
403 const QgsProcessingModelChildParameterSource source = value.value< QgsProcessingModelChildParameterSource >();
404 switch ( source.source() )
409 l.item = mParameterItems.value( source.parameterName() );
415 if ( !model->childAlgorithm( source.outputChildId() ).algorithm() )
422 if ( output->name() == source.outputName() )
426 if ( mChildAlgorithmItems.contains( source.outputChildId() ) )
429 l.item = mChildAlgorithmItems.value( source.outputChildId() );
430 l.edge = Qt::BottomEdge;
433 if ( i >= model->childAlgorithm( source.outputChildId() ).algorithm()->outputDefinitions().length() )
435 QString short_message = tr(
"Check output links for alg: %1" ).arg( model->childAlgorithm( source.outputChildId() ).algorithm()->name() );
436 QString long_message = tr(
"Cannot link output for alg: %1" ).arg( model->childAlgorithm( source.outputChildId() ).algorithm()->name() );
437 QString title( tr(
"Algorithm link error" ) );
439 showWarning(
const_cast<QString &
>( short_message ),
const_cast<QString &
>( title ),
const_cast<QString &
>( long_message ) );
454 const QMap<QString, QgsProcessingModelAlgorithm::VariableDefinition> variables = model->variablesForChildAlgorithm( childId, &context );
456 const QSet<QString> vars = exp.referencedVariables();
457 for (
const QString &v : vars )
459 if ( variables.contains( v ) )
461 res.append( linkSourcesForParameterValue( model, QVariant::fromValue( variables.value( v ).source ), childId, context ) );
476void QgsModelGraphicsScene::addCommentItemForComponent( QgsProcessingModelAlgorithm *model,
const QgsProcessingModelComponent &component, QgsModelComponentGraphicItem *parentItem )
478 if ( mFlags & FlagHideComments || !component.comment() || component.comment()->description().isEmpty() )
481 QgsModelComponentGraphicItem *commentItem = createCommentGraphicItem( model, component.comment()->clone(), parentItem );
482 commentItem->setPos( component.comment()->position().x(), component.comment()->position().y() );
483 addItem( commentItem );
484 connect( commentItem, &QgsModelComponentGraphicItem::requestModelRepaint,
this, &QgsModelGraphicsScene::rebuildRequired );
485 connect( commentItem, &QgsModelComponentGraphicItem::changed,
this, &QgsModelGraphicsScene::componentChanged );
486 connect( commentItem, &QgsModelComponentGraphicItem::aboutToChange,
this, &QgsModelGraphicsScene::componentAboutToChange );
488 std::unique_ptr< QgsModelArrowItem > arrow = std::make_unique< QgsModelArrowItem >( parentItem, QgsModelArrowItem::Circle, commentItem, QgsModelArrowItem::Circle );
489 arrow->setPenStyle( Qt::DotLine );
490 addItem( arrow.release() );
498void QgsModelGraphicsScene::setMessageBar(
QgsMessageBar *messageBar )
500 mMessageBar = messageBar;
503void QgsModelGraphicsScene::showWarning(
const QString &shortMessage,
const QString &title,
const QString &longMessage,
Qgis::MessageLevel level )
const
506 QPushButton *detailsButton =
new QPushButton( tr(
"Details" ) );
507 connect( detailsButton, &QPushButton::clicked, detailsButton, [ = ]
514 messageWidget->layout()->addWidget( detailsButton );
515 mMessageBar->clearWidgets();
516 mMessageBar->pushWidget( messageWidget, level, 0 );
MessageLevel
Level for messages This will be used both for message log and message bar in application.
@ Warning
Warning message.
@ ExpressionText
Parameter value is taken from a text with expressions, evaluated just before the algorithm runs.
@ ModelOutput
Parameter value is linked to an output parameter for the model.
@ ChildOutput
Parameter value is taken from an output generated by a child algorithm.
@ ModelParameter
Parameter value is taken from a parent model parameter.
@ StaticValue
Parameter value is a static value.
@ Expression
Parameter value is taken from an expression, evaluated just before the algorithm runs.
@ Hidden
Parameter is hidden and should not be shown to users.
Class for parsing and evaluation of expressions (formerly called "search strings").
Represents an item shown within a QgsMessageBar widget.
A bar for displaying non-blocking messages to the user.
static QgsMessageBarItem * createMessage(const QString &text, QWidget *parent=nullptr)
Creates message bar item widget containing a message text to be displayed on the bar.
static void logMessage(const QString &message, const QString &tag=QString(), Qgis::MessageLevel level=Qgis::MessageLevel::Warning, bool notifyUser=true)
Adds a message to the log instance (and creates it if necessary).
A generic message view for displaying QGIS messages.
void setTitle(const QString &title) override
Sets title for the messages.
void setMessage(const QString &message, MessageType msgType) override
Sets message, it won't be displayed until.
void showMessage(bool blocking=true) override
display the message to the user and deletes itself
QgsProcessingOutputDefinitions outputDefinitions() const
Returns an ordered list of output definitions utilized by the algorithm.
Contains information about the context in which a processing algorithm is executed.
Encapsulates the results of running a Processing model.
QMap< QString, QgsProcessingModelChildAlgorithmResult > childResults() const
Returns the map of child algorithm results.
Base class for the definition of processing outputs.
Base class for the definition of processing parameters.
virtual QStringList dependsOnOtherParameters() const
Returns a list of other parameter names on which this parameter is dependent (e.g.
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
QgsSignalBlocker< Object > whileBlocking(Object *object)
Temporarily blocks signals from a QObject while calling a single method from the object.
QList< const QgsProcessingOutputDefinition * > QgsProcessingOutputDefinitions
List of processing parameters.
QList< const QgsProcessingParameterDefinition * > QgsProcessingParameterDefinitions
List of processing parameters.