[QGIS Commit] r10777 - trunk/qgis/src/app/composer

svn_qgis at osgeo.org svn_qgis at osgeo.org
Wed May 13 06:15:15 EDT 2009


Author: mhugent
Date: 2009-05-13 06:15:15 -0400 (Wed, 13 May 2009)
New Revision: 10777

Modified:
   trunk/qgis/src/app/composer/qgscomposermapwidget.cpp
Log:
Keep width and height of the composer map fixed if user sets the composer map extent to the map canvas extent

Modified: trunk/qgis/src/app/composer/qgscomposermapwidget.cpp
===================================================================
--- trunk/qgis/src/app/composer/qgscomposermapwidget.cpp	2009-05-13 09:06:03 UTC (rev 10776)
+++ trunk/qgis/src/app/composer/qgscomposermapwidget.cpp	2009-05-13 10:15:15 UTC (rev 10777)
@@ -148,15 +148,34 @@
     const QgsMapRenderer* renderer = mComposerMap->mapRenderer();
     if ( renderer )
     {
-      QgsRectangle canvasExtent = renderer->extent();
+      QgsRectangle newExtent = renderer->extent();
 
+      //Make sure the width/height ratio is the same as in current composer map extent.
+      //This is to keep the map item frame and the page layout fixed
+      QgsRectangle currentMapExtent = mComposerMap->extent();
+      double currentWidthHeightRatio = currentMapExtent.width() / currentMapExtent.height();
+      double newWidthHeightRatio = newExtent.width() / newExtent.height();
+
+      if(currentWidthHeightRatio < newWidthHeightRatio)
+       {
+        //enlarge height of new extent
+        double newHeight = newExtent.width() / currentWidthHeightRatio;
+        newExtent.setYMinimum(newExtent.yMaximum() - newHeight);
+       }
+      else if(currentWidthHeightRatio > newWidthHeightRatio)
+      {
+        //enlarge width of new extent
+        double newWidth = currentWidthHeightRatio * newExtent.height();
+        newExtent.setXMaximum(newExtent.xMinimum() + newWidth);
+      }
+
       //fill text into line edits
-      mXMinLineEdit->setText( QString::number( canvasExtent.xMinimum() ) );
-      mXMaxLineEdit->setText( QString::number( canvasExtent.xMaximum() ) );
-      mYMinLineEdit->setText( QString::number( canvasExtent.yMinimum() ) );
-      mYMaxLineEdit->setText( QString::number( canvasExtent.yMaximum() ) );
+      mXMinLineEdit->setText( QString::number( newExtent.xMinimum() ) );
+      mXMaxLineEdit->setText( QString::number( newExtent.xMaximum() ) );
+      mYMinLineEdit->setText( QString::number( newExtent.yMinimum() ) );
+      mYMaxLineEdit->setText( QString::number( newExtent.yMaximum() ) );
 
-      mComposerMap->setNewExtent( canvasExtent );
+      mComposerMap->setNewExtent( newExtent );
     }
   }
 }



More information about the QGIS-commit mailing list