Skip to content

Commit a40eca4

Browse files
author
Sandro Santilli
committed
Use core support for map rotation in composer
Includes patch by Martin Dobias See http://lists.osgeo.org/pipermail/qgis-developer/2015-January/036309.html Fix #11912
1 parent 7d6b7cc commit a40eca4

File tree

3 files changed

+11
-45
lines changed

3 files changed

+11
-45
lines changed

src/core/composer/qgscomposermap.cpp

+8-42
Original file line numberDiff line numberDiff line change
@@ -204,6 +204,7 @@ QgsMapSettings QgsComposerMap::mapSettings( const QgsRectangle& extent, const QS
204204
jobMapSettings.setMapUnits( ms.mapUnits() );
205205
jobMapSettings.setBackgroundColor( Qt::transparent );
206206
jobMapSettings.setOutputImageFormat( ms.outputImageFormat() );
207+
jobMapSettings.setRotation( mEvaluatedMapRotation );
207208

208209
//set layers to render
209210
QStringList theLayerSet = layersToRender();
@@ -255,19 +256,16 @@ void QgsComposerMap::cache( void )
255256

256257
mDrawing = true;
257258

258-
//in case of rotation, we need to request a larger rectangle and create a larger cache image
259-
QgsRectangle requestExtent;
260-
requestedExtent( requestExtent );
261-
262259
double horizontalVScaleFactor = horizontalViewScaleFactor();
263260
if ( horizontalVScaleFactor < 0 )
264261
{
265262
//make sure scale factor is positive
266263
horizontalVScaleFactor = mLastValidViewScaleFactor > 0 ? mLastValidViewScaleFactor : 1;
267264
}
268265

269-
double widthMM = requestExtent.width() * mapUnitsToMM();
270-
double heightMM = requestExtent.height() * mapUnitsToMM();
266+
const QgsRectangle &ext = *currentMapExtent();
267+
double widthMM = ext.width() * mapUnitsToMM();
268+
double heightMM = ext.height() * mapUnitsToMM();
271269

272270
int w = widthMM * horizontalVScaleFactor;
273271
int h = heightMM * horizontalVScaleFactor;
@@ -302,7 +300,7 @@ void QgsComposerMap::cache( void )
302300

303301
QPainter p( &mCacheImage );
304302

305-
draw( &p, requestExtent, QSizeF( w, h ), mCacheImage.logicalDpiX() );
303+
draw( &p, ext, QSizeF( w, h ), mCacheImage.logicalDpiX() );
306304
p.end();
307305
mCacheUpdated = true;
308306

@@ -344,29 +342,12 @@ void QgsComposerMap::paint( QPainter* painter, const QStyleOptionGraphicsItem* i
344342

345343
//Background color is already included in cached image, so no need to draw
346344

347-
QgsRectangle requestRectangle;
348-
requestedExtent( requestRectangle );
349-
350-
QgsRectangle cExtent = *currentMapExtent();
351-
352-
double imagePixelWidth = cExtent.width() / requestRectangle.width() * mCacheImage.width() ; //how many pixels of the image are for the map extent?
345+
double imagePixelWidth = mCacheImage.width(); //how many pixels of the image are for the map extent?
353346
double scale = rect().width() / imagePixelWidth;
354-
QgsPoint rotationPoint = QgsPoint(( cExtent.xMaximum() + cExtent.xMinimum() ) / 2.0, ( cExtent.yMaximum() + cExtent.yMinimum() ) / 2.0 );
355-
356-
//shift such that rotation point is at 0/0 point in the coordinate system
357-
double yShiftMM = ( requestRectangle.yMaximum() - rotationPoint.y() ) * mapUnitsToMM();
358-
double xShiftMM = ( requestRectangle.xMinimum() - rotationPoint.x() ) * mapUnitsToMM();
359-
360-
//shift such that top left point of the extent at point 0/0 in item coordinate system
361-
double xTopLeftShift = ( rotationPoint.x() - cExtent.xMinimum() ) * mapUnitsToMM();
362-
double yTopLeftShift = ( cExtent.yMaximum() - rotationPoint.y() ) * mapUnitsToMM();
363347

364348
painter->save();
365349

366350
painter->translate( mXOffset, mYOffset );
367-
painter->translate( xTopLeftShift, yTopLeftShift );
368-
painter->rotate( mEvaluatedMapRotation );
369-
painter->translate( xShiftMM, -yShiftMM );
370351
painter->scale( scale, scale );
371352
painter->drawImage( 0, 0, mCacheImage );
372353

@@ -397,32 +378,17 @@ void QgsComposerMap::paint( QPainter* painter, const QStyleOptionGraphicsItem* i
397378
drawBackground( painter );
398379
}
399380

400-
QgsRectangle requestRectangle;
401-
requestedExtent( requestRectangle );
402-
403381
QgsRectangle cExtent = *currentMapExtent();
404382

405-
QSizeF theSize( requestRectangle.width() * mapUnitsToMM(), requestRectangle.height() * mapUnitsToMM() );
406-
407-
QgsPoint rotationPoint = QgsPoint(( cExtent.xMaximum() + cExtent.xMinimum() ) / 2.0, ( cExtent.yMaximum() + cExtent.yMinimum() ) / 2.0 );
408-
409-
//shift such that rotation point is at 0/0 point in the coordinate system
410-
double yShiftMM = ( requestRectangle.yMaximum() - rotationPoint.y() ) * mapUnitsToMM();
411-
double xShiftMM = ( requestRectangle.xMinimum() - rotationPoint.x() ) * mapUnitsToMM();
383+
QSizeF theSize( cExtent.width() * mapUnitsToMM(), cExtent.height() * mapUnitsToMM() );
412384

413-
//shift such that top left point of the extent at point 0/0 in item coordinate system
414-
double xTopLeftShift = ( rotationPoint.x() - cExtent.xMinimum() ) * mapUnitsToMM();
415-
double yTopLeftShift = ( cExtent.yMaximum() - rotationPoint.y() ) * mapUnitsToMM();
416385
painter->save();
417386
painter->translate( mXOffset, mYOffset );
418-
painter->translate( xTopLeftShift, yTopLeftShift );
419-
painter->rotate( mEvaluatedMapRotation );
420-
painter->translate( xShiftMM, -yShiftMM );
421387

422388
double dotsPerMM = thePaintDevice->logicalDpiX() / 25.4;
423389
theSize *= dotsPerMM; // output size will be in dots (pixels)
424390
painter->scale( 1 / dotsPerMM, 1 / dotsPerMM ); // scale painter from mm to dots
425-
draw( painter, requestRectangle, theSize, thePaintDevice->logicalDpiX() );
391+
draw( painter, cExtent, theSize, thePaintDevice->logicalDpiX() );
426392

427393
//restore rotation
428394
painter->restore();

tests/src/core/testqgscomposermapoverview.cpp

+2-2
Original file line numberDiff line numberDiff line change
@@ -127,7 +127,7 @@ void TestQgsComposerMapOverview::overviewMapRotated()
127127
overviewMap->overview()->setFrameMap( mComposerMap->id() );
128128
QgsCompositionChecker checker( "composermap_overview_rotated", mComposition );
129129

130-
bool testResult = checker.testComposition( mReport, 0, 0 );
130+
bool testResult = checker.testComposition( mReport, 0, 600 );
131131
mComposition->removeComposerItem( overviewMap );
132132
mComposerMap->setMapRotation( 0 );
133133
QVERIFY( testResult );
@@ -144,7 +144,7 @@ void TestQgsComposerMapOverview::overviewMapRotated2()
144144
overviewMap->overview()->setFrameMap( mComposerMap->id() );
145145
QgsCompositionChecker checker( "composermap_overview_rotated2", mComposition );
146146

147-
bool testResult = checker.testComposition( mReport, 0, 0 );
147+
bool testResult = checker.testComposition( mReport, 0, 600 );
148148
mComposition->removeComposerItem( overviewMap );
149149
QVERIFY( testResult );
150150
}

tests/src/core/testqgscomposerrotation.cpp

+1-1
Original file line numberDiff line numberDiff line change
@@ -187,7 +187,7 @@ void TestQgsComposerRotation::mapRotation()
187187
composerMap->setMapRotation( 90 );
188188

189189
QgsCompositionChecker checker( "composerrotation_maprotation", mComposition );
190-
QVERIFY( checker.testComposition( mReport, 0, 0 ) );
190+
QVERIFY( checker.testComposition( mReport, 0, 200 ) );
191191

192192
mComposition->removeItem( composerMap );
193193
delete composerMap;

0 commit comments

Comments
 (0)