Skip to content

Commit b583597

Browse files
committed
[composer] Add zoomToExtent for QgsComposerMap, allows easily fitting a set extent into the map item without altering the item's shape
1 parent dd78f09 commit b583597

File tree

4 files changed

+73
-32
lines changed

4 files changed

+73
-32
lines changed

python/core/composer/qgscomposermap.sip

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -129,9 +129,24 @@ class QgsComposerMap : QgsComposerItem
129129
/**Sets new scale and changes only mExtent*/
130130
void setNewScale( double scaleDenominator, bool forceUpdate = true );
131131

132-
/**Sets new Extent and changes width, height (and implicitely also scale)*/
132+
/**Sets new extent for the map. This method may change the width or height of the map
133+
* item to ensure that the extent exactly matches the specified extent, with no
134+
* overlap or margin. This method implicitly alters the map scale.
135+
* @param extent new extent for the map
136+
* @see zoomToExtent
137+
*/
133138
void setNewExtent( const QgsRectangle& extent );
134139

140+
/**Zooms the map so that the specified extent is fully visible within the map item.
141+
* This method will not change the width or height of the map, and may result in
142+
* an overlap or margin from the specified extent. This method implicitly alters the
143+
* map scale.
144+
* @param extent new extent for the map
145+
* @see setNewExtent
146+
* @note added in QGIS 2.5
147+
*/
148+
void zoomToExtent( const QgsRectangle& extent );
149+
135150
/**Sets new Extent for the current atlas preview and changes width, height (and implicitely also scale).
136151
Atlas preview extents are only temporary, and are regenerated whenever the atlas feature changes
137152
*/

src/app/composer/qgscomposermapwidget.cpp

Lines changed: 1 addition & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -447,37 +447,8 @@ void QgsComposerMapWidget::on_mSetToMapCanvasExtentButton_clicked()
447447

448448
QgsRectangle newExtent = mComposerMap->composition()->mapSettings().visibleExtent();
449449

450-
//Make sure the width/height ratio is the same as in current composer map extent.
451-
//This is to keep the map item frame and the page layout fixed
452-
QgsRectangle currentMapExtent = *( mComposerMap->currentMapExtent() );
453-
double currentWidthHeightRatio = currentMapExtent.width() / currentMapExtent.height();
454-
double newWidthHeightRatio = newExtent.width() / newExtent.height();
455-
456-
if ( currentWidthHeightRatio < newWidthHeightRatio )
457-
{
458-
//enlarge height of new extent, ensuring the map center stays the same
459-
double newHeight = newExtent.width() / currentWidthHeightRatio;
460-
double deltaHeight = newHeight - newExtent.height();
461-
newExtent.setYMinimum( newExtent.yMinimum() - deltaHeight / 2 );
462-
newExtent.setYMaximum( newExtent.yMaximum() + deltaHeight / 2 );
463-
}
464-
else
465-
{
466-
//enlarge width of new extent, ensuring the map center stays the same
467-
double newWidth = currentWidthHeightRatio * newExtent.height();
468-
double deltaWidth = newWidth - newExtent.width();
469-
newExtent.setXMinimum( newExtent.xMinimum() - deltaWidth / 2 );
470-
newExtent.setXMaximum( newExtent.xMaximum() + deltaWidth / 2 );
471-
}
472-
473-
//fill text into line edits
474-
mXMinLineEdit->setText( QString::number( newExtent.xMinimum() ) );
475-
mXMaxLineEdit->setText( QString::number( newExtent.xMaximum() ) );
476-
mYMinLineEdit->setText( QString::number( newExtent.yMinimum() ) );
477-
mYMaxLineEdit->setText( QString::number( newExtent.yMaximum() ) );
478-
479450
mComposerMap->beginCommand( tr( "Map extent changed" ) );
480-
mComposerMap->setNewExtent( newExtent );
451+
mComposerMap->zoomToExtent( newExtent );
481452
mComposerMap->endCommand();
482453
}
483454

src/core/composer/qgscomposermap.cpp

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -710,6 +710,46 @@ void QgsComposerMap::setNewExtent( const QgsRectangle& extent )
710710
updateItem();
711711
}
712712

713+
void QgsComposerMap::zoomToExtent( const QgsRectangle &extent )
714+
{
715+
QgsRectangle newExtent = extent;
716+
//Make sure the width/height ratio is the same as the current composer map extent.
717+
//This is to keep the map item frame size fixed
718+
double currentWidthHeightRatio = currentMapExtent()->width() / currentMapExtent()->height();
719+
double newWidthHeightRatio = newExtent.width() / newExtent.height();
720+
721+
if ( currentWidthHeightRatio < newWidthHeightRatio )
722+
{
723+
//enlarge height of new extent, ensuring the map center stays the same
724+
double newHeight = newExtent.width() / currentWidthHeightRatio;
725+
double deltaHeight = newHeight - newExtent.height();
726+
newExtent.setYMinimum( newExtent.yMinimum() - deltaHeight / 2 );
727+
newExtent.setYMaximum( newExtent.yMaximum() + deltaHeight / 2 );
728+
}
729+
else
730+
{
731+
//enlarge width of new extent, ensuring the map center stays the same
732+
double newWidth = currentWidthHeightRatio * newExtent.height();
733+
double deltaWidth = newWidth - newExtent.width();
734+
newExtent.setXMinimum( newExtent.xMinimum() - deltaWidth / 2 );
735+
newExtent.setXMaximum( newExtent.xMaximum() + deltaWidth / 2 );
736+
}
737+
738+
if ( *currentMapExtent() == newExtent )
739+
{
740+
return;
741+
}
742+
*currentMapExtent() = newExtent;
743+
744+
//recalculate data defined scale and extents, since that may override extent
745+
refreshMapExtents();
746+
747+
mCacheUpdated = false;
748+
updateItem();
749+
emit itemChanged();
750+
emit extentChanged();
751+
}
752+
713753
void QgsComposerMap::setNewAtlasFeatureExtent( const QgsRectangle& extent )
714754
{
715755
if ( mAtlasFeatureExtent != extent )

src/core/composer/qgscomposermap.h

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -172,9 +172,24 @@ class CORE_EXPORT QgsComposerMap : public QgsComposerItem
172172
/**Sets new scale and changes only mExtent*/
173173
void setNewScale( double scaleDenominator, bool forceUpdate = true );
174174

175-
/**Sets new Extent and changes width, height (and implicitely also scale)*/
175+
/**Sets new extent for the map. This method may change the width or height of the map
176+
* item to ensure that the extent exactly matches the specified extent, with no
177+
* overlap or margin. This method implicitly alters the map scale.
178+
* @param extent new extent for the map
179+
* @see zoomToExtent
180+
*/
176181
void setNewExtent( const QgsRectangle& extent );
177182

183+
/**Zooms the map so that the specified extent is fully visible within the map item.
184+
* This method will not change the width or height of the map, and may result in
185+
* an overlap or margin from the specified extent. This method implicitly alters the
186+
* map scale.
187+
* @param extent new extent for the map
188+
* @see setNewExtent
189+
* @note added in QGIS 2.5
190+
*/
191+
void zoomToExtent( const QgsRectangle& extent );
192+
178193
/**Sets new Extent for the current atlas preview and changes width, height (and implicitely also scale).
179194
Atlas preview extents are only temporary, and are regenerated whenever the atlas feature changes
180195
*/

0 commit comments

Comments
 (0)