42 changes: 27 additions & 15 deletions src/core/composer/qgsatlascomposition.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -443,38 +443,43 @@ void QgsAtlasComposition::prepareForFeature( int featureI )
atlasMaps << currentMap;
}

//clear the transformed bounds of the previous feature
mTransformedFeatureBounds = QgsRectangle();

if ( atlasMaps.isEmpty() )
{
//no atlas enabled maps
return;
}

//
// compute the new extent
// keep the original aspect ratio
// and apply a margin

const QgsCoordinateReferenceSystem& coverage_crs = mCoverageLayer->crs();
// transformation needed for feature geometries. This should be set on a per-atlas map basis,
// compute extent of current feature in the map CRS. This should be set on a per-atlas map basis,
// but given that it's not currently possible to have maps with different CRSes we can just
// calculate it once based on the first atlas maps' CRS.
const QgsCoordinateReferenceSystem& destination_crs = atlasMaps[0]->mapRenderer()->destinationCrs();
computeExtent( atlasMaps[0] );

//update atlas bounds of every atlas enabled composer map
for ( QList<QgsComposerMap*>::iterator mit = atlasMaps.begin(); mit != atlasMaps.end(); ++mit )
{
prepareMap( *mit );
}
}

void QgsAtlasComposition::computeExtent( QgsComposerMap* map )
{
// compute the extent of the current feature, in the crs of the specified map

const QgsCoordinateReferenceSystem& coverage_crs = mCoverageLayer->crs();
// transformation needed for feature geometries
const QgsCoordinateReferenceSystem& destination_crs = map->mapRenderer()->destinationCrs();
mTransform.setSourceCrs( coverage_crs );
mTransform.setDestCRS( destination_crs );

// QgsGeometry::boundingBox is expressed in the geometry"s native CRS
// We have to transform the grometry to the destination CRS and ask for the bounding box
// Note: we cannot directly take the transformation of the bounding box, since transformations are not linear

QgsGeometry tgeom( *mCurrentFeature.geometry() );
tgeom.transform( mTransform );
mTransformedFeatureBounds = tgeom.boundingBox();

//update atlas bounds of every atlas enabled composer map
for ( QList<QgsComposerMap*>::iterator mit = atlasMaps.begin(); mit != atlasMaps.end(); ++mit )
{
prepareMap( *mit );
}
}

void QgsAtlasComposition::prepareMap( QgsComposerMap* map )
Expand All @@ -484,6 +489,13 @@ void QgsAtlasComposition::prepareMap( QgsComposerMap* map )
return;
}

if ( mTransformedFeatureBounds.isEmpty() )
{
//transformed extent of current feature hasn't been calculated yet. This can happen if
//a map has been set to be atlas controlled after prepare feature was called
computeExtent( map );
}

double xa1 = mTransformedFeatureBounds.xMinimum();
double xa2 = mTransformedFeatureBounds.xMaximum();
double ya1 = mTransformedFeatureBounds.yMinimum();
Expand Down
3 changes: 3 additions & 0 deletions src/core/composer/qgsatlascomposition.h
Original file line number Diff line number Diff line change
Expand Up @@ -201,6 +201,9 @@ class CORE_EXPORT QgsAtlasComposition : public QObject

//forces all atlas enabled maps to redraw
void updateAtlasMaps();

//computes the extent of the current feature, in the crs of the specified map
void computeExtent( QgsComposerMap *map );
};

#endif
Expand Down
6 changes: 6 additions & 0 deletions src/core/composer/qgscomposerlabel.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,12 @@ QgsComposerLabel::QgsComposerLabel( QgsComposition *composition ):
//default to a 10 point font size
mFont.setPointSizeF( 10 );

if ( mComposition && mComposition->atlasMode() == QgsComposition::PreviewAtlas )
{
//a label added while atlas preview is enabled needs to have the expression context set,
//otherwise fields in the label aren't correctly evaluated until atlas preview feature changes (#9457)
setExpressionContext( mComposition->atlasComposition().currentFeature(), mComposition->atlasComposition().coverageLayer() );
}
}

QgsComposerLabel::~QgsComposerLabel()
Expand Down
13 changes: 12 additions & 1 deletion src/core/composer/qgscomposermousehandles.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
#include "qgscomposermousehandles.h"
#include "qgscomposeritem.h"
#include "qgscomposition.h"
#include "qgspaperitem.h"
#include "qgis.h"
#include "qgslogger.h"

Expand Down Expand Up @@ -1185,7 +1186,17 @@ void QgsComposerMouseHandles::collectAlignCoordinates( QMap< double, const QgsCo
{
continue;
}
QRectF itemRect = currentItem->sceneBoundingRect();
QRectF itemRect;
if ( dynamic_cast<const QgsPaperItem *>( *itemIt ) )
{
//if snapping to paper use the paper item's rect rather then the bounding rect,
//since we want to snap to the page edge and not any outlines drawn around the page
itemRect = currentItem->rect();
}
else
{
itemRect = currentItem->sceneBoundingRect();
}
alignCoordsX.insert( itemRect.left(), currentItem );
alignCoordsX.insert( itemRect.right(), currentItem );
alignCoordsX.insert( itemRect.center().x(), currentItem );
Expand Down
8 changes: 6 additions & 2 deletions tests/src/python/test_qgsblendmodes.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@
from PyQt4.QtGui import *

from qgis.core import (QgsVectorLayer,
QgsVectorSimplifyMethod,
QgsMapLayerRegistry,
QgsMapRenderer,
QgsCoordinateReferenceSystem,
Expand Down Expand Up @@ -63,16 +64,19 @@ def __init__(self, methodName):
self.mPointLayer = QgsVectorLayer(myShpFile, 'Points', 'ogr')
self.mMapRegistry.addMapLayer(self.mPointLayer)

self.mSimplifyMethod = QgsVectorSimplifyMethod() ;
self.mSimplifyMethod.setSimplifyHints(QgsVectorLayer.NoSimplification);

# create polygon layer
myShpFile = os.path.join(TEST_DATA_DIR, 'polys.shp')
self.mPolygonLayer = QgsVectorLayer(myShpFile, 'Polygons', 'ogr')
self.mPolygonLayer.setSimplifyDrawingHints(QgsVectorLayer.NoSimplification)
self.mPolygonLayer.setSimplifyMethod(self.mSimplifyMethod)
self.mMapRegistry.addMapLayer(self.mPolygonLayer)

# create line layer
myShpFile = os.path.join(TEST_DATA_DIR, 'lines.shp')
self.mLineLayer = QgsVectorLayer(myShpFile, 'Lines', 'ogr')
self.mLineLayer.setSimplifyDrawingHints(QgsVectorLayer.NoSimplification)
self.mLineLayer.setSimplifyMethod(self.mSimplifyMethod)
self.mMapRegistry.addMapLayer(self.mLineLayer)

# create two raster layers
Expand Down
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.