QGIS API Documentation 3.41.0-Master (cea29feecf2)
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 ), Qt::SolidPattern ) );
51 }
52 else
53 {
54 painter->setPen( QPen( Qt::transparent, 1.0 ) );
55 painter->setBrush( QBrush( Qt::transparent, Qt::SolidPattern ) );
56 }
57 const QPointF topLeft = mPosition - QPointF( std::floor( mSize.width() / 2 ), std::floor( mSize.height() / 2 ) );
58 const QRectF rect = QRectF( topLeft.x(), topLeft.y(), mSize.width(), mSize.height() );
59 painter->drawRect( rect );
60 painter->drawPicture( topLeft.x(), topLeft.y(), mPicture );
61}
62
63QRectF QgsModelDesignerFlatButtonGraphicItem::boundingRect() const
64{
65 return QRectF( mPosition.x() - std::floor( mSize.width() / 2 ), mPosition.y() - std::floor( mSize.height() / 2 ), mSize.width(), mSize.height() );
66}
67
68void QgsModelDesignerFlatButtonGraphicItem::hoverEnterEvent( QGraphicsSceneHoverEvent * )
69{
70 if ( view()->tool() && !view()->tool()->allowItemInteraction() )
71 mHoverState = false;
72 else
73 mHoverState = true;
74 update();
75}
76
77void QgsModelDesignerFlatButtonGraphicItem::hoverLeaveEvent( QGraphicsSceneHoverEvent * )
78{
79 mHoverState = false;
80 update();
81}
82
83void QgsModelDesignerFlatButtonGraphicItem::mousePressEvent( QGraphicsSceneMouseEvent * )
84{
85 if ( view()->tool() && view()->tool()->allowItemInteraction() )
86 emit clicked();
87}
88
89void QgsModelDesignerFlatButtonGraphicItem::modelHoverEnterEvent( QgsModelViewMouseEvent * )
90{
91 if ( view()->tool() && !view()->tool()->allowItemInteraction() )
92 mHoverState = false;
93 else
94 mHoverState = true;
95 update();
96}
97
98void QgsModelDesignerFlatButtonGraphicItem::modelHoverLeaveEvent( QgsModelViewMouseEvent * )
99{
100 mHoverState = false;
101 update();
102}
103
104void QgsModelDesignerFlatButtonGraphicItem::modelPressEvent( QgsModelViewMouseEvent *event )
105{
106 if ( view()->tool() && view()->tool()->allowItemInteraction() && event->button() == Qt::LeftButton )
107 {
108 QMetaObject::invokeMethod( this, "clicked", Qt::QueuedConnection );
109 mHoverState = false;
110 update();
111 }
112}
113
114void QgsModelDesignerFlatButtonGraphicItem::setPosition( const QPointF &position )
115{
116 mPosition = position;
117 prepareGeometryChange();
118 update();
119}
120
121QgsModelGraphicsView *QgsModelDesignerFlatButtonGraphicItem::view()
122{
123 return qobject_cast<QgsModelGraphicsView *>( scene()->views().first() );
124}
125
126void QgsModelDesignerFlatButtonGraphicItem::setPicture( const QPicture &picture )
127{
128 mPicture = picture;
129 update();
130}
131
132//
133// QgsModelDesignerFoldButtonGraphicItem
134//
135
136QgsModelDesignerFoldButtonGraphicItem::QgsModelDesignerFoldButtonGraphicItem( QGraphicsItem *parent, bool folded, const QPointF &position, const QSizeF &size )
137 : QgsModelDesignerFlatButtonGraphicItem( parent, QPicture(), position, size )
138 , mFolded( folded )
139{
140 QSvgRenderer svg( QgsApplication::iconPath( QStringLiteral( "mIconModelerExpand.svg" ) ) );
141 QPainter painter( &mPlusPicture );
142 svg.render( &painter );
143 painter.end();
144
145 QSvgRenderer svg2( QgsApplication::iconPath( QStringLiteral( "mIconModelerCollapse.svg" ) ) );
146 painter.begin( &mMinusPicture );
147 svg2.render( &painter );
148 painter.end();
149
150 setPicture( mFolded ? mPlusPicture : mMinusPicture );
151}
152
153void QgsModelDesignerFoldButtonGraphicItem::mousePressEvent( QGraphicsSceneMouseEvent *event )
154{
155 mFolded = !mFolded;
156 setPicture( mFolded ? mPlusPicture : mMinusPicture );
157 emit folded( mFolded );
158 QgsModelDesignerFlatButtonGraphicItem::mousePressEvent( event );
159}
160
161void QgsModelDesignerFoldButtonGraphicItem::modelPressEvent( QgsModelViewMouseEvent *event )
162{
163 mFolded = !mFolded;
164 setPicture( mFolded ? mPlusPicture : mMinusPicture );
165 emit folded( mFolded );
166 QgsModelDesignerFlatButtonGraphicItem::modelPressEvent( event );
167}
168
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...