Skip to content

Commit 626ab41

Browse files
author
mhugent
committed
Consider mouse wheel in composer map (ticket #1289)
git-svn-id: http://svn.osgeo.org/qgis/trunk/qgis@9297 c8812cc2-4d05-0410-92ff-de0c093fc19c
1 parent 2678dff commit 626ab41

File tree

5 files changed

+96
-1
lines changed

5 files changed

+96
-1
lines changed

src/core/composer/qgscomposeritem.h

+9-1
Original file line numberDiff line numberDiff line change
@@ -70,9 +70,17 @@ class CORE_EXPORT QgsComposerItem: public QGraphicsRectItem
7070
/**Moves item in canvas coordinates*/
7171
void move( double dx, double dy );
7272

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

78+
/**Zoom content of item. Does nothing per default (but implemented in composer map)
79+
@param delta value from wheel event that describes magnitude and direction (positive /negative number)
80+
@param x x-position of mouse cursor (in item coordinates)
81+
@param y y-position of mouse cursor (in item coordinates)*/
82+
virtual void zoomContent( int delta, double x, double y) {}
83+
7684
/**Sets this items bound in scene coordinates such that 1 item size units
7785
corresponds to 1 scene size unit*/
7886
virtual void setSceneRect( const QRectF& rectangle );

src/core/composer/qgscomposermap.cpp

+63
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@
3535
#include <QGraphicsScene>
3636
#include <QGraphicsView>
3737
#include <QPainter>
38+
#include <QSettings>
3839
#include <iostream>
3940
#include <cmath>
4041

@@ -278,6 +279,68 @@ void QgsComposerMap::moveContent( double dx, double dy )
278279
}
279280
}
280281

282+
void QgsComposerMap::zoomContent( int delta, double x, double y)
283+
{
284+
QSettings settings;
285+
286+
//read zoom mode
287+
//0: zoom, 1: zoom and recenter, 2: zoom to cursor, 3: nothing
288+
int zoomMode = settings.value("/qgis/wheel_action", 0 ).toInt();
289+
if(zoomMode == 3) //do nothing
290+
{
291+
return;
292+
}
293+
294+
double zoomFactor = settings.value("/qgis/zoom_factor", 2.0).toDouble();
295+
296+
//find out new center point
297+
double centerX = (mExtent.xMax() + mExtent.xMin()) / 2;
298+
double centerY = (mExtent.yMax() + mExtent.yMin()) / 2;
299+
300+
if(zoomMode != 0)
301+
{
302+
//find out map coordinates of mouse position
303+
double mapMouseX = mExtent.xMin() + (x / rect().width()) * (mExtent.xMax() - mExtent.xMin());
304+
double mapMouseY = mExtent.yMin() + (1 - (y / rect().height())) * (mExtent.yMax() - mExtent.yMin());
305+
if(zoomMode == 1) //zoom and recenter
306+
{
307+
centerX = mapMouseX;
308+
centerY = mapMouseY;
309+
}
310+
else if(zoomMode == 2) //zoom to cursor
311+
{
312+
centerX = mapMouseX + (centerX - mapMouseX) * (1.0 / zoomFactor);
313+
centerY = mapMouseY + (centerY - mapMouseY) * (1.0 / zoomFactor);
314+
}
315+
}
316+
317+
double newIntervalX, newIntervalY;
318+
319+
if(delta > 0)
320+
{
321+
newIntervalX = (mExtent.xMax() - mExtent.xMin()) / zoomFactor;
322+
newIntervalY = (mExtent.yMax() - mExtent.yMin()) / zoomFactor;
323+
}
324+
else if(delta < 0)
325+
{
326+
newIntervalX = (mExtent.xMax() - mExtent.xMin()) * zoomFactor;
327+
newIntervalY = (mExtent.yMax() - mExtent.yMin()) * zoomFactor;
328+
}
329+
else //no need to zoom
330+
{
331+
return;
332+
}
333+
334+
mExtent.setXMaximum(centerX + newIntervalX / 2);
335+
mExtent.setXMinimum(centerX - newIntervalX / 2);
336+
mExtent.setYMaximum(centerY + newIntervalY / 2);
337+
mExtent.setYMinimum(centerY - newIntervalY / 2);
338+
339+
emit extentChanged();
340+
cache();
341+
update();
342+
}
343+
281344
void QgsComposerMap::setSceneRect( const QRectF& rectangle )
282345
{
283346
double w = rectangle.width();

src/core/composer/qgscomposermap.h

+6
Original file line numberDiff line numberDiff line change
@@ -87,6 +87,12 @@ class CORE_EXPORT QgsComposerMap : /*public QWidget, private Ui::QgsComposerMapB
8787
@param dy move in y-direction (item and canvas coordinates)*/
8888
void moveContent( double dx, double dy );
8989

90+
/**Zoom content of map
91+
@param delta value from wheel event that describes magnitude and direction (positive /negative number)
92+
@param x x-coordinate of mouse position in item coordinates
93+
@param y y-coordinate of mouse position in item coordinates*/
94+
void zoomContent( int delta, double x, double y);
95+
9096
/**Sets new scene rectangle bounds and recalculates hight and extent*/
9197
void setSceneRect( const QRectF& rectangle );
9298

src/gui/qgscomposerview.cpp

+16
Original file line numberDiff line numberDiff line change
@@ -336,6 +336,22 @@ void QgsComposerView::keyReleaseEvent( QKeyEvent * e )
336336
}
337337
}
338338

339+
void QgsComposerView::wheelEvent( QWheelEvent* event)
340+
{
341+
QPointF scenePoint = mapToScene( event->pos() );
342+
343+
//select topmost item at position of event
344+
QgsComposerItem* theItem = composition()->composerItemAt( scenePoint );
345+
if (theItem)
346+
{
347+
if(theItem->isSelected())
348+
{
349+
QPointF itemPoint = theItem->mapFromScene(scenePoint);
350+
theItem->zoomContent(event->delta(), itemPoint.x(), itemPoint.y());
351+
}
352+
}
353+
}
354+
339355
void QgsComposerView::setComposition( QgsComposition* c )
340356
{
341357
setScene( c );

src/gui/qgscomposerview.h

+2
Original file line numberDiff line numberDiff line change
@@ -79,6 +79,8 @@ class GUI_EXPORT QgsComposerView: public QGraphicsView
7979
void keyPressEvent( QKeyEvent * e );
8080
void keyReleaseEvent( QKeyEvent * e );
8181

82+
void wheelEvent( QWheelEvent* event);
83+
8284
private:
8385
/**Status of shift key (used for multiple selection)*/
8486
bool mShiftKeyPressed;

0 commit comments

Comments
 (0)