QGIS API Documentation 3.99.0-Master (357b655ed83)
Loading...
Searching...
No Matches
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"
17
18#include "qgsapplication.h"
23#include "qgsmodelviewtool.h"
29
30#include <QGraphicsSceneMouseEvent>
31#include <QPainter>
32#include <QString>
33#include <QSvgRenderer>
34
35#include "moc_qgsmodelgraphicitem.cpp"
36
37using namespace Qt::StringLiterals;
38
40
41QgsModelDesignerFlatButtonGraphicItem::QgsModelDesignerFlatButtonGraphicItem( QGraphicsItem *parent, const QPicture &picture, const QPointF &position, const QSizeF &size )
42 : QGraphicsObject( parent )
43 , mPicture( picture )
44 , mPosition( position )
45 , mSize( size )
46{
47 setAcceptHoverEvents( true );
48 setFlag( QGraphicsItem::ItemIsMovable, false );
49 setCacheMode( QGraphicsItem::DeviceCoordinateCache );
50}
51
52void QgsModelDesignerFlatButtonGraphicItem::paint( QPainter *painter, const QStyleOptionGraphicsItem *, QWidget * )
53{
54 if ( QgsModelGraphicsScene *modelScene = qobject_cast<QgsModelGraphicsScene *>( scene() ) )
55 {
56 if ( modelScene->flags() & QgsModelGraphicsScene::FlagHideControls )
57 return;
58 }
59
60 if ( mHoverState )
61 {
62 painter->setPen( QPen( Qt::transparent, 1.0 ) );
63 painter->setBrush( QBrush( QColor( 55, 55, 55, 33 ), Qt::SolidPattern ) );
64 }
65 else
66 {
67 painter->setPen( QPen( Qt::transparent, 1.0 ) );
68 painter->setBrush( QBrush( Qt::transparent, Qt::SolidPattern ) );
69 }
70 const QPointF topLeft = mPosition - QPointF( std::floor( mSize.width() / 2 ), std::floor( mSize.height() / 2 ) );
71 const QRectF rect = QRectF( topLeft.x(), topLeft.y(), mSize.width(), mSize.height() );
72 painter->drawRect( rect );
73 painter->drawPicture( topLeft.x(), topLeft.y(), mPicture );
74}
75
76QRectF QgsModelDesignerFlatButtonGraphicItem::boundingRect() const
77{
78 return QRectF( mPosition.x() - std::floor( mSize.width() / 2 ), mPosition.y() - std::floor( mSize.height() / 2 ), mSize.width(), mSize.height() );
79}
80
81void QgsModelDesignerFlatButtonGraphicItem::hoverEnterEvent( QGraphicsSceneHoverEvent * )
82{
83 if ( view()->tool() && !view()->tool()->allowItemInteraction() )
84 mHoverState = false;
85 else
86 mHoverState = true;
87 update();
88}
89
90void QgsModelDesignerFlatButtonGraphicItem::hoverLeaveEvent( QGraphicsSceneHoverEvent * )
91{
92 mHoverState = false;
93 update();
94}
95
96void QgsModelDesignerFlatButtonGraphicItem::mousePressEvent( QGraphicsSceneMouseEvent * )
97{
98 if ( view()->tool() && view()->tool()->allowItemInteraction() )
99 emit clicked();
100}
101
102void QgsModelDesignerFlatButtonGraphicItem::modelHoverEnterEvent( QgsModelViewMouseEvent * )
103{
104 if ( view()->tool() && !view()->tool()->allowItemInteraction() )
105 mHoverState = false;
106 else
107 mHoverState = true;
108 update();
109}
110
111void QgsModelDesignerFlatButtonGraphicItem::modelHoverLeaveEvent( QgsModelViewMouseEvent * )
112{
113 mHoverState = false;
114 update();
115}
116
117void QgsModelDesignerFlatButtonGraphicItem::modelPressEvent( QgsModelViewMouseEvent *event )
118{
119 if ( view()->tool() && view()->tool()->allowItemInteraction() && event->button() == Qt::LeftButton )
120 {
121 QMetaObject::invokeMethod( this, "clicked", Qt::QueuedConnection );
122 mHoverState = false;
123 update();
124 }
125}
126
127void QgsModelDesignerFlatButtonGraphicItem::setPosition( const QPointF &position )
128{
129 mPosition = position;
130 prepareGeometryChange();
131 update();
132}
133
134QgsModelGraphicsView *QgsModelDesignerFlatButtonGraphicItem::view()
135{
136 return qobject_cast<QgsModelGraphicsView *>( scene()->views().first() );
137}
138
139void QgsModelDesignerFlatButtonGraphicItem::setPicture( const QPicture &picture )
140{
141 mPicture = picture;
142 update();
143}
144
145//
146// QgsModelDesignerFoldButtonGraphicItem
147//
148
149QgsModelDesignerFoldButtonGraphicItem::QgsModelDesignerFoldButtonGraphicItem( QGraphicsItem *parent, bool folded, const QPointF &position, const QSizeF &size )
150 : QgsModelDesignerFlatButtonGraphicItem( parent, QPicture(), position, size )
151 , mFolded( folded )
152{
153 QSvgRenderer svg( QgsApplication::iconPath( u"mIconModelerExpand.svg"_s ) );
154 QPainter painter( &mPlusPicture );
155 svg.render( &painter );
156 painter.end();
157
158 QSvgRenderer svg2( QgsApplication::iconPath( u"mIconModelerCollapse.svg"_s ) );
159 painter.begin( &mMinusPicture );
160 svg2.render( &painter );
161 painter.end();
162
163 setPicture( mFolded ? mPlusPicture : mMinusPicture );
164}
165
166void QgsModelDesignerFoldButtonGraphicItem::mousePressEvent( QGraphicsSceneMouseEvent *event )
167{
168 mFolded = !mFolded;
169 setPicture( mFolded ? mPlusPicture : mMinusPicture );
170 emit folded( mFolded );
171 QgsModelDesignerFlatButtonGraphicItem::mousePressEvent( event );
172}
173
174void QgsModelDesignerFoldButtonGraphicItem::modelPressEvent( QgsModelViewMouseEvent *event )
175{
176 mFolded = !mFolded;
177 setPicture( mFolded ? mPlusPicture : mMinusPicture );
178 emit folded( mFolded );
179 QgsModelDesignerFlatButtonGraphicItem::modelPressEvent( event );
180}
181
182
183QgsModelDesignerSocketGraphicItem::QgsModelDesignerSocketGraphicItem( QgsModelComponentGraphicItem *parent, QgsProcessingModelComponent *component, int index, const QPointF &position, Qt::Edge edge, const QSizeF &size )
184 : QgsModelDesignerFlatButtonGraphicItem( parent, QPicture(), position, size )
185 , mComponentItem( parent )
186 , mComponent( component )
187 , mIndex( index )
188 , mEdge( edge )
189{
190}
191
192void QgsModelDesignerSocketGraphicItem::paint( QPainter *painter, const QStyleOptionGraphicsItem *, QWidget * )
193{
194 QColor outlineColor = socketColor();
195 QColor fillColor = QColor( outlineColor );
196
197 if ( isInput() )
198 {
199 fillColor.setAlpha( isDefaultParameterValue() ? 30 : 255 );
200 }
201 else
202 {
203 // outputs are always filled sockets
204 fillColor.setAlpha( 255 );
205 }
206
207 // Outline style
208 painter->setPen( QPen( outlineColor, mHoverState ? mSocketOutlineWidth * 2 : mSocketOutlineWidth ) );
209
210 // Fill style
211 painter->setBrush( QBrush( fillColor, Qt::SolidPattern ) );
212
213 painter->setRenderHint( QPainter::Antialiasing );
214
215 // Radius of the socket circle
216 constexpr float DISPLAY_SIZE = 4;
217
218 // Offset of the socket to separate from the label
219 constexpr float ELLIPSE_OFFSET = 0.4;
220 QPointF ellipsePosition = QPointF( position().x() + ELLIPSE_OFFSET, position().y() + ELLIPSE_OFFSET );
221 painter->drawEllipse( ellipsePosition, DISPLAY_SIZE, DISPLAY_SIZE );
222
223 /* Uncomment to display bounding box */
224#if 0
225 painter->save();
226 painter->setPen( QPen() );
227 painter->setBrush( QBrush() );
228 painter->drawRect( boundingRect() );
229 painter->restore();
230#endif
231}
232
233
234QColor QgsModelDesignerSocketGraphicItem::socketColor() const
235{
236 return mComponentItem->linkColor( mEdge, mIndex );
237}
238
239
240bool QgsModelDesignerSocketGraphicItem::isDefaultParameterValue() const
241{
242 if ( !mComponent )
243 {
244 return false;
245 }
246
247 const QgsProcessingModelChildAlgorithm *child = dynamic_cast<const QgsProcessingModelChildAlgorithm *>( mComponent );
248
249 if ( !child )
250 {
251 return false;
252 }
253
254 bool isDefaultValue = true;
255
256 // We can only know if the socket should be filled if the algorithm is non null
257 if ( child->algorithm() )
258 {
259 switch ( mEdge )
260 {
261 // Input params
262 case Qt::TopEdge:
263 {
264 const QgsProcessingParameterDefinitions params = child->algorithm()->parameterDefinitions();
265 const QgsProcessingParameterDefinition *param = params.value( mIndex );
266 if ( !param )
267 break;
268
269 const QString name = param->name();
270
271 QgsProcessingModelChildParameterSources paramSources = child->parameterSources().value( name );
272 if ( paramSources.empty() )
273 {
274 break;
275 }
276
277 // The default value can only happen in the case of the parameter uses a static value
278 if ( paramSources[0].source() != Qgis::ProcessingModelChildParameterSource::StaticValue )
279 {
280 isDefaultValue = false;
281 break;
282 }
283
284 isDefaultValue = paramSources[0].staticValue() == param->defaultValue();
285 break;
286 }
287
288 // Outputs
289 case Qt::BottomEdge:
290 case Qt::LeftEdge:
291 case Qt::RightEdge:
292 break;
293 }
294 }
295
296 return isDefaultValue;
297}
298
299
300QgsModelDesignerFeatureCountGraphicItem::QgsModelDesignerFeatureCountGraphicItem( QgsModelArrowItem *link, const QString &text )
301 : QGraphicsTextItem( text )
302 , mLink( link )
303{
304 connect( link, &QgsModelArrowItem::painterPathUpdated, this, &QgsModelDesignerFeatureCountGraphicItem::setPosition );
305
306 QFont font = this->font();
307 font.setPointSize( FONT_SIZE );
308 setFont( font );
309
310 setZValue( QgsModelGraphicsScene::ZValues::ArrowDecoration );
311 setPosition();
312}
313
314void QgsModelDesignerFeatureCountGraphicItem::paint( QPainter *painter, const QStyleOptionGraphicsItem *itemStyle, QWidget *widget )
315{
316 const bool isDarkTheme = QApplication::palette().color( QPalette::Window ).lightness() < 128;
317 QPalette::ColorRole bgPalette = isDarkTheme ? QPalette::Dark : QPalette::Light;
318
319 // First draw a rounded rectangle as background
320 QColor backgroundColor = QApplication::palette().color( bgPalette );
321 backgroundColor.setAlpha( 220 ); // Add some transparency so we still see the arrow underneath
322 painter->setBrush( QBrush( backgroundColor ) );
323 painter->setPen( Qt::PenStyle::NoPen );
324 constexpr double RADIUS = 5;
325 painter->drawRoundedRect( boundingRect(), RADIUS, RADIUS );
326
327 // And finally draw the text on top
328 setDefaultTextColor( QApplication::palette().color( QPalette::Text ) );
329 QGraphicsTextItem::paint( painter, itemStyle, widget );
330}
331
332void QgsModelDesignerFeatureCountGraphicItem::setPosition()
333{
334 QPointF middlePos = mLink->path().pointAtPercent( 0.5 );
335 QRectF rect = boundingRect();
336 QPointF offset = rect.center();
337 setPos( middlePos - offset );
338 update();
339}
340
@ StaticValue
Parameter value is a static value.
Definition qgis.h:3921
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.
Base class for the definition of processing parameters.
QVariant defaultValue() const
Returns the default value for the parameter.
QString name() const
Returns the name of the parameter.
QList< const QgsProcessingParameterDefinition * > QgsProcessingParameterDefinitions
List of processing parameters.