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();
131 bool reverseZoom = settings.
value( QStringLiteral(
"qgis/reverse_wheel_zoom" ),
false ).toBool();
132 bool zoomIn = reverseZoom ?
event->angleDelta().y() < 0 :
event->angleDelta().y() > 0;
135 zoomFactor = 1.0 + ( zoomFactor - 1.0 ) / 120.0 * std::fabs( event->angleDelta().y() );
137 if ( event->modifiers() & Qt::ControlModifier )
140 zoomFactor = 1.0 + ( zoomFactor - 1.0 ) / 20.0;
144 double scaleFactor = ( zoomIn ? 1 / zoomFactor : zoomFactor );
147 QRect viewportRect( 0, 0, viewport()->width(), viewport()->height() );
151 QPointF scenePoint = mapToScene( event->position().x(), event->position().y() );
155 QgsPointXY newCenter( scenePoint.x() + ( ( oldCenter.x() - scenePoint.x() ) * scaleFactor ),
156 scenePoint.y() + ( ( oldCenter.y() - scenePoint.y() ) * scaleFactor ) );
157 centerOn( newCenter.x(), newCenter.y() );
162 scaleSafe( zoomFactor );
166 scaleSafe( 1 / zoomFactor );
170void QgsModelGraphicsView::scaleSafe(
double scale )
172 double currentScale = transform().m11();
173 scale *= currentScale;
175 setTransform( QTransform::fromScale( scale, scale ) );
178QPointF QgsModelGraphicsView::deltaForKeyEvent( QKeyEvent *event )
181 double increment = 1.0;
182 if ( event->modifiers() & Qt::ShiftModifier )
187 else if ( event->modifiers() & Qt::AltModifier )
190 double viewScale = transform().m11();
193 increment = 1 / viewScale;
199 switch ( event->key() )
217 return QPointF( deltaX, deltaY );
220void QgsModelGraphicsView::mousePressEvent( QMouseEvent *event )
228 mTool->modelPressEvent( me.get() );
229 event->setAccepted( me->isAccepted() );
232 if ( !mTool || !event->isAccepted() )
234 if ( event->button() == Qt::MiddleButton )
237 setTool( mMidMouseButtonPanTool );
242 QGraphicsView::mousePressEvent( event );
247void QgsModelGraphicsView::mouseReleaseEvent( QMouseEvent *event )
255 mTool->modelReleaseEvent( me.get() );
256 event->setAccepted( me->isAccepted() );
259 if ( !mTool || !event->isAccepted() )
260 QGraphicsView::mouseReleaseEvent( event );
263void QgsModelGraphicsView::mouseMoveEvent( QMouseEvent *event )
268 mMouseCurrentXY =
event->pos();
270 QPointF cursorPos = mapToScene( mMouseCurrentXY );
281 if ( me->isSnapped() )
283 cursorPos = me->snappedPoint();
286 mSnapMarker->setPos( me->snappedPoint() );
287 mSnapMarker->setVisible(
true );
290 else if ( mSnapMarker )
292 mSnapMarker->setVisible(
false );
295 mTool->modelMoveEvent( me.get() );
296 event->setAccepted( me->isAccepted() );
299 if ( !mTool || !event->isAccepted() )
300 QGraphicsView::mouseMoveEvent( event );
303void QgsModelGraphicsView::mouseDoubleClickEvent( QMouseEvent *event )
311 mTool->modelDoubleClickEvent( me.get() );
312 event->setAccepted( me->isAccepted() );
315 if ( !mTool || !event->isAccepted() )
316 QGraphicsView::mouseDoubleClickEvent( event );
319void QgsModelGraphicsView::keyPressEvent( QKeyEvent *event )
326 mTool->keyPressEvent( event );
329 if ( mTool && event->isAccepted() )
332 if ( event->key() == Qt::Key_Space && ! event->isAutoRepeat() )
334 if ( !( event->modifiers() & Qt::ControlModifier ) )
337 setTool( mSpacePanTool );
342 setTool( mSpaceZoomTool );
346 else if ( event->key() == Qt::Key_Left
347 || event->key() == Qt::Key_Right
348 || event->key() == Qt::Key_Up
349 || event->key() == Qt::Key_Down )
351 QgsModelGraphicsScene *s = modelScene();
352 const QList<QgsModelComponentGraphicItem *> itemList = s->selectedComponentItems();
353 if ( !itemList.empty() )
355 QPointF delta = deltaForKeyEvent( event );
357 itemList.at( 0 )->aboutToChange( tr(
"Move Items" ) );
358 for ( QgsModelComponentGraphicItem *item : itemList )
360 item->moveComponentBy( delta.x(), delta.y() );
362 itemList.at( 0 )->changed();
368void QgsModelGraphicsView::keyReleaseEvent( QKeyEvent *event )
375 mTool->keyReleaseEvent( event );
378 if ( !mTool || !event->isAccepted() )
379 QGraphicsView::keyReleaseEvent( event );
382void QgsModelGraphicsView::setModelScene( QgsModelGraphicsScene *scene )
388 mSnapMarker =
new QgsModelViewSnapMarker();
390 scene->addItem( mSnapMarker );
393QgsModelGraphicsScene *QgsModelGraphicsView::modelScene()
const
395 return qobject_cast< QgsModelGraphicsScene * >( QgsModelGraphicsView::scene() );
419 emit toolSet( mTool );
424 if ( mTool && mTool == tool )
427 emit toolSet(
nullptr );
428 setCursor( Qt::ArrowCursor );
437void QgsModelGraphicsView::startMacroCommand(
const QString &text )
439 emit macroCommandStarted( text );
442void QgsModelGraphicsView::endMacroCommand()
444 emit macroCommandEnded();
447void QgsModelGraphicsView::snapSelected()
449 QgsModelGraphicsScene *s = modelScene();
450 const QList<QgsModelComponentGraphicItem *> itemList = s->selectedComponentItems();
451 startMacroCommand( tr(
"Snap Items" ) );
452 if ( !itemList.empty() )
454 bool prevSetting = mSnapper.snapToGrid();
455 mSnapper.setSnapToGrid(
true );
456 for ( QgsModelComponentGraphicItem *item : itemList )
458 bool wasSnapped =
false;
459 QRectF snapped = mSnapper.snapRectWithResize( item->mapRectToScene( item->itemRect( ) ), transform().m11(), wasSnapped );
462 item->setItemRect( snapped );
465 mSnapper.setSnapToGrid( prevSetting );
470void QgsModelGraphicsView::copySelectedItems( QgsModelGraphicsView::ClipboardOperation operation )
472 copyItems( modelScene()->selectedComponentItems(), operation );
475void QgsModelGraphicsView::copyItems(
const QList<QgsModelComponentGraphicItem *> &items, QgsModelGraphicsView::ClipboardOperation operation )
482 QDomElement documentElement = doc.createElement( QStringLiteral(
"ModelComponentClipboard" ) );
483 if ( operation == ClipboardCut )
485 emit macroCommandStarted( tr(
"Cut Items" ) );
486 emit beginCommand( QString() );
489 QList< QVariant > paramComponents;
490 QList< QVariant > groupBoxComponents;
491 QList< QVariant > algComponents;
493 QList< QgsModelComponentGraphicItem * > selectedCommentParents;
494 QList< QgsProcessingModelOutput > selectedOutputs;
495 QList< QgsProcessingModelOutput > selectedOutputsComments;
496 for ( QgsModelComponentGraphicItem *item : items )
498 if (
const QgsModelCommentGraphicItem *commentItem =
dynamic_cast< QgsModelCommentGraphicItem *
>( item ) )
500 selectedCommentParents << commentItem->parentComponentItem();
501 if (
const QgsModelOutputGraphicItem *outputItem =
dynamic_cast< QgsModelOutputGraphicItem *
>( commentItem->parentComponentItem() ) )
503 selectedOutputsComments << *( static_cast< const QgsProcessingModelOutput *>( outputItem->component() ) );
506 else if (
const QgsModelOutputGraphicItem *outputItem =
dynamic_cast< QgsModelOutputGraphicItem *
>( item ) )
508 selectedOutputs << *( static_cast< const QgsProcessingModelOutput *>( outputItem->component() ) );
512 for ( QgsModelComponentGraphicItem *item : items )
514 if (
const QgsProcessingModelParameter *param =
dynamic_cast< QgsProcessingModelParameter *
>( item->component() ) )
516 QgsProcessingModelParameter component = *param;
519 if ( !selectedCommentParents.contains( item ) )
522 component.comment()->setDescription( QString() );
525 QVariantMap paramDef;
526 paramDef.insert( QStringLiteral(
"component" ), component.toVariant() );
528 paramDef.insert( QStringLiteral(
"definition" ), def->
toVariantMap() );
530 paramComponents << paramDef;
532 else if ( QgsProcessingModelGroupBox *groupBox =
dynamic_cast< QgsProcessingModelGroupBox *
>( item->component() ) )
534 groupBoxComponents << groupBox->toVariant();
536 else if (
const QgsProcessingModelChildAlgorithm *alg =
dynamic_cast< QgsProcessingModelChildAlgorithm *
>( item->component() ) )
538 QgsProcessingModelChildAlgorithm childAlg = *alg;
541 if ( !selectedCommentParents.contains( item ) )
548 QMap<QString, QgsProcessingModelOutput> clipboardOutputs;
549 const QMap<QString, QgsProcessingModelOutput> existingOutputs = childAlg.modelOutputs();
550 for (
auto it = existingOutputs.constBegin(); it != existingOutputs.constEnd(); ++ it )
553 for (
const QgsProcessingModelOutput &candidate : selectedOutputs )
555 if ( candidate.childId() == childAlg.childId() && candidate.name() == it.value().name() && candidate.childOutputName() == it.value().childOutputName() )
564 bool commentFound =
false;
565 for (
const QgsProcessingModelOutput &candidate : selectedOutputsComments )
567 if ( candidate.childId() == childAlg.childId() && candidate.name() == it.value().name() && candidate.childOutputName() == it.value().childOutputName() )
574 QgsProcessingModelOutput output = it.value();
576 output.comment()->setDescription( QString() );
578 clipboardOutputs.insert( it.key(), output );
581 childAlg.setModelOutputs( clipboardOutputs );
583 algComponents << childAlg.toVariant();
586 QVariantMap components;
587 components.insert( QStringLiteral(
"parameters" ), paramComponents );
588 components.insert( QStringLiteral(
"groupboxes" ), groupBoxComponents );
589 components.insert( QStringLiteral(
"algs" ), algComponents );
591 if ( operation == ClipboardCut )
593 emit deleteSelectedItems();
595 emit macroCommandEnded();
598 QMimeData *mimeData =
new QMimeData;
599 mimeData->setData( QStringLiteral(
"text/xml" ), doc.toByteArray() );
600 mimeData->setText( doc.toByteArray() );
601 QClipboard *clipboard = QApplication::clipboard();
602 clipboard->setMimeData( mimeData );
605void QgsModelGraphicsView::pasteItems( QgsModelGraphicsView::PasteMode mode )
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 if ( modelScene()->model()->childAlgorithms().contains( alg.childId() ) )
705 alg.generateChildId( *modelScene()->model() );
709 pastedAlgorithms << alg.childId();
711 if ( !pastedBounds.isValid( ) )
712 pastedBounds = QRectF( alg.position() - QPointF( alg.size().width() / 2.0, alg.size().height() / 2.0 ), alg.size() );
714 pastedBounds = pastedBounds.united( QRectF( alg.position() - QPointF( alg.size().width() / 2.0, alg.size().height() / 2.0 ), alg.size() ) );
716 if ( !alg.comment()->description().isEmpty() )
717 pastedBounds = pastedBounds.united( QRectF( alg.comment()->position() - QPointF( alg.comment()->size().width() / 2.0, alg.comment()->size().height() / 2.0 ), alg.comment()->size() ) );
719 const QMap<QString, QgsProcessingModelChildAlgorithm> existingAlgs = modelScene()->model()->childAlgorithms();
721 const QMap<QString, QgsProcessingModelOutput> outputs = alg.modelOutputs();
722 QMap<QString, QgsProcessingModelOutput> pastedOutputs;
723 for (
auto it = outputs.constBegin(); it != outputs.constEnd(); ++it )
725 QString name = it.value().name();
731 for (
auto algIt = existingAlgs.constBegin(); algIt != existingAlgs.constEnd(); ++algIt )
733 const QMap<QString, QgsProcessingModelOutput> algOutputs = algIt->modelOutputs();
734 for (
auto outputIt = algOutputs.constBegin(); outputIt != algOutputs.constEnd(); ++outputIt )
736 if ( outputIt.value().name() == name )
748 name = QStringLiteral(
"%1 (%2)" ).arg( it.value().name() ).arg( next );
751 QgsProcessingModelOutput newOutput = it.value();
752 newOutput.setName( name );
753 newOutput.setDescription( name );
754 pastedOutputs.insert( name, newOutput );
756 pastedBounds = pastedBounds.united( QRectF( newOutput.position() - QPointF( newOutput.size().width() / 2.0, newOutput.size().height() / 2.0 ), newOutput.size() ) );
758 if ( !alg.comment()->description().isEmpty() )
759 pastedBounds = pastedBounds.united( QRectF( newOutput.comment()->position() - QPointF( newOutput.comment()->size().width() / 2.0, newOutput.comment()->size().height() / 2.0 ), newOutput.comment()->size() ) );
761 alg.setModelOutputs( pastedOutputs );
763 modelScene()->model()->addChildAlgorithm( alg );
766 QPointF offset( 0, 0 );
769 case PasteModeInPlace:
772 case PasteModeCursor:
773 case PasteModeCenter:
775 offset = pt - pastedBounds.topLeft();
780 if ( !offset.isNull() )
782 for ( QgsProcessingModelGroupBox pastedGroup : std::as_const( pastedGroups ) )
784 pastedGroup.setPosition( pastedGroup.position() + offset );
785 modelScene()->model()->addGroupBox( pastedGroup );
787 for (
const QString &pastedParam : std::as_const( pastedParameters ) )
789 modelScene()->model()->parameterComponent( pastedParam ).setPosition( modelScene()->model()->parameterComponent( pastedParam ).position() + offset );
790 modelScene()->model()->parameterComponent( pastedParam ).comment()->setPosition( modelScene()->model()->parameterComponent( pastedParam ).comment()->position() + offset );
792 for (
const QString &pastedAlg : std::as_const( pastedAlgorithms ) )
794 modelScene()->model()->childAlgorithm( pastedAlg ).setPosition( modelScene()->model()->childAlgorithm( pastedAlg ).position() + offset );
795 modelScene()->model()->childAlgorithm( pastedAlg ).comment()->setPosition( modelScene()->model()->childAlgorithm( pastedAlg ).comment()->position() + offset );
797 const QMap<QString, QgsProcessingModelOutput> outputs = modelScene()->model()->childAlgorithm( pastedAlg ).modelOutputs();
798 for (
auto it = outputs.begin(); it != outputs.end(); ++it )
800 modelScene()->model()->childAlgorithm( pastedAlg ).modelOutput( it.key() ).setPosition( modelScene()->model()->childAlgorithm( pastedAlg ).modelOutput( it.key() ).position() + offset );
801 modelScene()->model()->childAlgorithm( pastedAlg ).modelOutput( it.key() ).comment()->setPosition( modelScene()->model()->childAlgorithm( pastedAlg ).modelOutput( it.key() ).comment()->position() + offset );
810 modelScene()->rebuildRequired();
813QgsModelViewSnapMarker::QgsModelViewSnapMarker()
814 : QGraphicsRectItem( QRectF( 0, 0, 0, 0 ) )
817 QFontMetrics fm( f );
818 mSize = fm.horizontalAdvance(
'X' );
819 setPen( QPen( Qt::transparent, mSize ) );
821 setFlags( flags() | QGraphicsItem::ItemIgnoresTransformations );
822 setZValue( QgsModelGraphicsScene::ZSnapIndicator );
825void QgsModelViewSnapMarker::paint( QPainter *p,
const QStyleOptionGraphicsItem *, QWidget * )
827 QPen pen( QColor( 255, 0, 0 ) );
830 p->setBrush( Qt::NoBrush );
832 double halfSize = mSize / 2.0;
833 p->drawLine( QLineF( -halfSize, -halfSize, halfSize, halfSize ) );
834 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
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.