mirror of
https://github.com/qgis/QGIS.git
synced 2025-02-28 00:17:30 -05:00
[FEATURE] Zoom compositions with mouse wheel
This commit is contained in:
parent
6950972d3d
commit
7ea31840d3
@ -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 )
|
||||
{
|
||||
if ( theItem->isSelected() )
|
||||
case QgsMapCanvas::WheelZoom:
|
||||
// zoom without changing extent
|
||||
if ( zoomIn )
|
||||
{
|
||||
scale( zoomFactor, zoomFactor );
|
||||
}
|
||||
else
|
||||
{
|
||||
scale( 1 / zoomFactor, 1 / zoomFactor );
|
||||
}
|
||||
break;
|
||||
|
||||
case QgsMapCanvas::WheelZoomAndRecenter:
|
||||
{
|
||||
QPointF itemPoint = theItem->mapFromScene( scenePoint );
|
||||
theItem->beginCommand( tr( "Zoom item content" ) );
|
||||
theItem->zoomContent( event->delta(), itemPoint.x(), itemPoint.y() );
|
||||
theItem->endCommand();
|
||||
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 )
|
||||
{
|
||||
QgsComposerMap* mypItem = dynamic_cast<QgsComposerMap *>( *itemIt );
|
||||
if (( mypItem ) && ( mypItem->previewMode() == QgsComposerMap::Render ) )
|
||||
{
|
||||
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:
|
||||
|
Loading…
x
Reference in New Issue
Block a user