25 #include "qgsprocessingmodelcomponent.h"
26 #include "qgsprocessingmodelparameter.h"
27 #include "qgsprocessingmodelchildalgorithm.h"
29 #include "qgsprocessingmodelalgorithm.h"
30 #include <QDragEnterEvent>
32 #include <QApplication>
39 #define MIN_VIEW_SCALE 0.05
40 #define MAX_VIEW_SCALE 1000.0
42 QgsModelGraphicsView::QgsModelGraphicsView( QWidget *parent )
43 : QGraphicsView( parent )
45 setResizeAnchor( QGraphicsView::AnchorViewCenter );
46 setMouseTracking(
true );
47 viewport()->setMouseTracking(
true );
48 setAcceptDrops(
true );
54 mSnapper.setSnapToGrid(
true );
57 QgsModelGraphicsView::~QgsModelGraphicsView()
62 void QgsModelGraphicsView::dragEnterEvent( QDragEnterEvent *event )
64 if ( event->mimeData()->hasText() || event->mimeData()->hasFormat( QStringLiteral(
"application/x-vnd.qgis.qgis.algorithmid" ) ) )
65 event->acceptProposedAction();
70 void QgsModelGraphicsView::dropEvent( QDropEvent *event )
72 const QPointF dropPoint = mapToScene( event->pos() );
73 if ( event->mimeData()->hasFormat( QStringLiteral(
"application/x-vnd.qgis.qgis.algorithmid" ) ) )
75 QByteArray data =
event->mimeData()->data( QStringLiteral(
"application/x-vnd.qgis.qgis.algorithmid" ) );
76 QDataStream stream( &data, QIODevice::ReadOnly );
78 stream >> algorithmId;
80 QTimer::singleShot( 0,
this, [
this, dropPoint, algorithmId ]
82 emit algorithmDropped( algorithmId, dropPoint );
86 else if ( event->mimeData()->hasText() )
88 const QString itemId =
event->mimeData()->text();
89 QTimer::singleShot( 0,
this, [
this, dropPoint, itemId ]
91 emit inputDropped( itemId, dropPoint );
101 void QgsModelGraphicsView::dragMoveEvent( QDragMoveEvent *event )
103 if ( event->mimeData()->hasText() || event->mimeData()->hasFormat( QStringLiteral(
"application/x-vnd.qgis.qgis.algorithmid" ) ) )
104 event->acceptProposedAction();
109 void QgsModelGraphicsView::wheelEvent( QWheelEvent *event )
116 mTool->wheelEvent( event );
119 if ( !mTool || !event->isAccepted() )
126 void QgsModelGraphicsView::wheelZoom( QWheelEvent *event )
130 double zoomFactor = settings.
value( QStringLiteral(
"qgis/zoom_factor" ), 2 ).toDouble();
133 zoomFactor = 1.0 + ( zoomFactor - 1.0 ) / 120.0 * std::fabs( event->angleDelta().y() );
135 if ( event->modifiers() & Qt::ControlModifier )
138 zoomFactor = 1.0 + ( zoomFactor - 1.0 ) / 20.0;
142 bool zoomIn =
event->angleDelta().y() > 0;
143 double scaleFactor = ( zoomIn ? 1 / zoomFactor : zoomFactor );
146 QRect viewportRect( 0, 0, viewport()->width(), viewport()->height() );
150 QPointF scenePoint = mapToScene( event->pos() );
154 QgsPointXY newCenter( scenePoint.x() + ( ( oldCenter.x() - scenePoint.x() ) * scaleFactor ),
155 scenePoint.y() + ( ( oldCenter.y() - scenePoint.y() ) * scaleFactor ) );
156 centerOn( newCenter.x(), newCenter.y() );
161 scaleSafe( zoomFactor );
165 scaleSafe( 1 / zoomFactor );
169 void QgsModelGraphicsView::scaleSafe(
double scale )
171 double currentScale = transform().m11();
172 scale *= currentScale;
174 setTransform( QTransform::fromScale( scale, scale ) );
177 QPointF QgsModelGraphicsView::deltaForKeyEvent( QKeyEvent *event )
180 double increment = 1.0;
181 if ( event->modifiers() & Qt::ShiftModifier )
186 else if ( event->modifiers() & Qt::AltModifier )
189 double viewScale = transform().m11();
192 increment = 1 / viewScale;
198 switch ( event->key() )
216 return QPointF( deltaX, deltaY );
219 void QgsModelGraphicsView::mousePressEvent( QMouseEvent *event )
227 mTool->modelPressEvent( me.get() );
228 event->setAccepted( me->isAccepted() );
231 if ( !mTool || !event->isAccepted() )
233 if ( event->button() == Qt::MidButton )
236 setTool( mMidMouseButtonPanTool );
241 QGraphicsView::mousePressEvent( event );
246 void QgsModelGraphicsView::mouseReleaseEvent( QMouseEvent *event )
254 mTool->modelReleaseEvent( me.get() );
255 event->setAccepted( me->isAccepted() );
258 if ( !mTool || !event->isAccepted() )
259 QGraphicsView::mouseReleaseEvent( event );
262 void QgsModelGraphicsView::mouseMoveEvent( QMouseEvent *event )
267 mMouseCurrentXY =
event->pos();
269 QPointF cursorPos = mapToScene( mMouseCurrentXY );
280 if ( me->isSnapped() )
282 cursorPos = me->snappedPoint();
285 mSnapMarker->setPos( me->snappedPoint() );
286 mSnapMarker->setVisible(
true );
289 else if ( mSnapMarker )
291 mSnapMarker->setVisible(
false );
294 mTool->modelMoveEvent( me.get() );
295 event->setAccepted( me->isAccepted() );
298 if ( !mTool || !event->isAccepted() )
299 QGraphicsView::mouseMoveEvent( event );
302 void QgsModelGraphicsView::mouseDoubleClickEvent( QMouseEvent *event )
310 mTool->modelDoubleClickEvent( me.get() );
311 event->setAccepted( me->isAccepted() );
314 if ( !mTool || !event->isAccepted() )
315 QGraphicsView::mouseDoubleClickEvent( event );
318 void QgsModelGraphicsView::keyPressEvent( QKeyEvent *event )
325 mTool->keyPressEvent( event );
328 if ( mTool && event->isAccepted() )
331 if ( event->key() == Qt::Key_Space && ! event->isAutoRepeat() )
333 if ( !( event->modifiers() & Qt::ControlModifier ) )
336 setTool( mSpacePanTool );
341 setTool( mSpaceZoomTool );
345 else if ( event->key() == Qt::Key_Left
346 || event->key() == Qt::Key_Right
347 || event->key() == Qt::Key_Up
348 || event->key() == Qt::Key_Down )
350 QgsModelGraphicsScene *s = modelScene();
351 const QList<QgsModelComponentGraphicItem *> itemList = s->selectedComponentItems();
352 if ( !itemList.empty() )
354 QPointF delta = deltaForKeyEvent( event );
356 itemList.at( 0 )->aboutToChange( tr(
"Move Items" ) );
357 for ( QgsModelComponentGraphicItem *item : itemList )
359 item->moveComponentBy( delta.x(), delta.y() );
361 itemList.at( 0 )->changed();
367 void QgsModelGraphicsView::keyReleaseEvent( QKeyEvent *event )
374 mTool->keyReleaseEvent( event );
377 if ( !mTool || !event->isAccepted() )
378 QGraphicsView::keyReleaseEvent( event );
381 void QgsModelGraphicsView::setModelScene( QgsModelGraphicsScene *scene )
387 mSnapMarker =
new QgsModelViewSnapMarker();
389 scene->addItem( mSnapMarker );
392 QgsModelGraphicsScene *QgsModelGraphicsView::modelScene()
const
394 return qobject_cast< QgsModelGraphicsScene * >( QgsModelGraphicsView::scene() );
418 emit toolSet( mTool );
423 if ( mTool && mTool == tool )
426 emit toolSet(
nullptr );
427 setCursor( Qt::ArrowCursor );
436 void QgsModelGraphicsView::startMacroCommand(
const QString &text )
438 emit macroCommandStarted( text );
441 void QgsModelGraphicsView::endMacroCommand()
443 emit macroCommandEnded();
446 void QgsModelGraphicsView::snapSelected()
448 QgsModelGraphicsScene *s = modelScene();
449 const QList<QgsModelComponentGraphicItem *> itemList = s->selectedComponentItems();
450 startMacroCommand( tr(
"Snap Items" ) );
451 if ( !itemList.empty() )
453 bool prevSetting = mSnapper.snapToGrid();
454 mSnapper.setSnapToGrid(
true );
455 for ( QgsModelComponentGraphicItem *item : itemList )
457 bool wasSnapped =
false;
458 QRectF snapped = mSnapper.snapRectWithResize( item->mapRectToScene( item->itemRect( ) ), transform().m11(), wasSnapped );
461 item->setItemRect( snapped );
464 mSnapper.setSnapToGrid( prevSetting );
469 void QgsModelGraphicsView::copySelectedItems( QgsModelGraphicsView::ClipboardOperation operation )
471 copyItems( modelScene()->selectedComponentItems(), operation );
474 void QgsModelGraphicsView::copyItems(
const QList<QgsModelComponentGraphicItem *> &items, QgsModelGraphicsView::ClipboardOperation operation )
481 QDomElement documentElement = doc.createElement( QStringLiteral(
"ModelComponentClipboard" ) );
482 if ( operation == ClipboardCut )
484 emit macroCommandStarted( tr(
"Cut Items" ) );
485 emit beginCommand( QString() );
488 QList< QVariant > paramComponents;
489 QList< QVariant > groupBoxComponents;
490 QList< QVariant > algComponents;
492 QList< QgsModelComponentGraphicItem * > selectedCommentParents;
493 QList< QgsProcessingModelOutput > selectedOutputs;
494 QList< QgsProcessingModelOutput > selectedOutputsComments;
495 for ( QgsModelComponentGraphicItem *item : items )
497 if (
const QgsModelCommentGraphicItem *commentItem =
dynamic_cast< QgsModelCommentGraphicItem *
>( item ) )
499 selectedCommentParents << commentItem->parentComponentItem();
500 if (
const QgsModelOutputGraphicItem *outputItem =
dynamic_cast< QgsModelOutputGraphicItem *
>( commentItem->parentComponentItem() ) )
502 selectedOutputsComments << *( static_cast< const QgsProcessingModelOutput *>( outputItem->component() ) );
505 else if (
const QgsModelOutputGraphicItem *outputItem =
dynamic_cast< QgsModelOutputGraphicItem *
>( item ) )
507 selectedOutputs << *( static_cast< const QgsProcessingModelOutput *>( outputItem->component() ) );
511 for ( QgsModelComponentGraphicItem *item : items )
513 if (
const QgsProcessingModelParameter *param =
dynamic_cast< QgsProcessingModelParameter *
>( item->component() ) )
515 QgsProcessingModelParameter component = *param;
518 if ( !selectedCommentParents.contains( item ) )
521 component.comment()->setDescription( QString() );
524 QVariantMap paramDef;
525 paramDef.insert( QStringLiteral(
"component" ), component.toVariant() );
527 paramDef.insert( QStringLiteral(
"definition" ), def->
toVariantMap() );
529 paramComponents << paramDef;
531 else if ( QgsProcessingModelGroupBox *groupBox =
dynamic_cast< QgsProcessingModelGroupBox *
>( item->component() ) )
533 groupBoxComponents << groupBox->toVariant();
535 else if (
const QgsProcessingModelChildAlgorithm *alg =
dynamic_cast< QgsProcessingModelChildAlgorithm *
>( item->component() ) )
537 QgsProcessingModelChildAlgorithm childAlg = *alg;
540 if ( !selectedCommentParents.contains( item ) )
547 QMap<QString, QgsProcessingModelOutput> clipboardOutputs;
548 const QMap<QString, QgsProcessingModelOutput> existingOutputs = childAlg.modelOutputs();
549 for (
auto it = existingOutputs.constBegin(); it != existingOutputs.constEnd(); ++ it )
552 for (
const QgsProcessingModelOutput &candidate : selectedOutputs )
554 if ( candidate.childId() == childAlg.childId() && candidate.name() == it.value().name() && candidate.childOutputName() == it.value().childOutputName() )
563 bool commentFound =
false;
564 for (
const QgsProcessingModelOutput &candidate : selectedOutputsComments )
566 if ( candidate.childId() == childAlg.childId() && candidate.name() == it.value().name() && candidate.childOutputName() == it.value().childOutputName() )
573 QgsProcessingModelOutput output = it.value();
575 output.comment()->setDescription( QString() );
577 clipboardOutputs.insert( it.key(), output );
580 childAlg.setModelOutputs( clipboardOutputs );
582 algComponents << childAlg.toVariant();
585 QVariantMap components;
586 components.insert( QStringLiteral(
"parameters" ), paramComponents );
587 components.insert( QStringLiteral(
"groupboxes" ), groupBoxComponents );
588 components.insert( QStringLiteral(
"algs" ), algComponents );
590 if ( operation == ClipboardCut )
592 emit deleteSelectedItems();
594 emit macroCommandEnded();
597 QMimeData *mimeData =
new QMimeData;
598 mimeData->setData( QStringLiteral(
"text/xml" ), doc.toByteArray() );
599 mimeData->setText( doc.toByteArray() );
600 QClipboard *clipboard = QApplication::clipboard();
601 clipboard->setMimeData( mimeData );
604 void QgsModelGraphicsView::pasteItems( QgsModelGraphicsView::PasteMode mode )
609 QList< QgsModelComponentGraphicItem * > pastedItems;
611 QClipboard *clipboard = QApplication::clipboard();
612 if ( doc.setContent( clipboard->mimeData()->data( QStringLiteral(
"text/xml" ) ) ) )
614 QDomElement docElem = doc.documentElement();
617 if ( res.contains( QStringLiteral(
"parameters" ) ) && res.contains( QStringLiteral(
"algs" ) ) )
622 case PasteModeCursor:
623 case PasteModeInPlace:
626 pt = mapToScene( mapFromGlobal( QCursor::pos() ) );
629 case PasteModeCenter:
632 pt = mapToScene( viewport()->rect().center() );
637 emit beginCommand( tr(
"Paste Items" ) );
641 QList< QgsProcessingModelGroupBox > pastedGroups;
642 for (
const QVariant &v : res.value( QStringLiteral(
"groupboxes" ) ).toList() )
644 QgsProcessingModelGroupBox box;
646 box.loadVariant( v.toMap(),
true );
650 modelScene()->model()->addGroupBox( box );
652 if ( !pastedBounds.isValid( ) )
653 pastedBounds = QRectF( box.position() - QPointF( box.size().width() / 2.0, box.size().height() / 2.0 ), box.size() );
655 pastedBounds = pastedBounds.united( QRectF( box.position() - QPointF( box.size().width() / 2.0, box.size().height() / 2.0 ), box.size() ) );
658 QStringList pastedParameters;
659 for (
const QVariant &v : res.value( QStringLiteral(
"parameters" ) ).toList() )
661 QVariantMap param = v.toMap();
662 QVariantMap componentDef = param.value( QStringLiteral(
"component" ) ).toMap();
663 QVariantMap paramDef = param.value( QStringLiteral(
"definition" ) ).toMap();
667 QgsProcessingModelParameter p;
668 p.loadVariant( componentDef );
671 QString name = p.parameterName();
672 QString description = paramDefinition->description();
674 while ( modelScene()->model()->parameterDefinition( name ) )
677 name = QStringLiteral(
"%1 (%2)" ).arg( p.parameterName() ).arg( next );
678 description = QStringLiteral(
"%1 (%2)" ).arg( paramDefinition->description() ).arg( next );
680 paramDefinition->setName( name );
681 paramDefinition->setDescription( description );
682 p.setParameterName( name );
684 modelScene()->model()->addModelParameter( paramDefinition.release(), p );
685 pastedParameters << p.parameterName();
687 if ( !pastedBounds.isValid( ) )
688 pastedBounds = QRectF( p.position() - QPointF( p.size().width() / 2.0, p.size().height() / 2.0 ), p.size() );
690 pastedBounds = pastedBounds.united( QRectF( p.position() - QPointF( p.size().width() / 2.0, p.size().height() / 2.0 ), p.size() ) );
692 if ( !p.comment()->description().isEmpty() )
693 pastedBounds = pastedBounds.united( QRectF( p.comment()->position() - QPointF( p.comment()->size().width() / 2.0, p.comment()->size().height() / 2.0 ), p.comment()->size() ) );
696 QStringList pastedAlgorithms;
697 for (
const QVariant &v : res.value( QStringLiteral(
"algs" ) ).toList() )
699 QgsProcessingModelChildAlgorithm alg;
700 alg.loadVariant( v.toMap() );
703 alg.generateChildId( *modelScene()->model() );
706 pastedAlgorithms << alg.childId();
708 if ( !pastedBounds.isValid( ) )
709 pastedBounds = QRectF( alg.position() - QPointF( alg.size().width() / 2.0, alg.size().height() / 2.0 ), alg.size() );
711 pastedBounds = pastedBounds.united( QRectF( alg.position() - QPointF( alg.size().width() / 2.0, alg.size().height() / 2.0 ), alg.size() ) );
713 if ( !alg.comment()->description().isEmpty() )
714 pastedBounds = pastedBounds.united( QRectF( alg.comment()->position() - QPointF( alg.comment()->size().width() / 2.0, alg.comment()->size().height() / 2.0 ), alg.comment()->size() ) );
716 const QMap<QString, QgsProcessingModelChildAlgorithm> existingAlgs = modelScene()->model()->childAlgorithms();
718 const QMap<QString, QgsProcessingModelOutput> outputs = alg.modelOutputs();
719 QMap<QString, QgsProcessingModelOutput> pastedOutputs;
720 for (
auto it = outputs.constBegin(); it != outputs.constEnd(); ++it )
722 QString name = it.value().name();
728 for (
auto algIt = existingAlgs.constBegin(); algIt != existingAlgs.constEnd(); ++algIt )
730 const QMap<QString, QgsProcessingModelOutput> algOutputs = algIt->modelOutputs();
731 for (
auto outputIt = algOutputs.constBegin(); outputIt != algOutputs.constEnd(); ++outputIt )
733 if ( outputIt.value().name() == name )
745 name = QStringLiteral(
"%1 (%2)" ).arg( it.value().name() ).arg( next );
748 QgsProcessingModelOutput newOutput = it.value();
749 newOutput.setName( name );
750 newOutput.setDescription( name );
751 pastedOutputs.insert( name, newOutput );
753 pastedBounds = pastedBounds.united( QRectF( newOutput.position() - QPointF( newOutput.size().width() / 2.0, newOutput.size().height() / 2.0 ), newOutput.size() ) );
755 if ( !alg.comment()->description().isEmpty() )
756 pastedBounds = pastedBounds.united( QRectF( newOutput.comment()->position() - QPointF( newOutput.comment()->size().width() / 2.0, newOutput.comment()->size().height() / 2.0 ), newOutput.comment()->size() ) );
758 alg.setModelOutputs( pastedOutputs );
760 modelScene()->model()->addChildAlgorithm( alg );
763 QPointF offset( 0, 0 );
766 case PasteModeInPlace:
769 case PasteModeCursor:
770 case PasteModeCenter:
772 offset = pt - pastedBounds.topLeft();
777 if ( !offset.isNull() )
779 for ( QgsProcessingModelGroupBox pastedGroup : qgis::as_const( pastedGroups ) )
781 pastedGroup.setPosition( pastedGroup.position() + offset );
782 modelScene()->model()->addGroupBox( pastedGroup );
784 for (
const QString &pastedParam : qgis::as_const( pastedParameters ) )
786 modelScene()->model()->parameterComponent( pastedParam ).setPosition( modelScene()->model()->parameterComponent( pastedParam ).position() + offset );
787 modelScene()->model()->parameterComponent( pastedParam ).comment()->setPosition( modelScene()->model()->parameterComponent( pastedParam ).comment()->position() + offset );
789 for (
const QString &pastedAlg : qgis::as_const( pastedAlgorithms ) )
791 modelScene()->model()->childAlgorithm( pastedAlg ).setPosition( modelScene()->model()->childAlgorithm( pastedAlg ).position() + offset );
792 modelScene()->model()->childAlgorithm( pastedAlg ).comment()->setPosition( modelScene()->model()->childAlgorithm( pastedAlg ).comment()->position() + offset );
794 const QMap<QString, QgsProcessingModelOutput> outputs = modelScene()->model()->childAlgorithm( pastedAlg ).modelOutputs();
795 for (
auto it = outputs.begin(); it != outputs.end(); ++it )
797 modelScene()->model()->childAlgorithm( pastedAlg ).modelOutput( it.key() ).setPosition( modelScene()->model()->childAlgorithm( pastedAlg ).modelOutput( it.key() ).position() + offset );
798 modelScene()->model()->childAlgorithm( pastedAlg ).modelOutput( it.key() ).comment()->setPosition( modelScene()->model()->childAlgorithm( pastedAlg ).modelOutput( it.key() ).comment()->position() + offset );
807 modelScene()->rebuildRequired();
810 QgsModelViewSnapMarker::QgsModelViewSnapMarker()
811 : QGraphicsRectItem( QRectF( 0, 0, 0, 0 ) )
814 QFontMetrics fm( f );
815 #if QT_VERSION < QT_VERSION_CHECK(5, 11, 0)
816 mSize = fm.width( QStringLiteral(
"X" ) );
818 mSize = fm.horizontalAdvance(
'X' );
820 setPen( QPen( Qt::transparent, mSize ) );
822 setFlags( flags() | QGraphicsItem::ItemIgnoresTransformations );
823 setZValue( QgsModelGraphicsScene::ZSnapIndicator );
826 void QgsModelViewSnapMarker::paint( QPainter *p,
const QStyleOptionGraphicsItem *, QWidget * )
828 QPen pen( QColor( 255, 0, 0 ) );
831 p->setBrush( Qt::NoBrush );
833 double halfSize = mSize / 2.0;
834 p->drawLine( QLineF( -halfSize, -halfSize, halfSize, halfSize ) );
835 p->drawLine( QLineF( -halfSize, halfSize, halfSize, -halfSize ) );
Manages snapping grids and preset snap lines in a layout, and handles snapping points to the nearest ...
A QgsModelViewMouseEvent is the result of a user interaction with the mouse on a QgsModelGraphicsView...
A class to represent a 2D point.
Base class for the definition of processing parameters.
void setDescription(const QString &description)
Sets the description for the parameter.
virtual QVariantMap toVariantMap() const
Saves this parameter to a QVariantMap.
static QgsProcessingParameterDefinition * parameterFromVariantMap(const QVariantMap &map)
Creates a new QgsProcessingParameterDefinition using the configuration from a supplied variant map.
The class is used as a container of context for various read/write operations on other objects.
A rectangle specified with double values.
QgsPointXY center() const SIP_HOLDGIL
Returns the center point of the rectangle.
This class is a composition of two QSettings instances:
QVariant value(const QString &key, const QVariant &defaultValue=QVariant(), Section section=NoSection) const
Returns the value for setting key.
static QDomElement writeVariant(const QVariant &value, QDomDocument &doc)
Write a QVariant to a QDomElement.
static QVariant readVariant(const QDomElement &element)
Read a QVariant from a QDomElement.