Skip to content

Commit a346736

Browse files
committedJul 11, 2017
Handle tool changes mid press-release operation
Because it's possible for users to change the tool while a press release operation is mid-way, e.g. by pressing a tool shortcut key.
1 parent 3f66520 commit a346736

10 files changed

+52
-14
lines changed
 

‎python/gui/layout/qgslayoutviewrubberband.sip

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,7 @@ class QgsLayoutViewRubberBand
5555
ending ``position`` (in layout coordinate space).
5656
%End
5757

58-
virtual QRectF finish( QPointF position, Qt::KeyboardModifiers modifiers ) = 0;
58+
virtual QRectF finish( QPointF position = QPointF(), Qt::KeyboardModifiers modifiers = 0 ) = 0;
5959
%Docstring
6060
Called when a rubber band use has finished and the rubber
6161
band is no longer required.
@@ -116,7 +116,7 @@ class QgsLayoutViewRectangularRubberBand : QgsLayoutViewRubberBand
116116

117117
virtual void update( QPointF position, Qt::KeyboardModifiers modifiers );
118118

119-
virtual QRectF finish( QPointF, Qt::KeyboardModifiers );
119+
virtual QRectF finish( QPointF position = QPointF(), Qt::KeyboardModifiers modifiers = 0 );
120120

121121

122122
};
@@ -146,7 +146,7 @@ class QgsLayoutViewEllipticalRubberBand : QgsLayoutViewRubberBand
146146

147147
virtual void update( QPointF position, Qt::KeyboardModifiers modifiers );
148148

149-
virtual QRectF finish( QPointF, Qt::KeyboardModifiers );
149+
virtual QRectF finish( QPointF position = QPointF(), Qt::KeyboardModifiers modifiers = 0 );
150150

151151

152152
};

‎python/gui/layout/qgslayoutviewtool.sip

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,11 +49,19 @@ class QgsLayoutViewTool : QObject
4949
virtual void layoutPressEvent( QgsLayoutViewMouseEvent *event );
5050
%Docstring
5151
Mouse press event for overriding. Default implementation does nothing.
52+
Note that subclasses must ensure that they correctly handle cases
53+
when a layoutPressEvent is called without a corresponding
54+
layoutReleaseEvent (e.g. due to tool being changed mid way
55+
through a press-release operation).
5256
%End
5357

5458
virtual void layoutReleaseEvent( QgsLayoutViewMouseEvent *event );
5559
%Docstring
5660
Mouse release event for overriding. Default implementation does nothing.
61+
Note that subclasses must ensure that they correctly handle cases
62+
when a layoutPressEvent is called without a corresponding
63+
layoutReleaseEvent (e.g. due to tool being changed mid way
64+
through a press-release operation).
5765
%End
5866

5967
virtual void wheelEvent( QWheelEvent *event );

‎python/gui/layout/qgslayoutviewtooladditem.sip

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,8 @@ class QgsLayoutViewToolAddItem : QgsLayoutViewTool
4141

4242
virtual void layoutReleaseEvent( QgsLayoutViewMouseEvent *event );
4343

44+
virtual void deactivate();
45+
4446

4547
};
4648

‎python/gui/layout/qgslayoutviewtoolpan.sip

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,8 @@ class QgsLayoutViewToolPan : QgsLayoutViewTool
3131

3232
virtual void layoutReleaseEvent( QgsLayoutViewMouseEvent *event );
3333

34+
virtual void deactivate();
35+
3436

3537
};
3638

‎src/gui/layout/qgslayoutviewrubberband.h

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -75,7 +75,7 @@ class GUI_EXPORT QgsLayoutViewRubberBand
7575
* band is no longer required.
7676
* Returns the final bounding box of the rubber band.
7777
*/
78-
virtual QRectF finish( QPointF position, Qt::KeyboardModifiers modifiers ) = 0;
78+
virtual QRectF finish( QPointF position = QPointF(), Qt::KeyboardModifiers modifiers = 0 ) = 0;
7979

8080
/**
8181
* Returns the view associated with the rubber band.
@@ -125,7 +125,7 @@ class GUI_EXPORT QgsLayoutViewRectangularRubberBand : public QgsLayoutViewRubber
125125

126126
void start( QPointF position, Qt::KeyboardModifiers modifiers ) override;
127127
void update( QPointF position, Qt::KeyboardModifiers modifiers ) override;
128-
QRectF finish( QPointF, Qt::KeyboardModifiers ) override;
128+
QRectF finish( QPointF position = QPointF(), Qt::KeyboardModifiers modifiers = 0 ) override;
129129

130130
private:
131131

@@ -156,7 +156,7 @@ class GUI_EXPORT QgsLayoutViewEllipticalRubberBand : public QgsLayoutViewRubberB
156156

157157
void start( QPointF position, Qt::KeyboardModifiers modifiers ) override;
158158
void update( QPointF position, Qt::KeyboardModifiers modifiers ) override;
159-
QRectF finish( QPointF, Qt::KeyboardModifiers ) override;
159+
QRectF finish( QPointF position = QPointF(), Qt::KeyboardModifiers modifiers = 0 ) override;
160160

161161
private:
162162

‎src/gui/layout/qgslayoutviewtool.h

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -72,11 +72,19 @@ class GUI_EXPORT QgsLayoutViewTool : public QObject
7272

7373
/**
7474
* Mouse press event for overriding. Default implementation does nothing.
75+
* Note that subclasses must ensure that they correctly handle cases
76+
* when a layoutPressEvent is called without a corresponding
77+
* layoutReleaseEvent (e.g. due to tool being changed mid way
78+
* through a press-release operation).
7579
*/
7680
virtual void layoutPressEvent( QgsLayoutViewMouseEvent *event );
7781

7882
/**
7983
* Mouse release event for overriding. Default implementation does nothing.
84+
* Note that subclasses must ensure that they correctly handle cases
85+
* when a layoutPressEvent is called without a corresponding
86+
* layoutReleaseEvent (e.g. due to tool being changed mid way
87+
* through a press-release operation).
8088
*/
8189
virtual void layoutReleaseEvent( QgsLayoutViewMouseEvent *event );
8290

‎src/gui/layout/qgslayoutviewtooladditem.cpp

Lines changed: 16 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,7 @@ void QgsLayoutViewToolAddItem::layoutPressEvent( QgsLayoutViewMouseEvent *event
4646
return;
4747
}
4848

49+
mDrawing = true;
4950
mMousePressStartPos = event->pos();
5051
mRubberBand.reset( QgsApplication::layoutItemRegistry()->createItemRubberBand( mItemType, view() ) );
5152
if ( mRubberBand )
@@ -56,25 +57,21 @@ void QgsLayoutViewToolAddItem::layoutPressEvent( QgsLayoutViewMouseEvent *event
5657

5758
void QgsLayoutViewToolAddItem::layoutMoveEvent( QgsLayoutViewMouseEvent *event )
5859
{
59-
if ( mRubberBand )
60+
if ( mDrawing && mRubberBand )
6061
{
6162
mRubberBand->update( event->layoutPoint(), event->modifiers() );
6263
}
6364
}
6465

6566
void QgsLayoutViewToolAddItem::layoutReleaseEvent( QgsLayoutViewMouseEvent *event )
6667
{
67-
if ( event->button() != Qt::LeftButton )
68+
if ( event->button() != Qt::LeftButton || !mDrawing )
6869
{
6970
return;
7071
}
72+
mDrawing = false;
7173

72-
QRectF rect = QRectF( view()->mapToScene( mMousePressStartPos ),
73-
event->layoutPoint() );
74-
if ( mRubberBand )
75-
{
76-
rect = mRubberBand->finish( event->layoutPoint(), event->modifiers() );
77-
}
74+
QRectF rect = mRubberBand->finish( event->layoutPoint(), event->modifiers() );
7875

7976
// click? or click-and-drag?
8077
QPoint mousePressStopPoint = event->pos();
@@ -92,6 +89,17 @@ void QgsLayoutViewToolAddItem::layoutReleaseEvent( QgsLayoutViewMouseEvent *even
9289
layout()->addItem( item );
9390
}
9491

92+
void QgsLayoutViewToolAddItem::deactivate()
93+
{
94+
if ( mDrawing )
95+
{
96+
// canceled mid operation
97+
mRubberBand->finish();
98+
mDrawing = false;
99+
}
100+
QgsLayoutViewTool::deactivate();
101+
}
102+
95103
int QgsLayoutViewToolAddItem::itemType() const
96104
{
97105
return mItemType;

‎src/gui/layout/qgslayoutviewtooladditem.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -51,9 +51,12 @@ class GUI_EXPORT QgsLayoutViewToolAddItem : public QgsLayoutViewTool
5151
void layoutPressEvent( QgsLayoutViewMouseEvent *event ) override;
5252
void layoutMoveEvent( QgsLayoutViewMouseEvent *event ) override;
5353
void layoutReleaseEvent( QgsLayoutViewMouseEvent *event ) override;
54+
void deactivate() override;
5455

5556
private:
5657

58+
bool mDrawing = false;
59+
5760
int mItemType = 0;
5861

5962
//! Rubber band item

‎src/gui/layout/qgslayoutviewtoolpan.cpp

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -56,3 +56,9 @@ void QgsLayoutViewToolPan::layoutReleaseEvent( QgsLayoutViewMouseEvent *event )
5656
mIsPanning = false;
5757
view()->setCursor( Qt::OpenHandCursor );
5858
}
59+
60+
void QgsLayoutViewToolPan::deactivate()
61+
{
62+
mIsPanning = false;
63+
QgsLayoutViewTool::deactivate();
64+
}

‎src/gui/layout/qgslayoutviewtoolpan.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,7 @@ class GUI_EXPORT QgsLayoutViewToolPan : public QgsLayoutViewTool
4242
void layoutPressEvent( QgsLayoutViewMouseEvent *event ) override;
4343
void layoutMoveEvent( QgsLayoutViewMouseEvent *event ) override;
4444
void layoutReleaseEvent( QgsLayoutViewMouseEvent *event ) override;
45+
void deactivate() override;
4546

4647
private:
4748

0 commit comments

Comments
 (0)
Please sign in to comment.