QGIS API Documentation 4.3.0-Master (2b7e6c9893e)
Loading...
Searching...
No Matches
qgsmodelcomponentgraphicitem.h
Go to the documentation of this file.
1/***************************************************************************
2 qgsmodelcomponentgraphicitem.h
3 ----------------------------------
4 Date : March 2020
5 Copyright : (C) 2020 Nyall Dawson
6 Email : nyall dot dawson at gmail dot com
7 ***************************************************************************
8 * *
9 * This program is free software; you can redistribute it and/or modify *
10 * it under the terms of the GNU General Public License as published by *
11 * the Free Software Foundation; either version 2 of the License, or *
12 * (at your option) any later version. *
13 * *
14 ***************************************************************************/
15
16#ifndef QGSMODELCOMPONENTGRAPHICITEM_H
17#define QGSMODELCOMPONENTGRAPHICITEM_H
18
19#include "qgis.h"
20#include "qgis_gui.h"
23
24#include <QFont>
25#include <QGraphicsObject>
26#include <QPicture>
27#include <QPointer>
28
29#define SIP_NO_FILE
30
31class QgsProcessingModelComponent;
32class QgsProcessingModelParameter;
33class QgsProcessingModelChildAlgorithm;
34class QgsProcessingModelOutput;
35class QgsProcessingModelComment;
36class QgsProcessingModelAlgorithm;
37class QgsModelDesignerFlatButtonGraphicItem;
38class QgsModelDesignerFoldButtonGraphicItem;
39class QgsModelDesignerSocketGraphicItem;
40class QgsModelGraphicsView;
42class QgsProcessingModelGroupBox;
43class QgsModelArrowItem;
45
47
54class GUI_EXPORT QgsModelComponentGraphicItem : public QGraphicsObject, public QgsProcessingWidgetContextGenerator
55{
56 Q_OBJECT
57
58 public:
60 enum State
61 {
62 Normal,
63 Selected,
64 Hover,
65 };
66
68 enum Flag
69 {
70 // For future API flexibility only and to avoid sip issues, remove when real entries are added to flags.
71 Unused = 1 << 0,
72 };
73 Q_DECLARE_FLAGS( Flags, Flag )
74
75
83 QgsModelComponentGraphicItem( QgsProcessingModelComponent *component, QgsProcessingModelAlgorithm *model, QGraphicsItem *parent );
84
85 ~QgsModelComponentGraphicItem() override;
86
90 virtual Flags flags() const;
91
95 QgsProcessingModelComponent *component();
96
100 const QgsProcessingModelComponent *component() const;
101
105 QgsProcessingModelAlgorithm *model();
106
110 const QgsProcessingModelAlgorithm *model() const;
111
115 QgsModelGraphicsView *view();
116
121 QFont font() const;
122
127 void setFont( const QFont &font );
128
134 void moveComponentBy( qreal dx, qreal dy );
135
139 void previewItemMove( qreal dx, qreal dy );
140
144 void setItemRect( QRectF rect );
145
151 virtual QColor linkColor( Qt::Edge edge, int index ) const;
152
156 QRectF previewItemRectChange( QRectF rect );
157
161 void finalizePreviewedItemRectChange( QRectF rect );
162
166 virtual void modelHoverEnterEvent( QgsModelViewMouseEvent *event );
167
171 virtual void modelHoverMoveEvent( QgsModelViewMouseEvent *event );
172
176 virtual void modelHoverLeaveEvent( QgsModelViewMouseEvent *event );
177
181 virtual void modelDoubleClickEvent( QgsModelViewMouseEvent *event );
182
183 void mouseDoubleClickEvent( QGraphicsSceneMouseEvent *event ) override;
184 void hoverEnterEvent( QGraphicsSceneHoverEvent *event ) override;
185 void hoverMoveEvent( QGraphicsSceneHoverEvent *event ) override;
186 void hoverLeaveEvent( QGraphicsSceneHoverEvent *event ) override;
187 QVariant itemChange( GraphicsItemChange change, const QVariant &value ) override;
188 QRectF boundingRect() const override;
189 bool contains( const QPointF &point ) const override;
190 void paint( QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget = nullptr ) override;
191
195 QRectF itemRect( bool storedRect = false ) const;
196
202 QString label() const;
203
209 void setLabel( const QString &label );
210
214 State state() const;
215
219 virtual int linkPointCount( Qt::Edge edge ) const;
220
224 virtual QString linkPointText( Qt::Edge edge, int index ) const;
225
229 QPointF linkPoint( Qt::Edge edge, int index, bool incoming ) const;
230
238 QPointF calculateAutomaticLinkPoint( QgsModelComponentGraphicItem *other, Qt::Edge &edge ) const;
239
247 QPointF calculateAutomaticLinkPoint( const QPointF &point, Qt::Edge &edge ) const;
248
255 QgsModelDesignerSocketGraphicItem *outSocketAt( int index ) const;
256
257
263 virtual void editComment() {}
264
268 virtual bool canDeleteComponent() { return false; }
269
275 virtual void deleteComponent() {}
276
283 QList< QgsModelArrowItem * > incomingArrows();
284
291 QList< QgsModelArrowItem * > outgoingArrows();
292
298 void registerWidgetContextGenerator( QgsProcessingWidgetContextGenerator *generator );
299
300 QgsProcessingParameterWidgetContext createWidgetContext() override;
301
302 signals:
303
304 // TODO - rework this, should be triggered externally when the model actually changes!
305
309 void requestModelRepaint();
310
318 void aboutToChange( const QString &text, const QString &id = QString() );
319
324 void changed();
325
329 void repaintArrows();
330
334 void updateArrowPaths();
335
339 void sizePositionChanged();
340
341 protected slots:
342
348 virtual void editComponent() {}
349
350 protected:
356 virtual void paintBackground( QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget = nullptr );
357
361 void paintOutline( QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget = nullptr );
362
367 QString truncatedTextForItem( const QString &text ) const;
368
372 virtual QColor fillColor( State state ) const = 0;
373
377 virtual QColor strokeColor( State state ) const = 0;
378
382 virtual QColor textColor( State state ) const = 0;
383
387 virtual Qt::PenStyle strokeStyle( State state ) const;
388
394 virtual QColor outlineColor() const { return QColor(); }
395
399 virtual Qt::Alignment titleAlignment() const;
400
404 virtual QPicture iconPicture() const;
405
409 virtual QPixmap iconPixmap() const;
410
414 virtual void updateStoredComponentPosition( const QPointF &pos, const QSizeF &size ) = 0;
415
419 void updateButtonPositions();
420
426 static constexpr QColor FALLBACK_COLOR = QColor( 128, 128, 128 ); /* mid gray */
427
428 private:
429 QSizeF itemSize() const;
430
431 void updateToolTip( const QPointF &pos );
432
433 void fold( Qt::Edge edge, bool folded );
434
435 std::unique_ptr<QgsProcessingModelComponent> mComponent;
436 QgsProcessingModelAlgorithm *mModel = nullptr;
437
438 bool mInitialized = false;
439 QgsModelDesignerFoldButtonGraphicItem *mExpandTopButton = nullptr;
440 QgsModelDesignerFoldButtonGraphicItem *mExpandBottomButton = nullptr;
441
442 QString mLabel;
443
444 QgsModelDesignerFlatButtonGraphicItem *mEditButton = nullptr;
445 QgsModelDesignerFlatButtonGraphicItem *mDeleteButton = nullptr;
446
447 QList< QgsModelDesignerSocketGraphicItem * > mInSockets;
448 QList< QgsModelDesignerSocketGraphicItem * > mOutSockets;
449
450 QgsProcessingWidgetContextGenerator *mWidgetContextGenerator = nullptr;
451
452 static constexpr double MIN_COMPONENT_WIDTH = 70;
453 static constexpr double MIN_COMPONENT_HEIGHT = 30;
454
455 static constexpr double DEFAULT_BUTTON_WIDTH = 16;
456 static constexpr double DEFAULT_BUTTON_HEIGHT = 16;
457 static constexpr double BUTTON_MARGIN = 2;
458 static constexpr double SOCKET_MARGIN = 25;
459 static constexpr double TEXT_MARGIN = 4;
460 static constexpr double RECT_PEN_SIZE = 2;
461 static constexpr double RECT_OUTLINE_SIZE = 10;
462 QSizeF mButtonSize { DEFAULT_BUTTON_WIDTH, DEFAULT_BUTTON_HEIGHT };
463
464 QFont mFont;
465
466 bool mIsHovering = false;
467 QSizeF mTempSize;
468};
469Q_DECLARE_OPERATORS_FOR_FLAGS( QgsModelComponentGraphicItem::Flags )
470
471
477class GUI_EXPORT QgsModelParameterGraphicItem : public QgsModelComponentGraphicItem
478{
479 Q_OBJECT
480
481 public:
490 QgsModelParameterGraphicItem( QgsProcessingModelParameter *parameter, QgsProcessingModelAlgorithm *model, QGraphicsItem *parent );
491
492 void contextMenuEvent( QGraphicsSceneContextMenuEvent *event ) override;
493 bool canDeleteComponent() override;
494 void editComponent() override;
495 void editComment() override;
496 QColor linkColor( Qt::Edge edge, int index ) const override;
497
503 QString applyEdit( std::unique_ptr< QgsProcessingParameterDefinition > newParameter, const QString &oldDescription, const QString &oldName, const QString &comment, const QColor &commentColor );
504
505 protected:
506 QColor fillColor( State state ) const override;
507 QColor strokeColor( State state ) const override;
508 QColor textColor( State state ) const override;
509 QPicture iconPicture() const override;
510
511 int linkPointCount( Qt::Edge edge ) const override;
512 QString linkPointText( Qt::Edge edge, int index ) const override;
513 void updateStoredComponentPosition( const QPointF &pos, const QSizeF &size ) override;
514
515 protected slots:
516
517 void deleteComponent() override;
518
519 private:
525 void edit( bool editComment = false );
526
527 QPicture mPicture;
528};
529
536class GUI_EXPORT QgsModelChildAlgorithmGraphicItem : public QgsModelComponentGraphicItem
537{
538 Q_OBJECT
539
540 public:
549 QgsModelChildAlgorithmGraphicItem( QgsProcessingModelChildAlgorithm *child, QgsProcessingModelAlgorithm *model, QGraphicsItem *parent );
550 void contextMenuEvent( QGraphicsSceneContextMenuEvent *event ) override;
551 bool canDeleteComponent() override;
552
556 void setResults( const QgsProcessingModelChildAlgorithmResult &results );
557
565 void setSourceFeatureCount( const QString &parameterName, long long featureCount );
566
574 void setSinkFeatureCount( const QString &outputName, long long featureCount );
575
581 QgsProcessingModelChildAlgorithmResult results() { return mResults; };
582
586 void setProgress( double progress );
587
593 void setStarted();
594
600 void setOutdated();
601
608 int indexForInput( const QString &parameterName ) const;
609
616 int indexForOutput( const QString &output ) const;
617
618 void editComponent() override;
619 void editComment() override;
620
626 void applyEdit( const QgsProcessingModelChildAlgorithm &algorithm );
627
628 signals:
629
635 void runFromHere();
636
642 void runSelected();
643
649 void showPreviousResults();
650
656 void showLog();
657
664 void rebuildConfigurationDockWidget();
665
666 protected:
667 void paintBackground( QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget = nullptr ) override;
668
669 QColor fillColor( State state ) const override;
670 QColor strokeColor( State state ) const override;
671 QColor textColor( State state ) const override;
672 QColor outlineColor() const override;
673 QPixmap iconPixmap() const override;
674 QPicture iconPicture() const override;
675
676 int linkPointCount( Qt::Edge edge ) const override;
677 QString linkPointText( Qt::Edge edge, int index ) const override;
678 void updateStoredComponentPosition( const QPointF &pos, const QSizeF &size ) override;
679
680 protected slots:
681
682 void deleteComponent() override;
683
684 private slots:
685 void deactivateAlgorithm();
686 void activateAlgorithm();
687
688 private:
694 void edit( bool editComment = false );
695
696 QPicture mPicture;
697 QPixmap mPixmap;
698 bool mStarted = false;
699 bool mOutdated = false;
700 QgsProcessingModelChildAlgorithmResult mResults;
701 double mProgress = -1;
702 bool mIsValid = true;
703};
704
711class GUI_EXPORT QgsModelOutputGraphicItem : public QgsModelComponentGraphicItem
712{
713 Q_OBJECT
714
715 public:
724 QgsModelOutputGraphicItem( QgsProcessingModelOutput *output, QgsProcessingModelAlgorithm *model, QGraphicsItem *parent );
725
726 bool canDeleteComponent() override;
727 void editComponent() override;
728 void editComment() override;
729
734 void applyEdit( const QString &name, const QString &description, const QVariant &defaultValue, bool mandatory, const QString &comment, const QColor &commentColor );
735
736 protected:
737 QColor fillColor( State state ) const override;
738 QColor strokeColor( State state ) const override;
739 QColor textColor( State state ) const override;
740 QPicture iconPicture() const override;
741 void updateStoredComponentPosition( const QPointF &pos, const QSizeF &size ) override;
742
743 protected slots:
744
745 void deleteComponent() override;
746
747 private:
748 void edit( bool editComment = false );
749
750 QPicture mPicture;
751};
752
759class GUI_EXPORT QgsModelCommentGraphicItem : public QgsModelComponentGraphicItem
760{
761 Q_OBJECT
762
763 public:
772 QgsModelCommentGraphicItem( QgsProcessingModelComment *comment, QgsModelComponentGraphicItem *parentItem, QgsProcessingModelAlgorithm *model, QGraphicsItem *parent );
773 ~QgsModelCommentGraphicItem() override;
774 void contextMenuEvent( QGraphicsSceneContextMenuEvent *event ) override;
775 bool canDeleteComponent() override;
776
780 QgsModelComponentGraphicItem *parentComponentItem() const;
781
782 protected:
783 QColor fillColor( State state ) const override;
784 QColor strokeColor( State state ) const override;
785 QColor textColor( State state ) const override;
786 Qt::PenStyle strokeStyle( State state ) const override;
787 void updateStoredComponentPosition( const QPointF &pos, const QSizeF &size ) override;
788
789 protected slots:
790
791 void deleteComponent() override;
792 void editComponent() override;
793
794 private:
795 QgsProcessingModelComment *modelComponent();
796
797 std::unique_ptr<QgsProcessingModelComponent> mParentComponent;
798 QPointer<QgsModelComponentGraphicItem> mParentItem;
799};
800
807class GUI_EXPORT QgsModelGroupBoxGraphicItem : public QgsModelComponentGraphicItem
808{
809 Q_OBJECT
810
811 public:
820 QgsModelGroupBoxGraphicItem( QgsProcessingModelGroupBox *box, QgsProcessingModelAlgorithm *model, QGraphicsItem *parent );
821 ~QgsModelGroupBoxGraphicItem() override;
822 void contextMenuEvent( QGraphicsSceneContextMenuEvent *event ) override;
823 bool canDeleteComponent() override;
824
830 void applyEdit( const QgsProcessingModelGroupBox &groupBox );
831
832 protected:
833 QColor fillColor( State state ) const override;
834 QColor strokeColor( State state ) const override;
835 QColor textColor( State state ) const override;
836 Qt::PenStyle strokeStyle( State state ) const override;
837 Qt::Alignment titleAlignment() const override;
838 void updateStoredComponentPosition( const QPointF &pos, const QSizeF &size ) override;
839
840 protected slots:
841
842 void deleteComponent() override;
843 void editComponent() override;
844
845 private:
846};
847
849
850#endif // QGSMODELCOMPONENTGRAPHICITEM_H
A mouse event which is the result of a user interaction with a QgsModelGraphicsView.
Base class for the definition of processing parameters.
An interface for objects which can create Processing widget contexts.
virtual QgsProcessingParameterWidgetContext createWidgetContext()=0
This method needs to be reimplemented in all classes which implement this interface and return a Proc...
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
Q_DECLARE_OPERATORS_FOR_FLAGS(QgsProjectionSelectionWidget::CrsOptions)