Skip to content

Commit

Permalink
[FEATURE] Zoom compositions with mouse wheel
Browse files Browse the repository at this point in the history
  • Loading branch information
nyalldawson authored and mhugent committed Oct 18, 2013
1 parent 6950972 commit 7ea3184
Show file tree
Hide file tree
Showing 4 changed files with 98 additions and 8 deletions.
5 changes: 5 additions & 0 deletions src/core/qgsrectangle.cpp
Expand Up @@ -109,6 +109,11 @@ void QgsRectangle::scale( double scaleFactor, const QgsPoint * cp )
centerX = xmin + width() / 2;
centerY = ymin + height() / 2;
}
scale( scaleFactor, centerX, centerY );
}

void QgsRectangle::scale( double scaleFactor, double centerX, double centerY )
{
double newWidth = width() * scaleFactor;
double newHeight = height() * scaleFactor;
xmin = centerX - newWidth / 2.0;
Expand Down
1 change: 1 addition & 0 deletions src/core/qgsrectangle.h
Expand Up @@ -81,6 +81,7 @@ class CORE_EXPORT QgsRectangle
QgsPoint center() const;
//! Scale the rectangle around its center point
void scale( double scaleFactor, const QgsPoint *c = 0 );
void scale( double scaleFactor, double centerX, double centerY );
//! return the intersection with the given rectangle
QgsRectangle intersect( const QgsRectangle *rect ) const;
//! returns true when rectangle intersects with other rectangle
Expand Down
97 changes: 89 additions & 8 deletions src/gui/qgscomposerview.cpp
Expand Up @@ -40,6 +40,7 @@
#include "qgslogger.h"
#include "qgsaddremovemultiframecommand.h"
#include "qgspaperitem.h"
#include "qgsmapcanvas.h" //for QgsMapCanvas::WheelAction

QgsComposerView::QgsComposerView( QWidget* parent, const char* name, Qt::WFlags f )
: QGraphicsView( parent )
Expand Down Expand Up @@ -908,18 +909,98 @@ void QgsComposerView::keyReleaseEvent( QKeyEvent * e )

void QgsComposerView::wheelEvent( QWheelEvent* event )
{
if ( currentTool() == MoveItemContent )
{
//move item content tool, so scroll events get handled by the selected composer item

QPointF scenePoint = mapToScene( event->pos() );
//select topmost item at position of event
QgsComposerItem* theItem = composition()->composerItemAt( scenePoint );
if ( theItem )
{
if ( theItem->isSelected() )
{
QPointF itemPoint = theItem->mapFromScene( scenePoint );
theItem->beginCommand( tr( "Zoom item content" ) );
theItem->zoomContent( event->delta(), itemPoint.x(), itemPoint.y() );
theItem->endCommand();
}
}
}
else
{
//not using move item content tool, so zoom whole composition
wheelZoom( event );
}
}

void QgsComposerView::wheelZoom( QWheelEvent * event )
{
//get mouse wheel zoom behaviour settings
QSettings mySettings;
int wheelAction = mySettings.value( "/qgis/wheel_action", 2 ).toInt();
double zoomFactor = mySettings.value( "/qgis/zoom_factor", 2 ).toDouble();

//caculate zoom scale factor
bool zoomIn = event->delta() > 0;
double scaleFactor = ( zoomIn ? 1 / zoomFactor : zoomFactor );

//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( event->pos() );

//select topmost item at position of event
QgsComposerItem* theItem = composition()->composerItemAt( scenePoint );
if ( theItem )
//zoom composition, respecting wheel action setting
switch (( QgsMapCanvas::WheelAction )wheelAction )
{
case QgsMapCanvas::WheelZoom:
// zoom without changing extent
if ( zoomIn )
{
scale( zoomFactor, zoomFactor );
}
else
{
scale( 1 / zoomFactor, 1 / zoomFactor );
}
break;

case QgsMapCanvas::WheelZoomAndRecenter:
{
visibleRect.scale( scaleFactor, scenePoint.x(), scenePoint.y() );
fitInView( visibleRect.toRectF(), Qt::KeepAspectRatio );
break;
}

case QgsMapCanvas::WheelZoomToMouseCursor:
{
QgsPoint oldCenter( visibleRect.center() );
QgsPoint newCenter( scenePoint.x() + (( oldCenter.x() - scenePoint.x() ) * scaleFactor ),
scenePoint.y() + (( oldCenter.y() - scenePoint.y() ) * scaleFactor ) );

visibleRect.scale( scaleFactor, newCenter.x(), newCenter.y() );
fitInView( visibleRect.toRectF(), Qt::KeepAspectRatio );
break;
}

case QgsMapCanvas::WheelNothing:
return;
}

//update composition for new zoom
updateRulers();
update();
//redraw cached map items
QList<QGraphicsItem *> itemList = composition()->items();
QList<QGraphicsItem *>::iterator itemIt = itemList.begin();
for ( ; itemIt != itemList.end(); ++itemIt )
{
if ( theItem->isSelected() )
QgsComposerMap* mypItem = dynamic_cast<QgsComposerMap *>( *itemIt );
if (( mypItem ) && ( mypItem->previewMode() == QgsComposerMap::Render ) )
{
QPointF itemPoint = theItem->mapFromScene( scenePoint );
theItem->beginCommand( tr( "Zoom item content" ) );
theItem->zoomContent( event->delta(), itemPoint.x(), itemPoint.y() );
theItem->endCommand();
mypItem->updateCachedImage();
}
}
}
Expand Down
3 changes: 3 additions & 0 deletions src/gui/qgscomposerview.h
Expand Up @@ -173,6 +173,9 @@ class GUI_EXPORT QgsComposerView: public QGraphicsView
QPoint mMouseLastXY;
QPoint mMouseCurrentXY;

/**Zoom composition from a mouse wheel event*/
void wheelZoom( QWheelEvent * event );

//void connectAddRemoveCommandSignals( QgsAddRemoveItemCommand* c );

signals:
Expand Down

0 comments on commit 7ea3184

Please sign in to comment.