Skip to content

Commit ca41916

Browse files
committed
Add method for preventing composer items from changing mouse cursor and workaround for qt bug 3732
1 parent b2ad0a9 commit ca41916

File tree

4 files changed

+63
-8
lines changed

4 files changed

+63
-8
lines changed

src/core/composer/qgscomposermousehandles.cpp

+45-6
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@
2828
QgsComposerMouseHandles::QgsComposerMouseHandles( QgsComposition *composition ) : QObject( 0 ),
2929
QGraphicsRectItem( 0 ),
3030
mComposition( composition ),
31+
mGraphicsView( 0 ),
3132
mBeginHandleWidth( 0 ),
3233
mBeginHandleHeight( 0 ),
3334
mIsDragging( false ),
@@ -47,6 +48,29 @@ QgsComposerMouseHandles::~QgsComposerMouseHandles()
4748

4849
}
4950

51+
QGraphicsView* QgsComposerMouseHandles::graphicsView()
52+
{
53+
//have we already found the current view?
54+
if ( mGraphicsView )
55+
{
56+
return mGraphicsView;
57+
}
58+
59+
//otherwise, try and find current view attached to composition
60+
if ( scene() )
61+
{
62+
QList<QGraphicsView*> viewList = scene()->views();
63+
if ( viewList.size() > 0 )
64+
{
65+
mGraphicsView = viewList.at( 0 );
66+
return mGraphicsView;
67+
}
68+
}
69+
70+
//no view attached to composition
71+
return 0;
72+
}
73+
5074
void QgsComposerMouseHandles::paint( QPainter* painter, const QStyleOptionGraphicsItem* itemStyle, QWidget* pWidget )
5175
{
5276
Q_UNUSED( itemStyle );
@@ -216,13 +240,11 @@ QRectF QgsComposerMouseHandles::selectionBounds() const
216240
return bounds;
217241
}
218242

219-
double QgsComposerMouseHandles::rectHandlerBorderTolerance() const
243+
double QgsComposerMouseHandles::rectHandlerBorderTolerance()
220244
{
221245
//calculate size for resize handles
222246
//get view scale factor
223-
QList<QGraphicsView*> viewList = mComposition->views();
224-
QGraphicsView* currentView = viewList.at( 0 );
225-
double viewScaleFactor = currentView->transform().m11();
247+
double viewScaleFactor = graphicsView()->transform().m11();
226248

227249
//size of handle boxes depends on zoom level in composer view
228250
double rectHandlerSize = 10.0 / viewScaleFactor;
@@ -357,7 +379,24 @@ QgsComposerMouseHandles::MouseAction QgsComposerMouseHandles::mouseActionForScen
357379

358380
void QgsComposerMouseHandles::hoverMoveEvent( QGraphicsSceneHoverEvent * event )
359381
{
360-
setCursor( cursorForPosition( event->pos() ) );
382+
setViewportCursor( cursorForPosition( event->pos() ) );
383+
}
384+
385+
void QgsComposerMouseHandles::hoverLeaveEvent( QGraphicsSceneHoverEvent * event )
386+
{
387+
Q_UNUSED( event );
388+
setViewportCursor( Qt::ArrowCursor );
389+
}
390+
391+
void QgsComposerMouseHandles::setViewportCursor( Qt::CursorShape cursor )
392+
{
393+
//workaround qt bug #3732 by setting cursor for QGraphicsView viewport,
394+
//rather then setting it directly here
395+
396+
if ( !mComposition->preventCursorChange() )
397+
{
398+
graphicsView()->viewport()->setCursor( cursor );
399+
}
361400
}
362401

363402
void QgsComposerMouseHandles::mouseMoveEvent( QGraphicsSceneMouseEvent* event )
@@ -466,7 +505,7 @@ void QgsComposerMouseHandles::mouseReleaseEvent( QGraphicsSceneMouseEvent* event
466505

467506
//reset default action
468507
mCurrentMouseMoveAction = QgsComposerMouseHandles::MoveItem;
469-
setCursor( Qt::ArrowCursor );
508+
setViewportCursor( Qt::ArrowCursor );
470509
//redraw handles
471510
resetTransform();
472511
updateHandles();

src/core/composer/qgscomposermousehandles.h

+9-1
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@
2222

2323
class QgsComposition;
2424
class QgsComposerItem;
25+
class QGraphicsView;
2526

2627
/** \ingroup MapComposer
2728
* Handles drawing of selection outlines and mouse handles. Responsible for mouse
@@ -84,6 +85,7 @@ class CORE_EXPORT QgsComposerMouseHandles: public QObject, public QGraphicsRectI
8485
void mouseReleaseEvent( QGraphicsSceneMouseEvent* event );
8586
void mousePressEvent( QGraphicsSceneMouseEvent* event );
8687
void hoverMoveEvent( QGraphicsSceneHoverEvent * event );
88+
void hoverLeaveEvent( QGraphicsSceneHoverEvent * event );
8789

8890
public slots:
8991

@@ -96,6 +98,7 @@ class CORE_EXPORT QgsComposerMouseHandles: public QObject, public QGraphicsRectI
9698
private:
9799

98100
QgsComposition* mComposition; //reference to composition
101+
QGraphicsView* mGraphicsView; //reference to QGraphicsView
99102

100103
QgsComposerMouseHandles::MouseAction mCurrentMouseMoveAction;
101104
/**Start point of the last mouse move action (in scene coordinates)*/
@@ -131,7 +134,7 @@ class CORE_EXPORT QgsComposerMouseHandles: public QObject, public QGraphicsRectI
131134

132135
/**Returns the current (zoom level dependent) tolerance to decide if mouse position is close enough to the
133136
item border for resizing*/
134-
double rectHandlerBorderTolerance() const;
137+
double rectHandlerBorderTolerance();
135138

136139
/**Finds out the appropriate cursor for the current mouse position in the widget (e.g. move in the middle, resize at border)*/
137140
Qt::CursorShape cursorForPosition( const QPointF& itemCoordPos );
@@ -169,6 +172,11 @@ class CORE_EXPORT QgsComposerMouseHandles: public QObject, public QGraphicsRectI
169172
bool nearestItem( const QMap< double, const QgsComposerItem* >& coords, double value, double& nearestValue ) const;
170173
void checkNearestItem( double checkCoord, const QMap< double, const QgsComposerItem* >& alignCoords, double& smallestDiff, double itemCoordOffset, double& itemCoord, double& alignCoord ) const;
171174

175+
//tries to return the current QGraphicsView attached to the composition
176+
QGraphicsView* graphicsView();
177+
178+
//sets the mouse cursor for the QGraphicsView attached to the composition (workaround qt bug #3732)
179+
void setViewportCursor( Qt::CursorShape cursor );
172180
};
173181

174182
#endif // QGSCOMPOSERMOUSEHANDLES_H

src/core/composer/qgscomposition.cpp

+3-1
Original file line numberDiff line numberDiff line change
@@ -69,6 +69,7 @@ QgsComposition::QgsComposition( QgsMapRenderer* mapRenderer )
6969
, mActiveItemCommand( 0 )
7070
, mActiveMultiFrameCommand( 0 )
7171
, mAtlasComposition( this )
72+
, mPreventCursorChange( false )
7273
{
7374
setBackgroundBrush( Qt::gray );
7475
addPaperItem();
@@ -104,7 +105,8 @@ QgsComposition::QgsComposition()
104105
mSelectionHandles( 0 ),
105106
mActiveItemCommand( 0 ),
106107
mActiveMultiFrameCommand( 0 ),
107-
mAtlasComposition( this )
108+
mAtlasComposition( this ),
109+
mPreventCursorChange( false )
108110
{
109111
loadSettings();
110112

src/core/composer/qgscomposition.h

+6
Original file line numberDiff line numberDiff line change
@@ -351,6 +351,10 @@ class CORE_EXPORT QgsComposition : public QGraphicsScene
351351
/**Convenience function to create a QgsAddRemoveItemCommand, connect its signals and push it to the undo stack*/
352352
void pushAddRemoveCommand( QgsComposerItem* item, const QString& text, QgsAddRemoveItemCommand::State state = QgsAddRemoveItemCommand::Added );
353353

354+
/**If true, prevents any mouse cursor changes by the composition or by any composer items
355+
Used by QgsComposer and QgsComposerView to prevent unwanted cursor changes*/
356+
void setPreventCursorChange( bool preventChange ) { mPreventCursorChange = preventChange; };
357+
bool preventCursorChange() { return mPreventCursorChange; };
354358

355359
//printing
356360

@@ -462,6 +466,8 @@ class CORE_EXPORT QgsComposition : public QGraphicsScene
462466

463467
static QString encodeStringForXML( const QString& str );
464468

469+
bool mPreventCursorChange;
470+
465471
signals:
466472
void paperSizeChanged();
467473
void nPagesChanged();

0 commit comments

Comments
 (0)