QGIS API Documentation 4.1.0-Master (31622b25bb0)
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"
22
23#include <QFont>
24#include <QGraphicsObject>
25#include <QPicture>
26#include <QPointer>
27
28class QgsProcessingModelComponent;
29class QgsProcessingModelParameter;
30class QgsProcessingModelChildAlgorithm;
31class QgsProcessingModelOutput;
32class QgsProcessingModelComment;
33class QgsProcessingModelAlgorithm;
34class QgsModelDesignerFlatButtonGraphicItem;
35class QgsModelDesignerFoldButtonGraphicItem;
36class QgsModelDesignerSocketGraphicItem;
37class QgsModelGraphicsView;
39class QgsProcessingModelGroupBox;
40class QgsModelArrowItem;
41
43
50class GUI_EXPORT QgsModelComponentGraphicItem : public QGraphicsObject
51{
52 Q_OBJECT
53
54 public:
56 enum State
57 {
58 Normal,
59 Selected,
60 Hover,
61 };
62
64 enum Flag SIP_ENUM_BASETYPE( IntFlag )
65 {
66 // For future API flexibility only and to avoid sip issues, remove when real entries are added to flags.
67 Unused = 1 << 0,
68 };
69 Q_DECLARE_FLAGS( Flags, Flag )
70
71
79 QgsModelComponentGraphicItem( QgsProcessingModelComponent *component SIP_TRANSFER, QgsProcessingModelAlgorithm *model, QGraphicsItem *parent SIP_TRANSFERTHIS );
80
81 ~QgsModelComponentGraphicItem() override;
82
86 virtual Flags flags() const;
87
91 QgsProcessingModelComponent *component();
92
96 const QgsProcessingModelComponent *component() const SIP_SKIP;
97
101 QgsProcessingModelAlgorithm *model();
102
106 const QgsProcessingModelAlgorithm *model() const SIP_SKIP;
107
111 QgsModelGraphicsView *view();
112
117 QFont font() const;
118
123 void setFont( const QFont &font );
124
130 void moveComponentBy( qreal dx, qreal dy );
131
135 void previewItemMove( qreal dx, qreal dy );
136
140 void setItemRect( QRectF rect );
141
142#ifndef SIP_RUN
143
149 virtual QColor linkColor( Qt::Edge edge, int index ) const;
150
154 QRectF previewItemRectChange( QRectF rect );
155
159 void finalizePreviewedItemRectChange( QRectF rect );
160
164 virtual void modelHoverEnterEvent( QgsModelViewMouseEvent *event );
165
169 virtual void modelHoverMoveEvent( QgsModelViewMouseEvent *event );
170
174 virtual void modelHoverLeaveEvent( QgsModelViewMouseEvent *event );
175
179 virtual void modelDoubleClickEvent( QgsModelViewMouseEvent *event );
180#endif
181 void mouseDoubleClickEvent( QGraphicsSceneMouseEvent *event ) override;
182 void hoverEnterEvent( QGraphicsSceneHoverEvent *event ) override;
183 void hoverMoveEvent( QGraphicsSceneHoverEvent *event ) override;
184 void hoverLeaveEvent( QGraphicsSceneHoverEvent *event ) override;
185 QVariant itemChange( GraphicsItemChange change, const QVariant &value ) override;
186 QRectF boundingRect() const override;
187 bool contains( const QPointF &point ) const override;
188 void paint( QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget = nullptr ) override;
189
193 QRectF itemRect( bool storedRect = false ) const;
194
200 QString label() const;
201
207 void setLabel( const QString &label );
208
212 State state() const;
213
217 virtual int linkPointCount( Qt::Edge edge ) const;
218
222 virtual QString linkPointText( Qt::Edge edge, int index ) const;
223
227 QPointF linkPoint( Qt::Edge edge, int index, bool incoming ) const;
228
236 QPointF calculateAutomaticLinkPoint( QgsModelComponentGraphicItem *other, Qt::Edge &edge SIP_OUT ) const;
237
245 QPointF calculateAutomaticLinkPoint( const QPointF &point, Qt::Edge &edge SIP_OUT ) const;
246
253 QgsModelDesignerSocketGraphicItem *outSocketAt( int index ) const;
254
255
261 virtual void editComment() {}
262
266 virtual bool canDeleteComponent() { return false; }
267
273 virtual void deleteComponent() {}
274
281 QList< QgsModelArrowItem * > incomingArrows();
282
289 QList< QgsModelArrowItem * > outgoingArrows();
290
291 signals:
292
293 // TODO - rework this, should be triggered externally when the model actually changes!
294
298 void requestModelRepaint();
299
307 void aboutToChange( const QString &text, const QString &id = QString() );
308
313 void changed();
314
318 void repaintArrows();
319
323 void updateArrowPaths();
324
328 void sizePositionChanged();
329
330 protected slots:
331
337 virtual void editComponent() {}
338
339 protected:
345 virtual void paintBackground( QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget = nullptr );
346
350 void paintOutline( QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget = nullptr );
351
356 QString truncatedTextForItem( const QString &text ) const;
357
361 virtual QColor fillColor( State state ) const = 0;
362
366 virtual QColor strokeColor( State state ) const = 0;
367
371 virtual QColor textColor( State state ) const = 0;
372
376 virtual Qt::PenStyle strokeStyle( State state ) const;
377
383 virtual QColor outlineColor() const { return QColor(); }
384
388 virtual Qt::Alignment titleAlignment() const;
389
393 virtual QPicture iconPicture() const;
394
398 virtual QPixmap iconPixmap() const;
399
403 virtual void updateStoredComponentPosition( const QPointF &pos, const QSizeF &size ) = 0;
404
408 void updateButtonPositions();
409
415 SIP_SKIP static constexpr QColor FALLBACK_COLOR = QColor( 128, 128, 128 ); /* mid gray */
416
417 private:
418 QSizeF itemSize() const;
419
420 void updateToolTip( const QPointF &pos );
421
422 void fold( Qt::Edge edge, bool folded );
423
424 std::unique_ptr<QgsProcessingModelComponent> mComponent;
425 QgsProcessingModelAlgorithm *mModel = nullptr;
426
427 bool mInitialized = false;
428 QgsModelDesignerFoldButtonGraphicItem *mExpandTopButton = nullptr;
429 QgsModelDesignerFoldButtonGraphicItem *mExpandBottomButton = nullptr;
430
431 QString mLabel;
432
433 QgsModelDesignerFlatButtonGraphicItem *mEditButton = nullptr;
434 QgsModelDesignerFlatButtonGraphicItem *mDeleteButton = nullptr;
435
436 QList< QgsModelDesignerSocketGraphicItem * > mInSockets;
437 QList< QgsModelDesignerSocketGraphicItem * > mOutSockets;
438
439
440 static constexpr double MIN_COMPONENT_WIDTH = 70;
441 static constexpr double MIN_COMPONENT_HEIGHT = 30;
442
443 static constexpr double DEFAULT_BUTTON_WIDTH = 16;
444 static constexpr double DEFAULT_BUTTON_HEIGHT = 16;
445 static constexpr double BUTTON_MARGIN = 2;
446 static constexpr double TEXT_MARGIN = 4;
447 static constexpr double RECT_PEN_SIZE = 2;
448 static constexpr double RECT_OUTLINE_SIZE = 10;
449 QSizeF mButtonSize { DEFAULT_BUTTON_WIDTH, DEFAULT_BUTTON_HEIGHT };
450
451 QFont mFont;
452
453 bool mIsHovering = false;
454 QSizeF mTempSize;
455};
456Q_DECLARE_OPERATORS_FOR_FLAGS( QgsModelComponentGraphicItem::Flags )
457
458
464class GUI_EXPORT QgsModelParameterGraphicItem : public QgsModelComponentGraphicItem
465{
466 Q_OBJECT
467
468 public:
477 QgsModelParameterGraphicItem( QgsProcessingModelParameter *parameter SIP_TRANSFER, QgsProcessingModelAlgorithm *model, QGraphicsItem *parent SIP_TRANSFERTHIS );
478
479 void contextMenuEvent( QGraphicsSceneContextMenuEvent *event ) override;
480 bool canDeleteComponent() override;
481
482 QColor linkColor( Qt::Edge edge, int index ) const override;
483
484 protected:
485 QColor fillColor( State state ) const override;
486 QColor strokeColor( State state ) const override;
487 QColor textColor( State state ) const override;
488 QPicture iconPicture() const override;
489
490 int linkPointCount( Qt::Edge edge ) const override;
491 QString linkPointText( Qt::Edge edge, int index ) const override;
492 void updateStoredComponentPosition( const QPointF &pos, const QSizeF &size ) override;
493
494 protected slots:
495
496 void deleteComponent() override;
497
498 private:
499 QPicture mPicture;
500};
501
508class GUI_EXPORT QgsModelChildAlgorithmGraphicItem : public QgsModelComponentGraphicItem
509{
510 Q_OBJECT
511
512 public:
521 QgsModelChildAlgorithmGraphicItem( QgsProcessingModelChildAlgorithm *child SIP_TRANSFER, QgsProcessingModelAlgorithm *model, QGraphicsItem *parent SIP_TRANSFERTHIS );
522 void contextMenuEvent( QGraphicsSceneContextMenuEvent *event ) override;
523 bool canDeleteComponent() override;
524
528 void setResults( const QgsProcessingModelChildAlgorithmResult &results );
529
537 void setSourceFeatureCount( const QString &parameterName, long long featureCount );
538
546 void setSinkFeatureCount( const QString &outputName, long long featureCount );
547
553 QgsProcessingModelChildAlgorithmResult results() { return mResults; };
554
558 void setProgress( double progress );
559
565 void setStarted();
566
572 void setOutdated();
573
580 int indexForInput( const QString &parameterName ) const;
581
588 int indexForOutput( const QString &output ) const;
589
590 signals:
591
597 void runFromHere();
598
604 void runSelected();
605
611 void showPreviousResults();
612
618 void showLog();
619
626 void rebuildConfigurationDockWidget();
627
628 protected:
629 void paintBackground( QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget = nullptr ) override;
630
631 QColor fillColor( State state ) const override;
632 QColor strokeColor( State state ) const override;
633 QColor textColor( State state ) const override;
634 QColor outlineColor() const override;
635 QPixmap iconPixmap() const override;
636 QPicture iconPicture() const override;
637
638 int linkPointCount( Qt::Edge edge ) const override;
639 QString linkPointText( Qt::Edge edge, int index ) const override;
640 void updateStoredComponentPosition( const QPointF &pos, const QSizeF &size ) override;
641
642 protected slots:
643
644 void deleteComponent() override;
645
646 private slots:
647 void deactivateAlgorithm();
648 void activateAlgorithm();
649
650 private:
651 QPicture mPicture;
652 QPixmap mPixmap;
653 bool mStarted = false;
654 bool mOutdated = false;
655 QgsProcessingModelChildAlgorithmResult mResults;
656 double mProgress = -1;
657 bool mIsValid = true;
658};
659
660
667class GUI_EXPORT QgsModelOutputGraphicItem : public QgsModelComponentGraphicItem
668{
669 Q_OBJECT
670
671 public:
680 QgsModelOutputGraphicItem( QgsProcessingModelOutput *output SIP_TRANSFER, QgsProcessingModelAlgorithm *model, QGraphicsItem *parent SIP_TRANSFERTHIS );
681
682 bool canDeleteComponent() override;
683
684 protected:
685 QColor fillColor( State state ) const override;
686 QColor strokeColor( State state ) const override;
687 QColor textColor( State state ) const override;
688 QPicture iconPicture() const override;
689 void updateStoredComponentPosition( const QPointF &pos, const QSizeF &size ) override;
690
691 protected slots:
692
693 void deleteComponent() override;
694
695 private:
696 QPicture mPicture;
697};
698
699
706class GUI_EXPORT QgsModelCommentGraphicItem : public QgsModelComponentGraphicItem
707{
708 Q_OBJECT
709
710 public:
719 QgsModelCommentGraphicItem( QgsProcessingModelComment *comment SIP_TRANSFER, QgsModelComponentGraphicItem *parentItem, QgsProcessingModelAlgorithm *model, QGraphicsItem *parent SIP_TRANSFERTHIS );
720 ~QgsModelCommentGraphicItem() override;
721 void contextMenuEvent( QGraphicsSceneContextMenuEvent *event ) override;
722 bool canDeleteComponent() override;
723
727 QgsModelComponentGraphicItem *parentComponentItem() const;
728
729 protected:
730 QColor fillColor( State state ) const override;
731 QColor strokeColor( State state ) const override;
732 QColor textColor( State state ) const override;
733 Qt::PenStyle strokeStyle( State state ) const override;
734 void updateStoredComponentPosition( const QPointF &pos, const QSizeF &size ) override;
735
736 protected slots:
737
738 void deleteComponent() override;
739 void editComponent() override;
740
741 private:
742 QgsProcessingModelComment *modelComponent();
743
744 std::unique_ptr<QgsProcessingModelComponent> mParentComponent;
745 QPointer<QgsModelComponentGraphicItem> mParentItem;
746};
747
748
755class GUI_EXPORT QgsModelGroupBoxGraphicItem : public QgsModelComponentGraphicItem
756{
757 Q_OBJECT
758
759 public:
768 QgsModelGroupBoxGraphicItem( QgsProcessingModelGroupBox *box SIP_TRANSFER, QgsProcessingModelAlgorithm *model, QGraphicsItem *parent SIP_TRANSFERTHIS );
769 ~QgsModelGroupBoxGraphicItem() override;
770 void contextMenuEvent( QGraphicsSceneContextMenuEvent *event ) override;
771 bool canDeleteComponent() override;
772
778 void applyEdit( const QgsProcessingModelGroupBox &groupBox );
779
780 protected:
781 QColor fillColor( State state ) const override;
782 QColor strokeColor( State state ) const override;
783 QColor textColor( State state ) const override;
784 Qt::PenStyle strokeStyle( State state ) const override;
785 Qt::Alignment titleAlignment() const override;
786 void updateStoredComponentPosition( const QPointF &pos, const QSizeF &size ) override;
787
788 protected slots:
789
790 void deleteComponent() override;
791 void editComponent() override;
792
793 private:
794};
795
797
798#endif // QGSMODELCOMPONENTGRAPHICITEM_H
A mouse event which is the result of a user interaction with a QgsModelGraphicsView.
#define SIP_TRANSFERTHIS
Definition qgis_sip.h:52
#define SIP_ENUM_BASETYPE(type)
Definition qgis_sip.h:272
#define SIP_SKIP
Definition qgis_sip.h:133
#define SIP_TRANSFER
Definition qgis_sip.h:35
#define SIP_OUT
Definition qgis_sip.h:57
Q_DECLARE_OPERATORS_FOR_FLAGS(QgsProjectionSelectionWidget::CrsOptions)