Navigation Menu

Skip to content

Commit

Permalink
Add method for preventing composer items from changing mouse cursor a…
Browse files Browse the repository at this point in the history
…nd workaround for qt bug 3732
  • Loading branch information
nyalldawson committed Oct 10, 2013
1 parent b2ad0a9 commit ca41916
Show file tree
Hide file tree
Showing 4 changed files with 63 additions and 8 deletions.
51 changes: 45 additions & 6 deletions src/core/composer/qgscomposermousehandles.cpp
Expand Up @@ -28,6 +28,7 @@
QgsComposerMouseHandles::QgsComposerMouseHandles( QgsComposition *composition ) : QObject( 0 ),
QGraphicsRectItem( 0 ),
mComposition( composition ),
mGraphicsView( 0 ),
mBeginHandleWidth( 0 ),
mBeginHandleHeight( 0 ),
mIsDragging( false ),
Expand All @@ -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 );
Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -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 )
Expand Down Expand Up @@ -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();
Expand Down
10 changes: 9 additions & 1 deletion src/core/composer/qgscomposermousehandles.h
Expand Up @@ -22,6 +22,7 @@

class QgsComposition;
class QgsComposerItem;
class QGraphicsView;

/** \ingroup MapComposer
* Handles drawing of selection outlines and mouse handles. Responsible for mouse
Expand Down Expand Up @@ -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:

Expand All @@ -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)*/
Expand Down Expand Up @@ -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 );
Expand Down Expand Up @@ -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
4 changes: 3 additions & 1 deletion src/core/composer/qgscomposition.cpp
Expand Up @@ -69,6 +69,7 @@ QgsComposition::QgsComposition( QgsMapRenderer* mapRenderer )
, mActiveItemCommand( 0 )
, mActiveMultiFrameCommand( 0 )
, mAtlasComposition( this )
, mPreventCursorChange( false )
{
setBackgroundBrush( Qt::gray );
addPaperItem();
Expand Down Expand Up @@ -104,7 +105,8 @@ QgsComposition::QgsComposition()
mSelectionHandles( 0 ),
mActiveItemCommand( 0 ),
mActiveMultiFrameCommand( 0 ),
mAtlasComposition( this )
mAtlasComposition( this ),
mPreventCursorChange( false )
{
loadSettings();

Expand Down
6 changes: 6 additions & 0 deletions src/core/composer/qgscomposition.h
Expand Up @@ -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

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

static QString encodeStringForXML( const QString& str );

bool mPreventCursorChange;

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

0 comments on commit ca41916

Please sign in to comment.