QGIS API Documentation  3.14.0-Pi (9f7028fd23)
qgsmodelviewtool.cpp
Go to the documentation of this file.
1 /***************************************************************************
2  qgsmodelviewtool.cpp
3  ---------------------
4  Date : March 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 "qgsmodelviewtool.h"
17 #include "qgsmodelgraphicsview.h"
18 #include "qgsmodelgraphicsscene.h"
19 #include "qgsmodelviewmouseevent.h"
20 
21 QgsModelViewTool::QgsModelViewTool( QgsModelGraphicsView *view, const QString &name )
22  : QObject( view )
23  , mView( view )
24  , mToolName( name )
25 {
26  connect( mView, &QgsModelGraphicsView::willBeDeleted, this, [ = ]
27  {
28  mView = nullptr;
29  } );
30 }
31 
32 bool QgsModelViewTool::isClickAndDrag( QPoint startViewPoint, QPoint endViewPoint ) const
33 {
34  int diffX = endViewPoint.x() - startViewPoint.x();
35  int diffY = endViewPoint.y() - startViewPoint.y();
36  return std::abs( diffX ) >= 2 || std::abs( diffY ) >= 2;
37 }
38 
39 QgsModelGraphicsView *QgsModelViewTool::view() const
40 {
41  return mView;
42 }
43 
44 QgsModelGraphicsScene *QgsModelViewTool::scene() const
45 {
46  return qobject_cast< QgsModelGraphicsScene * >( mView->scene() );
47 }
48 
50 {
51  if ( mView )
52  mView->unsetTool( this );
53 }
54 
55 QgsModelViewTool::Flags QgsModelViewTool::flags() const
56 {
57  return mFlags;
58 }
59 
60 void QgsModelViewTool::setFlags( QgsModelViewTool::Flags flags )
61 {
62  mFlags = flags;
63 }
64 
66 {
67  event->ignore();
68 }
69 
71 {
72  event->ignore();
73 }
74 
76 {
77  event->ignore();
78 }
79 
81 {
82  event->ignore();
83 }
84 
85 void QgsModelViewTool::wheelEvent( QWheelEvent *event )
86 {
87  event->ignore();
88 }
89 
90 void QgsModelViewTool::keyPressEvent( QKeyEvent *event )
91 {
92  event->ignore();
93 }
94 
95 void QgsModelViewTool::keyReleaseEvent( QKeyEvent *event )
96 {
97  event->ignore();
98 }
99 
101 {
102  return false;
103 }
104 
105 void QgsModelViewTool::setAction( QAction *action )
106 {
107  mAction = action;
108 }
109 
111 {
112  return mAction;
113 }
114 
115 void QgsModelViewTool::setCursor( const QCursor &cursor )
116 {
117  mCursor = cursor;
118 }
119 
121 {
122  // make action and/or button active
123  if ( mAction )
124  mAction->setChecked( true );
125 
126  mView->viewport()->setCursor( mCursor );
127  emit activated();
128 }
129 
131 {
132  if ( mAction )
133  mAction->setChecked( false );
134 
135  emit deactivated();
136 }
QgsModelViewTool::setAction
void setAction(QAction *action)
Associates an action with this tool.
Definition: qgsmodelviewtool.cpp:105
QgsModelViewTool::isClickAndDrag
bool isClickAndDrag(QPoint startViewPoint, QPoint endViewPoint) const
Returns true if a mouse press/release operation which started at startViewPoint and ended at endViewP...
Definition: qgsmodelviewtool.cpp:32
QgsModelViewTool::activate
virtual void activate()
Called when tool is set as the currently active model tool.
Definition: qgsmodelviewtool.cpp:120
QgsModelViewTool::flags
QgsModelViewTool::Flags flags() const
Returns the current combination of flags set for the tool.
Definition: qgsmodelviewtool.cpp:55
QgsModelViewMouseEvent
Definition: qgsmodelviewmouseevent.h:36
QgsModelViewTool::deactivate
virtual void deactivate()
Called when tool is deactivated.
Definition: qgsmodelviewtool.cpp:130
QgsModelViewTool::modelPressEvent
virtual void modelPressEvent(QgsModelViewMouseEvent *event)
Mouse press event for overriding.
Definition: qgsmodelviewtool.cpp:75
QgsModelViewTool::action
QAction * action()
Returns the action associated with the tool or nullptr if no action is associated.
Definition: qgsmodelviewtool.cpp:110
QgsModelViewTool::setFlags
void setFlags(QgsModelViewTool::Flags flags)
Sets the combination of flags that will be used for the tool.
Definition: qgsmodelviewtool.cpp:60
QgsModelViewTool::deactivated
void deactivated()
Emitted when the tool is deactivated.
qgsmodelviewmouseevent.h
QgsModelViewTool::keyReleaseEvent
virtual void keyReleaseEvent(QKeyEvent *event)
Key release event for overriding.
Definition: qgsmodelviewtool.cpp:95
qgsmodelgraphicsview.h
QgsModelViewTool::keyPressEvent
virtual void keyPressEvent(QKeyEvent *event)
Key press event for overriding.
Definition: qgsmodelviewtool.cpp:90
QgsModelViewTool::~QgsModelViewTool
~QgsModelViewTool() override
Definition: qgsmodelviewtool.cpp:49
QgsModelViewTool::wheelEvent
virtual void wheelEvent(QWheelEvent *event)
Mouse wheel event for overriding.
Definition: qgsmodelviewtool.cpp:85
QgsModelViewTool::modelReleaseEvent
virtual void modelReleaseEvent(QgsModelViewMouseEvent *event)
Mouse release event for overriding.
Definition: qgsmodelviewtool.cpp:80
QgsModelViewTool::activated
void activated()
Emitted when the tool is activated.
qgsmodelgraphicsscene.h
QgsModelViewTool::allowItemInteraction
virtual bool allowItemInteraction()
Returns true if the tool allows interaction with component graphic items.
Definition: qgsmodelviewtool.cpp:100
QgsModelViewTool::modelMoveEvent
virtual void modelMoveEvent(QgsModelViewMouseEvent *event)
Mouse move event for overriding.
Definition: qgsmodelviewtool.cpp:65
QgsModelViewTool::view
QgsModelGraphicsView * view() const
Returns the view associated with the tool.
Definition: qgsmodelviewtool.cpp:39
QgsModelViewTool::QgsModelViewTool
QgsModelViewTool(QgsModelGraphicsView *view, const QString &name)
Constructor for QgsModelViewTool, taking a model view and tool name as parameters.
Definition: qgsmodelviewtool.cpp:21
QgsModelViewTool::scene
QgsModelGraphicsScene * scene() const
Returns the scene associated with the tool.
Definition: qgsmodelviewtool.cpp:44
QgsModelViewTool::setCursor
void setCursor(const QCursor &cursor)
Sets a user defined cursor for use when the tool is active.
Definition: qgsmodelviewtool.cpp:115
qgsmodelviewtool.h
QgsModelViewTool::modelDoubleClickEvent
virtual void modelDoubleClickEvent(QgsModelViewMouseEvent *event)
Mouse double-click event for overriding.
Definition: qgsmodelviewtool.cpp:70