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] {
150 emit runFromChild( childId );
152 connect( item, &QgsModelChildAlgorithmGraphicItem::runSelected,
this, &QgsModelGraphicsScene::runSelected );
153 connect( item, &QgsModelChildAlgorithmGraphicItem::showPreviousResults,
this, [
this, childId] {
154 emit showChildAlgorithmOutputs( childId );
156 connect( item, &QgsModelChildAlgorithmGraphicItem::showLog,
this, [
this, childId] {
157 emit showChildAlgorithmLog( childId );
160 addCommentItemForComponent( model, it.value(), item );
164 for (
auto it = childAlgs.constBegin(); it != childAlgs.constEnd(); ++it )
168 if ( !it.value().algorithm() )
176 QList<QgsProcessingModelChildParameterSource> sources;
177 if ( it.value().parameterSources().contains( parameter->name() ) )
178 sources = it.value().parameterSources()[parameter->name()];
179 for (
const QgsProcessingModelChildParameterSource &source : std::as_const( sources ) )
181 const QList<LinkSource> sourceItems = linkSourcesForParameterValue( model, QVariant::fromValue( source ), it.value().childId(), context );
182 for (
const LinkSource &link : sourceItems )
186 QgsModelArrowItem *arrow =
nullptr;
187 if ( link.linkIndex == -1 )
188 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 );
190 arrow =
new QgsModelArrowItem( link.item, link.edge, link.linkIndex,
true, QgsModelArrowItem::Marker::Circle, mChildAlgorithmItems.value( it.value().childId() ), parameter->isDestination() ? Qt::BottomEdge : Qt::TopEdge, parameter->isDestination() ? bottomIdx : topIdx, true, QgsModelArrowItem::Marker::Circle );
194 if ( parameter->isDestination() )
200 const QList<QgsProcessingModelChildDependency> dependencies = it.value().dependencies();
201 for (
const QgsProcessingModelChildDependency &depend : dependencies )
203 if ( depend.conditionalBranch.isEmpty() || !model->childAlgorithm( depend.childId ).algorithm() )
205 addItem(
new QgsModelArrowItem( mChildAlgorithmItems.value( depend.childId ), QgsModelArrowItem::Marker::Circle, mChildAlgorithmItems.value( it.value().childId() ), QgsModelArrowItem::Marker::ArrowHead ) );
215 if ( output->name() == depend.conditionalBranch )
223 addItem(
new QgsModelArrowItem( mChildAlgorithmItems.value( depend.childId ), Qt::BottomEdge, i, QgsModelArrowItem::Marker::Circle, mChildAlgorithmItems.value( it.value().childId() ), QgsModelArrowItem::Marker::ArrowHead ) );
229 for (
auto it = childAlgs.constBegin(); it != childAlgs.constEnd(); ++it )
231 const QMap<QString, QgsProcessingModelOutput> outputs = it.value().modelOutputs();
232 QMap<QString, QgsModelComponentGraphicItem *> outputItems;
236 QgsProcessingModelComponent *algItem = mChildAlgorithmItems[it.value().childId()]->component();
237 const double outputOffsetX = algItem->size().width();
238 double outputOffsetY = 1.5 * algItem->size().height();
240 for (
auto outputIt = outputs.constBegin(); outputIt != outputs.constEnd(); ++outputIt )
242 QgsModelComponentGraphicItem *item = createOutputGraphicItem( model, outputIt.value().clone() );
244 connect( item, &QgsModelComponentGraphicItem::requestModelRepaint,
this, &QgsModelGraphicsScene::rebuildRequired );
245 connect( item, &QgsModelComponentGraphicItem::changed,
this, &QgsModelGraphicsScene::componentChanged );
246 connect( item, &QgsModelComponentGraphicItem::aboutToChange,
this, &QgsModelGraphicsScene::componentAboutToChange );
251 QPointF pos = outputIt.value().position();
254 pos = algItem->position() + QPointF( outputOffsetX, outputOffsetY );
255 outputOffsetY += 1.5 * outputIt.value().size().height();
260 if ( it.value().algorithm() )
265 if ( childAlgOutput->name() == outputIt.value().childOutputName() )
275 item->component()->setPosition( pos );
276 outputItems.insert( outputIt.key(), item );
277 addItem(
new QgsModelArrowItem( mChildAlgorithmItems[it.value().childId()], Qt::BottomEdge, idx, QgsModelArrowItem::Marker::Circle, item, QgsModelArrowItem::Marker::Circle ) );
279 addCommentItemForComponent( model, outputIt.value(), item );
281 mOutputItems.insert( it.value().childId(), outputItems );
285QList<QgsModelComponentGraphicItem *> QgsModelGraphicsScene::selectedComponentItems()
287 QList<QgsModelComponentGraphicItem *> componentItemList;
289 const QList<QGraphicsItem *> graphicsItemList = selectedItems();
290 for ( QGraphicsItem *item : graphicsItemList )
292 if ( QgsModelComponentGraphicItem *componentItem =
dynamic_cast<QgsModelComponentGraphicItem *
>( item ) )
294 componentItemList.push_back( componentItem );
298 return componentItemList;
301QgsModelComponentGraphicItem *QgsModelGraphicsScene::componentItemAt( QPointF position )
const
304 const QList<QGraphicsItem *> itemList = items( position, Qt::IntersectsItemShape, Qt::DescendingOrder );
306 for ( QGraphicsItem *graphicsItem : itemList )
308 if ( QgsModelComponentGraphicItem *componentItem =
dynamic_cast<QgsModelComponentGraphicItem *
>( graphicsItem ) )
310 return componentItem;
316QgsModelComponentGraphicItem *QgsModelGraphicsScene::groupBoxItem(
const QString &uuid )
318 return mGroupBoxItems.value( uuid );
321void QgsModelGraphicsScene::selectAll()
324 QgsModelComponentGraphicItem *focusedItem =
nullptr;
325 const QList<QGraphicsItem *> itemList = items();
326 for ( QGraphicsItem *graphicsItem : itemList )
328 if ( QgsModelComponentGraphicItem *componentItem =
dynamic_cast<QgsModelComponentGraphicItem *
>( graphicsItem ) )
330 componentItem->setSelected(
true );
332 focusedItem = componentItem;
335 emit selectedItemChanged( focusedItem );
338void QgsModelGraphicsScene::deselectAll()
343 const QList<QGraphicsItem *> selectedItemList = selectedItems();
344 for ( QGraphicsItem *item : selectedItemList )
346 if ( QgsModelComponentGraphicItem *componentItem =
dynamic_cast<QgsModelComponentGraphicItem *
>( item ) )
348 componentItem->setSelected(
false );
351 emit selectedItemChanged(
nullptr );
354void QgsModelGraphicsScene::setSelectedItem( QgsModelComponentGraphicItem *item )
359 item->setSelected(
true );
361 emit selectedItemChanged( item );
366 mLastResult = result;
369 for (
auto it = childResults.constBegin(); it != childResults.constEnd(); ++it )
371 if ( QgsModelChildAlgorithmGraphicItem *item = mChildAlgorithmItems.value( it.key() ) )
373 item->setResults( it.value() );
378QList<QgsModelGraphicsScene::LinkSource> QgsModelGraphicsScene::linkSourcesForParameterValue( QgsProcessingModelAlgorithm *model,
const QVariant &value,
const QString &childId,
QgsProcessingContext &context )
const
380 QList<QgsModelGraphicsScene::LinkSource> res;
381 if ( value.userType() == QMetaType::Type::QVariantList )
383 const QVariantList list = value.toList();
384 for (
const QVariant &v : list )
385 res.append( linkSourcesForParameterValue( model, v, childId, context ) );
387 else if ( value.userType() == QMetaType::Type::QStringList )
389 const QStringList list = value.toStringList();
390 for (
const QString &v : list )
391 res.append( linkSourcesForParameterValue( model, v, childId, context ) );
393 else if ( value.userType() == qMetaTypeId<QgsProcessingModelChildParameterSource>() )
395 const QgsProcessingModelChildParameterSource source = value.value<QgsProcessingModelChildParameterSource>();
396 switch ( source.source() )
401 l.item = mParameterItems.value( source.parameterName() );
407 if ( !model->childAlgorithm( source.outputChildId() ).algorithm() )
414 if ( output->name() == source.outputName() )
418 if ( mChildAlgorithmItems.contains( source.outputChildId() ) )
421 l.item = mChildAlgorithmItems.value( source.outputChildId() );
422 l.edge = Qt::BottomEdge;
425 if ( i >= model->childAlgorithm( source.outputChildId() ).algorithm()->outputDefinitions().length() )
427 QString short_message = tr(
"Check output links for alg: %1" ).arg( model->childAlgorithm( source.outputChildId() ).algorithm()->name() );
428 QString long_message = tr(
"Cannot link output for alg: %1" ).arg( model->childAlgorithm( source.outputChildId() ).algorithm()->name() );
429 QString title( tr(
"Algorithm link error" ) );
431 showWarning(
const_cast<QString &
>( short_message ),
const_cast<QString &
>( title ),
const_cast<QString &
>( long_message ) );
446 const QMap<QString, QgsProcessingModelAlgorithm::VariableDefinition> variables = model->variablesForChildAlgorithm( childId, &context );
448 const QSet<QString> vars = exp.referencedVariables();
449 for (
const QString &v : vars )
451 if ( variables.contains( v ) )
453 res.append( linkSourcesForParameterValue( model, QVariant::fromValue( variables.value( v ).source ), childId, context ) );
468void QgsModelGraphicsScene::addCommentItemForComponent( QgsProcessingModelAlgorithm *model,
const QgsProcessingModelComponent &component, QgsModelComponentGraphicItem *parentItem )
470 if ( mFlags & FlagHideComments || !component.comment() || component.comment()->description().isEmpty() )
473 QgsModelComponentGraphicItem *commentItem = createCommentGraphicItem( model, component.comment()->clone(), parentItem );
474 commentItem->setPos( component.comment()->position().x(), component.comment()->position().y() );
475 addItem( commentItem );
476 connect( commentItem, &QgsModelComponentGraphicItem::requestModelRepaint,
this, &QgsModelGraphicsScene::rebuildRequired );
477 connect( commentItem, &QgsModelComponentGraphicItem::changed,
this, &QgsModelGraphicsScene::componentChanged );
478 connect( commentItem, &QgsModelComponentGraphicItem::aboutToChange,
this, &QgsModelGraphicsScene::componentAboutToChange );
480 std::unique_ptr<QgsModelArrowItem> arrow = std::make_unique<QgsModelArrowItem>( parentItem, QgsModelArrowItem::Circle, commentItem, QgsModelArrowItem::Circle );
481 arrow->setPenStyle( Qt::DotLine );
482 addItem( arrow.release() );
490void QgsModelGraphicsScene::setMessageBar(
QgsMessageBar *messageBar )
492 mMessageBar = messageBar;
495void QgsModelGraphicsScene::showWarning(
const QString &shortMessage,
const QString &title,
const QString &longMessage,
Qgis::MessageLevel level )
const
498 QPushButton *detailsButton =
new QPushButton( tr(
"Details" ) );
499 connect( detailsButton, &QPushButton::clicked, detailsButton, [=] {
505 messageWidget->layout()->addWidget( detailsButton );
506 mMessageBar->clearWidgets();
507 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.