Skip to content

Commit

Permalink
Consider mouse wheel in composer map (ticket #1289)
Browse files Browse the repository at this point in the history
git-svn-id: http://svn.osgeo.org/qgis/trunk@9297 c8812cc2-4d05-0410-92ff-de0c093fc19c
  • Loading branch information
mhugent committed Sep 11, 2008
1 parent 077f0a6 commit 4a49d3d
Show file tree
Hide file tree
Showing 5 changed files with 96 additions and 1 deletion.
10 changes: 9 additions & 1 deletion src/core/composer/qgscomposeritem.h
Original file line number Diff line number Diff line change
Expand Up @@ -70,9 +70,17 @@ class CORE_EXPORT QgsComposerItem: public QGraphicsRectItem
/**Moves item in canvas coordinates*/
void move( double dx, double dy );

/**Move Content of item. Does nothing per default (but implemented in composer map)*/
/**Move Content of item. Does nothing per default (but implemented in composer map)
@param dx move in x-direction (canvas coordinates)
@param dy move in y-direction(canvas coordinates)*/
virtual void moveContent( double dx, double dy ) {}

/**Zoom content of item. Does nothing per default (but implemented in composer map)
@param delta value from wheel event that describes magnitude and direction (positive /negative number)
@param x x-position of mouse cursor (in item coordinates)
@param y y-position of mouse cursor (in item coordinates)*/
virtual void zoomContent( int delta, double x, double y) {}

/**Sets this items bound in scene coordinates such that 1 item size units
corresponds to 1 scene size unit*/
virtual void setSceneRect( const QRectF& rectangle );
Expand Down
63 changes: 63 additions & 0 deletions src/core/composer/qgscomposermap.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@
#include <QGraphicsScene>
#include <QGraphicsView>
#include <QPainter>
#include <QSettings>
#include <iostream>
#include <cmath>

Expand Down Expand Up @@ -278,6 +279,68 @@ void QgsComposerMap::moveContent( double dx, double dy )
}
}

void QgsComposerMap::zoomContent( int delta, double x, double y)
{
QSettings settings;

//read zoom mode
//0: zoom, 1: zoom and recenter, 2: zoom to cursor, 3: nothing
int zoomMode = settings.value("/qgis/wheel_action", 0 ).toInt();
if(zoomMode == 3) //do nothing
{
return;
}

double zoomFactor = settings.value("/qgis/zoom_factor", 2.0).toDouble();

//find out new center point
double centerX = (mExtent.xMax() + mExtent.xMin()) / 2;
double centerY = (mExtent.yMax() + mExtent.yMin()) / 2;

if(zoomMode != 0)
{
//find out map coordinates of mouse position
double mapMouseX = mExtent.xMin() + (x / rect().width()) * (mExtent.xMax() - mExtent.xMin());
double mapMouseY = mExtent.yMin() + (1 - (y / rect().height())) * (mExtent.yMax() - mExtent.yMin());
if(zoomMode == 1) //zoom and recenter
{
centerX = mapMouseX;
centerY = mapMouseY;
}
else if(zoomMode == 2) //zoom to cursor
{
centerX = mapMouseX + (centerX - mapMouseX) * (1.0 / zoomFactor);
centerY = mapMouseY + (centerY - mapMouseY) * (1.0 / zoomFactor);
}
}

double newIntervalX, newIntervalY;

if(delta > 0)
{
newIntervalX = (mExtent.xMax() - mExtent.xMin()) / zoomFactor;
newIntervalY = (mExtent.yMax() - mExtent.yMin()) / zoomFactor;
}
else if(delta < 0)
{
newIntervalX = (mExtent.xMax() - mExtent.xMin()) * zoomFactor;
newIntervalY = (mExtent.yMax() - mExtent.yMin()) * zoomFactor;
}
else //no need to zoom
{
return;
}

mExtent.setXMaximum(centerX + newIntervalX / 2);
mExtent.setXMinimum(centerX - newIntervalX / 2);
mExtent.setYMaximum(centerY + newIntervalY / 2);
mExtent.setYMinimum(centerY - newIntervalY / 2);

emit extentChanged();
cache();
update();
}

void QgsComposerMap::setSceneRect( const QRectF& rectangle )
{
double w = rectangle.width();
Expand Down
6 changes: 6 additions & 0 deletions src/core/composer/qgscomposermap.h
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,12 @@ class CORE_EXPORT QgsComposerMap : /*public QWidget, private Ui::QgsComposerMapB
@param dy move in y-direction (item and canvas coordinates)*/
void moveContent( double dx, double dy );

/**Zoom content of map
@param delta value from wheel event that describes magnitude and direction (positive /negative number)
@param x x-coordinate of mouse position in item coordinates
@param y y-coordinate of mouse position in item coordinates*/
void zoomContent( int delta, double x, double y);

/**Sets new scene rectangle bounds and recalculates hight and extent*/
void setSceneRect( const QRectF& rectangle );

Expand Down
16 changes: 16 additions & 0 deletions src/gui/qgscomposerview.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -336,6 +336,22 @@ void QgsComposerView::keyReleaseEvent( QKeyEvent * e )
}
}

void QgsComposerView::wheelEvent( QWheelEvent* event)
{
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->zoomContent(event->delta(), itemPoint.x(), itemPoint.y());
}
}
}

void QgsComposerView::setComposition( QgsComposition* c )
{
setScene( c );
Expand Down
2 changes: 2 additions & 0 deletions src/gui/qgscomposerview.h
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,8 @@ class GUI_EXPORT QgsComposerView: public QGraphicsView
void keyPressEvent( QKeyEvent * e );
void keyReleaseEvent( QKeyEvent * e );

void wheelEvent( QWheelEvent* event);

private:
/**Status of shift key (used for multiple selection)*/
bool mShiftKeyPressed;
Expand Down

0 comments on commit 4a49d3d

Please sign in to comment.