17#include "moc_qgsmodelgraphicsview.cpp"
31#include <QDragEnterEvent>
33#include <QApplication>
40#define MIN_VIEW_SCALE 0.05
41#define MAX_VIEW_SCALE 1000.0
43QgsModelGraphicsView::QgsModelGraphicsView( QWidget *parent )
44 : QGraphicsView( parent )
46 setResizeAnchor( QGraphicsView::AnchorViewCenter );
47 setMouseTracking(
true );
48 viewport()->setMouseTracking(
true );
49 setAcceptDrops(
true );
55 mSnapper.setSnapToGrid(
true );
58QgsModelGraphicsView::~QgsModelGraphicsView()
63void QgsModelGraphicsView::dragEnterEvent( QDragEnterEvent *event )
65 if ( event->mimeData()->hasText() || event->mimeData()->hasFormat( QStringLiteral(
"application/x-vnd.qgis.qgis.algorithmid" ) ) )
66 event->acceptProposedAction();
71void QgsModelGraphicsView::dropEvent( QDropEvent *event )
73 const QPointF dropPoint = mapToScene( event->pos() );
74 if ( event->mimeData()->hasFormat( QStringLiteral(
"application/x-vnd.qgis.qgis.algorithmid" ) ) )
76 QByteArray data =
event->mimeData()->data( QStringLiteral(
"application/x-vnd.qgis.qgis.algorithmid" ) );
77 QDataStream stream( &data, QIODevice::ReadOnly );
79 stream >> algorithmId;
81 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] {
90 emit inputDropped( itemId, dropPoint );
100void QgsModelGraphicsView::dragMoveEvent( QDragMoveEvent *event )
102 if ( event->mimeData()->hasText() || event->mimeData()->hasFormat( QStringLiteral(
"application/x-vnd.qgis.qgis.algorithmid" ) ) )
103 event->acceptProposedAction();
108void QgsModelGraphicsView::wheelEvent( QWheelEvent *event )
115 mTool->wheelEvent( event );
118 if ( !mTool || !event->isAccepted() )
125void QgsModelGraphicsView::wheelZoom( QWheelEvent *event )
129 double zoomFactor = settings.
value( QStringLiteral(
"qgis/zoom_factor" ), 2 ).toDouble();
130 bool reverseZoom = settings.
value( QStringLiteral(
"qgis/reverse_wheel_zoom" ),
false ).toBool();
131 bool zoomIn = reverseZoom ?
event->angleDelta().y() < 0 :
event->angleDelta().y() > 0;
134 zoomFactor = 1.0 + ( zoomFactor - 1.0 ) / 120.0 * std::fabs( event->angleDelta().y() );
136 if ( event->modifiers() & Qt::ControlModifier )
139 zoomFactor = 1.0 + ( zoomFactor - 1.0 ) / 20.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 ), scenePoint.y() + ( ( oldCenter.y() - scenePoint.y() ) * scaleFactor ) );
155 centerOn( newCenter.x(), newCenter.y() );
160 scaleSafe( zoomFactor );
164 scaleSafe( 1 / zoomFactor );
168void QgsModelGraphicsView::scaleSafe(
double scale )
170 double currentScale = transform().m11();
171 scale *= currentScale;
173 setTransform( QTransform::fromScale( scale, scale ) );
176QPointF QgsModelGraphicsView::deltaForKeyEvent( QKeyEvent *event )
179 double increment = 1.0;
180 if ( event->modifiers() & Qt::ShiftModifier )
185 else if ( event->modifiers() & Qt::AltModifier )
188 double viewScale = transform().m11();
191 increment = 1 / viewScale;
197 switch ( event->key() )
215 return QPointF( deltaX, deltaY );
218void QgsModelGraphicsView::mousePressEvent( QMouseEvent *event )
226 mTool->modelPressEvent( me.get() );
227 event->setAccepted( me->isAccepted() );
230 if ( !mTool || !event->isAccepted() )
232 if ( event->button() == Qt::MiddleButton )
235 setTool( mMidMouseButtonPanTool );
240 QGraphicsView::mousePressEvent( event );
245void QgsModelGraphicsView::mouseReleaseEvent( QMouseEvent *event )
253 mTool->modelReleaseEvent( me.get() );
254 event->setAccepted( me->isAccepted() );
257 if ( !mTool || !event->isAccepted() )
258 QGraphicsView::mouseReleaseEvent( event );
261void QgsModelGraphicsView::mouseMoveEvent( QMouseEvent *event )
266 mMouseCurrentXY =
event->pos();
268 QPointF cursorPos = mapToScene( mMouseCurrentXY );
279 if ( me->isSnapped() )
281 cursorPos = me->snappedPoint();
284 mSnapMarker->setPos( me->snappedPoint() );
285 mSnapMarker->setVisible(
true );
288 else if ( mSnapMarker )
290 mSnapMarker->setVisible(
false );
293 mTool->modelMoveEvent( me.get() );
294 event->setAccepted( me->isAccepted() );
297 if ( !mTool || !event->isAccepted() )
298 QGraphicsView::mouseMoveEvent( event );
301void QgsModelGraphicsView::mouseDoubleClickEvent( QMouseEvent *event )
309 mTool->modelDoubleClickEvent( me.get() );
310 event->setAccepted( me->isAccepted() );
313 if ( !mTool || !event->isAccepted() )
314 QGraphicsView::mouseDoubleClickEvent( event );
317void QgsModelGraphicsView::keyPressEvent( QKeyEvent *event )
324 mTool->keyPressEvent( event );
327 if ( mTool && event->isAccepted() )
330 if ( event->key() == Qt::Key_Space && !event->isAutoRepeat() )
332 if ( !( event->modifiers() & Qt::ControlModifier ) )
335 setTool( mSpacePanTool );
340 setTool( mSpaceZoomTool );
344 else if ( event->key() == Qt::Key_Left
345 || event->key() == Qt::Key_Right
346 || event->key() == Qt::Key_Up
347 || event->key() == Qt::Key_Down )
349 QgsModelGraphicsScene *s = modelScene();
350 const QList<QgsModelComponentGraphicItem *> itemList = s->selectedComponentItems();
351 if ( !itemList.empty() )
353 QPointF delta = deltaForKeyEvent( event );
355 itemList.at( 0 )->aboutToChange( tr(
"Move Items" ) );
356 for ( QgsModelComponentGraphicItem *item : itemList )
358 item->moveComponentBy( delta.x(), delta.y() );
360 itemList.at( 0 )->changed();
366void QgsModelGraphicsView::keyReleaseEvent( QKeyEvent *event )
373 mTool->keyReleaseEvent( event );
376 if ( !mTool || !event->isAccepted() )
377 QGraphicsView::keyReleaseEvent( event );
380void QgsModelGraphicsView::setModelScene( QgsModelGraphicsScene *scene )
386 mSnapMarker =
new QgsModelViewSnapMarker();
388 scene->addItem( mSnapMarker );
391QgsModelGraphicsScene *QgsModelGraphicsView::modelScene()
const
393 return qobject_cast<QgsModelGraphicsScene *>( QgsModelGraphicsView::scene() );
417 emit toolSet( mTool );
422 if ( mTool && mTool == tool )
425 emit toolSet(
nullptr );
426 setCursor( Qt::ArrowCursor );
435void QgsModelGraphicsView::startMacroCommand(
const QString &text )
437 emit macroCommandStarted( text );
440void QgsModelGraphicsView::endMacroCommand()
442 emit macroCommandEnded();
445void QgsModelGraphicsView::snapSelected()
447 QgsModelGraphicsScene *s = modelScene();
448 const QList<QgsModelComponentGraphicItem *> itemList = s->selectedComponentItems();
449 startMacroCommand( tr(
"Snap Items" ) );
450 if ( !itemList.empty() )
452 bool prevSetting = mSnapper.snapToGrid();
453 mSnapper.setSnapToGrid(
true );
454 for ( QgsModelComponentGraphicItem *item : itemList )
456 bool wasSnapped =
false;
457 QRectF snapped = mSnapper.snapRectWithResize( item->mapRectToScene( item->itemRect() ), transform().m11(), wasSnapped );
460 item->setItemRect( snapped );
463 mSnapper.setSnapToGrid( prevSetting );
468void QgsModelGraphicsView::copySelectedItems( QgsModelGraphicsView::ClipboardOperation operation )
470 copyItems( modelScene()->selectedComponentItems(), operation );
473void QgsModelGraphicsView::copyItems(
const QList<QgsModelComponentGraphicItem *> &items, QgsModelGraphicsView::ClipboardOperation operation )
480 QDomElement documentElement = doc.createElement( QStringLiteral(
"ModelComponentClipboard" ) );
481 if ( operation == ClipboardCut )
483 emit macroCommandStarted( tr(
"Cut Items" ) );
484 emit beginCommand( QString() );
487 QList<QVariant> paramComponents;
488 QList<QVariant> groupBoxComponents;
489 QList<QVariant> algComponents;
491 QList<QgsModelComponentGraphicItem *> selectedCommentParents;
492 QList<QgsProcessingModelOutput> selectedOutputs;
493 QList<QgsProcessingModelOutput> selectedOutputsComments;
494 for ( QgsModelComponentGraphicItem *item : items )
496 if (
const QgsModelCommentGraphicItem *commentItem =
dynamic_cast<QgsModelCommentGraphicItem *
>( item ) )
498 selectedCommentParents << commentItem->parentComponentItem();
499 if (
const QgsModelOutputGraphicItem *outputItem =
dynamic_cast<QgsModelOutputGraphicItem *
>( commentItem->parentComponentItem() ) )
501 selectedOutputsComments << *( static_cast<const QgsProcessingModelOutput *>( outputItem->component() ) );
504 else if (
const QgsModelOutputGraphicItem *outputItem =
dynamic_cast<QgsModelOutputGraphicItem *
>( item ) )
506 selectedOutputs << *( static_cast<const QgsProcessingModelOutput *>( outputItem->component() ) );
510 for ( QgsModelComponentGraphicItem *item : items )
512 if (
const QgsProcessingModelParameter *param =
dynamic_cast<QgsProcessingModelParameter *
>( item->component() ) )
514 QgsProcessingModelParameter component = *param;
517 if ( !selectedCommentParents.contains( item ) )
520 component.comment()->setDescription( QString() );
523 QVariantMap paramDef;
524 paramDef.insert( QStringLiteral(
"component" ), component.toVariant() );
526 paramDef.insert( QStringLiteral(
"definition" ), def->
toVariantMap() );
528 paramComponents << paramDef;
530 else if ( QgsProcessingModelGroupBox *groupBox =
dynamic_cast<QgsProcessingModelGroupBox *
>( item->component() ) )
532 groupBoxComponents << groupBox->toVariant();
534 else if (
const QgsProcessingModelChildAlgorithm *alg =
dynamic_cast<QgsProcessingModelChildAlgorithm *
>( item->component() ) )
536 QgsProcessingModelChildAlgorithm childAlg = *alg;
539 if ( !selectedCommentParents.contains( item ) )
546 QMap<QString, QgsProcessingModelOutput> clipboardOutputs;
547 const QMap<QString, QgsProcessingModelOutput> existingOutputs = childAlg.modelOutputs();
548 for (
auto it = existingOutputs.constBegin(); it != existingOutputs.constEnd(); ++it )
551 for (
const QgsProcessingModelOutput &candidate : selectedOutputs )
553 if ( candidate.childId() == childAlg.childId() && candidate.name() == it.value().name() && candidate.childOutputName() == it.value().childOutputName() )
562 bool commentFound =
false;
563 for (
const QgsProcessingModelOutput &candidate : selectedOutputsComments )
565 if ( candidate.childId() == childAlg.childId() && candidate.name() == it.value().name() && candidate.childOutputName() == it.value().childOutputName() )
572 QgsProcessingModelOutput output = it.value();
574 output.comment()->setDescription( QString() );
576 clipboardOutputs.insert( it.key(), output );
579 childAlg.setModelOutputs( clipboardOutputs );
581 algComponents << childAlg.toVariant();
584 QVariantMap components;
585 components.insert( QStringLiteral(
"parameters" ), paramComponents );
586 components.insert( QStringLiteral(
"groupboxes" ), groupBoxComponents );
587 components.insert( QStringLiteral(
"algs" ), algComponents );
589 if ( operation == ClipboardCut )
591 emit deleteSelectedItems();
593 emit macroCommandEnded();
596 QMimeData *mimeData =
new QMimeData;
597 mimeData->setData( QStringLiteral(
"text/xml" ), doc.toByteArray() );
598 mimeData->setText( doc.toByteArray() );
599 QClipboard *clipboard = QApplication::clipboard();
600 clipboard->setMimeData( mimeData );
603void QgsModelGraphicsView::pasteItems( QgsModelGraphicsView::PasteMode mode )
609 QClipboard *clipboard = QApplication::clipboard();
610 if ( doc.setContent( clipboard->mimeData()->data( QStringLiteral(
"text/xml" ) ) ) )
612 QDomElement docElem = doc.documentElement();
615 if ( res.contains( QStringLiteral(
"parameters" ) ) && res.contains( QStringLiteral(
"algs" ) ) )
620 case PasteModeCursor:
621 case PasteModeInPlace:
624 pt = mapToScene( mapFromGlobal( QCursor::pos() ) );
627 case PasteModeCenter:
630 pt = mapToScene( viewport()->rect().center() );
635 emit beginCommand( tr(
"Paste Items" ) );
639 QList<QgsProcessingModelGroupBox> pastedGroups;
640 for (
const QVariant &v : res.value( QStringLiteral(
"groupboxes" ) ).toList() )
642 QgsProcessingModelGroupBox box;
644 box.loadVariant( v.toMap(),
true );
648 modelScene()->model()->addGroupBox( box );
650 if ( !pastedBounds.isValid() )
651 pastedBounds = QRectF( box.position() - QPointF( box.size().width() / 2.0, box.size().height() / 2.0 ), box.size() );
653 pastedBounds = pastedBounds.united( QRectF( box.position() - QPointF( box.size().width() / 2.0, box.size().height() / 2.0 ), box.size() ) );
656 QStringList pastedParameters;
657 for (
const QVariant &v : res.value( QStringLiteral(
"parameters" ) ).toList() )
659 QVariantMap param = v.toMap();
660 QVariantMap componentDef = param.value( QStringLiteral(
"component" ) ).toMap();
661 QVariantMap paramDef = param.value( QStringLiteral(
"definition" ) ).toMap();
665 QgsProcessingModelParameter p;
666 p.loadVariant( componentDef );
669 QString name = p.parameterName();
670 QString description = paramDefinition->description();
672 while ( modelScene()->model()->parameterDefinition( name ) )
675 name = QStringLiteral(
"%1 (%2)" ).arg( p.parameterName() ).arg( next );
676 description = QStringLiteral(
"%1 (%2)" ).arg( paramDefinition->description() ).arg( next );
678 paramDefinition->setName( name );
679 paramDefinition->setDescription( description );
680 p.setParameterName( name );
682 modelScene()->model()->addModelParameter( paramDefinition.release(), p );
683 pastedParameters << p.parameterName();
685 if ( !pastedBounds.isValid() )
686 pastedBounds = QRectF( p.position() - QPointF( p.size().width() / 2.0, p.size().height() / 2.0 ), p.size() );
688 pastedBounds = pastedBounds.united( QRectF( p.position() - QPointF( p.size().width() / 2.0, p.size().height() / 2.0 ), p.size() ) );
690 if ( !p.comment()->description().isEmpty() )
691 pastedBounds = pastedBounds.united( QRectF( p.comment()->position() - QPointF( p.comment()->size().width() / 2.0, p.comment()->size().height() / 2.0 ), p.comment()->size() ) );
694 QStringList pastedAlgorithms;
695 for (
const QVariant &v : res.value( QStringLiteral(
"algs" ) ).toList() )
697 QgsProcessingModelChildAlgorithm alg;
698 alg.loadVariant( v.toMap() );
701 if ( modelScene()->model()->childAlgorithms().contains( alg.childId() ) )
703 alg.generateChildId( *modelScene()->model() );
707 pastedAlgorithms << alg.childId();
709 if ( !pastedBounds.isValid() )
710 pastedBounds = QRectF( alg.position() - QPointF( alg.size().width() / 2.0, alg.size().height() / 2.0 ), alg.size() );
712 pastedBounds = pastedBounds.united( QRectF( alg.position() - QPointF( alg.size().width() / 2.0, alg.size().height() / 2.0 ), alg.size() ) );
714 if ( !alg.comment()->description().isEmpty() )
715 pastedBounds = pastedBounds.united( QRectF( alg.comment()->position() - QPointF( alg.comment()->size().width() / 2.0, alg.comment()->size().height() / 2.0 ), alg.comment()->size() ) );
717 const QMap<QString, QgsProcessingModelChildAlgorithm> existingAlgs = modelScene()->model()->childAlgorithms();
719 const QMap<QString, QgsProcessingModelOutput> outputs = alg.modelOutputs();
720 QMap<QString, QgsProcessingModelOutput> pastedOutputs;
721 for (
auto it = outputs.constBegin(); it != outputs.constEnd(); ++it )
723 QString name = it.value().name();
729 for (
auto algIt = existingAlgs.constBegin(); algIt != existingAlgs.constEnd(); ++algIt )
731 const QMap<QString, QgsProcessingModelOutput> algOutputs = algIt->modelOutputs();
732 for (
auto outputIt = algOutputs.constBegin(); outputIt != algOutputs.constEnd(); ++outputIt )
734 if ( outputIt.value().name() == name )
746 name = QStringLiteral(
"%1 (%2)" ).arg( it.value().name() ).arg( next );
749 QgsProcessingModelOutput newOutput = it.value();
750 newOutput.setName( name );
751 newOutput.setDescription( name );
752 pastedOutputs.insert( name, newOutput );
754 pastedBounds = pastedBounds.united( QRectF( newOutput.position() - QPointF( newOutput.size().width() / 2.0, newOutput.size().height() / 2.0 ), newOutput.size() ) );
756 if ( !alg.comment()->description().isEmpty() )
757 pastedBounds = pastedBounds.united( QRectF( newOutput.comment()->position() - QPointF( newOutput.comment()->size().width() / 2.0, newOutput.comment()->size().height() / 2.0 ), newOutput.comment()->size() ) );
759 alg.setModelOutputs( pastedOutputs );
761 modelScene()->model()->addChildAlgorithm( alg );
764 QPointF offset( 0, 0 );
767 case PasteModeInPlace:
770 case PasteModeCursor:
771 case PasteModeCenter:
773 offset = pt - pastedBounds.topLeft();
778 if ( !offset.isNull() )
780 for ( QgsProcessingModelGroupBox pastedGroup : std::as_const( pastedGroups ) )
782 pastedGroup.setPosition( pastedGroup.position() + offset );
783 modelScene()->model()->addGroupBox( pastedGroup );
785 for (
const QString &pastedParam : std::as_const( pastedParameters ) )
787 modelScene()->model()->parameterComponent( pastedParam ).setPosition( modelScene()->model()->parameterComponent( pastedParam ).position() + offset );
788 modelScene()->model()->parameterComponent( pastedParam ).comment()->setPosition( modelScene()->model()->parameterComponent( pastedParam ).comment()->position() + offset );
790 for (
const QString &pastedAlg : std::as_const( pastedAlgorithms ) )
792 modelScene()->model()->childAlgorithm( pastedAlg ).setPosition( modelScene()->model()->childAlgorithm( pastedAlg ).position() + offset );
793 modelScene()->model()->childAlgorithm( pastedAlg ).comment()->setPosition( modelScene()->model()->childAlgorithm( pastedAlg ).comment()->position() + offset );
795 const QMap<QString, QgsProcessingModelOutput> outputs = modelScene()->model()->childAlgorithm( pastedAlg ).modelOutputs();
796 for (
auto it = outputs.begin(); it != outputs.end(); ++it )
798 modelScene()->model()->childAlgorithm( pastedAlg ).modelOutput( it.key() ).setPosition( modelScene()->model()->childAlgorithm( pastedAlg ).modelOutput( it.key() ).position() + offset );
799 modelScene()->model()->childAlgorithm( pastedAlg ).modelOutput( it.key() ).comment()->setPosition( modelScene()->model()->childAlgorithm( pastedAlg ).modelOutput( it.key() ).comment()->position() + offset );
808 modelScene()->rebuildRequired();
811QgsModelViewSnapMarker::QgsModelViewSnapMarker()
812 : QGraphicsRectItem( QRectF( 0, 0, 0, 0 ) )
815 QFontMetrics fm( f );
816 mSize = fm.horizontalAdvance(
'X' );
817 setPen( QPen( Qt::transparent, mSize ) );
819 setFlags( flags() | QGraphicsItem::ItemIgnoresTransformations );
820 setZValue( QgsModelGraphicsScene::ZSnapIndicator );
823void QgsModelViewSnapMarker::paint( QPainter *p,
const QStyleOptionGraphicsItem *, QWidget * )
825 QPen pen( QColor( 255, 0, 0 ) );
828 p->setBrush( Qt::NoBrush );
830 double halfSize = mSize / 2.0;
831 p->drawLine( QLineF( -halfSize, -halfSize, halfSize, halfSize ) );
832 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.
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.