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