QGIS API Documentation  3.21.0-Master (909859188c)
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"
21 #include <QGraphicsObject>
22 #include <QFont>
23 #include <QPicture>
24 #include <QPointer>
25 
26 class QgsProcessingModelComponent;
27 class QgsProcessingModelParameter;
28 class QgsProcessingModelChildAlgorithm;
29 class QgsProcessingModelOutput;
30 class QgsProcessingModelComment;
31 class QgsProcessingModelAlgorithm;
32 class QgsModelDesignerFlatButtonGraphicItem;
33 class QgsModelDesignerFoldButtonGraphicItem;
34 class QgsModelGraphicsView;
36 class QgsProcessingModelGroupBox;
37 
39 
46 class GUI_EXPORT QgsModelComponentGraphicItem : public QGraphicsObject
47 {
48  Q_OBJECT
49 
50  public:
51 
53  enum State
54  {
55  Normal,
56  Selected,
57  Hover,
58  };
59 
61  enum Flag
62  {
63  };
64  Q_DECLARE_FLAGS( Flags, Flag )
65 
66 
74  QgsModelComponentGraphicItem( QgsProcessingModelComponent *component SIP_TRANSFER,
75  QgsProcessingModelAlgorithm *model,
76  QGraphicsItem *parent SIP_TRANSFERTHIS );
77 
78  ~QgsModelComponentGraphicItem() override;
79 
83  virtual Flags flags() const;
84 
88  QgsProcessingModelComponent *component();
89 
93  const QgsProcessingModelComponent *component() const SIP_SKIP;
94 
98  QgsProcessingModelAlgorithm *model();
99 
103  QgsModelGraphicsView *view();
104 
109  QFont font() const;
110 
115  void setFont( const QFont &font );
116 
122  void moveComponentBy( qreal dx, qreal dy );
123 
127  void previewItemMove( qreal dx, qreal dy );
128 
132  void setItemRect( QRectF rect );
133 
134 #ifndef SIP_RUN
135 
139  QRectF previewItemRectChange( QRectF rect );
140 
144  void finalizePreviewedItemRectChange( QRectF rect );
145 
149  virtual void modelHoverEnterEvent( QgsModelViewMouseEvent *event );
150 
154  virtual void modelHoverMoveEvent( QgsModelViewMouseEvent *event );
155 
159  virtual void modelHoverLeaveEvent( QgsModelViewMouseEvent *event );
160 
164  virtual void modelDoubleClickEvent( QgsModelViewMouseEvent *event );
165 #endif
166  void mouseDoubleClickEvent( QGraphicsSceneMouseEvent *event ) override;
167  void hoverEnterEvent( QGraphicsSceneHoverEvent *event ) override;
168  void hoverMoveEvent( QGraphicsSceneHoverEvent *event ) override;
169  void hoverLeaveEvent( QGraphicsSceneHoverEvent *event ) override;
170  QVariant itemChange( GraphicsItemChange change, const QVariant &value ) override;
171  QRectF boundingRect() const override;
172  bool contains( const QPointF &point ) const override;
173  void paint( QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget = nullptr ) override;
174 
178  QRectF itemRect( bool storedRect = false ) const;
179 
185  QString label() const;
186 
192  void setLabel( const QString &label );
193 
197  State state() const;
198 
202  virtual int linkPointCount( Qt::Edge edge ) const;
203 
207  virtual QString linkPointText( Qt::Edge edge, int index ) const;
208 
212  QPointF linkPoint( Qt::Edge edge, int index, bool incoming ) const;
213 
221  QPointF calculateAutomaticLinkPoint( QgsModelComponentGraphicItem *other, Qt::Edge &edge SIP_OUT ) const;
222 
230  QPointF calculateAutomaticLinkPoint( const QPointF &point, Qt::Edge &edge SIP_OUT ) const;
231 
237  virtual void editComment() {}
238 
242  virtual bool canDeleteComponent() { return false; }
243 
249  virtual void deleteComponent() {}
250 
251  signals:
252 
253  // TODO - rework this, should be triggered externally when the model actually changes!
254 
258  void requestModelRepaint();
259 
267  void aboutToChange( const QString &text, int id = 0 );
268 
273  void changed();
274 
278  void repaintArrows();
279 
283  void updateArrowPaths();
284 
288  void sizePositionChanged();
289 
290  protected slots:
291 
297  virtual void editComponent() {}
298 
299  protected:
300 
305  QString truncatedTextForItem( const QString &text ) const;
306 
310  virtual QColor fillColor( State state ) const = 0;
311 
315  virtual QColor strokeColor( State state ) const = 0;
316 
320  virtual QColor textColor( State state ) const = 0;
321 
325  virtual Qt::PenStyle strokeStyle( State state ) const;
326 
330  virtual Qt::Alignment titleAlignment() const;
331 
335  virtual QPicture iconPicture() const;
336 
340  virtual QPixmap iconPixmap() const;
341 
345  virtual void updateStoredComponentPosition( const QPointF &pos, const QSizeF &size ) = 0;
346 
350  void updateButtonPositions();
351 
352  private:
353 
354  QSizeF itemSize() const;
355 
356  void updateToolTip( const QPointF &pos );
357 
358  void fold( Qt::Edge edge, bool folded );
359 
360  std::unique_ptr< QgsProcessingModelComponent > mComponent;
361  QgsProcessingModelAlgorithm *mModel = nullptr;
362 
363  bool mInitialized = false;
364  QgsModelDesignerFoldButtonGraphicItem *mExpandTopButton = nullptr;
365  QgsModelDesignerFoldButtonGraphicItem *mExpandBottomButton = nullptr;
366 
367  QString mLabel;
368 
369  QgsModelDesignerFlatButtonGraphicItem *mEditButton = nullptr;
370  QgsModelDesignerFlatButtonGraphicItem *mDeleteButton = nullptr;
371 
372  static constexpr double MIN_COMPONENT_WIDTH = 70;
373  static constexpr double MIN_COMPONENT_HEIGHT = 30;
374 
375  static constexpr double DEFAULT_BUTTON_WIDTH = 16;
376  static constexpr double DEFAULT_BUTTON_HEIGHT = 16;
377  static constexpr double BUTTON_MARGIN = 2;
378  static constexpr double TEXT_MARGIN = 4;
379  static constexpr double RECT_PEN_SIZE = 2;
380  QSizeF mButtonSize { DEFAULT_BUTTON_WIDTH, DEFAULT_BUTTON_HEIGHT };
381 
382  QFont mFont;
383 
384  bool mIsHovering = false;
385  bool mIsMoving = false;
386  QSizeF mTempSize;
387 
388 };
389 Q_DECLARE_OPERATORS_FOR_FLAGS( QgsModelComponentGraphicItem::Flags )
390 
391 
397 class GUI_EXPORT QgsModelParameterGraphicItem : public QgsModelComponentGraphicItem
398 {
399  Q_OBJECT
400 
401  public:
402 
411  QgsModelParameterGraphicItem( QgsProcessingModelParameter *parameter SIP_TRANSFER,
412  QgsProcessingModelAlgorithm *model,
413  QGraphicsItem *parent SIP_TRANSFERTHIS );
414 
415  void contextMenuEvent( QGraphicsSceneContextMenuEvent *event ) override;
416  bool canDeleteComponent() override;
417 
418  protected:
419 
420  QColor fillColor( State state ) const override;
421  QColor strokeColor( State state ) const override;
422  QColor textColor( State state ) const override;
423  QPicture iconPicture() const override;
424  void updateStoredComponentPosition( const QPointF &pos, const QSizeF &size ) override;
425 
426  protected slots:
427 
428  void deleteComponent() override;
429 
430  private:
431  QPicture mPicture;
432 
433 };
434 
441 class GUI_EXPORT QgsModelChildAlgorithmGraphicItem : public QgsModelComponentGraphicItem
442 {
443  Q_OBJECT
444 
445  public:
446 
455  QgsModelChildAlgorithmGraphicItem( QgsProcessingModelChildAlgorithm *child SIP_TRANSFER,
456  QgsProcessingModelAlgorithm *model,
457  QGraphicsItem *parent SIP_TRANSFERTHIS );
458  void contextMenuEvent( QGraphicsSceneContextMenuEvent *event ) override;
459  bool canDeleteComponent() override;
460 
464  void setResults( const QVariantMap &results );
465 
469  void setInputs( const QVariantMap &inputs );
470 
471  protected:
472 
473  QColor fillColor( State state ) const override;
474  QColor strokeColor( State state ) const override;
475  QColor textColor( State state ) const override;
476  QPixmap iconPixmap() const override;
477  QPicture iconPicture() const override;
478 
479  int linkPointCount( Qt::Edge edge ) const override;
480  QString linkPointText( Qt::Edge edge, int index ) const override;
481  void updateStoredComponentPosition( const QPointF &pos, const QSizeF &size ) override;
482 
483  protected slots:
484 
485  void deleteComponent() override;
486 
487  private slots:
488  void deactivateAlgorithm();
489  void activateAlgorithm();
490 
491  private:
492  QPicture mPicture;
493  QPixmap mPixmap;
494  QVariantMap mResults;
495  QVariantMap mInputs;
496  bool mIsValid = true;
497 };
498 
499 
506 class GUI_EXPORT QgsModelOutputGraphicItem : public QgsModelComponentGraphicItem
507 {
508  Q_OBJECT
509 
510  public:
511 
520  QgsModelOutputGraphicItem( QgsProcessingModelOutput *output SIP_TRANSFER,
521  QgsProcessingModelAlgorithm *model,
522  QGraphicsItem *parent SIP_TRANSFERTHIS );
523 
524  bool canDeleteComponent() override;
525 
526  protected:
527 
528  QColor fillColor( State state ) const override;
529  QColor strokeColor( State state ) const override;
530  QColor textColor( State state ) const override;
531  QPicture iconPicture() const override;
532  void updateStoredComponentPosition( const QPointF &pos, const QSizeF &size ) override;
533 
534  protected slots:
535 
536  void deleteComponent() override;
537 
538  private:
539 
540  QPicture mPicture;
541 };
542 
543 
544 
551 class GUI_EXPORT QgsModelCommentGraphicItem : public QgsModelComponentGraphicItem
552 {
553  Q_OBJECT
554 
555  public:
556 
565  QgsModelCommentGraphicItem( QgsProcessingModelComment *comment SIP_TRANSFER,
566  QgsModelComponentGraphicItem *parentItem,
567  QgsProcessingModelAlgorithm *model,
568  QGraphicsItem *parent SIP_TRANSFERTHIS );
569  ~QgsModelCommentGraphicItem() override;
570  void contextMenuEvent( QGraphicsSceneContextMenuEvent *event ) override;
571  bool canDeleteComponent() override;
572 
576  QgsModelComponentGraphicItem *parentComponentItem() const;
577 
578  protected:
579 
580  QColor fillColor( State state ) const override;
581  QColor strokeColor( State state ) const override;
582  QColor textColor( State state ) const override;
583  Qt::PenStyle strokeStyle( State state ) const override;
584  void updateStoredComponentPosition( const QPointF &pos, const QSizeF &size ) override;
585 
586  protected slots:
587 
588  void deleteComponent() override;
589  void editComponent() override;
590  private:
591 
592  QgsProcessingModelComment *modelComponent();
593 
594  std::unique_ptr< QgsProcessingModelComponent > mParentComponent;
595  QPointer< QgsModelComponentGraphicItem > mParentItem;
596 
597 
598 };
599 
600 
607 class GUI_EXPORT QgsModelGroupBoxGraphicItem : public QgsModelComponentGraphicItem
608 {
609  Q_OBJECT
610 
611  public:
612 
621  QgsModelGroupBoxGraphicItem( QgsProcessingModelGroupBox *box SIP_TRANSFER,
622  QgsProcessingModelAlgorithm *model,
623  QGraphicsItem *parent SIP_TRANSFERTHIS );
624  ~QgsModelGroupBoxGraphicItem() override;
625  void contextMenuEvent( QGraphicsSceneContextMenuEvent *event ) override;
626  bool canDeleteComponent() override;
627  protected:
628 
629  QColor fillColor( State state ) const override;
630  QColor strokeColor( State state ) const override;
631  QColor textColor( State state ) const override;
632  Qt::PenStyle strokeStyle( State state ) const override;
633  Qt::Alignment titleAlignment() const override;
634  void updateStoredComponentPosition( const QPointF &pos, const QSizeF &size ) override;
635 
636  protected slots:
637 
638  void deleteComponent() override;
639  void editComponent() override;
640  private:
641 
642 
643 };
644 
646 
647 #endif // QGSMODELCOMPONENTGRAPHICITEM_H
A QgsModelViewMouseEvent is the result of a user interaction with the mouse on a QgsModelGraphicsView...
#define SIP_TRANSFERTHIS
Definition: qgis_sip.h:53
#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(QgsField::ConfigurationFlags) CORE_EXPORT QDataStream &operator<<(QDataStream &out
Writes the field to stream out. QGIS version compatibility is not guaranteed.