124 changes: 123 additions & 1 deletion src/gui/qgscomposerview.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@ QgsComposerView::QgsComposerView( QWidget* parent, const char* name, Qt::WFlags
, mPaintingEnabled( true )
, mHorizontalRuler( 0 )
, mVerticalRuler( 0 )
, mPanning( false )
{
Q_UNUSED( f );
Q_UNUSED( name );
Expand All @@ -59,6 +60,29 @@ QgsComposerView::QgsComposerView( QWidget* parent, const char* name, Qt::WFlags
setFrameShape( QFrame::NoFrame );
}

void QgsComposerView::setCurrentTool( QgsComposerView::Tool t )
{
mCurrentTool = t;

//update mouse cursor for current tool
if ( !composition() )
{
return;
}
if ( t == QgsComposerView::Pan )
{
//lock cursor to prevent composer items changing it
composition()->setPreventCursorChange( true );
viewport()->setCursor( Qt::OpenHandCursor );
}
else
{
//not using pan tool, composer items can change cursor
composition()->setPreventCursorChange( false );
viewport()->setCursor( Qt::ArrowCursor );
}
}

void QgsComposerView::mousePressEvent( QMouseEvent* e )
{
if ( !composition() )
Expand All @@ -81,6 +105,19 @@ void QgsComposerView::mousePressEvent( QMouseEvent* e )
}
return;
}
else if ( e->button() == Qt::MidButton )
{
//pan composer with middle button
mPanning = true;
mMouseLastXY = e->pos();
if ( composition() )
{
//lock cursor to closed hand cursor
composition()->setPreventCursorChange( true );
}
viewport()->setCursor( Qt::ClosedHandCursor );
return;
}

switch ( mCurrentTool )
{
Expand Down Expand Up @@ -166,6 +203,15 @@ void QgsComposerView::mousePressEvent( QMouseEvent* e )
break;
}

case Pan:
{
//pan action
mPanning = true;
mMouseLastXY = e->pos();
viewport()->setCursor( Qt::ClosedHandCursor );
break;
}

case MoveItemContent:
{
//get a list of items at clicked position
Expand Down Expand Up @@ -341,6 +387,27 @@ void QgsComposerView::mouseReleaseEvent( QMouseEvent* e )

QPointF scenePoint = mapToScene( e->pos() );

if ( mPanning )
{
mPanning = false;

//set new cursor
if ( mCurrentTool == Pan )
{
viewport()->setCursor( Qt::OpenHandCursor );
}
else
{
if ( composition() )
{
//allow composer items to change cursor
composition()->setPreventCursorChange( false );
}
viewport()->setCursor( Qt::ArrowCursor );
}
return;
}

switch ( mCurrentTool )
{
case Select:
Expand Down Expand Up @@ -444,6 +511,8 @@ void QgsComposerView::mouseMoveEvent( QMouseEvent* e )
return;
}

mMouseCurrentXY = e->pos();

updateRulers();
if ( mHorizontalRuler )
{
Expand All @@ -454,7 +523,15 @@ void QgsComposerView::mouseMoveEvent( QMouseEvent* e )
mVerticalRuler->updateMarker( e->posF() );
}

if ( e->buttons() == Qt::NoButton )
if ( mPanning )
{
//panning, so scroll view
horizontalScrollBar()->setValue( horizontalScrollBar()->value() - ( e->x() - mMouseLastXY.x() ) );
verticalScrollBar()->setValue( verticalScrollBar()->value() - ( e->y() - mMouseLastXY.y() ) );
mMouseLastXY = e->pos();
return;
}
else if ( e->buttons() == Qt::NoButton )
{
if ( mCurrentTool == Select )
{
Expand Down Expand Up @@ -735,6 +812,26 @@ void QgsComposerView::keyPressEvent( QKeyEvent * e )
return;
}

if ( mPanning )
return;

if ( e->key() == Qt::Key_Space )
{
// Pan composer with space bar
if ( ! e->isAutoRepeat() )
{
mPanning = true;
mMouseLastXY = mMouseCurrentXY;
if ( composition() )
{
//prevent cursor changes while panning
composition()->setPreventCursorChange( true );
}
viewport()->setCursor( Qt::ClosedHandCursor );
}
return;
}

QList<QgsComposerItem*> composerItemList = composition()->selectedComposerItems();
QList<QgsComposerItem*>::iterator itemIt = composerItemList.begin();

Expand Down Expand Up @@ -784,6 +881,31 @@ void QgsComposerView::keyPressEvent( QKeyEvent * e )
}
}

void QgsComposerView::keyReleaseEvent( QKeyEvent * e )
{
if ( e->key() == Qt::Key_Space && !e->isAutoRepeat() && mPanning )
{
//end of panning with space key
mPanning = false;

//reset cursor
if ( mCurrentTool == Pan )
{
viewport()->setCursor( Qt::OpenHandCursor );
}
else
{
if ( composition() )
{
//allow cursor changes again
composition()->setPreventCursorChange( false );
}
viewport()->setCursor( Qt::ArrowCursor );
}
return;
}
}

void QgsComposerView::wheelEvent( QWheelEvent* event )
{
QPointF scenePoint = mapToScene( event->pos() );
Expand Down
10 changes: 8 additions & 2 deletions src/gui/qgscomposerview.h
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,8 @@ class GUI_EXPORT QgsComposerView: public QGraphicsView
AddEllipse,
AddTriangle,
AddTable, //add attribute table
MoveItemContent //move content of item (e.g. content of map)
MoveItemContent, //move content of item (e.g. content of map)
Pan
};

enum ClipboardMode
Expand Down Expand Up @@ -108,7 +109,7 @@ class GUI_EXPORT QgsComposerView: public QGraphicsView
void selectInvert();

QgsComposerView::Tool currentTool() const {return mCurrentTool;}
void setCurrentTool( QgsComposerView::Tool t ) {mCurrentTool = t;}
void setCurrentTool( QgsComposerView::Tool t );

/**Sets composition (derived from QGraphicsScene)*/
void setComposition( QgsComposition* c );
Expand All @@ -134,6 +135,7 @@ class GUI_EXPORT QgsComposerView: public QGraphicsView
void mouseDoubleClickEvent( QMouseEvent* e );

void keyPressEvent( QKeyEvent * e );
void keyReleaseEvent( QKeyEvent * e );

void wheelEvent( QWheelEvent* event );

Expand Down Expand Up @@ -167,6 +169,10 @@ class GUI_EXPORT QgsComposerView: public QGraphicsView
/** Draw a shape on the canvas */
void addShape( Tool currentTool );

bool mPanning;
QPoint mMouseLastXY;
QPoint mMouseCurrentXY;

//void connectAddRemoveCommandSignals( QgsAddRemoveItemCommand* c );

signals:
Expand Down
10 changes: 10 additions & 0 deletions src/ui/qgscomposerbase.ui
Original file line number Diff line number Diff line change
Expand Up @@ -96,6 +96,7 @@
<attribute name="toolBarBreak">
<bool>true</bool>
</attribute>
<addaction name="mActionPan"/>
<addaction name="mActionSelectMoveItem"/>
<addaction name="mActionMoveItemContent"/>
<addaction name="mActionGroupItems"/>
Expand Down Expand Up @@ -673,6 +674,15 @@
<string>Ctrl+Alt+]</string>
</property>
</action>
<action name="mActionPan">
<property name="icon">
<iconset resource="../../images/images.qrc">
<normaloff>:/images/themes/default/mActionPan.svg</normaloff>:/images/themes/default/mActionPan.svg</iconset>
</property>
<property name="text">
<string>Pan Composer</string>
</property>
</action>
</widget>
<resources>
<include location="../../images/images.qrc"/>
Expand Down