QGIS API Documentation 3.99.0-Master (21b3aa880ba)
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
493 signals:
494
500 void runFromHere();
501
507 void runSelected();
508
514 void showPreviousResults();
515
521 void showLog();
522
523 protected:
524 QColor fillColor( State state ) const override;
525 QColor strokeColor( State state ) const override;
526 QColor textColor( State state ) const override;
527 QPixmap iconPixmap() const override;
528 QPicture iconPicture() const override;
529
530 int linkPointCount( Qt::Edge edge ) const override;
531 QString linkPointText( Qt::Edge edge, int index ) const override;
532 void updateStoredComponentPosition( const QPointF &pos, const QSizeF &size ) override;
533
534 protected slots:
535
536 void deleteComponent() override;
537
538 private slots:
539 void deactivateAlgorithm();
540 void activateAlgorithm();
541
542 private:
543 QPicture mPicture;
544 QPixmap mPixmap;
545 QgsProcessingModelChildAlgorithmResult mResults;
546 bool mIsValid = true;
547};
548
549
556class GUI_EXPORT QgsModelOutputGraphicItem : public QgsModelComponentGraphicItem
557{
558 Q_OBJECT
559
560 public:
569 QgsModelOutputGraphicItem( QgsProcessingModelOutput *output SIP_TRANSFER, QgsProcessingModelAlgorithm *model, QGraphicsItem *parent SIP_TRANSFERTHIS );
570
571 bool canDeleteComponent() override;
572
573 protected:
574 QColor fillColor( State state ) const override;
575 QColor strokeColor( State state ) const override;
576 QColor textColor( State state ) const override;
577 QPicture iconPicture() const override;
578 void updateStoredComponentPosition( const QPointF &pos, const QSizeF &size ) override;
579
580 protected slots:
581
582 void deleteComponent() override;
583
584 private:
585 QPicture mPicture;
586};
587
588
595class GUI_EXPORT QgsModelCommentGraphicItem : public QgsModelComponentGraphicItem
596{
597 Q_OBJECT
598
599 public:
608 QgsModelCommentGraphicItem( QgsProcessingModelComment *comment SIP_TRANSFER, QgsModelComponentGraphicItem *parentItem, QgsProcessingModelAlgorithm *model, QGraphicsItem *parent SIP_TRANSFERTHIS );
609 ~QgsModelCommentGraphicItem() override;
610 void contextMenuEvent( QGraphicsSceneContextMenuEvent *event ) override;
611 bool canDeleteComponent() override;
612
616 QgsModelComponentGraphicItem *parentComponentItem() const;
617
618 protected:
619 QColor fillColor( State state ) const override;
620 QColor strokeColor( State state ) const override;
621 QColor textColor( State state ) const override;
622 Qt::PenStyle strokeStyle( State state ) const override;
623 void updateStoredComponentPosition( const QPointF &pos, const QSizeF &size ) override;
624
625 protected slots:
626
627 void deleteComponent() override;
628 void editComponent() override;
629
630 private:
631 QgsProcessingModelComment *modelComponent();
632
633 std::unique_ptr<QgsProcessingModelComponent> mParentComponent;
634 QPointer<QgsModelComponentGraphicItem> mParentItem;
635};
636
637
644class GUI_EXPORT QgsModelGroupBoxGraphicItem : public QgsModelComponentGraphicItem
645{
646 Q_OBJECT
647
648 public:
657 QgsModelGroupBoxGraphicItem( QgsProcessingModelGroupBox *box SIP_TRANSFER, QgsProcessingModelAlgorithm *model, QGraphicsItem *parent SIP_TRANSFERTHIS );
658 ~QgsModelGroupBoxGraphicItem() override;
659 void contextMenuEvent( QGraphicsSceneContextMenuEvent *event ) override;
660 bool canDeleteComponent() override;
661
662 protected:
663 QColor fillColor( State state ) const override;
664 QColor strokeColor( State state ) const override;
665 QColor textColor( State state ) const override;
666 Qt::PenStyle strokeStyle( State state ) const override;
667 Qt::Alignment titleAlignment() const override;
668 void updateStoredComponentPosition( const QPointF &pos, const QSizeF &size ) override;
669
670 protected slots:
671
672 void deleteComponent() override;
673 void editComponent() override;
674
675 private:
676};
677
679
680#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)