QGIS API Documentation  3.27.0-Master (597e8eebd4)
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  // For future API flexibility only and to avoid sip issues, remove when real entries are added to flags.
64  Unused = 1 << 0,
65  };
66  Q_DECLARE_FLAGS( Flags, Flag )
67 
68 
76  QgsModelComponentGraphicItem( QgsProcessingModelComponent *component SIP_TRANSFER,
77  QgsProcessingModelAlgorithm *model,
78  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  QgsModelGraphicsView *view();
106 
111  QFont font() const;
112 
117  void setFont( const QFont &font );
118 
124  void moveComponentBy( qreal dx, qreal dy );
125 
129  void previewItemMove( qreal dx, qreal dy );
130 
134  void setItemRect( QRectF rect );
135 
136 #ifndef SIP_RUN
137 
141  QRectF previewItemRectChange( QRectF rect );
142 
146  void finalizePreviewedItemRectChange( QRectF rect );
147 
151  virtual void modelHoverEnterEvent( QgsModelViewMouseEvent *event );
152 
156  virtual void modelHoverMoveEvent( QgsModelViewMouseEvent *event );
157 
161  virtual void modelHoverLeaveEvent( QgsModelViewMouseEvent *event );
162 
166  virtual void modelDoubleClickEvent( QgsModelViewMouseEvent *event );
167 #endif
168  void mouseDoubleClickEvent( QGraphicsSceneMouseEvent *event ) override;
169  void hoverEnterEvent( QGraphicsSceneHoverEvent *event ) override;
170  void hoverMoveEvent( QGraphicsSceneHoverEvent *event ) override;
171  void hoverLeaveEvent( QGraphicsSceneHoverEvent *event ) override;
172  QVariant itemChange( GraphicsItemChange change, const QVariant &value ) override;
173  QRectF boundingRect() const override;
174  bool contains( const QPointF &point ) const override;
175  void paint( QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget = nullptr ) override;
176 
180  QRectF itemRect( bool storedRect = false ) const;
181 
187  QString label() const;
188 
194  void setLabel( const QString &label );
195 
199  State state() const;
200 
204  virtual int linkPointCount( Qt::Edge edge ) const;
205 
209  virtual QString linkPointText( Qt::Edge edge, int index ) const;
210 
214  QPointF linkPoint( Qt::Edge edge, int index, bool incoming ) const;
215 
223  QPointF calculateAutomaticLinkPoint( QgsModelComponentGraphicItem *other, Qt::Edge &edge SIP_OUT ) const;
224 
232  QPointF calculateAutomaticLinkPoint( const QPointF &point, Qt::Edge &edge SIP_OUT ) const;
233 
239  virtual void editComment() {}
240 
244  virtual bool canDeleteComponent() { return false; }
245 
251  virtual void deleteComponent() {}
252 
253  signals:
254 
255  // TODO - rework this, should be triggered externally when the model actually changes!
256 
260  void requestModelRepaint();
261 
269  void aboutToChange( const QString &text, int id = 0 );
270 
275  void changed();
276 
280  void repaintArrows();
281 
285  void updateArrowPaths();
286 
290  void sizePositionChanged();
291 
292  protected slots:
293 
299  virtual void editComponent() {}
300 
301  protected:
302 
307  QString truncatedTextForItem( const QString &text ) const;
308 
312  virtual QColor fillColor( State state ) const = 0;
313 
317  virtual QColor strokeColor( State state ) const = 0;
318 
322  virtual QColor textColor( State state ) const = 0;
323 
327  virtual Qt::PenStyle strokeStyle( State state ) const;
328 
332  virtual Qt::Alignment titleAlignment() const;
333 
337  virtual QPicture iconPicture() const;
338 
342  virtual QPixmap iconPixmap() const;
343 
347  virtual void updateStoredComponentPosition( const QPointF &pos, const QSizeF &size ) = 0;
348 
352  void updateButtonPositions();
353 
354  private:
355 
356  QSizeF itemSize() const;
357 
358  void updateToolTip( const QPointF &pos );
359 
360  void fold( Qt::Edge edge, bool folded );
361 
362  std::unique_ptr< QgsProcessingModelComponent > mComponent;
363  QgsProcessingModelAlgorithm *mModel = nullptr;
364 
365  bool mInitialized = false;
366  QgsModelDesignerFoldButtonGraphicItem *mExpandTopButton = nullptr;
367  QgsModelDesignerFoldButtonGraphicItem *mExpandBottomButton = nullptr;
368 
369  QString mLabel;
370 
371  QgsModelDesignerFlatButtonGraphicItem *mEditButton = nullptr;
372  QgsModelDesignerFlatButtonGraphicItem *mDeleteButton = nullptr;
373 
374  static constexpr double MIN_COMPONENT_WIDTH = 70;
375  static constexpr double MIN_COMPONENT_HEIGHT = 30;
376 
377  static constexpr double DEFAULT_BUTTON_WIDTH = 16;
378  static constexpr double DEFAULT_BUTTON_HEIGHT = 16;
379  static constexpr double BUTTON_MARGIN = 2;
380  static constexpr double TEXT_MARGIN = 4;
381  static constexpr double RECT_PEN_SIZE = 2;
382  QSizeF mButtonSize { DEFAULT_BUTTON_WIDTH, DEFAULT_BUTTON_HEIGHT };
383 
384  QFont mFont;
385 
386  bool mIsHovering = false;
387  bool mIsMoving = false;
388  QSizeF mTempSize;
389 
390 };
391 Q_DECLARE_OPERATORS_FOR_FLAGS( QgsModelComponentGraphicItem::Flags )
392 
393 
399 class 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 
443 class 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 QVariantMap &results );
467 
471  void setInputs( const QVariantMap &inputs );
472 
473  protected:
474 
475  QColor fillColor( State state ) const override;
476  QColor strokeColor( State state ) const override;
477  QColor textColor( State state ) const override;
478  QPixmap iconPixmap() const override;
479  QPicture iconPicture() const override;
480 
481  int linkPointCount( Qt::Edge edge ) const override;
482  QString linkPointText( Qt::Edge edge, int index ) const override;
483  void updateStoredComponentPosition( const QPointF &pos, const QSizeF &size ) override;
484 
485  protected slots:
486 
487  void deleteComponent() override;
488 
489  private slots:
490  void deactivateAlgorithm();
491  void activateAlgorithm();
492 
493  private:
494  QPicture mPicture;
495  QPixmap mPixmap;
496  QVariantMap mResults;
497  QVariantMap mInputs;
498  bool mIsValid = true;
499 };
500 
501 
508 class GUI_EXPORT QgsModelOutputGraphicItem : public QgsModelComponentGraphicItem
509 {
510  Q_OBJECT
511 
512  public:
513 
522  QgsModelOutputGraphicItem( QgsProcessingModelOutput *output SIP_TRANSFER,
523  QgsProcessingModelAlgorithm *model,
524  QGraphicsItem *parent SIP_TRANSFERTHIS );
525 
526  bool canDeleteComponent() override;
527 
528  protected:
529 
530  QColor fillColor( State state ) const override;
531  QColor strokeColor( State state ) const override;
532  QColor textColor( State state ) const override;
533  QPicture iconPicture() const override;
534  void updateStoredComponentPosition( const QPointF &pos, const QSizeF &size ) override;
535 
536  protected slots:
537 
538  void deleteComponent() override;
539 
540  private:
541 
542  QPicture mPicture;
543 };
544 
545 
546 
553 class GUI_EXPORT QgsModelCommentGraphicItem : public QgsModelComponentGraphicItem
554 {
555  Q_OBJECT
556 
557  public:
558 
567  QgsModelCommentGraphicItem( QgsProcessingModelComment *comment SIP_TRANSFER,
568  QgsModelComponentGraphicItem *parentItem,
569  QgsProcessingModelAlgorithm *model,
570  QGraphicsItem *parent SIP_TRANSFERTHIS );
571  ~QgsModelCommentGraphicItem() override;
572  void contextMenuEvent( QGraphicsSceneContextMenuEvent *event ) override;
573  bool canDeleteComponent() override;
574 
578  QgsModelComponentGraphicItem *parentComponentItem() const;
579 
580  protected:
581 
582  QColor fillColor( State state ) const override;
583  QColor strokeColor( State state ) const override;
584  QColor textColor( State state ) const override;
585  Qt::PenStyle strokeStyle( State state ) const override;
586  void updateStoredComponentPosition( const QPointF &pos, const QSizeF &size ) override;
587 
588  protected slots:
589 
590  void deleteComponent() override;
591  void editComponent() override;
592  private:
593 
594  QgsProcessingModelComment *modelComponent();
595 
596  std::unique_ptr< QgsProcessingModelComponent > mParentComponent;
597  QPointer< QgsModelComponentGraphicItem > mParentItem;
598 
599 
600 };
601 
602 
609 class GUI_EXPORT QgsModelGroupBoxGraphicItem : public QgsModelComponentGraphicItem
610 {
611  Q_OBJECT
612 
613  public:
614 
623  QgsModelGroupBoxGraphicItem( QgsProcessingModelGroupBox *box SIP_TRANSFER,
624  QgsProcessingModelAlgorithm *model,
625  QGraphicsItem *parent SIP_TRANSFERTHIS );
626  ~QgsModelGroupBoxGraphicItem() override;
627  void contextMenuEvent( QGraphicsSceneContextMenuEvent *event ) override;
628  bool canDeleteComponent() override;
629  protected:
630 
631  QColor fillColor( State state ) const override;
632  QColor strokeColor( State state ) const override;
633  QColor textColor( State state ) const override;
634  Qt::PenStyle strokeStyle( State state ) const override;
635  Qt::Alignment titleAlignment() const override;
636  void updateStoredComponentPosition( const QPointF &pos, const QSizeF &size ) override;
637 
638  protected slots:
639 
640  void deleteComponent() override;
641  void editComponent() override;
642  private:
643 
644 
645 };
646 
648 
649 #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.