QGIS API Documentation 3.41.0-Master (cea29feecf2)
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:
53 enum State
54 {
55 Normal,
56 Selected,
57 Hover,
58 };
59
61 enum Flag SIP_ENUM_BASETYPE( IntFlag )
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, QgsProcessingModelAlgorithm *model, 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:
304 QString truncatedTextForItem( const QString &text ) const;
305
309 virtual QColor fillColor( State state ) const = 0;
310
314 virtual QColor strokeColor( State state ) const = 0;
315
319 virtual QColor textColor( State state ) const = 0;
320
324 virtual Qt::PenStyle strokeStyle( State state ) const;
325
329 virtual Qt::Alignment titleAlignment() const;
330
334 virtual QPicture iconPicture() const;
335
339 virtual QPixmap iconPixmap() const;
340
344 virtual void updateStoredComponentPosition( const QPointF &pos, const QSizeF &size ) = 0;
345
349 void updateButtonPositions();
350
351 private:
352 QSizeF itemSize() const;
353
354 void updateToolTip( const QPointF &pos );
355
356 void fold( Qt::Edge edge, bool folded );
357
358 std::unique_ptr<QgsProcessingModelComponent> mComponent;
359 QgsProcessingModelAlgorithm *mModel = nullptr;
360
361 bool mInitialized = false;
362 QgsModelDesignerFoldButtonGraphicItem *mExpandTopButton = nullptr;
363 QgsModelDesignerFoldButtonGraphicItem *mExpandBottomButton = nullptr;
364
365 QString mLabel;
366
367 QgsModelDesignerFlatButtonGraphicItem *mEditButton = nullptr;
368 QgsModelDesignerFlatButtonGraphicItem *mDeleteButton = nullptr;
369
370 static constexpr double MIN_COMPONENT_WIDTH = 70;
371 static constexpr double MIN_COMPONENT_HEIGHT = 30;
372
373 static constexpr double DEFAULT_BUTTON_WIDTH = 16;
374 static constexpr double DEFAULT_BUTTON_HEIGHT = 16;
375 static constexpr double BUTTON_MARGIN = 2;
376 static constexpr double TEXT_MARGIN = 4;
377 static constexpr double RECT_PEN_SIZE = 2;
378 QSizeF mButtonSize { DEFAULT_BUTTON_WIDTH, DEFAULT_BUTTON_HEIGHT };
379
380 QFont mFont;
381
382 bool mIsHovering = false;
383 QSizeF mTempSize;
384};
385Q_DECLARE_OPERATORS_FOR_FLAGS( QgsModelComponentGraphicItem::Flags )
386
387
393class GUI_EXPORT QgsModelParameterGraphicItem : public QgsModelComponentGraphicItem
394{
395 Q_OBJECT
396
397 public:
406 QgsModelParameterGraphicItem( QgsProcessingModelParameter *parameter SIP_TRANSFER, QgsProcessingModelAlgorithm *model, QGraphicsItem *parent SIP_TRANSFERTHIS );
407
408 void contextMenuEvent( QGraphicsSceneContextMenuEvent *event ) override;
409 bool canDeleteComponent() override;
410
411 protected:
412 QColor fillColor( State state ) const override;
413 QColor strokeColor( State state ) const override;
414 QColor textColor( State state ) const override;
415 QPicture iconPicture() const override;
416 void updateStoredComponentPosition( const QPointF &pos, const QSizeF &size ) override;
417
418 protected slots:
419
420 void deleteComponent() override;
421
422 private:
423 QPicture mPicture;
424};
425
432class GUI_EXPORT QgsModelChildAlgorithmGraphicItem : public QgsModelComponentGraphicItem
433{
434 Q_OBJECT
435
436 public:
445 QgsModelChildAlgorithmGraphicItem( QgsProcessingModelChildAlgorithm *child SIP_TRANSFER, QgsProcessingModelAlgorithm *model, QGraphicsItem *parent SIP_TRANSFERTHIS );
446 void contextMenuEvent( QGraphicsSceneContextMenuEvent *event ) override;
447 bool canDeleteComponent() override;
448
452 void setResults( const QgsProcessingModelChildAlgorithmResult &results );
453
454 signals:
455
461 void runFromHere();
462
468 void runSelected();
469
475 void showPreviousResults();
476
482 void showLog();
483
484 protected:
485 QColor fillColor( State state ) const override;
486 QColor strokeColor( State state ) const override;
487 QColor textColor( State state ) const override;
488 QPixmap iconPixmap() const override;
489 QPicture iconPicture() const override;
490
491 int linkPointCount( Qt::Edge edge ) const override;
492 QString linkPointText( Qt::Edge edge, int index ) const override;
493 void updateStoredComponentPosition( const QPointF &pos, const QSizeF &size ) override;
494
495 protected slots:
496
497 void deleteComponent() override;
498
499 private slots:
500 void deactivateAlgorithm();
501 void activateAlgorithm();
502
503 private:
504 QPicture mPicture;
505 QPixmap mPixmap;
507 bool mIsValid = true;
508};
509
510
517class GUI_EXPORT QgsModelOutputGraphicItem : public QgsModelComponentGraphicItem
518{
519 Q_OBJECT
520
521 public:
530 QgsModelOutputGraphicItem( QgsProcessingModelOutput *output SIP_TRANSFER, QgsProcessingModelAlgorithm *model, QGraphicsItem *parent SIP_TRANSFERTHIS );
531
532 bool canDeleteComponent() override;
533
534 protected:
535 QColor fillColor( State state ) const override;
536 QColor strokeColor( State state ) const override;
537 QColor textColor( State state ) const override;
538 QPicture iconPicture() const override;
539 void updateStoredComponentPosition( const QPointF &pos, const QSizeF &size ) override;
540
541 protected slots:
542
543 void deleteComponent() override;
544
545 private:
546 QPicture mPicture;
547};
548
549
556class GUI_EXPORT QgsModelCommentGraphicItem : public QgsModelComponentGraphicItem
557{
558 Q_OBJECT
559
560 public:
569 QgsModelCommentGraphicItem( QgsProcessingModelComment *comment SIP_TRANSFER, QgsModelComponentGraphicItem *parentItem, QgsProcessingModelAlgorithm *model, QGraphicsItem *parent SIP_TRANSFERTHIS );
570 ~QgsModelCommentGraphicItem() override;
571 void contextMenuEvent( QGraphicsSceneContextMenuEvent *event ) override;
572 bool canDeleteComponent() override;
573
577 QgsModelComponentGraphicItem *parentComponentItem() const;
578
579 protected:
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
591 private:
592 QgsProcessingModelComment *modelComponent();
593
594 std::unique_ptr<QgsProcessingModelComponent> mParentComponent;
595 QPointer<QgsModelComponentGraphicItem> mParentItem;
596};
597
598
605class GUI_EXPORT QgsModelGroupBoxGraphicItem : public QgsModelComponentGraphicItem
606{
607 Q_OBJECT
608
609 public:
618 QgsModelGroupBoxGraphicItem( QgsProcessingModelGroupBox *box SIP_TRANSFER, QgsProcessingModelAlgorithm *model, QGraphicsItem *parent SIP_TRANSFERTHIS );
619 ~QgsModelGroupBoxGraphicItem() override;
620 void contextMenuEvent( QGraphicsSceneContextMenuEvent *event ) override;
621 bool canDeleteComponent() override;
622
623 protected:
624 QColor fillColor( State state ) const override;
625 QColor strokeColor( State state ) const override;
626 QColor textColor( State state ) const override;
627 Qt::PenStyle strokeStyle( State state ) const override;
628 Qt::Alignment titleAlignment() const override;
629 void updateStoredComponentPosition( const QPointF &pos, const QSizeF &size ) override;
630
631 protected slots:
632
633 void deleteComponent() override;
634 void editComponent() override;
635
636 private:
637};
638
640
641#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)