Skip to content

Commit

Permalink
Remove some more QgsComposition::mapSettings use
Browse files Browse the repository at this point in the history
  • Loading branch information
nyalldawson committed Jan 17, 2017
1 parent 308fc56 commit 305f9f0
Show file tree
Hide file tree
Showing 13 changed files with 42 additions and 25 deletions.
2 changes: 1 addition & 1 deletion python/core/composer/qgscomposerutils.sip
Original file line number Diff line number Diff line change
Expand Up @@ -249,5 +249,5 @@ class QgsComposerUtils
* QgsComposition::referenceMap().
* @note added in QGIS 3.0
*/
static QgsRenderContext createRenderContext( QgsComposition* composition, QPainter& painter );
static QgsRenderContext createRenderContext( QgsComposition* composition, QPainter* painter );
};
2 changes: 1 addition & 1 deletion src/core/composer/qgscomposerarrow.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -182,7 +182,7 @@ void QgsComposerArrow::drawLine( QPainter *painter )
painter->scale( 1 / dotsPerMM, 1 / dotsPerMM ); //scale painter from mm to dots

//setup render context
QgsRenderContext context = QgsComposerUtils::createRenderContext( mComposition, *painter );
QgsRenderContext context = QgsComposerUtils::createRenderContext( mComposition, painter );
context.setForceVectorOutput( true );

QgsExpressionContext expressionContext = createExpressionContext();
Expand Down
8 changes: 4 additions & 4 deletions src/core/composer/qgscomposerlegend.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -111,10 +111,10 @@ void QgsComposerLegend::paint( QPainter* painter, const QStyleOptionGraphicsItem
mSettings.setMmPerMapUnit( mComposerMap->mapUnitsToMM() );

// use a temporary QgsMapSettings to find out real map scale
QgsMapSettings ms = mComposerMap->composition()->mapSettings();
ms.setOutputSize( QSizeF( mComposerMap->rect().width() * dotsPerMM, mComposerMap->rect().height() * dotsPerMM ).toSize() );
ms.setExtent( *mComposerMap->currentMapExtent() );
ms.setOutputDpi( dpi );
QSizeF mapSizePixels = QSizeF( mComposerMap->rect().width() * dotsPerMM, mComposerMap->rect().height() * dotsPerMM );
QgsRectangle mapExtent = *mComposerMap->currentMapExtent();

QgsMapSettings ms = mComposerMap->mapSettings( mapExtent, mapSizePixels, dpi );
mSettings.setMapScale( ms.scale() );
}
mInitialMapScaleCalculated = true;
Expand Down
5 changes: 2 additions & 3 deletions src/core/composer/qgscomposermapgrid.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -636,7 +636,7 @@ void QgsComposerMapGrid::draw( QPainter* p )
p->scale( 1 / dotsPerMM, 1 / dotsPerMM ); //scale painter from mm to dots

//setup render context
QgsRenderContext context = QgsComposerUtils::createRenderContext( mComposition, *p );
QgsRenderContext context = QgsComposerUtils::createRenderContext( mComposition, p );
context.setForceVectorOutput( true );
QgsExpressionContext expressionContext = createExpressionContext();
context.setExpressionContext( expressionContext );
Expand Down Expand Up @@ -2056,8 +2056,7 @@ void QgsComposerMapGrid::calculateMaxExtension( double& top, double& right, doub
}

//setup render context
QgsMapSettings ms = mComposerMap->composition()->mapSettings();
QgsRenderContext context = QgsRenderContext::fromMapSettings( ms );
QgsRenderContext context = QgsComposerUtils::createRenderContext( mComposition, nullptr );
QgsExpressionContext expressionContext = createExpressionContext();
context.setExpressionContext( expressionContext );

Expand Down
2 changes: 1 addition & 1 deletion src/core/composer/qgscomposermapoverview.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,7 @@ void QgsComposerMapOverview::draw( QPainter *painter )
double dotsPerMM = painter->device()->logicalDpiX() / 25.4;

//setup render context
QgsRenderContext context = QgsComposerUtils::createRenderContext( mComposition, *painter );
QgsRenderContext context = QgsComposerUtils::createRenderContext( mComposition, painter );
context.setForceVectorOutput( true );
QgsExpressionContext expressionContext = createExpressionContext();
context.setExpressionContext( expressionContext );
Expand Down
4 changes: 2 additions & 2 deletions src/core/composer/qgscomposernodesitem.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -137,7 +137,7 @@ void QgsComposerNodesItem::drawNodes( QPainter *painter ) const
symbol.data()->setSize( rectSize );
symbol.data()->setAngle( 45 );

QgsRenderContext context = QgsComposerUtils::createRenderContext( mComposition, *painter );
QgsRenderContext context = QgsComposerUtils::createRenderContext( mComposition, painter );
context.setForceVectorOutput( true );

QgsExpressionContext expressionContext = createExpressionContext();
Expand Down Expand Up @@ -168,7 +168,7 @@ void QgsComposerNodesItem::drawSelectedNode( QPainter *painter ) const
symbol.reset( QgsMarkerSymbol::createSimple( properties ) );
symbol.data()->setSize( rectSize );

QgsRenderContext context = QgsComposerUtils::createRenderContext( mComposition, *painter );
QgsRenderContext context = QgsComposerUtils::createRenderContext( mComposition, painter );
context.setForceVectorOutput( true );

QgsExpressionContext expressionContext = createExpressionContext();
Expand Down
2 changes: 1 addition & 1 deletion src/core/composer/qgscomposerpolygon.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@ void QgsComposerPolygon::_draw( QPainter *painter )
//setup painter scaling to dots so that raster symbology is drawn to scale
const double dotsPerMM = painter->device()->logicalDpiX() / 25.4;

QgsRenderContext context = QgsComposerUtils::createRenderContext( mComposition, *painter );
QgsRenderContext context = QgsComposerUtils::createRenderContext( mComposition, painter );
context.setForceVectorOutput( true );
context.setExpressionContext( createExpressionContext() );

Expand Down
2 changes: 1 addition & 1 deletion src/core/composer/qgscomposerpolyline.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -100,7 +100,7 @@ void QgsComposerPolyline::_draw( QPainter *painter )
{
double dotsPerMM = painter->device()->logicalDpiX() / 25.4;

QgsRenderContext context = QgsComposerUtils::createRenderContext( mComposition, *painter );
QgsRenderContext context = QgsComposerUtils::createRenderContext( mComposition, painter );
context.setForceVectorOutput( true );

QgsExpressionContext expressionContext = createExpressionContext();
Expand Down
2 changes: 1 addition & 1 deletion src/core/composer/qgscomposershape.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -179,7 +179,7 @@ void QgsComposerShape::drawShapeUsingSymbol( QPainter* p )
double dotsPerMM = p->device()->logicalDpiX() / 25.4;

//setup render context
QgsRenderContext context = QgsComposerUtils::createRenderContext( mComposition, *p );
QgsRenderContext context = QgsComposerUtils::createRenderContext( mComposition, p );
context.setForceVectorOutput( true );
QgsExpressionContext expressionContext = createExpressionContext();
context.setExpressionContext( expressionContext );
Expand Down
10 changes: 6 additions & 4 deletions src/core/composer/qgscomposerutils.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -548,15 +548,16 @@ void QgsComposerUtils::drawText( QPainter *painter, const QRectF &rect, const QS
painter->restore();
}

QgsRenderContext QgsComposerUtils::createRenderContext( QgsComposition* composition, QPainter &painter )
QgsRenderContext QgsComposerUtils::createRenderContext( QgsComposition* composition, QPainter* painter )
{
QgsComposerMap* referenceMap = composition ? composition->referenceMap() : nullptr;
if ( !referenceMap )
{
return QgsRenderContext::fromQPainter( &painter );
return QgsRenderContext::fromQPainter( painter );
}

int dpi = painter.device()->logicalDpiX();
// default to 88 dpi if no painter specified
int dpi = ( painter && painter->device() ) ? painter->device()->logicalDpiX() : 88;
double dotsPerMM = dpi / 25.4;

// get map settings from reference map
Expand All @@ -565,6 +566,7 @@ QgsRenderContext QgsComposerUtils::createRenderContext( QgsComposition* composit
QgsMapSettings ms = referenceMap->mapSettings( extent, mapSizeMM * dotsPerMM, dpi );

QgsRenderContext context = QgsRenderContext::fromMapSettings( ms );
context.setPainter( &painter );
if ( painter )
context.setPainter( painter );
return context;
}
2 changes: 1 addition & 1 deletion src/core/composer/qgscomposerutils.h
Original file line number Diff line number Diff line change
Expand Up @@ -272,7 +272,7 @@ class CORE_EXPORT QgsComposerUtils
* QgsComposition::referenceMap().
* @note added in QGIS 3.0
*/
static QgsRenderContext createRenderContext( QgsComposition* composition, QPainter& painter );
static QgsRenderContext createRenderContext( QgsComposition* composition, QPainter* painter );

};

Expand Down
2 changes: 1 addition & 1 deletion src/core/composer/qgspaperitem.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -156,7 +156,7 @@ void QgsPaperItem::paint( QPainter* painter, const QStyleOptionGraphicsItem* ite
double dotsPerMM = painter->device()->logicalDpiX() / 25.4;

//setup render context
QgsRenderContext context = QgsComposerUtils::createRenderContext( mComposition, *painter );
QgsRenderContext context = QgsComposerUtils::createRenderContext( mComposition, painter );
context.setForceVectorOutput( true );

QgsExpressionContext expressionContext = createExpressionContext();
Expand Down
24 changes: 20 additions & 4 deletions tests/src/core/testqgscomposerutils.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -712,31 +712,47 @@ void TestQgsComposerUtils::createRenderContext()
QPainter p( &testImage );

// no composition
QgsRenderContext rc = QgsComposerUtils::createRenderContext( nullptr, p );
QgsRenderContext rc = QgsComposerUtils::createRenderContext( nullptr, &p );
QGSCOMPARENEAR( rc.scaleFactor(), 150 / 25.4, 0.001 );
QCOMPARE( rc.painter(), &p );

// no composition, no painter
rc = QgsComposerUtils::createRenderContext( nullptr, nullptr );
QGSCOMPARENEAR( rc.scaleFactor(), 88 / 25.4, 0.001 );
QVERIFY( !rc.painter() );

//create composition with no reference map
QgsRectangle extent( 2000, 2800, 2500, 2900 );
QgsMapSettings ms;
ms.setExtent( extent );
QgsComposition* composition = new QgsComposition( ms, QgsProject::instance() );
rc = QgsComposerUtils::createRenderContext( composition, p );
rc = QgsComposerUtils::createRenderContext( composition, &p );
QGSCOMPARENEAR( rc.scaleFactor(), 150 / 25.4, 0.001 );
QCOMPARE( rc.painter(), &p );

// composition, no map, no painter
rc = QgsComposerUtils::createRenderContext( composition, nullptr );
QGSCOMPARENEAR( rc.scaleFactor(), 88 / 25.4, 0.001 );
QVERIFY( !rc.painter() );

// add a reference map
QgsComposerMap* map = new QgsComposerMap( composition );
map->setNewExtent( extent );
map->setSceneRect( QRectF( 30, 60, 200, 100 ) );
composition->addComposerMap( map );
composition->setReferenceMap( map );

rc = QgsComposerUtils::createRenderContext( composition, p );
rc = QgsComposerUtils::createRenderContext( composition, &p );
QGSCOMPARENEAR( rc.scaleFactor(), 150 / 25.4, 0.001 );
QGSCOMPARENEAR( rc.rendererScale(), map->scale(), 100000 );
QGSCOMPARENEAR( rc.rendererScale(), map->scale(), 1000000 );
QCOMPARE( rc.painter(), &p );

// composition, reference map, no painter
rc = QgsComposerUtils::createRenderContext( composition, nullptr );
QGSCOMPARENEAR( rc.scaleFactor(), 88 / 25.4, 0.001 );
QGSCOMPARENEAR( rc.rendererScale(), map->scale(), 1000000 );
QVERIFY( !rc.painter() );

p.end();
}

Expand Down

0 comments on commit 305f9f0

Please sign in to comment.