QGIS API Documentation 3.43.0-Master (e01d6d7c4c0)
qgsmodelgraphicitem.cpp
Go to the documentation of this file.
1/***************************************************************************
2 qgsmodelgraphicitem.cpp
3 ----------------------------------
4 Date : February 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#include "qgsmodelgraphicitem.h"
18#include "moc_qgsmodelgraphicitem.cpp"
19#include "qgsapplication.h"
22#include "qgsmodelviewtool.h"
24#include <QGraphicsSceneMouseEvent>
25#include <QPainter>
26#include <QSvgRenderer>
27
29
30QgsModelDesignerFlatButtonGraphicItem::QgsModelDesignerFlatButtonGraphicItem( QGraphicsItem *parent, const QPicture &picture, const QPointF &position, const QSizeF &size )
31 : QGraphicsObject( parent )
32 , mPicture( picture )
33 , mPosition( position )
34 , mSize( size )
35{
36 setAcceptHoverEvents( true );
37 setFlag( QGraphicsItem::ItemIsMovable, false );
38 setCacheMode( QGraphicsItem::DeviceCoordinateCache );
39}
40
41void QgsModelDesignerFlatButtonGraphicItem::paint( QPainter *painter, const QStyleOptionGraphicsItem *, QWidget * )
42{
43 if ( QgsModelGraphicsScene *modelScene = qobject_cast<QgsModelGraphicsScene *>( scene() ) )
44 {
45 if ( modelScene->flags() & QgsModelGraphicsScene::FlagHideControls )
46 return;
47 }
48
49 if ( mHoverState )
50 {
51 painter->setPen( QPen( Qt::transparent, 1.0 ) );
52 painter->setBrush( QBrush( QColor( 55, 55, 55, 33 ), Qt::SolidPattern ) );
53 }
54 else
55 {
56 painter->setPen( QPen( Qt::transparent, 1.0 ) );
57 painter->setBrush( QBrush( Qt::transparent, Qt::SolidPattern ) );
58 }
59 const QPointF topLeft = mPosition - QPointF( std::floor( mSize.width() / 2 ), std::floor( mSize.height() / 2 ) );
60 const QRectF rect = QRectF( topLeft.x(), topLeft.y(), mSize.width(), mSize.height() );
61 painter->drawRect( rect );
62 painter->drawPicture( topLeft.x(), topLeft.y(), mPicture );
63}
64
65QRectF QgsModelDesignerFlatButtonGraphicItem::boundingRect() const
66{
67 return QRectF( mPosition.x() - std::floor( mSize.width() / 2 ), mPosition.y() - std::floor( mSize.height() / 2 ), mSize.width(), mSize.height() );
68}
69
70void QgsModelDesignerFlatButtonGraphicItem::hoverEnterEvent( QGraphicsSceneHoverEvent * )
71{
72 if ( view()->tool() && !view()->tool()->allowItemInteraction() )
73 mHoverState = false;
74 else
75 mHoverState = true;
76 update();
77}
78
79void QgsModelDesignerFlatButtonGraphicItem::hoverLeaveEvent( QGraphicsSceneHoverEvent * )
80{
81 mHoverState = false;
82 update();
83}
84
85void QgsModelDesignerFlatButtonGraphicItem::mousePressEvent( QGraphicsSceneMouseEvent * )
86{
87 if ( view()->tool() && view()->tool()->allowItemInteraction() )
88 emit clicked();
89}
90
91void QgsModelDesignerFlatButtonGraphicItem::modelHoverEnterEvent( QgsModelViewMouseEvent * )
92{
93 if ( view()->tool() && !view()->tool()->allowItemInteraction() )
94 mHoverState = false;
95 else
96 mHoverState = true;
97 update();
98}
99
100void QgsModelDesignerFlatButtonGraphicItem::modelHoverLeaveEvent( QgsModelViewMouseEvent * )
101{
102 mHoverState = false;
103 update();
104}
105
106void QgsModelDesignerFlatButtonGraphicItem::modelPressEvent( QgsModelViewMouseEvent *event )
107{
108 if ( view()->tool() && view()->tool()->allowItemInteraction() && event->button() == Qt::LeftButton )
109 {
110 QMetaObject::invokeMethod( this, "clicked", Qt::QueuedConnection );
111 mHoverState = false;
112 update();
113 }
114}
115
116void QgsModelDesignerFlatButtonGraphicItem::setPosition( const QPointF &position )
117{
118 mPosition = position;
119 prepareGeometryChange();
120 update();
121}
122
123QgsModelGraphicsView *QgsModelDesignerFlatButtonGraphicItem::view()
124{
125 return qobject_cast<QgsModelGraphicsView *>( scene()->views().first() );
126}
127
128void QgsModelDesignerFlatButtonGraphicItem::setPicture( const QPicture &picture )
129{
130 mPicture = picture;
131 update();
132}
133
134//
135// QgsModelDesignerFoldButtonGraphicItem
136//
137
138QgsModelDesignerFoldButtonGraphicItem::QgsModelDesignerFoldButtonGraphicItem( QGraphicsItem *parent, bool folded, const QPointF &position, const QSizeF &size )
139 : QgsModelDesignerFlatButtonGraphicItem( parent, QPicture(), position, size )
140 , mFolded( folded )
141{
142 QSvgRenderer svg( QgsApplication::iconPath( QStringLiteral( "mIconModelerExpand.svg" ) ) );
143 QPainter painter( &mPlusPicture );
144 svg.render( &painter );
145 painter.end();
146
147 QSvgRenderer svg2( QgsApplication::iconPath( QStringLiteral( "mIconModelerCollapse.svg" ) ) );
148 painter.begin( &mMinusPicture );
149 svg2.render( &painter );
150 painter.end();
151
152 setPicture( mFolded ? mPlusPicture : mMinusPicture );
153}
154
155void QgsModelDesignerFoldButtonGraphicItem::mousePressEvent( QGraphicsSceneMouseEvent *event )
156{
157 mFolded = !mFolded;
158 setPicture( mFolded ? mPlusPicture : mMinusPicture );
159 emit folded( mFolded );
160 QgsModelDesignerFlatButtonGraphicItem::mousePressEvent( event );
161}
162
163void QgsModelDesignerFoldButtonGraphicItem::modelPressEvent( QgsModelViewMouseEvent *event )
164{
165 mFolded = !mFolded;
166 setPicture( mFolded ? mPlusPicture : mMinusPicture );
167 emit folded( mFolded );
168 QgsModelDesignerFlatButtonGraphicItem::modelPressEvent( event );
169}
170
171
172QgsModelDesignerSocketGraphicItem::QgsModelDesignerSocketGraphicItem( QgsModelComponentGraphicItem *parent, QgsProcessingModelComponent *component, int index, const QPointF &position, Qt::Edge edge, const QSizeF &size )
173 : QgsModelDesignerFlatButtonGraphicItem( parent, QPicture(), position, size )
174 , mComponentItem( parent )
175 , mComponent( component )
176 , mIndex( index )
177 , mEdge( edge )
178{
179}
180
181void QgsModelDesignerSocketGraphicItem::paint( QPainter *painter, const QStyleOptionGraphicsItem *, QWidget * )
182{
183 painter->setPen( QPen() );
184 painter->setBrush( QBrush( QColor( 0, 0, 0, mHoverState ? 200 : 33 ), Qt::SolidPattern ) );
185
186 painter->setRenderHint( QPainter::Antialiasing );
187
188 constexpr float DISPLAY_SIZE = 3.2;
189 painter->drawEllipse( position(), DISPLAY_SIZE, DISPLAY_SIZE );
190 /* Uncomment to display bounding box */
191#if 0
192 painter->save();
193 painter->setPen( QPen() );
194 painter->setBrush( QBrush() );
195 painter->drawRect( boundingRect() );
196 painter->restore();
197#endif
198}
199
static QString iconPath(const QString &iconFile)
Returns path to the desired icon file.
A mouse event which is the result of a user interaction with a QgsModelGraphicsView.