QGIS API Documentation 3.41.0-Master (45a0abf3bec)
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 QSizeF mTempSize;
389
390};
391Q_DECLARE_OPERATORS_FOR_FLAGS( QgsModelComponentGraphicItem::Flags )
392
393
399class GUI_EXPORT QgsModelParameterGraphicItem : public QgsModelComponentGraphicItem
400{
401 Q_OBJECT
402
403 public:
404
413 QgsModelParameterGraphicItem( QgsProcessingModelParameter *parameter SIP_TRANSFER,
414 QgsProcessingModelAlgorithm *model,
415 QGraphicsItem *parent SIP_TRANSFERTHIS );
416
417 void contextMenuEvent( QGraphicsSceneContextMenuEvent *event ) override;
418 bool canDeleteComponent() override;
419
420 protected:
421
422 QColor fillColor( State state ) const override;
423 QColor strokeColor( State state ) const override;
424 QColor textColor( State state ) const override;
425 QPicture iconPicture() const override;
426 void updateStoredComponentPosition( const QPointF &pos, const QSizeF &size ) override;
427
428 protected slots:
429
430 void deleteComponent() override;
431
432 private:
433 QPicture mPicture;
434
435};
436
443class GUI_EXPORT QgsModelChildAlgorithmGraphicItem : public QgsModelComponentGraphicItem
444{
445 Q_OBJECT
446
447 public:
448
457 QgsModelChildAlgorithmGraphicItem( QgsProcessingModelChildAlgorithm *child SIP_TRANSFER,
458 QgsProcessingModelAlgorithm *model,
459 QGraphicsItem *parent SIP_TRANSFERTHIS );
460 void contextMenuEvent( QGraphicsSceneContextMenuEvent *event ) override;
461 bool canDeleteComponent() override;
462
466 void setResults( const QgsProcessingModelChildAlgorithmResult &results );
467
468 signals:
469
475 void runFromHere();
476
482 void runSelected();
483
489 void showPreviousResults();
490
496 void showLog();
497
498 protected:
499
500 QColor fillColor( State state ) const override;
501 QColor strokeColor( State state ) const override;
502 QColor textColor( State state ) const override;
503 QPixmap iconPixmap() const override;
504 QPicture iconPicture() const override;
505
506 int linkPointCount( Qt::Edge edge ) const override;
507 QString linkPointText( Qt::Edge edge, int index ) const override;
508 void updateStoredComponentPosition( const QPointF &pos, const QSizeF &size ) override;
509
510 protected slots:
511
512 void deleteComponent() override;
513
514 private slots:
515 void deactivateAlgorithm();
516 void activateAlgorithm();
517
518 private:
519 QPicture mPicture;
520 QPixmap mPixmap;
522 bool mIsValid = true;
523};
524
525
532class GUI_EXPORT QgsModelOutputGraphicItem : public QgsModelComponentGraphicItem
533{
534 Q_OBJECT
535
536 public:
537
546 QgsModelOutputGraphicItem( QgsProcessingModelOutput *output SIP_TRANSFER,
547 QgsProcessingModelAlgorithm *model,
548 QGraphicsItem *parent SIP_TRANSFERTHIS );
549
550 bool canDeleteComponent() override;
551
552 protected:
553
554 QColor fillColor( State state ) const override;
555 QColor strokeColor( State state ) const override;
556 QColor textColor( State state ) const override;
557 QPicture iconPicture() const override;
558 void updateStoredComponentPosition( const QPointF &pos, const QSizeF &size ) override;
559
560 protected slots:
561
562 void deleteComponent() override;
563
564 private:
565
566 QPicture mPicture;
567};
568
569
570
577class GUI_EXPORT QgsModelCommentGraphicItem : public QgsModelComponentGraphicItem
578{
579 Q_OBJECT
580
581 public:
582
591 QgsModelCommentGraphicItem( QgsProcessingModelComment *comment SIP_TRANSFER,
592 QgsModelComponentGraphicItem *parentItem,
593 QgsProcessingModelAlgorithm *model,
594 QGraphicsItem *parent SIP_TRANSFERTHIS );
595 ~QgsModelCommentGraphicItem() override;
596 void contextMenuEvent( QGraphicsSceneContextMenuEvent *event ) override;
597 bool canDeleteComponent() override;
598
602 QgsModelComponentGraphicItem *parentComponentItem() const;
603
604 protected:
605
606 QColor fillColor( State state ) const override;
607 QColor strokeColor( State state ) const override;
608 QColor textColor( State state ) const override;
609 Qt::PenStyle strokeStyle( State state ) const override;
610 void updateStoredComponentPosition( const QPointF &pos, const QSizeF &size ) override;
611
612 protected slots:
613
614 void deleteComponent() override;
615 void editComponent() override;
616 private:
617
618 QgsProcessingModelComment *modelComponent();
619
620 std::unique_ptr< QgsProcessingModelComponent > mParentComponent;
621 QPointer< QgsModelComponentGraphicItem > mParentItem;
622
623
624};
625
626
633class GUI_EXPORT QgsModelGroupBoxGraphicItem : public QgsModelComponentGraphicItem
634{
635 Q_OBJECT
636
637 public:
638
647 QgsModelGroupBoxGraphicItem( QgsProcessingModelGroupBox *box SIP_TRANSFER,
648 QgsProcessingModelAlgorithm *model,
649 QGraphicsItem *parent SIP_TRANSFERTHIS );
650 ~QgsModelGroupBoxGraphicItem() override;
651 void contextMenuEvent( QGraphicsSceneContextMenuEvent *event ) override;
652 bool canDeleteComponent() override;
653 protected:
654
655 QColor fillColor( State state ) const override;
656 QColor strokeColor( State state ) const override;
657 QColor textColor( State state ) const override;
658 Qt::PenStyle strokeStyle( State state ) const override;
659 Qt::Alignment titleAlignment() const override;
660 void updateStoredComponentPosition( const QPointF &pos, const QSizeF &size ) override;
661
662 protected slots:
663
664 void deleteComponent() override;
665 void editComponent() override;
666 private:
667
668
669};
670
672
673#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)