Showing with 117 additions and 9 deletions.
  1. +3 −0 src/app/composer/qgscomposer.cpp
  2. +5 −0 src/core/qgsrectangle.cpp
  3. +1 −0 src/core/qgsrectangle.h
  4. +89 −8 src/gui/qgscomposerview.cpp
  5. +3 −0 src/gui/qgscomposerview.h
  6. +16 −1 src/ui/qgscomposerbase.ui
3 changes: 3 additions & 0 deletions src/app/composer/qgscomposer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -244,6 +244,9 @@ QgsComposer::QgsComposer( QgisApp *qgis, const QString& title )
editMenu->addAction( mActionSelectNextAbove );

QMenu *viewMenu = menuBar()->addMenu( tr( "View" ) );
//Ctrl+= should also trigger zoom in
QShortcut* ctrlEquals = new QShortcut( QKeySequence( "Ctrl+=" ), this );
connect( ctrlEquals, SIGNAL( activated() ), mActionZoomIn, SLOT( trigger() ) );
viewMenu->addAction( mActionZoomIn );
viewMenu->addAction( mActionZoomOut );
viewMenu->addAction( mActionZoomAll );
Expand Down
5 changes: 5 additions & 0 deletions src/core/qgsrectangle.cpp
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
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
17 changes: 16 additions & 1 deletion src/ui/qgscomposerbase.ui
Original file line number Diff line number Diff line change
Expand Up @@ -149,6 +149,9 @@
<property name="toolTip">
<string>Zoom full</string>
</property>
<property name="shortcut">
<string>Ctrl+0</string>
</property>
</action>
<action name="mActionZoomIn">
<property name="icon">
Expand All @@ -161,6 +164,9 @@
<property name="toolTip">
<string>Zoom in</string>
</property>
<property name="shortcut">
<string>Ctrl++</string>
</property>
</action>
<action name="mActionZoomOut">
<property name="icon">
Expand All @@ -173,6 +179,9 @@
<property name="toolTip">
<string>Zoom out</string>
</property>
<property name="shortcut">
<string>Ctrl+-</string>
</property>
</action>
<action name="mActionAddNewMap">
<property name="icon">
Expand Down Expand Up @@ -303,6 +312,9 @@
<property name="toolTip">
<string>Group items</string>
</property>
<property name="shortcut">
<string>Ctrl+G</string>
</property>
</action>
<action name="mActionUngroupItems">
<property name="text">
Expand All @@ -311,6 +323,9 @@
<property name="toolTip">
<string>Ungroup items</string>
</property>
<property name="shortcut">
<string>Ctrl+Shift+G</string>
</property>
</action>
<action name="mActionRaiseItems">
<property name="text">
Expand Down Expand Up @@ -454,7 +469,7 @@
</action>
<action name="mActionUndo">
<property name="icon">
<iconset resource="../../images/images.qrc">
<iconset>
<normalon>:/images/themes/default/mActionUndo.png</normalon>
</iconset>
</property>
Expand Down