Skip to content

Commit 48b4680

Browse files
committed
Merge pull request #930 from nyalldawson/pan_composer3
Panning controls and shortcuts for composer
2 parents 591d9df + 9290d7a commit 48b4680

File tree

9 files changed

+218
-12
lines changed

9 files changed

+218
-12
lines changed

src/app/composer/qgscomposer.cpp

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -144,6 +144,7 @@ QgsComposer::QgsComposer( QgisApp *qgis, const QString& title )
144144

145145
QActionGroup* toggleActionGroup = new QActionGroup( this );
146146
toggleActionGroup->addAction( mActionMoveItemContent );
147+
toggleActionGroup->addAction( mActionPan );
147148
toggleActionGroup->addAction( mActionAddNewMap );
148149
toggleActionGroup->addAction( mActionAddNewLabel );
149150
toggleActionGroup->addAction( mActionAddNewLegend );
@@ -166,6 +167,7 @@ QgsComposer::QgsComposer( QgisApp *qgis, const QString& title )
166167
mActionAddNewScalebar->setCheckable( true );
167168
mActionAddImage->setCheckable( true );
168169
mActionMoveItemContent->setCheckable( true );
170+
mActionPan->setCheckable( true );
169171
mActionAddArrow->setCheckable( true );
170172

171173
#ifdef Q_WS_MAC
@@ -1673,6 +1675,14 @@ void QgsComposer::on_mActionMoveItemContent_triggered()
16731675
}
16741676
}
16751677

1678+
void QgsComposer::on_mActionPan_triggered()
1679+
{
1680+
if ( mView )
1681+
{
1682+
mView->setCurrentTool( QgsComposerView::Pan );
1683+
}
1684+
}
1685+
16761686
void QgsComposer::on_mActionGroupItems_triggered()
16771687
{
16781688
if ( mView )

src/app/composer/qgscomposer.h

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -204,6 +204,9 @@ class QgsComposer: public QMainWindow, private Ui::QgsComposerBase
204204
//! Set tool to move item content
205205
void on_mActionMoveItemContent_triggered();
206206

207+
//! Set tool to move item content
208+
void on_mActionPan_triggered();
209+
207210
//! Group selected items
208211
void on_mActionGroupItems_triggered();
209212

@@ -242,7 +245,7 @@ class QgsComposer: public QMainWindow, private Ui::QgsComposerBase
242245

243246
//! Select next item below
244247
void on_mActionSelectNextAbove_triggered();
245-
248+
246249
//! Select next item above
247250
void on_mActionSelectNextBelow_triggered();
248251

src/core/composer/qgscomposermousehandles.cpp

Lines changed: 45 additions & 6 deletions
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 );
@@ -222,13 +246,11 @@ QRectF QgsComposerMouseHandles::selectionBounds() const
222246
return bounds;
223247
}
224248

225-
double QgsComposerMouseHandles::rectHandlerBorderTolerance() const
249+
double QgsComposerMouseHandles::rectHandlerBorderTolerance()
226250
{
227251
//calculate size for resize handles
228252
//get view scale factor
229-
QList<QGraphicsView*> viewList = mComposition->views();
230-
QGraphicsView* currentView = viewList.at( 0 );
231-
double viewScaleFactor = currentView->transform().m11();
253+
double viewScaleFactor = graphicsView()->transform().m11();
232254

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

364386
void QgsComposerMouseHandles::hoverMoveEvent( QGraphicsSceneHoverEvent * event )
365387
{
366-
setCursor( cursorForPosition( event->pos() ) );
388+
setViewportCursor( cursorForPosition( event->pos() ) );
389+
}
390+
391+
void QgsComposerMouseHandles::hoverLeaveEvent( QGraphicsSceneHoverEvent * event )
392+
{
393+
Q_UNUSED( event );
394+
setViewportCursor( Qt::ArrowCursor );
395+
}
396+
397+
void QgsComposerMouseHandles::setViewportCursor( Qt::CursorShape cursor )
398+
{
399+
//workaround qt bug #3732 by setting cursor for QGraphicsView viewport,
400+
//rather then setting it directly here
401+
402+
if ( !mComposition->preventCursorChange() )
403+
{
404+
graphicsView()->viewport()->setCursor( cursor );
405+
}
367406
}
368407

369408
void QgsComposerMouseHandles::mouseMoveEvent( QGraphicsSceneMouseEvent* event )
@@ -472,7 +511,7 @@ void QgsComposerMouseHandles::mouseReleaseEvent( QGraphicsSceneMouseEvent* event
472511

473512
//reset default action
474513
mCurrentMouseMoveAction = QgsComposerMouseHandles::MoveItem;
475-
setCursor( Qt::ArrowCursor );
514+
setViewportCursor( Qt::ArrowCursor );
476515
//redraw handles
477516
resetTransform();
478517
updateHandles();

src/core/composer/qgscomposermousehandles.h

Lines changed: 9 additions & 1 deletion
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)*/
@@ -133,7 +136,7 @@ class CORE_EXPORT QgsComposerMouseHandles: public QObject, public QGraphicsRectI
133136

134137
/**Returns the current (zoom level dependent) tolerance to decide if mouse position is close enough to the
135138
item border for resizing*/
136-
double rectHandlerBorderTolerance() const;
139+
double rectHandlerBorderTolerance();
137140

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

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

176184
#endif // QGSCOMPOSERMOUSEHANDLES_H

src/core/composer/qgscomposition.cpp

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -70,6 +70,7 @@ QgsComposition::QgsComposition( QgsMapRenderer* mapRenderer )
7070
, mActiveItemCommand( 0 )
7171
, mActiveMultiFrameCommand( 0 )
7272
, mAtlasComposition( this )
73+
, mPreventCursorChange( false )
7374
{
7475
setBackgroundBrush( Qt::gray );
7576
addPaperItem();
@@ -106,7 +107,8 @@ QgsComposition::QgsComposition()
106107
mSelectionHandles( 0 ),
107108
mActiveItemCommand( 0 ),
108109
mActiveMultiFrameCommand( 0 ),
109-
mAtlasComposition( this )
110+
mAtlasComposition( this ),
111+
mPreventCursorChange( false )
110112
{
111113
loadSettings();
112114

src/core/composer/qgscomposition.h

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -354,6 +354,10 @@ class CORE_EXPORT QgsComposition : public QGraphicsScene
354354
/**Convenience function to create a QgsAddRemoveItemCommand, connect its signals and push it to the undo stack*/
355355
void pushAddRemoveCommand( QgsComposerItem* item, const QString& text, QgsAddRemoveItemCommand::State state = QgsAddRemoveItemCommand::Added );
356356

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

358362
//printing
359363

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

467471
static QString encodeStringForXML( const QString& str );
468472

473+
bool mPreventCursorChange;
474+
469475
signals:
470476
void paperSizeChanged();
471477
void nPagesChanged();

0 commit comments

Comments
 (0)