30#include <QDragEnterEvent>
32#include <QApplication>
39#define MIN_VIEW_SCALE 0.05
40#define MAX_VIEW_SCALE 1000.0
42QgsModelGraphicsView::QgsModelGraphicsView( QWidget *parent )
43 : QGraphicsView( parent )
45 setResizeAnchor( QGraphicsView::AnchorViewCenter );
46 setMouseTracking(
true );
47 viewport()->setMouseTracking(
true );
48 setAcceptDrops(
true );
54 mSnapper.setSnapToGrid(
true );
57QgsModelGraphicsView::~QgsModelGraphicsView()
62void QgsModelGraphicsView::dragEnterEvent( QDragEnterEvent *event )
64 if ( event->mimeData()->hasText() || event->mimeData()->hasFormat( QStringLiteral(
"application/x-vnd.qgis.qgis.algorithmid" ) ) )
65 event->acceptProposedAction();
70void 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 );
101void QgsModelGraphicsView::dragMoveEvent( QDragMoveEvent *event )
103 if ( event->mimeData()->hasText() || event->mimeData()->hasFormat( QStringLiteral(
"application/x-vnd.qgis.qgis.algorithmid" ) ) )
104 event->acceptProposedAction();
109void QgsModelGraphicsView::wheelEvent( QWheelEvent *event )
116 mTool->wheelEvent( event );
119 if ( !mTool || !event->isAccepted() )
126void 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->position().x(), event->position().y() );
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 );
169void QgsModelGraphicsView::scaleSafe(
double scale )
171 double currentScale = transform().m11();
172 scale *= currentScale;
174 setTransform( QTransform::fromScale( scale, scale ) );
177QPointF 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 );
219void QgsModelGraphicsView::mousePressEvent( QMouseEvent *event )
227 mTool->modelPressEvent( me.get() );
228 event->setAccepted( me->isAccepted() );
231 if ( !mTool || !event->isAccepted() )
233 if ( event->button() == Qt::MiddleButton )
236 setTool( mMidMouseButtonPanTool );
241 QGraphicsView::mousePressEvent( event );
246void QgsModelGraphicsView::mouseReleaseEvent( QMouseEvent *event )
254 mTool->modelReleaseEvent( me.get() );
255 event->setAccepted( me->isAccepted() );
258 if ( !mTool || !event->isAccepted() )
259 QGraphicsView::mouseReleaseEvent( event );
262void 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 );
302void QgsModelGraphicsView::mouseDoubleClickEvent( QMouseEvent *event )
310 mTool->modelDoubleClickEvent( me.get() );
311 event->setAccepted( me->isAccepted() );
314 if ( !mTool || !event->isAccepted() )
315 QGraphicsView::mouseDoubleClickEvent( event );
318void 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();
367void QgsModelGraphicsView::keyReleaseEvent( QKeyEvent *event )
374 mTool->keyReleaseEvent( event );
377 if ( !mTool || !event->isAccepted() )
378 QGraphicsView::keyReleaseEvent( event );
381void QgsModelGraphicsView::setModelScene( QgsModelGraphicsScene *scene )
387 mSnapMarker =
new QgsModelViewSnapMarker();
389 scene->addItem( mSnapMarker );
392QgsModelGraphicsScene *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 );
436void QgsModelGraphicsView::startMacroCommand(
const QString &text )
438 emit macroCommandStarted( text );
441void QgsModelGraphicsView::endMacroCommand()
443 emit macroCommandEnded();
446void 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 );
469void QgsModelGraphicsView::copySelectedItems( QgsModelGraphicsView::ClipboardOperation operation )
471 copyItems( modelScene()->selectedComponentItems(), operation );
474void 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 );
604void QgsModelGraphicsView::pasteItems( QgsModelGraphicsView::PasteMode mode )
610 QClipboard *clipboard = QApplication::clipboard();
611 if ( doc.setContent( clipboard->mimeData()->data( QStringLiteral(
"text/xml" ) ) ) )
613 QDomElement docElem = doc.documentElement();
616 if ( res.contains( QStringLiteral(
"parameters" ) ) && res.contains( QStringLiteral(
"algs" ) ) )
621 case PasteModeCursor:
622 case PasteModeInPlace:
625 pt = mapToScene( mapFromGlobal( QCursor::pos() ) );
628 case PasteModeCenter:
631 pt = mapToScene( viewport()->rect().center() );
636 emit beginCommand( tr(
"Paste Items" ) );
640 QList< QgsProcessingModelGroupBox > pastedGroups;
641 for (
const QVariant &v : res.value( QStringLiteral(
"groupboxes" ) ).toList() )
643 QgsProcessingModelGroupBox box;
645 box.loadVariant( v.toMap(),
true );
649 modelScene()->model()->addGroupBox( box );
651 if ( !pastedBounds.isValid( ) )
652 pastedBounds = QRectF( box.position() - QPointF( box.size().width() / 2.0, box.size().height() / 2.0 ), box.size() );
654 pastedBounds = pastedBounds.united( QRectF( box.position() - QPointF( box.size().width() / 2.0, box.size().height() / 2.0 ), box.size() ) );
657 QStringList pastedParameters;
658 for (
const QVariant &v : res.value( QStringLiteral(
"parameters" ) ).toList() )
660 QVariantMap param = v.toMap();
661 QVariantMap componentDef = param.value( QStringLiteral(
"component" ) ).toMap();
662 QVariantMap paramDef = param.value( QStringLiteral(
"definition" ) ).toMap();
666 QgsProcessingModelParameter p;
667 p.loadVariant( componentDef );
670 QString name = p.parameterName();
671 QString description = paramDefinition->description();
673 while ( modelScene()->model()->parameterDefinition( name ) )
676 name = QStringLiteral(
"%1 (%2)" ).arg( p.parameterName() ).arg( next );
677 description = QStringLiteral(
"%1 (%2)" ).arg( paramDefinition->description() ).arg( next );
679 paramDefinition->setName( name );
680 paramDefinition->setDescription( description );
681 p.setParameterName( name );
683 modelScene()->model()->addModelParameter( paramDefinition.release(), p );
684 pastedParameters << p.parameterName();
686 if ( !pastedBounds.isValid( ) )
687 pastedBounds = QRectF( p.position() - QPointF( p.size().width() / 2.0, p.size().height() / 2.0 ), p.size() );
689 pastedBounds = pastedBounds.united( QRectF( p.position() - QPointF( p.size().width() / 2.0, p.size().height() / 2.0 ), p.size() ) );
691 if ( !p.comment()->description().isEmpty() )
692 pastedBounds = pastedBounds.united( QRectF( p.comment()->position() - QPointF( p.comment()->size().width() / 2.0, p.comment()->size().height() / 2.0 ), p.comment()->size() ) );
695 QStringList pastedAlgorithms;
696 for (
const QVariant &v : res.value( QStringLiteral(
"algs" ) ).toList() )
698 QgsProcessingModelChildAlgorithm alg;
699 alg.loadVariant( v.toMap() );
702 if ( modelScene()->model()->childAlgorithms().contains( alg.childId() ) )
704 alg.generateChildId( *modelScene()->model() );
708 pastedAlgorithms << alg.childId();
710 if ( !pastedBounds.isValid( ) )
711 pastedBounds = QRectF( alg.position() - QPointF( alg.size().width() / 2.0, alg.size().height() / 2.0 ), alg.size() );
713 pastedBounds = pastedBounds.united( QRectF( alg.position() - QPointF( alg.size().width() / 2.0, alg.size().height() / 2.0 ), alg.size() ) );
715 if ( !alg.comment()->description().isEmpty() )
716 pastedBounds = pastedBounds.united( QRectF( alg.comment()->position() - QPointF( alg.comment()->size().width() / 2.0, alg.comment()->size().height() / 2.0 ), alg.comment()->size() ) );
718 const QMap<QString, QgsProcessingModelChildAlgorithm> existingAlgs = modelScene()->model()->childAlgorithms();
720 const QMap<QString, QgsProcessingModelOutput> outputs = alg.modelOutputs();
721 QMap<QString, QgsProcessingModelOutput> pastedOutputs;
722 for (
auto it = outputs.constBegin(); it != outputs.constEnd(); ++it )
724 QString name = it.value().name();
730 for (
auto algIt = existingAlgs.constBegin(); algIt != existingAlgs.constEnd(); ++algIt )
732 const QMap<QString, QgsProcessingModelOutput> algOutputs = algIt->modelOutputs();
733 for (
auto outputIt = algOutputs.constBegin(); outputIt != algOutputs.constEnd(); ++outputIt )
735 if ( outputIt.value().name() == name )
747 name = QStringLiteral(
"%1 (%2)" ).arg( it.value().name() ).arg( next );
750 QgsProcessingModelOutput newOutput = it.value();
751 newOutput.setName( name );
752 newOutput.setDescription( name );
753 pastedOutputs.insert( name, newOutput );
755 pastedBounds = pastedBounds.united( QRectF( newOutput.position() - QPointF( newOutput.size().width() / 2.0, newOutput.size().height() / 2.0 ), newOutput.size() ) );
757 if ( !alg.comment()->description().isEmpty() )
758 pastedBounds = pastedBounds.united( QRectF( newOutput.comment()->position() - QPointF( newOutput.comment()->size().width() / 2.0, newOutput.comment()->size().height() / 2.0 ), newOutput.comment()->size() ) );
760 alg.setModelOutputs( pastedOutputs );
762 modelScene()->model()->addChildAlgorithm( alg );
765 QPointF offset( 0, 0 );
768 case PasteModeInPlace:
771 case PasteModeCursor:
772 case PasteModeCenter:
774 offset = pt - pastedBounds.topLeft();
779 if ( !offset.isNull() )
781 for ( QgsProcessingModelGroupBox pastedGroup : std::as_const( pastedGroups ) )
783 pastedGroup.setPosition( pastedGroup.position() + offset );
784 modelScene()->model()->addGroupBox( pastedGroup );
786 for (
const QString &pastedParam : std::as_const( pastedParameters ) )
788 modelScene()->model()->parameterComponent( pastedParam ).setPosition( modelScene()->model()->parameterComponent( pastedParam ).position() + offset );
789 modelScene()->model()->parameterComponent( pastedParam ).comment()->setPosition( modelScene()->model()->parameterComponent( pastedParam ).comment()->position() + offset );
791 for (
const QString &pastedAlg : std::as_const( pastedAlgorithms ) )
793 modelScene()->model()->childAlgorithm( pastedAlg ).setPosition( modelScene()->model()->childAlgorithm( pastedAlg ).position() + offset );
794 modelScene()->model()->childAlgorithm( pastedAlg ).comment()->setPosition( modelScene()->model()->childAlgorithm( pastedAlg ).comment()->position() + offset );
796 const QMap<QString, QgsProcessingModelOutput> outputs = modelScene()->model()->childAlgorithm( pastedAlg ).modelOutputs();
797 for (
auto it = outputs.begin(); it != outputs.end(); ++it )
799 modelScene()->model()->childAlgorithm( pastedAlg ).modelOutput( it.key() ).setPosition( modelScene()->model()->childAlgorithm( pastedAlg ).modelOutput( it.key() ).position() + offset );
800 modelScene()->model()->childAlgorithm( pastedAlg ).modelOutput( it.key() ).comment()->setPosition( modelScene()->model()->childAlgorithm( pastedAlg ).modelOutput( it.key() ).comment()->position() + offset );
809 modelScene()->rebuildRequired();
812QgsModelViewSnapMarker::QgsModelViewSnapMarker()
813 : QGraphicsRectItem( QRectF( 0, 0, 0, 0 ) )
816 QFontMetrics fm( f );
817 mSize = fm.horizontalAdvance(
'X' );
818 setPen( QPen( Qt::transparent, mSize ) );
820 setFlags( flags() | QGraphicsItem::ItemIgnoresTransformations );
821 setZValue( QgsModelGraphicsScene::ZSnapIndicator );
824void QgsModelViewSnapMarker::paint( QPainter *p,
const QStyleOptionGraphicsItem *, QWidget * )
826 QPen pen( QColor( 255, 0, 0 ) );
829 p->setBrush( Qt::NoBrush );
831 double halfSize = mSize / 2.0;
832 p->drawLine( QLineF( -halfSize, -halfSize, halfSize, halfSize ) );
833 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.