QGIS API Documentation 3.43.0-Master (1af40ea95ba)
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#include <QGraphicsObject>
23#include <QFont>
24#include <QPicture>
25#include <QPointer>
26
27class QgsProcessingModelComponent;
28class QgsProcessingModelParameter;
29class QgsProcessingModelChildAlgorithm;
30class QgsProcessingModelOutput;
31class QgsProcessingModelComment;
32class QgsProcessingModelAlgorithm;
33class QgsModelDesignerFlatButtonGraphicItem;
34class QgsModelDesignerFoldButtonGraphicItem;
35class QgsModelDesignerSocketGraphicItem;
36class QgsModelGraphicsView;
38class QgsProcessingModelGroupBox;
39
41
48class GUI_EXPORT QgsModelComponentGraphicItem : public QGraphicsObject
49{
50 Q_OBJECT
51
52 public:
54 enum State
55 {
56 Normal,
57 Selected,
58 Hover,
59 };
60
62 enum Flag SIP_ENUM_BASETYPE( IntFlag )
63 {
64 // For future API flexibility only and to avoid sip issues, remove when real entries are added to flags.
65 Unused = 1 << 0,
66 };
67 Q_DECLARE_FLAGS( Flags, Flag )
68
69
77 QgsModelComponentGraphicItem( QgsProcessingModelComponent *component SIP_TRANSFER, QgsProcessingModelAlgorithm *model, QGraphicsItem *parent SIP_TRANSFERTHIS );
78
79 ~QgsModelComponentGraphicItem() override;
80
84 virtual Flags flags() const;
85
89 QgsProcessingModelComponent *component();
90
94 const QgsProcessingModelComponent *component() const SIP_SKIP;
95
99 QgsProcessingModelAlgorithm *model();
100
104 const QgsProcessingModelAlgorithm *model() const SIP_SKIP;
105
109 QgsModelGraphicsView *view();
110
115 QFont font() const;
116
121 void setFont( const QFont &font );
122
128 void moveComponentBy( qreal dx, qreal dy );
129
133 void previewItemMove( qreal dx, qreal dy );
134
138 void setItemRect( QRectF rect );
139
140#ifndef SIP_RUN
141
145 QRectF previewItemRectChange( QRectF rect );
146
150 void finalizePreviewedItemRectChange( QRectF rect );
151
155 virtual void modelHoverEnterEvent( QgsModelViewMouseEvent *event );
156
160 virtual void modelHoverMoveEvent( QgsModelViewMouseEvent *event );
161
165 virtual void modelHoverLeaveEvent( QgsModelViewMouseEvent *event );
166
170 virtual void modelDoubleClickEvent( QgsModelViewMouseEvent *event );
171#endif
172 void mouseDoubleClickEvent( QGraphicsSceneMouseEvent *event ) override;
173 void hoverEnterEvent( QGraphicsSceneHoverEvent *event ) override;
174 void hoverMoveEvent( QGraphicsSceneHoverEvent *event ) override;
175 void hoverLeaveEvent( QGraphicsSceneHoverEvent *event ) override;
176 QVariant itemChange( GraphicsItemChange change, const QVariant &value ) override;
177 QRectF boundingRect() const override;
178 bool contains( const QPointF &point ) const override;
179 void paint( QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget = nullptr ) override;
180
184 QRectF itemRect( bool storedRect = false ) const;
185
191 QString label() const;
192
198 void setLabel( const QString &label );
199
203 State state() const;
204
208 virtual int linkPointCount( Qt::Edge edge ) const;
209
213 virtual QString linkPointText( Qt::Edge edge, int index ) const;
214
218 QPointF linkPoint( Qt::Edge edge, int index, bool incoming ) const;
219
227 QPointF calculateAutomaticLinkPoint( QgsModelComponentGraphicItem *other, Qt::Edge &edge SIP_OUT ) const;
228
236 QPointF calculateAutomaticLinkPoint( const QPointF &point, Qt::Edge &edge SIP_OUT ) const;
237
244 QgsModelDesignerSocketGraphicItem *outSocketAt( int index ) const;
245
246
252 virtual void editComment() {}
253
257 virtual bool canDeleteComponent() { return false; }
258
264 virtual void deleteComponent() {}
265
266 signals:
267
268 // TODO - rework this, should be triggered externally when the model actually changes!
269
273 void requestModelRepaint();
274
282 void aboutToChange( const QString &text, int id = 0 );
283
288 void changed();
289
293 void repaintArrows();
294
298 void updateArrowPaths();
299
303 void sizePositionChanged();
304
305 protected slots:
306
312 virtual void editComponent() {}
313
314 protected:
319 QString truncatedTextForItem( const QString &text ) const;
320
324 virtual QColor fillColor( State state ) const = 0;
325
329 virtual QColor strokeColor( State state ) const = 0;
330
334 virtual QColor textColor( State state ) const = 0;
335
339 virtual Qt::PenStyle strokeStyle( State state ) const;
340
344 virtual Qt::Alignment titleAlignment() const;
345
349 virtual QPicture iconPicture() const;
350
354 virtual QPixmap iconPixmap() const;
355
359 virtual void updateStoredComponentPosition( const QPointF &pos, const QSizeF &size ) = 0;
360
364 void updateButtonPositions();
365
366 private:
367 QSizeF itemSize() const;
368
369 void updateToolTip( const QPointF &pos );
370
371 void fold( Qt::Edge edge, bool folded );
372
373 std::unique_ptr<QgsProcessingModelComponent> mComponent;
374 QgsProcessingModelAlgorithm *mModel = nullptr;
375
376 bool mInitialized = false;
377 QgsModelDesignerFoldButtonGraphicItem *mExpandTopButton = nullptr;
378 QgsModelDesignerFoldButtonGraphicItem *mExpandBottomButton = nullptr;
379
380 QString mLabel;
381
382 QgsModelDesignerFlatButtonGraphicItem *mEditButton = nullptr;
383 QgsModelDesignerFlatButtonGraphicItem *mDeleteButton = nullptr;
384
385 QList< QgsModelDesignerSocketGraphicItem * > mInSockets;
386 QList< QgsModelDesignerSocketGraphicItem * > mOutSockets;
387
388
389 static constexpr double MIN_COMPONENT_WIDTH = 70;
390 static constexpr double MIN_COMPONENT_HEIGHT = 30;
391
392 static constexpr double DEFAULT_BUTTON_WIDTH = 16;
393 static constexpr double DEFAULT_BUTTON_HEIGHT = 16;
394 static constexpr double BUTTON_MARGIN = 2;
395 static constexpr double TEXT_MARGIN = 4;
396 static constexpr double RECT_PEN_SIZE = 2;
397 QSizeF mButtonSize { DEFAULT_BUTTON_WIDTH, DEFAULT_BUTTON_HEIGHT };
398
399 QFont mFont;
400
401 bool mIsHovering = false;
402 QSizeF mTempSize;
403};
404Q_DECLARE_OPERATORS_FOR_FLAGS( QgsModelComponentGraphicItem::Flags )
405
406
412class GUI_EXPORT QgsModelParameterGraphicItem : public QgsModelComponentGraphicItem
413{
414 Q_OBJECT
415
416 public:
425 QgsModelParameterGraphicItem( QgsProcessingModelParameter *parameter SIP_TRANSFER, QgsProcessingModelAlgorithm *model, QGraphicsItem *parent SIP_TRANSFERTHIS );
426
427 void contextMenuEvent( QGraphicsSceneContextMenuEvent *event ) override;
428 bool canDeleteComponent() override;
429
430 protected:
431 QColor fillColor( State state ) const override;
432 QColor strokeColor( State state ) const override;
433 QColor textColor( State state ) const override;
434 QPicture iconPicture() const override;
435
436 int linkPointCount( Qt::Edge edge ) const override;
437 QString linkPointText( Qt::Edge edge, int index ) const override;
438 void updateStoredComponentPosition( const QPointF &pos, const QSizeF &size ) override;
439
440 protected slots:
441
442 void deleteComponent() override;
443
444 private:
445 QPicture mPicture;
446};
447
454class GUI_EXPORT QgsModelChildAlgorithmGraphicItem : public QgsModelComponentGraphicItem
455{
456 Q_OBJECT
457
458 public:
467 QgsModelChildAlgorithmGraphicItem( QgsProcessingModelChildAlgorithm *child SIP_TRANSFER, QgsProcessingModelAlgorithm *model, QGraphicsItem *parent SIP_TRANSFERTHIS );
468 void contextMenuEvent( QGraphicsSceneContextMenuEvent *event ) override;
469 bool canDeleteComponent() override;
470
474 void setResults( const QgsProcessingModelChildAlgorithmResult &results );
475
476 signals:
477
483 void runFromHere();
484
490 void runSelected();
491
497 void showPreviousResults();
498
504 void showLog();
505
506 protected:
507 QColor fillColor( State state ) const override;
508 QColor strokeColor( State state ) const override;
509 QColor textColor( State state ) const override;
510 QPixmap iconPixmap() const override;
511 QPicture iconPicture() const override;
512
513 int linkPointCount( Qt::Edge edge ) const override;
514 QString linkPointText( Qt::Edge edge, int index ) const override;
515 void updateStoredComponentPosition( const QPointF &pos, const QSizeF &size ) override;
516
517 protected slots:
518
519 void deleteComponent() override;
520
521 private slots:
522 void deactivateAlgorithm();
523 void activateAlgorithm();
524
525 private:
526 QPicture mPicture;
527 QPixmap mPixmap;
529 bool mIsValid = true;
530};
531
532
539class GUI_EXPORT QgsModelOutputGraphicItem : public QgsModelComponentGraphicItem
540{
541 Q_OBJECT
542
543 public:
552 QgsModelOutputGraphicItem( QgsProcessingModelOutput *output SIP_TRANSFER, QgsProcessingModelAlgorithm *model, QGraphicsItem *parent SIP_TRANSFERTHIS );
553
554 bool canDeleteComponent() override;
555
556 protected:
557 QColor fillColor( State state ) const override;
558 QColor strokeColor( State state ) const override;
559 QColor textColor( State state ) const override;
560 QPicture iconPicture() const override;
561 void updateStoredComponentPosition( const QPointF &pos, const QSizeF &size ) override;
562
563 protected slots:
564
565 void deleteComponent() override;
566
567 private:
568 QPicture mPicture;
569};
570
571
578class GUI_EXPORT QgsModelCommentGraphicItem : public QgsModelComponentGraphicItem
579{
580 Q_OBJECT
581
582 public:
591 QgsModelCommentGraphicItem( QgsProcessingModelComment *comment SIP_TRANSFER, QgsModelComponentGraphicItem *parentItem, QgsProcessingModelAlgorithm *model, QGraphicsItem *parent SIP_TRANSFERTHIS );
592 ~QgsModelCommentGraphicItem() override;
593 void contextMenuEvent( QGraphicsSceneContextMenuEvent *event ) override;
594 bool canDeleteComponent() override;
595
599 QgsModelComponentGraphicItem *parentComponentItem() const;
600
601 protected:
602 QColor fillColor( State state ) const override;
603 QColor strokeColor( State state ) const override;
604 QColor textColor( State state ) const override;
605 Qt::PenStyle strokeStyle( State state ) const override;
606 void updateStoredComponentPosition( const QPointF &pos, const QSizeF &size ) override;
607
608 protected slots:
609
610 void deleteComponent() override;
611 void editComponent() override;
612
613 private:
614 QgsProcessingModelComment *modelComponent();
615
616 std::unique_ptr<QgsProcessingModelComponent> mParentComponent;
617 QPointer<QgsModelComponentGraphicItem> mParentItem;
618};
619
620
627class GUI_EXPORT QgsModelGroupBoxGraphicItem : public QgsModelComponentGraphicItem
628{
629 Q_OBJECT
630
631 public:
640 QgsModelGroupBoxGraphicItem( QgsProcessingModelGroupBox *box SIP_TRANSFER, QgsProcessingModelAlgorithm *model, QGraphicsItem *parent SIP_TRANSFERTHIS );
641 ~QgsModelGroupBoxGraphicItem() override;
642 void contextMenuEvent( QGraphicsSceneContextMenuEvent *event ) override;
643 bool canDeleteComponent() override;
644
645 protected:
646 QColor fillColor( State state ) const override;
647 QColor strokeColor( State state ) const override;
648 QColor textColor( State state ) const override;
649 Qt::PenStyle strokeStyle( State state ) const override;
650 Qt::Alignment titleAlignment() const override;
651 void updateStoredComponentPosition( const QPointF &pos, const QSizeF &size ) override;
652
653 protected slots:
654
655 void deleteComponent() override;
656 void editComponent() override;
657
658 private:
659};
660
662
663#endif // QGSMODELCOMPONENTGRAPHICITEM_H
A mouse event which is the result of a user interaction with a QgsModelGraphicsView.
Encapsulates the results of running a child algorithm within a model.
#define SIP_TRANSFERTHIS
Definition qgis_sip.h:53
#define SIP_ENUM_BASETYPE(type)
Definition qgis_sip.h:278
#define SIP_SKIP
Definition qgis_sip.h:126
#define SIP_TRANSFER
Definition qgis_sip.h:36
#define SIP_OUT
Definition qgis_sip.h:58
Q_DECLARE_OPERATORS_FOR_FLAGS(QgsTextRendererUtils::CurvedTextFlags)