Skip to content

Commit 8725c2d

Browse files
committedSep 29, 2014
[composer] Fix ctrl modifier not applying to wheel events when in move item
content mode, add missing undo merge command for item zoom (refs #7974)
1 parent 583151a commit 8725c2d

File tree

7 files changed

+132
-44
lines changed

7 files changed

+132
-44
lines changed
 

‎python/core/composer/qgscomposeritem.sip

Lines changed: 24 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -133,6 +133,16 @@ class QgsComposerItem : QgsComposerObject, QGraphicsRectItem
133133
LowerRight
134134
};
135135

136+
/** Modes for zooming item content
137+
*/
138+
enum ZoomMode
139+
{
140+
Zoom = 0, /*< Zoom to center of content */
141+
ZoomRecenter, /*< Zoom and recenter content to point */
142+
ZoomToPoint, /*< Zoom while maintaining relative position of point */
143+
NoZoom /*< No zoom */
144+
};
145+
136146
/**Constructor
137147
@param composition parent composition
138148
@param manageZValue true if the z-Value of this object should be managed by mComposition*/
@@ -183,10 +193,20 @@ class QgsComposerItem : QgsComposerObject, QGraphicsRectItem
183193
virtual void moveContent( double dx, double dy );
184194

185195
/**Zoom content of item. Does nothing per default (but implemented in composer map)
186-
@param delta value from wheel event that describes magnitude and direction (positive /negative number)
187-
@param x x-position of mouse cursor (in item coordinates)
188-
@param y y-position of mouse cursor (in item coordinates)*/
189-
virtual void zoomContent( int delta, double x, double y );
196+
* @param delta value from wheel event that describes direction (positive /negative number)
197+
* @param x x-position of mouse cursor (in item coordinates)
198+
* @param y y-position of mouse cursor (in item coordinates)
199+
* @deprecated use zoomContent( double, QPointF, ZoomMode ) instead
200+
*/
201+
virtual void zoomContent( int delta, double x, double y ) /Deprecated/;
202+
203+
/**Zoom content of item. Does nothing per default (but implemented in composer map)
204+
* @param factor zoom factor, where > 1 results in a zoom in and < 1 results in a zoom out
205+
* @param point item point for zoom center
206+
* @param mode zoom mode
207+
* @note added in QGIS 2.5
208+
*/
209+
virtual void zoomContent( const double factor, const QPointF point, const ZoomMode mode = QgsComposerItem::Zoom );
190210

191211
/**Gets the page the item is currently on.
192212
* @returns page number for item

‎python/core/composer/qgscomposermap.sip

Lines changed: 14 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -130,10 +130,20 @@ class QgsComposerMap : QgsComposerItem
130130
void moveContent( double dx, double dy );
131131

132132
/**Zoom content of map
133-
@param delta value from wheel event that describes magnitude and direction (positive /negative number)
134-
@param x x-coordinate of mouse position in item coordinates
135-
@param y y-coordinate of mouse position in item coordinates*/
136-
void zoomContent( int delta, double x, double y );
133+
* @param delta value from wheel event that describes direction (positive /negative number)
134+
* @param x x-position of mouse cursor (in item coordinates)
135+
* @param y y-position of mouse cursor (in item coordinates)
136+
* @deprecated use zoomContent( double, QPointF, ZoomMode ) instead
137+
*/
138+
void zoomContent( int delta, double x, double y ) /Deprecated/;
139+
140+
/**Zoom content of item. Does nothing per default (but implemented in composer map)
141+
* @param factor zoom factor, where > 1 results in a zoom in and < 1 results in a zoom out
142+
* @param point item point for zoom center
143+
* @param mode zoom mode
144+
* @note added in QGIS 2.5
145+
*/
146+
virtual void zoomContent( const double factor, const QPointF point, const ZoomMode mode = QgsComposerItem::Zoom );
137147

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

‎src/core/composer/qgscomposeritem.h

Lines changed: 26 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -89,6 +89,18 @@ class CORE_EXPORT QgsComposerItem: public QgsComposerObject, public QGraphicsRec
8989
LowerRight
9090
};
9191

92+
//note - must sync with QgsMapCanvas::WheelAction.
93+
//TODO - QGIS 3.0 move QgsMapCanvas::WheelAction from GUI->CORE and remove this enum
94+
/** Modes for zooming item content
95+
*/
96+
enum ZoomMode
97+
{
98+
Zoom = 0, /*< Zoom to center of content */
99+
ZoomRecenter, /*< Zoom and recenter content to point */
100+
ZoomToPoint, /*< Zoom while maintaining relative position of point */
101+
NoZoom /*< No zoom */
102+
};
103+
92104
/**Constructor
93105
@param composition parent composition
94106
@param manageZValue true if the z-Value of this object should be managed by mComposition*/
@@ -139,10 +151,20 @@ class CORE_EXPORT QgsComposerItem: public QgsComposerObject, public QGraphicsRec
139151
virtual void moveContent( double dx, double dy ) { Q_UNUSED( dx ); Q_UNUSED( dy ); }
140152

141153
/**Zoom content of item. Does nothing per default (but implemented in composer map)
142-
@param delta value from wheel event that describes magnitude and direction (positive /negative number)
143-
@param x x-position of mouse cursor (in item coordinates)
144-
@param y y-position of mouse cursor (in item coordinates)*/
145-
virtual void zoomContent( int delta, double x, double y ) { Q_UNUSED( delta ); Q_UNUSED( x ); Q_UNUSED( y ); }
154+
* @param delta value from wheel event that describes direction (positive /negative number)
155+
* @param x x-position of mouse cursor (in item coordinates)
156+
* @param y y-position of mouse cursor (in item coordinates)
157+
* @deprecated use zoomContent( double, QPointF, ZoomMode ) instead
158+
*/
159+
Q_DECL_DEPRECATED virtual void zoomContent( int delta, double x, double y ) { Q_UNUSED( delta ); Q_UNUSED( x ); Q_UNUSED( y ); }
160+
161+
/**Zoom content of item. Does nothing per default (but implemented in composer map)
162+
* @param factor zoom factor, where > 1 results in a zoom in and < 1 results in a zoom out
163+
* @param point item point for zoom center
164+
* @param mode zoom mode
165+
* @note added in QGIS 2.5
166+
*/
167+
virtual void zoomContent( const double factor, const QPointF point, const ZoomMode mode = QgsComposerItem::Zoom ) { Q_UNUSED( factor ); Q_UNUSED( point ); Q_UNUSED( mode ); }
146168

147169
/**Gets the page the item is currently on.
148170
* @returns page number for item

‎src/core/composer/qgscomposeritemcommand.h

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -119,7 +119,8 @@ class CORE_EXPORT QgsComposerMergeCommand: public QgsComposerItemCommand
119119
ItemOutlineWidth,
120120
ItemMove,
121121
ItemRotation,
122-
ItemTransparency
122+
ItemTransparency,
123+
ItemZoomContent
123124
};
124125

125126
QgsComposerMergeCommand( Context c, QgsComposerItem* item, const QString& text );

‎src/core/composer/qgscomposermap.cpp

Lines changed: 33 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -605,55 +605,63 @@ void QgsComposerMap::moveContent( double dx, double dy )
605605

606606
void QgsComposerMap::zoomContent( int delta, double x, double y )
607607
{
608-
if ( mDrawing )
608+
QSettings settings;
609+
610+
//read zoom mode
611+
QgsComposerItem::ZoomMode zoomMode = ( QgsComposerItem::ZoomMode )settings.value( "/qgis/wheel_action", 2 ).toInt();
612+
if ( zoomMode == QgsComposerItem::NoZoom )
609613
{
614+
//do nothing
610615
return;
611616
}
612617

613-
QSettings settings;
618+
double zoomFactor = settings.value( "/qgis/zoom_factor", 2.0 ).toDouble();
619+
zoomFactor = delta > 0 ? zoomFactor : 1 / zoomFactor;
614620

615-
//read zoom mode
616-
//0: zoom, 1: zoom and recenter, 2: zoom to cursor, 3: nothing
617-
int zoomMode = settings.value( "/qgis/wheel_action", 2 ).toInt();
618-
if ( zoomMode == 3 ) //do nothing
621+
zoomContent( zoomFactor, QPointF( x, y ), zoomMode );
622+
}
623+
624+
void QgsComposerMap::zoomContent( const double factor, const QPointF point, const ZoomMode mode )
625+
{
626+
if ( mDrawing )
619627
{
620628
return;
621629
}
622630

623-
double zoomFactor = settings.value( "/qgis/zoom_factor", 2.0 ).toDouble();
631+
if ( mode == QgsComposerItem::NoZoom )
632+
{
633+
//do nothing
634+
return;
635+
}
636+
637+
//find out map coordinates of position
638+
double mapX = currentMapExtent()->xMinimum() + ( point.x() / rect().width() ) * ( currentMapExtent()->xMaximum() - currentMapExtent()->xMinimum() );
639+
double mapY = currentMapExtent()->yMinimum() + ( 1 - ( point.y() / rect().height() ) ) * ( currentMapExtent()->yMaximum() - currentMapExtent()->yMinimum() );
624640

625641
//find out new center point
626642
double centerX = ( currentMapExtent()->xMaximum() + currentMapExtent()->xMinimum() ) / 2;
627643
double centerY = ( currentMapExtent()->yMaximum() + currentMapExtent()->yMinimum() ) / 2;
628644

629-
if ( zoomMode != 0 )
645+
if ( mode != QgsComposerItem::Zoom )
630646
{
631-
//find out map coordinates of mouse position
632-
double mapMouseX = currentMapExtent()->xMinimum() + ( x / rect().width() ) * ( currentMapExtent()->xMaximum() - currentMapExtent()->xMinimum() );
633-
double mapMouseY = currentMapExtent()->yMinimum() + ( 1 - ( y / rect().height() ) ) * ( currentMapExtent()->yMaximum() - currentMapExtent()->yMinimum() );
634-
if ( zoomMode == 1 ) //zoom and recenter
647+
if ( mode == QgsComposerItem::ZoomRecenter )
635648
{
636-
centerX = mapMouseX;
637-
centerY = mapMouseY;
649+
centerX = mapX;
650+
centerY = mapY;
638651
}
639-
else if ( zoomMode == 2 ) //zoom to cursor
652+
else if ( mode == QgsComposerItem::ZoomToPoint )
640653
{
641-
centerX = mapMouseX + ( centerX - mapMouseX ) * ( 1.0 / zoomFactor );
642-
centerY = mapMouseY + ( centerY - mapMouseY ) * ( 1.0 / zoomFactor );
654+
centerX = mapX + ( centerX - mapX ) * ( 1.0 / factor );
655+
centerY = mapY + ( centerY - mapY ) * ( 1.0 / factor );
643656
}
644657
}
645658

646659
double newIntervalX, newIntervalY;
647660

648-
if ( delta > 0 )
649-
{
650-
newIntervalX = ( currentMapExtent()->xMaximum() - currentMapExtent()->xMinimum() ) / zoomFactor;
651-
newIntervalY = ( currentMapExtent()->yMaximum() - currentMapExtent()->yMinimum() ) / zoomFactor;
652-
}
653-
else if ( delta < 0 )
661+
if ( factor > 0 )
654662
{
655-
newIntervalX = ( currentMapExtent()->xMaximum() - currentMapExtent()->xMinimum() ) * zoomFactor;
656-
newIntervalY = ( currentMapExtent()->yMaximum() - currentMapExtent()->yMinimum() ) * zoomFactor;
663+
newIntervalX = ( currentMapExtent()->xMaximum() - currentMapExtent()->xMinimum() ) / factor;
664+
newIntervalY = ( currentMapExtent()->yMaximum() - currentMapExtent()->yMinimum() ) / factor;
657665
}
658666
else //no need to zoom
659667
{

‎src/core/composer/qgscomposermap.h

Lines changed: 14 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -170,10 +170,20 @@ class CORE_EXPORT QgsComposerMap : public QgsComposerItem
170170
void moveContent( double dx, double dy );
171171

172172
/**Zoom content of map
173-
@param delta value from wheel event that describes magnitude and direction (positive /negative number)
174-
@param x x-coordinate of mouse position in item coordinates
175-
@param y y-coordinate of mouse position in item coordinates*/
176-
void zoomContent( int delta, double x, double y );
173+
* @param delta value from wheel event that describes direction (positive /negative number)
174+
* @param x x-position of mouse cursor (in item coordinates)
175+
* @param y y-position of mouse cursor (in item coordinates)
176+
* @deprecated use zoomContent( double, QPointF, ZoomMode ) instead
177+
*/
178+
Q_DECL_DEPRECATED void zoomContent( int delta, double x, double y );
179+
180+
/**Zoom content of item. Does nothing per default (but implemented in composer map)
181+
* @param factor zoom factor, where > 1 results in a zoom in and < 1 results in a zoom out
182+
* @param point item point for zoom center
183+
* @param mode zoom mode
184+
* @note added in QGIS 2.5
185+
*/
186+
virtual void zoomContent( const double factor, const QPointF point, const ZoomMode mode = QgsComposerItem::Zoom );
177187

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

‎src/gui/qgscomposerview.cpp

Lines changed: 19 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1558,9 +1558,26 @@ void QgsComposerView::wheelEvent( QWheelEvent* event )
15581558
{
15591559
if ( theItem->isSelected() )
15601560
{
1561+
QSettings settings;
1562+
//read zoom mode
1563+
QgsComposerItem::ZoomMode zoomMode = ( QgsComposerItem::ZoomMode )settings.value( "/qgis/wheel_action", 2 ).toInt();
1564+
if ( zoomMode == QgsComposerItem::NoZoom )
1565+
{
1566+
//do nothing
1567+
return;
1568+
}
1569+
1570+
double zoomFactor = settings.value( "/qgis/zoom_factor", 2.0 ).toDouble();
1571+
if ( event->modifiers() & Qt::ControlModifier )
1572+
{
1573+
//holding ctrl while wheel zooming results in a finer zoom
1574+
zoomFactor = 1.0 + ( zoomFactor - 1.0 ) / 20.0;
1575+
}
1576+
zoomFactor = event->delta() > 0 ? zoomFactor : 1 / zoomFactor;
1577+
15611578
QPointF itemPoint = theItem->mapFromScene( scenePoint );
1562-
theItem->beginCommand( tr( "Zoom item content" ) );
1563-
theItem->zoomContent( event->delta(), itemPoint.x(), itemPoint.y() );
1579+
theItem->beginCommand( tr( "Zoom item content" ), QgsComposerMergeCommand::ItemZoomContent );
1580+
theItem->zoomContent( zoomFactor, itemPoint, zoomMode );
15641581
theItem->endCommand();
15651582
}
15661583
}

0 commit comments

Comments
 (0)
Please sign in to comment.