Skip to content
Permalink
Browse files

[FEATURE] Zoom compositions with mouse wheel

  • Loading branch information
nyalldawson authored and mhugent committed Oct 18, 2013
1 parent 6950972 commit 7ea31840d31d89f042775c283d1c9242380fb75a
Showing with 98 additions and 8 deletions.
  1. +5 −0 src/core/qgsrectangle.cpp
  2. +1 −0 src/core/qgsrectangle.h
  3. +89 −8 src/gui/qgscomposerview.cpp
  4. +3 −0 src/gui/qgscomposerview.h
@@ -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;
@@ -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
@@ -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 )
@@ -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();
}
}
}
@@ -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:

0 comments on commit 7ea3184

Please sign in to comment.
You can’t perform that action at this time.