QGIS API Documentation 3.41.0-Master (fda2aa46e9a)
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#include "moc_qgsmodelgraphicitem.cpp"
18#include "qgsapplication.h"
21#include "qgsmodelviewtool.h"
23#include <QPainter>
24#include <QSvgRenderer>
25
27
28QgsModelDesignerFlatButtonGraphicItem::QgsModelDesignerFlatButtonGraphicItem( QGraphicsItem *parent, const QPicture &picture, const QPointF &position, const QSizeF &size )
29 : QGraphicsObject( parent )
30 , mPicture( picture )
31 , mPosition( position )
32 , mSize( size )
33{
34 setAcceptHoverEvents( true );
35 setFlag( QGraphicsItem::ItemIsMovable, false );
36 setCacheMode( QGraphicsItem::DeviceCoordinateCache );
37}
38
39void QgsModelDesignerFlatButtonGraphicItem::paint( QPainter *painter, const QStyleOptionGraphicsItem *, QWidget * )
40{
41 if ( QgsModelGraphicsScene *modelScene = qobject_cast< QgsModelGraphicsScene * >( scene() ) )
42 {
43 if ( modelScene->flags() & QgsModelGraphicsScene::FlagHideControls )
44 return;
45 }
46
47 if ( mHoverState )
48 {
49 painter->setPen( QPen( Qt::transparent, 1.0 ) );
50 painter->setBrush( QBrush( QColor( 55, 55, 55, 33 ),
51 Qt::SolidPattern ) );
52 }
53 else
54 {
55 painter->setPen( QPen( Qt::transparent, 1.0 ) );
56 painter->setBrush( QBrush( Qt::transparent,
57 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 ),
68 mPosition.y() - std::floor( mSize.height() / 2 ),
69 mSize.width(),
70 mSize.height() );
71}
72
73void QgsModelDesignerFlatButtonGraphicItem::hoverEnterEvent( QGraphicsSceneHoverEvent * )
74{
75 if ( view()->tool() && !view()->tool()->allowItemInteraction() )
76 mHoverState = false;
77 else
78 mHoverState = true;
79 update();
80}
81
82void QgsModelDesignerFlatButtonGraphicItem::hoverLeaveEvent( QGraphicsSceneHoverEvent * )
83{
84 mHoverState = false;
85 update();
86}
87
88void QgsModelDesignerFlatButtonGraphicItem::mousePressEvent( QGraphicsSceneMouseEvent * )
89{
90 if ( view()->tool() && view()->tool()->allowItemInteraction() )
91 emit clicked();
92}
93
94void QgsModelDesignerFlatButtonGraphicItem::modelHoverEnterEvent( QgsModelViewMouseEvent * )
95{
96 if ( view()->tool() && !view()->tool()->allowItemInteraction() )
97 mHoverState = false;
98 else
99 mHoverState = true;
100 update();
101}
102
103void QgsModelDesignerFlatButtonGraphicItem::modelHoverLeaveEvent( QgsModelViewMouseEvent * )
104{
105 mHoverState = false;
106 update();
107}
108
109void QgsModelDesignerFlatButtonGraphicItem::modelPressEvent( QgsModelViewMouseEvent *event )
110{
111 if ( view()->tool() && view()->tool()->allowItemInteraction() && event->button() == Qt::LeftButton )
112 {
113 QMetaObject::invokeMethod( this, "clicked", Qt::QueuedConnection );
114 mHoverState = false;
115 update();
116 }
117}
118
119void QgsModelDesignerFlatButtonGraphicItem::setPosition( const QPointF &position )
120{
121 mPosition = position;
122 prepareGeometryChange();
123 update();
124}
125
126QgsModelGraphicsView *QgsModelDesignerFlatButtonGraphicItem::view()
127{
128 return qobject_cast< QgsModelGraphicsView * >( scene()->views().first() );
129}
130
131void QgsModelDesignerFlatButtonGraphicItem::setPicture( const QPicture &picture )
132{
133 mPicture = picture;
134 update();
135}
136
137//
138// QgsModelDesignerFoldButtonGraphicItem
139//
140
141QgsModelDesignerFoldButtonGraphicItem::QgsModelDesignerFoldButtonGraphicItem( QGraphicsItem *parent, bool folded, const QPointF &position, const QSizeF &size )
142 : QgsModelDesignerFlatButtonGraphicItem( parent, QPicture(), position, size )
143 , mFolded( folded )
144{
145 QSvgRenderer svg( QgsApplication::iconPath( QStringLiteral( "mIconModelerExpand.svg" ) ) );
146 QPainter painter( &mPlusPicture );
147 svg.render( &painter );
148 painter.end();
149
150 QSvgRenderer svg2( QgsApplication::iconPath( QStringLiteral( "mIconModelerCollapse.svg" ) ) );
151 painter.begin( &mMinusPicture );
152 svg2.render( &painter );
153 painter.end();
154
155 setPicture( mFolded ? mPlusPicture : mMinusPicture );
156}
157
158void QgsModelDesignerFoldButtonGraphicItem::mousePressEvent( QGraphicsSceneMouseEvent *event )
159{
160 mFolded = !mFolded;
161 setPicture( mFolded ? mPlusPicture : mMinusPicture );
162 emit folded( mFolded );
163 QgsModelDesignerFlatButtonGraphicItem::mousePressEvent( event );
164}
165
166void QgsModelDesignerFoldButtonGraphicItem::modelPressEvent( QgsModelViewMouseEvent *event )
167{
168 mFolded = !mFolded;
169 setPicture( mFolded ? mPlusPicture : mMinusPicture );
170 emit folded( mFolded );
171 QgsModelDesignerFlatButtonGraphicItem::modelPressEvent( event );
172}
173
175
static QString iconPath(const QString &iconFile)
Returns path to the desired icon file.
A QgsModelViewMouseEvent is the result of a user interaction with the mouse on a QgsModelGraphicsView...