QGIS API Documentation 3.39.0-Master (3aed037ce22)
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#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 QgsModelGraphicsView;
37class QgsProcessingModelGroupBox;
38
40
47class GUI_EXPORT QgsModelComponentGraphicItem : public QGraphicsObject
48{
49 Q_OBJECT
50
51 public:
52
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,
78 QgsProcessingModelAlgorithm *model,
79 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 QgsModelGraphicsView *view();
107
112 QFont font() const;
113
118 void setFont( const QFont &font );
119
125 void moveComponentBy( qreal dx, qreal dy );
126
130 void previewItemMove( qreal dx, qreal dy );
131
135 void setItemRect( QRectF rect );
136
137#ifndef SIP_RUN
138
142 QRectF previewItemRectChange( QRectF rect );
143
147 void finalizePreviewedItemRectChange( QRectF rect );
148
152 virtual void modelHoverEnterEvent( QgsModelViewMouseEvent *event );
153
157 virtual void modelHoverMoveEvent( QgsModelViewMouseEvent *event );
158
162 virtual void modelHoverLeaveEvent( QgsModelViewMouseEvent *event );
163
167 virtual void modelDoubleClickEvent( QgsModelViewMouseEvent *event );
168#endif
169 void mouseDoubleClickEvent( QGraphicsSceneMouseEvent *event ) override;
170 void hoverEnterEvent( QGraphicsSceneHoverEvent *event ) override;
171 void hoverMoveEvent( QGraphicsSceneHoverEvent *event ) override;
172 void hoverLeaveEvent( QGraphicsSceneHoverEvent *event ) override;
173 QVariant itemChange( GraphicsItemChange change, const QVariant &value ) override;
174 QRectF boundingRect() const override;
175 bool contains( const QPointF &point ) const override;
176 void paint( QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget = nullptr ) override;
177
181 QRectF itemRect( bool storedRect = false ) const;
182
188 QString label() const;
189
195 void setLabel( const QString &label );
196
200 State state() const;
201
205 virtual int linkPointCount( Qt::Edge edge ) const;
206
210 virtual QString linkPointText( Qt::Edge edge, int index ) const;
211
215 QPointF linkPoint( Qt::Edge edge, int index, bool incoming ) const;
216
224 QPointF calculateAutomaticLinkPoint( QgsModelComponentGraphicItem *other, Qt::Edge &edge SIP_OUT ) const;
225
233 QPointF calculateAutomaticLinkPoint( const QPointF &point, Qt::Edge &edge SIP_OUT ) const;
234
240 virtual void editComment() {}
241
245 virtual bool canDeleteComponent() { return false; }
246
252 virtual void deleteComponent() {}
253
254 signals:
255
256 // TODO - rework this, should be triggered externally when the model actually changes!
257
261 void requestModelRepaint();
262
270 void aboutToChange( const QString &text, int id = 0 );
271
276 void changed();
277
281 void repaintArrows();
282
286 void updateArrowPaths();
287
291 void sizePositionChanged();
292
293 protected slots:
294
300 virtual void editComponent() {}
301
302 protected:
303
308 QString truncatedTextForItem( const QString &text ) const;
309
313 virtual QColor fillColor( State state ) const = 0;
314
318 virtual QColor strokeColor( State state ) const = 0;
319
323 virtual QColor textColor( State state ) const = 0;
324
328 virtual Qt::PenStyle strokeStyle( State state ) const;
329
333 virtual Qt::Alignment titleAlignment() const;
334
338 virtual QPicture iconPicture() const;
339
343 virtual QPixmap iconPixmap() const;
344
348 virtual void updateStoredComponentPosition( const QPointF &pos, const QSizeF &size ) = 0;
349
353 void updateButtonPositions();
354
355 private:
356
357 QSizeF itemSize() const;
358
359 void updateToolTip( const QPointF &pos );
360
361 void fold( Qt::Edge edge, bool folded );
362
363 std::unique_ptr< QgsProcessingModelComponent > mComponent;
364 QgsProcessingModelAlgorithm *mModel = nullptr;
365
366 bool mInitialized = false;
367 QgsModelDesignerFoldButtonGraphicItem *mExpandTopButton = nullptr;
368 QgsModelDesignerFoldButtonGraphicItem *mExpandBottomButton = nullptr;
369
370 QString mLabel;
371
372 QgsModelDesignerFlatButtonGraphicItem *mEditButton = nullptr;
373 QgsModelDesignerFlatButtonGraphicItem *mDeleteButton = nullptr;
374
375 static constexpr double MIN_COMPONENT_WIDTH = 70;
376 static constexpr double MIN_COMPONENT_HEIGHT = 30;
377
378 static constexpr double DEFAULT_BUTTON_WIDTH = 16;
379 static constexpr double DEFAULT_BUTTON_HEIGHT = 16;
380 static constexpr double BUTTON_MARGIN = 2;
381 static constexpr double TEXT_MARGIN = 4;
382 static constexpr double RECT_PEN_SIZE = 2;
383 QSizeF mButtonSize { DEFAULT_BUTTON_WIDTH, DEFAULT_BUTTON_HEIGHT };
384
385 QFont mFont;
386
387 bool mIsHovering = false;
388 bool mIsMoving = false;
389 QSizeF mTempSize;
390
391};
392Q_DECLARE_OPERATORS_FOR_FLAGS( QgsModelComponentGraphicItem::Flags )
393
394
400class GUI_EXPORT QgsModelParameterGraphicItem : public QgsModelComponentGraphicItem
401{
402 Q_OBJECT
403
404 public:
405
414 QgsModelParameterGraphicItem( QgsProcessingModelParameter *parameter SIP_TRANSFER,
415 QgsProcessingModelAlgorithm *model,
416 QGraphicsItem *parent SIP_TRANSFERTHIS );
417
418 void contextMenuEvent( QGraphicsSceneContextMenuEvent *event ) override;
419 bool canDeleteComponent() override;
420
421 protected:
422
423 QColor fillColor( State state ) const override;
424 QColor strokeColor( State state ) const override;
425 QColor textColor( State state ) const override;
426 QPicture iconPicture() const override;
427 void updateStoredComponentPosition( const QPointF &pos, const QSizeF &size ) override;
428
429 protected slots:
430
431 void deleteComponent() override;
432
433 private:
434 QPicture mPicture;
435
436};
437
444class GUI_EXPORT QgsModelChildAlgorithmGraphicItem : public QgsModelComponentGraphicItem
445{
446 Q_OBJECT
447
448 public:
449
458 QgsModelChildAlgorithmGraphicItem( QgsProcessingModelChildAlgorithm *child SIP_TRANSFER,
459 QgsProcessingModelAlgorithm *model,
460 QGraphicsItem *parent SIP_TRANSFERTHIS );
461 void contextMenuEvent( QGraphicsSceneContextMenuEvent *event ) override;
462 bool canDeleteComponent() override;
463
467 void setResults( const QgsProcessingModelChildAlgorithmResult &results );
468
469 signals:
470
476 void runFromHere();
477
483 void runSelected();
484
490 void showPreviousResults();
491
497 void showLog();
498
499 protected:
500
501 QColor fillColor( State state ) const override;
502 QColor strokeColor( State state ) const override;
503 QColor textColor( State state ) const override;
504 QPixmap iconPixmap() const override;
505 QPicture iconPicture() const override;
506
507 int linkPointCount( Qt::Edge edge ) const override;
508 QString linkPointText( Qt::Edge edge, int index ) const override;
509 void updateStoredComponentPosition( const QPointF &pos, const QSizeF &size ) override;
510
511 protected slots:
512
513 void deleteComponent() override;
514
515 private slots:
516 void deactivateAlgorithm();
517 void activateAlgorithm();
518
519 private:
520 QPicture mPicture;
521 QPixmap mPixmap;
523 bool mIsValid = true;
524};
525
526
533class GUI_EXPORT QgsModelOutputGraphicItem : public QgsModelComponentGraphicItem
534{
535 Q_OBJECT
536
537 public:
538
547 QgsModelOutputGraphicItem( QgsProcessingModelOutput *output SIP_TRANSFER,
548 QgsProcessingModelAlgorithm *model,
549 QGraphicsItem *parent SIP_TRANSFERTHIS );
550
551 bool canDeleteComponent() override;
552
553 protected:
554
555 QColor fillColor( State state ) const override;
556 QColor strokeColor( State state ) const override;
557 QColor textColor( State state ) const override;
558 QPicture iconPicture() const override;
559 void updateStoredComponentPosition( const QPointF &pos, const QSizeF &size ) override;
560
561 protected slots:
562
563 void deleteComponent() override;
564
565 private:
566
567 QPicture mPicture;
568};
569
570
571
578class GUI_EXPORT QgsModelCommentGraphicItem : public QgsModelComponentGraphicItem
579{
580 Q_OBJECT
581
582 public:
583
592 QgsModelCommentGraphicItem( QgsProcessingModelComment *comment SIP_TRANSFER,
593 QgsModelComponentGraphicItem *parentItem,
594 QgsProcessingModelAlgorithm *model,
595 QGraphicsItem *parent SIP_TRANSFERTHIS );
596 ~QgsModelCommentGraphicItem() override;
597 void contextMenuEvent( QGraphicsSceneContextMenuEvent *event ) override;
598 bool canDeleteComponent() override;
599
603 QgsModelComponentGraphicItem *parentComponentItem() const;
604
605 protected:
606
607 QColor fillColor( State state ) const override;
608 QColor strokeColor( State state ) const override;
609 QColor textColor( State state ) const override;
610 Qt::PenStyle strokeStyle( State state ) const override;
611 void updateStoredComponentPosition( const QPointF &pos, const QSizeF &size ) override;
612
613 protected slots:
614
615 void deleteComponent() override;
616 void editComponent() override;
617 private:
618
619 QgsProcessingModelComment *modelComponent();
620
621 std::unique_ptr< QgsProcessingModelComponent > mParentComponent;
622 QPointer< QgsModelComponentGraphicItem > mParentItem;
623
624
625};
626
627
634class GUI_EXPORT QgsModelGroupBoxGraphicItem : public QgsModelComponentGraphicItem
635{
636 Q_OBJECT
637
638 public:
639
648 QgsModelGroupBoxGraphicItem( QgsProcessingModelGroupBox *box SIP_TRANSFER,
649 QgsProcessingModelAlgorithm *model,
650 QGraphicsItem *parent SIP_TRANSFERTHIS );
651 ~QgsModelGroupBoxGraphicItem() override;
652 void contextMenuEvent( QGraphicsSceneContextMenuEvent *event ) override;
653 bool canDeleteComponent() override;
654 protected:
655
656 QColor fillColor( State state ) const override;
657 QColor strokeColor( State state ) const override;
658 QColor textColor( State state ) const override;
659 Qt::PenStyle strokeStyle( State state ) const override;
660 Qt::Alignment titleAlignment() const override;
661 void updateStoredComponentPosition( const QPointF &pos, const QSizeF &size ) override;
662
663 protected slots:
664
665 void deleteComponent() override;
666 void editComponent() override;
667 private:
668
669
670};
671
673
674#endif // QGSMODELCOMPONENTGRAPHICITEM_H
A QgsModelViewMouseEvent is the result of a user interaction with the mouse on 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)