Skip to content
Permalink
Browse files

[composer] Fix world file map is not restored from project

  • Loading branch information
nyalldawson committed Apr 8, 2016
1 parent 8722ace commit 4764b53518af8ff6fb67cc2ba3612f6e1d34f9f4
@@ -378,10 +378,34 @@ class QgsComposition : QGraphicsScene
bool printAsRaster() const;
void setPrintAsRaster( const bool enabled );

/** Returns true if the composition will generate corresponding world files when pages
* are exported.
* @see setGenerateWorldFile()
* @see worldFileMap()
*/
bool generateWorldFile() const;
void setGenerateWorldFile( const bool enabled );

QgsComposerMap* worldFileMap();
/** Sets whether the composition will generate corresponding world files when pages
* are exported.
* @param enabled set to true to generate world files
* @see generateWorldFile()
* @see setWorldFileMap()
*/
void setGenerateWorldFile( bool 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.
* @see setWorldFileMap()
* @see generateWorldFile()
*/
QgsComposerMap* worldFileMap() const;

/** Sets the map item which will be used to generate corresponding world files when the
* composition is exported.
* @param map composer map item
* @see worldFileMap()
* @see setGenerateWorldFile()
*/
void setWorldFileMap( QgsComposerMap* map );

/** Returns true if a composition should use advanced effects such as blend modes */
@@ -3563,7 +3563,6 @@ void QgsComposer::readXML( const QDomElement& composerElem, const QDomDocument&

connectViewSlots();
connectCompositionSlots();
createCompositionWidget();

//read and restore all the items
QDomElement atlasElem;
@@ -3581,23 +3580,8 @@ void QgsComposer::readXML( const QDomElement& composerElem, const QDomDocument&

mActionShowPage->setChecked( mComposition->pagesVisible() );

// look for world file composer map, if needed
// Note: this must be done after maps have been added by addItemsFromXML
if ( mComposition->generateWorldFile() )
{
QDomElement compositionElem = compositionNodeList.at( 0 ).toElement();
QgsComposerMap* worldFileMap = nullptr;
QList<const QgsComposerMap*> maps = mComposition->composerMapItems();
for ( QList<const QgsComposerMap*>::const_iterator it = maps.begin(); it != maps.end(); ++it )
{
if (( *it )->id() == compositionElem.attribute( "worldFileMap" ).toInt() )
{
worldFileMap = const_cast<QgsComposerMap*>( *it );
break;
}
}
mComposition->setWorldFileMap( worldFileMap );
}
// update composition widget, must be done after items loaded
createCompositionWidget();

//make sure z values are consistent
mComposition->refreshZList();
@@ -78,20 +78,24 @@ QgsCompositionWidget::QgsCompositionWidget( QWidget* parent, QgsComposition* c )

// world file generation
mGenerateWorldFileCheckBox->setChecked( mComposition->generateWorldFile() );
mWorldFileMapComboBox->setEnabled( mComposition->generateWorldFile() );

// populate the map list
mWorldFileMapComboBox->clear();
QList<const QgsComposerMap*> availableMaps = mComposition->composerMapItems();
QList<const QgsComposerMap*>::const_iterator mapItemIt = availableMaps.constBegin();
for ( ; mapItemIt != availableMaps.constEnd(); ++mapItemIt )
{
mWorldFileMapComboBox->addItem( tr( "Map %1" ).arg(( *mapItemIt )->id() ), qVariantFromValue(( void* )*mapItemIt ) );
mWorldFileMapComboBox->addItem( tr( "Map %1" ).arg(( *mapItemIt )->id() ), ( *mapItemIt )->id() );
}

int idx = mWorldFileMapComboBox->findData( qVariantFromValue(( void* )mComposition->worldFileMap() ) );
if ( idx != -1 )
if ( mComposition->worldFileMap() )
{
mWorldFileMapComboBox->setCurrentIndex( idx );
int idx = mWorldFileMapComboBox->findData( mComposition->worldFileMap()->id() );
if ( idx != -1 )
{
mWorldFileMapComboBox->setCurrentIndex( idx );
}
}

// Connect to addition / removal of maps
@@ -679,7 +683,7 @@ void QgsCompositionWidget::onComposerMapAdded( QgsComposerMap* map )
return;
}

mWorldFileMapComboBox->addItem( tr( "Map %1" ).arg( map->id() ), qVariantFromValue(( void* )map ) );
mWorldFileMapComboBox->addItem( tr( "Map %1" ).arg( map->id() ), map->id() );
if ( mWorldFileMapComboBox->count() == 1 )
{
mComposition->setWorldFileMap( map );
@@ -696,7 +700,7 @@ void QgsCompositionWidget::onItemRemoved( QgsComposerItem* item )
QgsComposerMap* map = dynamic_cast<QgsComposerMap*>( item );
if ( map )
{
int idx = mWorldFileMapComboBox->findData( qVariantFromValue(( void* )map ) );
int idx = mWorldFileMapComboBox->findData( map->id() );
if ( idx != -1 )
{
mWorldFileMapComboBox->removeItem( idx );
@@ -720,7 +724,8 @@ void QgsCompositionWidget::on_mWorldFileMapComboBox_currentIndexChanged( int ind
}
else
{
QgsComposerMap* map = reinterpret_cast<QgsComposerMap*>( mWorldFileMapComboBox->itemData( index ).value<void*>() );
int mapId = mWorldFileMapComboBox->itemData( index ).toInt();
QgsComposerMap* map = const_cast< QgsComposerMap* >( mComposition->getComposerMapById( mapId ) );
mComposition->setWorldFileMap( map );
}
}
@@ -772,5 +777,7 @@ void QgsCompositionWidget::blockSignals( bool block )
mOffsetXSpinBox->blockSignals( block );
mOffsetYSpinBox->blockSignals( block );
mSnapToleranceSpinBox->blockSignals( block );
mGenerateWorldFileCheckBox->blockSignals( block );
mWorldFileMapComboBox->blockSignals( block );
}

@@ -86,7 +86,6 @@ void QgsComposition::init()
mPageStyleSymbol = nullptr;
mPrintAsRaster = false;
mGenerateWorldFile = false;
mWorldFileMap = nullptr;
mUseAdvancedEffects = true;
mSnapToGrid = false;
mGridVisible = false;
@@ -164,7 +163,6 @@ QgsComposition::QgsComposition()
, mPageStyleSymbol( 0 )
, mPrintAsRaster( false )
, mGenerateWorldFile( false )
, mWorldFileMap( 0 )
, mUseAdvancedEffects( true )
, mSnapToGrid( false )
, mGridVisible( false )
@@ -851,6 +849,17 @@ void QgsComposition::setPrintResolution( const int dpi )
QgsProject::instance()->setDirty( true );
}

QgsComposerMap* QgsComposition::worldFileMap() const
{
return dynamic_cast< QgsComposerMap* >( const_cast< QgsComposerItem* >( getComposerItemByUuid( mWorldFileMapId ) ) );
}

void QgsComposition::setWorldFileMap( QgsComposerMap* map )
{
mWorldFileMapId = map ? map->uuid() : QString();
QgsProject::instance()->setDirty( true );
}

void QgsComposition::setUseAdvancedEffects( const bool effectsEnabled )
{
mUseAdvancedEffects = effectsEnabled;
@@ -933,10 +942,7 @@ bool QgsComposition::writeXML( QDomElement& composerElem, QDomDocument& doc )
compositionElem.setAttribute( "printAsRaster", mPrintAsRaster );

compositionElem.setAttribute( "generateWorldFile", mGenerateWorldFile ? 1 : 0 );
if ( mGenerateWorldFile && mWorldFileMap )
{
compositionElem.setAttribute( "worldFileMap", mWorldFileMap->id() );
}
compositionElem.setAttribute( "worldFileMap", mWorldFileMapId );

compositionElem.setAttribute( "alignmentSnap", mAlignmentSnap ? 1 : 0 );
compositionElem.setAttribute( "guidesVisible", mGuidesVisible ? 1 : 0 );
@@ -1047,6 +1053,7 @@ bool QgsComposition::readXML( const QDomElement& compositionElem, const QDomDocu
mPrintResolution = compositionElem.attribute( "printResolution", "300" ).toInt();

mGenerateWorldFile = compositionElem.attribute( "generateWorldFile", "0" ).toInt() == 1 ? true : false;
mWorldFileMapId = compositionElem.attribute( "worldFileMap" );

//data defined properties
QgsComposerUtils::readDataDefinedPropertyMap( compositionElem, &mDataDefinedNames, &mDataDefinedProperties );
@@ -3105,12 +3112,13 @@ QGraphicsView *QgsComposition::graphicsView() const

void QgsComposition::computeWorldFileParameters( double& a, double& b, double& c, double& d, double& e, double& f ) const
{
if ( !mWorldFileMap )
const QgsComposerMap* map = worldFileMap();
if ( !map )
{
return;
}

int pageNumber = mWorldFileMap->page() - 1;
int pageNumber = map->page() - 1;
double pageY = pageNumber * ( mPageHeight + mSpaceBetweenPages );
QRectF pageRect( 0, pageY, mPageWidth, mPageHeight );
computeWorldFileParameters( pageRect, a, b, c, d, e, f );
@@ -3119,19 +3127,19 @@ void QgsComposition::computeWorldFileParameters( double& a, double& b, double& c
void QgsComposition::computeWorldFileParameters( const QRectF& exportRegion, double& a, double& b, double& c, double& d, double& e, double& f ) const
{
// World file parameters : affine transformation parameters from pixel coordinates to map coordinates

if ( !mWorldFileMap )
QgsComposerMap* map = worldFileMap();
if ( !map )
{
return;
}

double destinationHeight = exportRegion.height();
double destinationWidth = exportRegion.width();

QRectF mapItemSceneRect = mWorldFileMap->mapRectToScene( mWorldFileMap->rect() );
QgsRectangle mapExtent = *mWorldFileMap->currentMapExtent();
QRectF mapItemSceneRect = map->mapRectToScene( map->rect() );
QgsRectangle mapExtent = *map->currentMapExtent();

double alpha = mWorldFileMap->mapRotation() / 180 * M_PI;
double alpha = map->mapRotation() / 180 * M_PI;

double xRatio = mapExtent.width() / mapItemSceneRect.width();
double yRatio = mapExtent.height() / mapItemSceneRect.height();
@@ -3140,7 +3148,7 @@ void QgsComposition::computeWorldFileParameters( const QRectF& exportRegion, dou
double yCenter = mapExtent.center().y();

// get the extent (in map units) for the region
QPointF mapItemPos = mWorldFileMap->pos();
QPointF mapItemPos = map->pos();
//adjust item position so it is relative to export region
mapItemPos.rx() -= exportRegion.left();
mapItemPos.ry() -= exportRegion.top();
@@ -442,11 +442,35 @@ class CORE_EXPORT QgsComposition : public QGraphicsScene
bool printAsRaster() const {return mPrintAsRaster;}
void setPrintAsRaster( const bool enabled ) { mPrintAsRaster = enabled; }

/** Returns true if the composition will generate corresponding world files when pages
* are exported.
* @see setGenerateWorldFile()
* @see worldFileMap()
*/
bool generateWorldFile() const { return mGenerateWorldFile; }
void setGenerateWorldFile( const bool enabled ) { mGenerateWorldFile = enabled; }

QgsComposerMap* worldFileMap() const { return mWorldFileMap; }
void setWorldFileMap( QgsComposerMap* map ) { mWorldFileMap = map; }
/** Sets whether the composition will generate corresponding world files when pages
* are exported.
* @param enabled set to true to generate world files
* @see generateWorldFile()
* @see setWorldFileMap()
*/
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.
* @see setWorldFileMap()
* @see generateWorldFile()
*/
QgsComposerMap* worldFileMap() const;

/** Sets the map item which will be used to generate corresponding world files when the
* composition is exported.
* @param map composer map item
* @see worldFileMap()
* @see setGenerateWorldFile()
*/
void setWorldFileMap( QgsComposerMap* map );

/** Returns true if a composition should use advanced effects such as blend modes */
bool useAdvancedEffects() const {return mUseAdvancedEffects;}
@@ -912,8 +936,9 @@ class CORE_EXPORT QgsComposition : public QGraphicsScene

/** Flag if a world file should be generated on raster export */
bool mGenerateWorldFile;
/** Composer map to use for the world file generation */
QgsComposerMap* mWorldFileMap;

/** Item ID for composer map to use for the world file generation */
QString mWorldFileMapId;

/** Flag if advanced visual effects such as blend modes should be used. True by default*/
bool mUseAdvancedEffects;

0 comments on commit 4764b53

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