Skip to content
Permalink
Browse files

[composer] Use largest map in composition as reference map if no

reference map is explicitly set
  • Loading branch information
nyalldawson committed Jan 16, 2017
1 parent e0934d7 commit f32d11b50879ad76eb1dfe9d55bf567d3cc7598e
Showing with 53 additions and 2 deletions.
  1. +18 −1 src/core/composer/qgscomposition.cpp
  2. +2 −1 src/core/composer/qgscomposition.h
  3. +33 −0 tests/src/core/testqgscomposition.cpp
@@ -791,7 +791,24 @@ void QgsComposition::setPrintResolution( const int dpi )

QgsComposerMap* QgsComposition::referenceMap() const
{
return dynamic_cast< QgsComposerMap* >( const_cast< QgsComposerItem* >( getComposerItemByUuid( mWorldFileMapId ) ) );
// prefer explicitly set reference map
if ( QgsComposerMap* map = dynamic_cast< QgsComposerMap* >( const_cast< QgsComposerItem* >( getComposerItemByUuid( mWorldFileMapId ) ) ) )
return map;

// else try to find largest map
QList< const QgsComposerMap* > maps = composerMapItems();
const QgsComposerMap* largestMap = nullptr;
double largestMapArea = 0;
Q_FOREACH ( const QgsComposerMap* map, maps )
{
double area = map->rect().width() * map->rect().height();
if ( area > largestMapArea )
{
largestMapArea = area;
largestMap = map;
}
}
return const_cast< QgsComposerMap* >( largestMap );
}

void QgsComposition::setReferenceMap( QgsComposerMap* map )
@@ -424,7 +424,8 @@ class CORE_EXPORT QgsComposition : public QGraphicsScene, public QgsExpressionCo
void setGenerateWorldFile( bool enabled ) { mGenerateWorldFile = enabled; }

/** Returns the map item which will be used to generate corresponding world files when the
* composition is exported, or nullptr if no corresponding map is set.
* composition is exported. If no map was explicitly set via setReferenceMap(), the largest
* map in the composition will be returned (or nullptr if there are no maps in the composition).
* @see setReferenceMap()
* @see generateWorldFile()
*/
@@ -57,6 +57,7 @@ class TestQgsComposition : public QObject
void georeference();
void variablesEdited();
void itemVariablesFunction();
void referenceMap();

private:
QgsComposition *mComposition;
@@ -639,5 +640,37 @@ void TestQgsComposition::itemVariablesFunction()
delete composition;
}

void TestQgsComposition::referenceMap()
{
QgsRectangle extent( 2000, 2800, 2500, 2900 );
QgsMapSettings ms;
ms.setExtent( extent );
QgsComposition* composition = new QgsComposition( ms, QgsProject::instance() );

// no maps
QVERIFY( !composition->referenceMap() );

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

QCOMPARE( composition->referenceMap(), map );

// add a larger map
QgsComposerMap* map2 = new QgsComposerMap( composition );
map2->setNewExtent( extent );
map2->setSceneRect( QRectF( 30, 60, 250, 150 ) );
composition->addComposerMap( map2 );

QCOMPARE( composition->referenceMap(), map2 );

// explicitly set reference map
composition->setReferenceMap( map );
QCOMPARE( composition->referenceMap(), map );

delete composition;
}

QGSTEST_MAIN( TestQgsComposition )
#include "testqgscomposition.moc"

0 comments on commit f32d11b

Please sign in to comment.
You can’t perform that action at this time.