Skip to content
Permalink
Browse files

Add method for preventing composer items from changing mouse cursor a…

…nd workaround for qt bug 3732
  • Loading branch information
nyalldawson committed Oct 10, 2013
1 parent b2ad0a9 commit ca41916e4f79a17db29fb88f2f1cba8016db5002
@@ -28,6 +28,7 @@
QgsComposerMouseHandles::QgsComposerMouseHandles( QgsComposition *composition ) : QObject( 0 ),
QGraphicsRectItem( 0 ),
mComposition( composition ),
mGraphicsView( 0 ),
mBeginHandleWidth( 0 ),
mBeginHandleHeight( 0 ),
mIsDragging( false ),
@@ -47,6 +48,29 @@ QgsComposerMouseHandles::~QgsComposerMouseHandles()

}

QGraphicsView* QgsComposerMouseHandles::graphicsView()
{
//have we already found the current view?
if ( mGraphicsView )
{
return mGraphicsView;
}

//otherwise, try and find current view attached to composition
if ( scene() )
{
QList<QGraphicsView*> viewList = scene()->views();
if ( viewList.size() > 0 )
{
mGraphicsView = viewList.at( 0 );
return mGraphicsView;
}
}

//no view attached to composition
return 0;
}

void QgsComposerMouseHandles::paint( QPainter* painter, const QStyleOptionGraphicsItem* itemStyle, QWidget* pWidget )
{
Q_UNUSED( itemStyle );
@@ -216,13 +240,11 @@ QRectF QgsComposerMouseHandles::selectionBounds() const
return bounds;
}

double QgsComposerMouseHandles::rectHandlerBorderTolerance() const
double QgsComposerMouseHandles::rectHandlerBorderTolerance()
{
//calculate size for resize handles
//get view scale factor
QList<QGraphicsView*> viewList = mComposition->views();
QGraphicsView* currentView = viewList.at( 0 );
double viewScaleFactor = currentView->transform().m11();
double viewScaleFactor = graphicsView()->transform().m11();

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

void QgsComposerMouseHandles::hoverMoveEvent( QGraphicsSceneHoverEvent * event )
{
setCursor( cursorForPosition( event->pos() ) );
setViewportCursor( cursorForPosition( event->pos() ) );
}

void QgsComposerMouseHandles::hoverLeaveEvent( QGraphicsSceneHoverEvent * event )
{
Q_UNUSED( event );
setViewportCursor( Qt::ArrowCursor );
}

void QgsComposerMouseHandles::setViewportCursor( Qt::CursorShape cursor )
{
//workaround qt bug #3732 by setting cursor for QGraphicsView viewport,
//rather then setting it directly here

if ( !mComposition->preventCursorChange() )
{
graphicsView()->viewport()->setCursor( cursor );
}
}

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

//reset default action
mCurrentMouseMoveAction = QgsComposerMouseHandles::MoveItem;
setCursor( Qt::ArrowCursor );
setViewportCursor( Qt::ArrowCursor );
//redraw handles
resetTransform();
updateHandles();
@@ -22,6 +22,7 @@

class QgsComposition;
class QgsComposerItem;
class QGraphicsView;

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

public slots:

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

QgsComposition* mComposition; //reference to composition
QGraphicsView* mGraphicsView; //reference to QGraphicsView

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

/**Returns the current (zoom level dependent) tolerance to decide if mouse position is close enough to the
item border for resizing*/
double rectHandlerBorderTolerance() const;
double rectHandlerBorderTolerance();

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

//tries to return the current QGraphicsView attached to the composition
QGraphicsView* graphicsView();

//sets the mouse cursor for the QGraphicsView attached to the composition (workaround qt bug #3732)
void setViewportCursor( Qt::CursorShape cursor );
};

#endif // QGSCOMPOSERMOUSEHANDLES_H
@@ -69,6 +69,7 @@ QgsComposition::QgsComposition( QgsMapRenderer* mapRenderer )
, mActiveItemCommand( 0 )
, mActiveMultiFrameCommand( 0 )
, mAtlasComposition( this )
, mPreventCursorChange( false )
{
setBackgroundBrush( Qt::gray );
addPaperItem();
@@ -104,7 +105,8 @@ QgsComposition::QgsComposition()
mSelectionHandles( 0 ),
mActiveItemCommand( 0 ),
mActiveMultiFrameCommand( 0 ),
mAtlasComposition( this )
mAtlasComposition( this ),
mPreventCursorChange( false )
{
loadSettings();

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

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

//printing

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

static QString encodeStringForXML( const QString& str );

bool mPreventCursorChange;

signals:
void paperSizeChanged();
void nPagesChanged();

0 comments on commit ca41916

Please sign in to comment.
You can’t perform that action at this time.