Skip to content

Commit

Permalink
[FEATURE] Add composer zoom tool for marquee based zoom into a specif…
Browse files Browse the repository at this point in the history
…ic composer region
  • Loading branch information
nyalldawson committed Oct 25, 2013
1 parent 5f5cd4c commit a3b2629
Show file tree
Hide file tree
Showing 5 changed files with 156 additions and 5 deletions.
12 changes: 11 additions & 1 deletion src/app/composer/qgscomposer.cpp
Expand Up @@ -145,6 +145,7 @@ QgsComposer::QgsComposer( QgisApp *qgis, const QString& title )
QActionGroup* toggleActionGroup = new QActionGroup( this );
toggleActionGroup->addAction( mActionMoveItemContent );
toggleActionGroup->addAction( mActionPan );
toggleActionGroup->addAction( mActionMouseZoom );
toggleActionGroup->addAction( mActionAddNewMap );
toggleActionGroup->addAction( mActionAddNewLabel );
toggleActionGroup->addAction( mActionAddNewLegend );
Expand All @@ -159,7 +160,6 @@ QgsComposer::QgsComposer( QgisApp *qgis, const QString& title )
toggleActionGroup->addAction( mActionAddHtml );
toggleActionGroup->setExclusive( true );


mActionAddNewMap->setCheckable( true );
mActionAddNewLabel->setCheckable( true );
mActionAddNewLegend->setCheckable( true );
Expand All @@ -168,6 +168,7 @@ QgsComposer::QgsComposer( QgisApp *qgis, const QString& title )
mActionAddImage->setCheckable( true );
mActionMoveItemContent->setCheckable( true );
mActionPan->setCheckable( true );
mActionMouseZoom->setCheckable( true );
mActionAddArrow->setCheckable( true );

#ifdef Q_WS_MAC
Expand Down Expand Up @@ -447,6 +448,7 @@ void QgsComposer::setupTheme()
mActionZoomAll->setIcon( QgsApplication::getThemeIcon( "/mActionZoomFullExtent.svg" ) );
mActionZoomIn->setIcon( QgsApplication::getThemeIcon( "/mActionZoomIn.svg" ) );
mActionZoomOut->setIcon( QgsApplication::getThemeIcon( "/mActionZoomOut.svg" ) );
mActionMouseZoom->setIcon( QgsApplication::getThemeIcon( "/mActionZoomToSelected.svg" ) );
mActionRefreshView->setIcon( QgsApplication::getThemeIcon( "/mActionDraw.svg" ) );
mActionUndo->setIcon( QgsApplication::getThemeIcon( "/mActionUndo.png" ) );
mActionRedo->setIcon( QgsApplication::getThemeIcon( "/mActionRedo.png" ) );
Expand Down Expand Up @@ -647,6 +649,14 @@ void QgsComposer::on_mActionZoomOut_triggered()
emit zoomLevelChanged();
}

void QgsComposer::on_mActionMouseZoom_triggered()
{
if ( mView )
{
mView->setCurrentTool( QgsComposerView::Zoom );
}
}

void QgsComposer::on_mActionRefreshView_triggered()
{
if ( !mComposition )
Expand Down
3 changes: 3 additions & 0 deletions src/app/composer/qgscomposer.h
Expand Up @@ -207,6 +207,9 @@ class QgsComposer: public QMainWindow, private Ui::QgsComposerBase
//! Set tool to move item content
void on_mActionPan_triggered();

//! Set tool to mouse zoom
void on_mActionMouseZoom_triggered();

//! Group selected items
void on_mActionGroupItems_triggered();

Expand Down
120 changes: 119 additions & 1 deletion src/gui/qgscomposerview.cpp
Expand Up @@ -41,13 +41,15 @@
#include "qgsaddremovemultiframecommand.h"
#include "qgspaperitem.h"
#include "qgsmapcanvas.h" //for QgsMapCanvas::WheelAction
#include "qgscursors.h"

QgsComposerView::QgsComposerView( QWidget* parent, const char* name, Qt::WFlags f )
: QGraphicsView( parent )
, mRubberBandItem( 0 )
, mRubberBandLineItem( 0 )
, mMoveContentItem( 0 )
, mMarqueeSelect( false )
, mMarqueeZoom( false )
, mPaintingEnabled( true )
, mHorizontalRuler( 0 )
, mVerticalRuler( 0 )
Expand Down Expand Up @@ -77,6 +79,15 @@ void QgsComposerView::setCurrentTool( QgsComposerView::Tool t )
composition()->setPreventCursorChange( true );
viewport()->setCursor( Qt::OpenHandCursor );
}
else if ( t == QgsComposerView::Zoom )
{
//lock cursor to prevent composer items changing it
composition()->setPreventCursorChange( true );
//set the cursor to zoom in
QPixmap myZoomQPixmap = QPixmap(( const char ** )( zoom_in ) );
QCursor zoomCursor = QCursor( myZoomQPixmap, 7, 7 );
viewport()->setCursor( zoomCursor );
}
else
{
//not using pan tool, composer items can change cursor
Expand Down Expand Up @@ -206,6 +217,33 @@ void QgsComposerView::mousePressEvent( QMouseEvent* e )
break;
}

case Zoom:
{
if ( !( e->modifiers() & Qt::ShiftModifier ) )
{
//zoom in action
startMarqueeZoom( scenePoint );
}
else
{
//zoom out action, so zoom out and recenter on clicked point
double scaleFactor = 2;
//get current visible part of scene
QRect viewportRect( 0, 0, viewport()->width(), viewport()->height() );
QgsRectangle visibleRect = QgsRectangle( mapToScene( viewportRect ).boundingRect() );

//transform the mouse pos to scene coordinates
QPointF scenePoint = mapToScene( e->pos() );

visibleRect.scale( scaleFactor, scenePoint.x(), scenePoint.y() );
QRectF boundsRect = visibleRect.toRectF();

//zoom view to fit desired bounds
fitInView( boundsRect, Qt::KeepAspectRatio );
}
break;
}

case Pan:
{
//pan action
Expand Down Expand Up @@ -473,6 +511,55 @@ void QgsComposerView::endMarqueeSelect( QMouseEvent* e )
}
}

void QgsComposerView::startMarqueeZoom( QPointF & scenePoint )
{
mMarqueeZoom = true;

QTransform t;
mRubberBandItem = new QGraphicsRectItem( 0, 0, 0, 0 );
mRubberBandItem->setBrush( QBrush( QColor( 70, 50, 255, 25 ) ) );
mRubberBandItem->setPen( QPen( QColor( 70, 50, 255, 100 ) ) );
mRubberBandStartPos = QPointF( scenePoint.x(), scenePoint.y() );
t.translate( scenePoint.x(), scenePoint.y() );
mRubberBandItem->setTransform( t );
mRubberBandItem->setZValue( 1000 );
scene()->addItem( mRubberBandItem );
scene()->update();
}

void QgsComposerView::endMarqueeZoom( QMouseEvent* e )
{
mMarqueeZoom = false;

QRectF boundsRect;

if ( !mRubberBandItem || ( mRubberBandItem->rect().width() < 0.1 && mRubberBandItem->rect().height() < 0.1 ) )
{
//just a click, so zoom to clicked point and recenter
double scaleFactor = 0.5;
//get current visible part of scene
QRect viewportRect( 0, 0, viewport()->width(), viewport()->height() );
QgsRectangle visibleRect = QgsRectangle( mapToScene( viewportRect ).boundingRect() );

//transform the mouse pos to scene coordinates
QPointF scenePoint = mapToScene( e->pos() );

visibleRect.scale( scaleFactor, scenePoint.x(), scenePoint.y() );
boundsRect = visibleRect.toRectF();
}
else
{
//marquee zoom
//zoom bounds are size marquee object
boundsRect = QRectF( mRubberBandItem->transform().dx(), mRubberBandItem->transform().dy(),
mRubberBandItem->rect().width(), mRubberBandItem->rect().height() );
}

removeRubberBand();
//zoom view to fit desired bounds
fitInView( boundsRect, Qt::KeepAspectRatio );
}

void QgsComposerView::mouseReleaseEvent( QMouseEvent* e )
{
if ( !composition() )
Expand Down Expand Up @@ -516,6 +603,15 @@ void QgsComposerView::mouseReleaseEvent( QMouseEvent* e )
break;
}

case Zoom:
{
if ( mMarqueeZoom )
{
endMarqueeZoom( e );
}
break;
}

case MoveItemContent:
{
if ( mMoveContentItem )
Expand Down Expand Up @@ -633,7 +729,7 @@ void QgsComposerView::mouseMoveEvent( QMouseEvent* e )
{
QPointF scenePoint = mapToScene( e->pos() );

if ( mMarqueeSelect )
if ( mMarqueeSelect || mMarqueeZoom )
{
updateRubberBand( scenePoint );
return;
Expand Down Expand Up @@ -934,6 +1030,17 @@ void QgsComposerView::keyPressEvent( QKeyEvent * e )
return;
}

if ( mCurrentTool == QgsComposerView::Zoom )
{
if ( ! e->isAutoRepeat() )
{
QPixmap myZoomQPixmap = QPixmap(( const char ** )( e->modifiers() & Qt::ShiftModifier ? zoom_out : zoom_in ) );
QCursor zoomCursor = QCursor( myZoomQPixmap, 7, 7 );
viewport()->setCursor( zoomCursor );
}
return;
}

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

Expand Down Expand Up @@ -1006,6 +1113,17 @@ void QgsComposerView::keyReleaseEvent( QKeyEvent * e )
}
return;
}

if ( mCurrentTool == QgsComposerView::Zoom )
{
if ( ! e->isAutoRepeat() )
{
QPixmap myZoomQPixmap = QPixmap(( const char ** )( e->modifiers() & Qt::ShiftModifier ? zoom_out : zoom_in ) );
QCursor zoomCursor = QCursor( myZoomQPixmap, 7, 7 );
viewport()->setCursor( zoomCursor );
}
return;
}
}

void QgsComposerView::wheelEvent( QWheelEvent* event )
Expand Down
9 changes: 8 additions & 1 deletion src/gui/qgscomposerview.h
Expand Up @@ -66,7 +66,8 @@ class GUI_EXPORT QgsComposerView: public QGraphicsView
AddTriangle,
AddTable, //add attribute table
MoveItemContent, //move content of item (e.g. content of map)
Pan
Pan,
Zoom
};

enum ClipboardMode
Expand Down Expand Up @@ -163,6 +164,8 @@ class GUI_EXPORT QgsComposerView: public QGraphicsView

/**True if user is currently selecting by marquee*/
bool mMarqueeSelect;
/**True if user is currently zooming by marquee*/
bool mMarqueeZoom;

bool mPaintingEnabled;

Expand All @@ -187,6 +190,10 @@ class GUI_EXPORT QgsComposerView: public QGraphicsView
void startMarqueeSelect( QPointF & scenePoint );
/**Finalises a marquee selection*/
void endMarqueeSelect( QMouseEvent* e );
/**Starts a zoom in marquee*/
void startMarqueeZoom( QPointF & scenePoint );
/**Finalises a marquee zoom*/
void endMarqueeZoom( QMouseEvent* e );

//void connectAddRemoveCommandSignals( QgsAddRemoveItemCommand* c );

Expand Down
17 changes: 15 additions & 2 deletions src/ui/qgscomposerbase.ui
Expand Up @@ -97,6 +97,7 @@
<bool>true</bool>
</attribute>
<addaction name="mActionPan"/>
<addaction name="mActionMouseZoom"/>
<addaction name="mActionSelectMoveItem"/>
<addaction name="mActionMoveItemContent"/>
<addaction name="mActionGroupItems"/>
Expand Down Expand Up @@ -183,6 +184,18 @@
<string>Ctrl+-</string>
</property>
</action>
<action name="mActionMouseZoom">
<property name="icon">
<iconset resource="../../images/images.qrc">
<normaloff>:/images/themes/default/mActionZoomToSelected.svg</normaloff>:/images/themes/default/mActionZoomToSelected.svg</iconset>
</property>
<property name="text">
<string>Zoom</string>
</property>
<property name="toolTip">
<string>Zoom</string>
</property>
</action>
<action name="mActionAddNewMap">
<property name="icon">
<iconset resource="../../images/images.qrc">
Expand Down Expand Up @@ -688,7 +701,7 @@
<property name="shortcut">
<string>Ctrl+Alt+]</string>
</property>
</action>
</action>
<action name="mActionPan">
<property name="icon">
<iconset resource="../../images/images.qrc">
Expand All @@ -697,7 +710,7 @@
<property name="text">
<string>Pan Composer</string>
</property>
</action>
</action>
</widget>
<resources>
<include location="../../images/images.qrc"/>
Expand Down

0 comments on commit a3b2629

Please sign in to comment.