QGIS API Documentation 3.99.0-Master (09f76ad7019)
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;
40
42
49class GUI_EXPORT QgsModelComponentGraphicItem : public QGraphicsObject
50{
51 Q_OBJECT
52
53 public:
55 enum State
56 {
57 Normal,
58 Selected,
59 Hover,
60 };
61
63 enum Flag SIP_ENUM_BASETYPE( IntFlag )
64 {
65 // For future API flexibility only and to avoid sip issues, remove when real entries are added to flags.
66 Unused = 1 << 0,
67 };
68 Q_DECLARE_FLAGS( Flags, Flag )
69
70
78 QgsModelComponentGraphicItem( QgsProcessingModelComponent *component SIP_TRANSFER, QgsProcessingModelAlgorithm *model, QGraphicsItem *parent SIP_TRANSFERTHIS );
79
80 ~QgsModelComponentGraphicItem() override;
81
85 virtual Flags flags() const;
86
90 QgsProcessingModelComponent *component();
91
95 const QgsProcessingModelComponent *component() const SIP_SKIP;
96
100 QgsProcessingModelAlgorithm *model();
101
105 const QgsProcessingModelAlgorithm *model() const SIP_SKIP;
106
110 QgsModelGraphicsView *view();
111
116 QFont font() const;
117
122 void setFont( const QFont &font );
123
129 void moveComponentBy( qreal dx, qreal dy );
130
134 void previewItemMove( qreal dx, qreal dy );
135
139 void setItemRect( QRectF rect );
140
141#ifndef SIP_RUN
142
148 virtual QColor linkColor( Qt::Edge edge, int index ) const;
149
153 QRectF previewItemRectChange( QRectF rect );
154
158 void finalizePreviewedItemRectChange( QRectF rect );
159
163 virtual void modelHoverEnterEvent( QgsModelViewMouseEvent *event );
164
168 virtual void modelHoverMoveEvent( QgsModelViewMouseEvent *event );
169
173 virtual void modelHoverLeaveEvent( QgsModelViewMouseEvent *event );
174
178 virtual void modelDoubleClickEvent( QgsModelViewMouseEvent *event );
179#endif
180 void mouseDoubleClickEvent( QGraphicsSceneMouseEvent *event ) override;
181 void hoverEnterEvent( QGraphicsSceneHoverEvent *event ) override;
182 void hoverMoveEvent( QGraphicsSceneHoverEvent *event ) override;
183 void hoverLeaveEvent( QGraphicsSceneHoverEvent *event ) override;
184 QVariant itemChange( GraphicsItemChange change, const QVariant &value ) override;
185 QRectF boundingRect() const override;
186 bool contains( const QPointF &point ) const override;
187 void paint( QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget = nullptr ) override;
188
192 QRectF itemRect( bool storedRect = false ) const;
193
199 QString label() const;
200
206 void setLabel( const QString &label );
207
211 State state() const;
212
216 virtual int linkPointCount( Qt::Edge edge ) const;
217
221 virtual QString linkPointText( Qt::Edge edge, int index ) const;
222
226 QPointF linkPoint( Qt::Edge edge, int index, bool incoming ) const;
227
235 QPointF calculateAutomaticLinkPoint( QgsModelComponentGraphicItem *other, Qt::Edge &edge SIP_OUT ) const;
236
244 QPointF calculateAutomaticLinkPoint( const QPointF &point, Qt::Edge &edge SIP_OUT ) const;
245
252 QgsModelDesignerSocketGraphicItem *outSocketAt( int index ) const;
253
254
260 virtual void editComment() {}
261
265 virtual bool canDeleteComponent() { return false; }
266
272 virtual void deleteComponent() {}
273
274 signals:
275
276 // TODO - rework this, should be triggered externally when the model actually changes!
277
281 void requestModelRepaint();
282
290 void aboutToChange( const QString &text, int id = 0 );
291
296 void changed();
297
301 void repaintArrows();
302
306 void updateArrowPaths();
307
311 void sizePositionChanged();
312
313 protected slots:
314
320 virtual void editComponent() {}
321
322 protected:
327 QString truncatedTextForItem( const QString &text ) const;
328
332 virtual QColor fillColor( State state ) const = 0;
333
337 virtual QColor strokeColor( State state ) const = 0;
338
342 virtual QColor textColor( State state ) const = 0;
343
347 virtual Qt::PenStyle strokeStyle( State state ) const;
348
352 virtual Qt::Alignment titleAlignment() const;
353
357 virtual QPicture iconPicture() const;
358
362 virtual QPixmap iconPixmap() const;
363
367 virtual void updateStoredComponentPosition( const QPointF &pos, const QSizeF &size ) = 0;
368
372 void updateButtonPositions();
373
379 SIP_SKIP static constexpr QColor FALLBACK_COLOR = QColor( 128, 128, 128 ); /* mid gray */
380
381 private:
382 QSizeF itemSize() const;
383
384 void updateToolTip( const QPointF &pos );
385
386 void fold( Qt::Edge edge, bool folded );
387
388 std::unique_ptr<QgsProcessingModelComponent> mComponent;
389 QgsProcessingModelAlgorithm *mModel = nullptr;
390
391 bool mInitialized = false;
392 QgsModelDesignerFoldButtonGraphicItem *mExpandTopButton = nullptr;
393 QgsModelDesignerFoldButtonGraphicItem *mExpandBottomButton = nullptr;
394
395 QString mLabel;
396
397 QgsModelDesignerFlatButtonGraphicItem *mEditButton = nullptr;
398 QgsModelDesignerFlatButtonGraphicItem *mDeleteButton = nullptr;
399
400 QList< QgsModelDesignerSocketGraphicItem * > mInSockets;
401 QList< QgsModelDesignerSocketGraphicItem * > mOutSockets;
402
403
404 static constexpr double MIN_COMPONENT_WIDTH = 70;
405 static constexpr double MIN_COMPONENT_HEIGHT = 30;
406
407 static constexpr double DEFAULT_BUTTON_WIDTH = 16;
408 static constexpr double DEFAULT_BUTTON_HEIGHT = 16;
409 static constexpr double BUTTON_MARGIN = 2;
410 static constexpr double TEXT_MARGIN = 4;
411 static constexpr double RECT_PEN_SIZE = 2;
412 QSizeF mButtonSize { DEFAULT_BUTTON_WIDTH, DEFAULT_BUTTON_HEIGHT };
413
414 QFont mFont;
415
416 bool mIsHovering = false;
417 QSizeF mTempSize;
418};
419Q_DECLARE_OPERATORS_FOR_FLAGS( QgsModelComponentGraphicItem::Flags )
420
421
427class GUI_EXPORT QgsModelParameterGraphicItem : public QgsModelComponentGraphicItem
428{
429 Q_OBJECT
430
431 public:
440 QgsModelParameterGraphicItem( QgsProcessingModelParameter *parameter SIP_TRANSFER, QgsProcessingModelAlgorithm *model, QGraphicsItem *parent SIP_TRANSFERTHIS );
441
442 void contextMenuEvent( QGraphicsSceneContextMenuEvent *event ) override;
443 bool canDeleteComponent() override;
444
445 QColor linkColor( Qt::Edge edge, int index ) const override;
446
447 protected:
448 QColor fillColor( State state ) const override;
449 QColor strokeColor( State state ) const override;
450 QColor textColor( State state ) const override;
451 QPicture iconPicture() const override;
452
453 int linkPointCount( Qt::Edge edge ) const override;
454 QString linkPointText( Qt::Edge edge, int index ) const override;
455 void updateStoredComponentPosition( const QPointF &pos, const QSizeF &size ) override;
456
457 protected slots:
458
459 void deleteComponent() override;
460
461 private:
462 QPicture mPicture;
463};
464
471class GUI_EXPORT QgsModelChildAlgorithmGraphicItem : public QgsModelComponentGraphicItem
472{
473 Q_OBJECT
474
475 public:
484 QgsModelChildAlgorithmGraphicItem( QgsProcessingModelChildAlgorithm *child SIP_TRANSFER, QgsProcessingModelAlgorithm *model, QGraphicsItem *parent SIP_TRANSFERTHIS );
485 void contextMenuEvent( QGraphicsSceneContextMenuEvent *event ) override;
486 bool canDeleteComponent() override;
487
491 void setResults( const QgsProcessingModelChildAlgorithmResult &results );
492
498 QgsProcessingModelChildAlgorithmResult results() { return mResults; };
499
500
501 signals:
502
508 void runFromHere();
509
515 void runSelected();
516
522 void showPreviousResults();
523
529 void showLog();
530
531 protected:
532 QColor fillColor( State state ) const override;
533 QColor strokeColor( State state ) const override;
534 QColor textColor( State state ) const override;
535 QPixmap iconPixmap() const override;
536 QPicture iconPicture() const override;
537
538 int linkPointCount( Qt::Edge edge ) const override;
539 QString linkPointText( Qt::Edge edge, int index ) const override;
540 void updateStoredComponentPosition( const QPointF &pos, const QSizeF &size ) override;
541
542 protected slots:
543
544 void deleteComponent() override;
545
546 private slots:
547 void deactivateAlgorithm();
548 void activateAlgorithm();
549
550 private:
551 QPicture mPicture;
552 QPixmap mPixmap;
553 QgsProcessingModelChildAlgorithmResult mResults;
554 bool mIsValid = true;
555};
556
557
564class GUI_EXPORT QgsModelOutputGraphicItem : public QgsModelComponentGraphicItem
565{
566 Q_OBJECT
567
568 public:
577 QgsModelOutputGraphicItem( QgsProcessingModelOutput *output SIP_TRANSFER, QgsProcessingModelAlgorithm *model, QGraphicsItem *parent SIP_TRANSFERTHIS );
578
579 bool canDeleteComponent() override;
580
581 protected:
582 QColor fillColor( State state ) const override;
583 QColor strokeColor( State state ) const override;
584 QColor textColor( State state ) const override;
585 QPicture iconPicture() const override;
586 void updateStoredComponentPosition( const QPointF &pos, const QSizeF &size ) override;
587
588 protected slots:
589
590 void deleteComponent() override;
591
592 private:
593 QPicture mPicture;
594};
595
596
603class GUI_EXPORT QgsModelCommentGraphicItem : public QgsModelComponentGraphicItem
604{
605 Q_OBJECT
606
607 public:
616 QgsModelCommentGraphicItem( QgsProcessingModelComment *comment SIP_TRANSFER, QgsModelComponentGraphicItem *parentItem, QgsProcessingModelAlgorithm *model, QGraphicsItem *parent SIP_TRANSFERTHIS );
617 ~QgsModelCommentGraphicItem() override;
618 void contextMenuEvent( QGraphicsSceneContextMenuEvent *event ) override;
619 bool canDeleteComponent() override;
620
624 QgsModelComponentGraphicItem *parentComponentItem() const;
625
626 protected:
627 QColor fillColor( State state ) const override;
628 QColor strokeColor( State state ) const override;
629 QColor textColor( State state ) const override;
630 Qt::PenStyle strokeStyle( State state ) const override;
631 void updateStoredComponentPosition( const QPointF &pos, const QSizeF &size ) override;
632
633 protected slots:
634
635 void deleteComponent() override;
636 void editComponent() override;
637
638 private:
639 QgsProcessingModelComment *modelComponent();
640
641 std::unique_ptr<QgsProcessingModelComponent> mParentComponent;
642 QPointer<QgsModelComponentGraphicItem> mParentItem;
643};
644
645
652class GUI_EXPORT QgsModelGroupBoxGraphicItem : public QgsModelComponentGraphicItem
653{
654 Q_OBJECT
655
656 public:
665 QgsModelGroupBoxGraphicItem( QgsProcessingModelGroupBox *box SIP_TRANSFER, QgsProcessingModelAlgorithm *model, QGraphicsItem *parent SIP_TRANSFERTHIS );
666 ~QgsModelGroupBoxGraphicItem() override;
667 void contextMenuEvent( QGraphicsSceneContextMenuEvent *event ) override;
668 bool canDeleteComponent() override;
669
670 protected:
671 QColor fillColor( State state ) const override;
672 QColor strokeColor( State state ) const override;
673 QColor textColor( State state ) const override;
674 Qt::PenStyle strokeStyle( State state ) const override;
675 Qt::Alignment titleAlignment() const override;
676 void updateStoredComponentPosition( const QPointF &pos, const QSizeF &size ) override;
677
678 protected slots:
679
680 void deleteComponent() override;
681 void editComponent() override;
682
683 private:
684};
685
687
688#endif // QGSMODELCOMPONENTGRAPHICITEM_H
A mouse event which is the result of a user interaction with a QgsModelGraphicsView.
#define SIP_TRANSFERTHIS
Definition qgis_sip.h:53
#define SIP_ENUM_BASETYPE(type)
Definition qgis_sip.h:275
#define SIP_SKIP
Definition qgis_sip.h:134
#define SIP_TRANSFER
Definition qgis_sip.h:36
#define SIP_OUT
Definition qgis_sip.h:58
Q_DECLARE_OPERATORS_FOR_FLAGS(QgsProjectionSelectionWidget::CrsOptions)