Skip to content
Permalink
Browse files
Merge pull request #4370 from nyalldawson/backport_composer_template_…
…fixes

Backport composer template fixes
  • Loading branch information
nyalldawson committed Apr 19, 2017
2 parents 1cc2c41 + 4c7cff5 commit b3d0515c893019264b7603ec1a56b04f958b63ea
@@ -672,6 +672,8 @@ SET(QGIS_CORE_HDRS
qgslegendsettings.h
qgslogger.h
qgsmaphittest.h
qgsmaplayerlistutils.h
qgsmaplayerref.h
qgsmaplayerrenderer.h
qgsmaplayerstylemanager.h
qgsmapsettings.h
@@ -723,6 +725,7 @@ SET(QGIS_CORE_HDRS

qgsvectordataprovider.h
qgsvectorlayercache.h
qgsvectorlayerref.h
qgsvectorfilewriter.h
qgsvectorlayerdiagramprovider.h
qgsvectorlayereditutils.h
@@ -35,7 +35,6 @@ QgsAtlasComposition::QgsAtlasComposition( QgsComposition* composition )
, mEnabled( false )
, mHideCoverage( false )
, mFilenamePattern( "'output_'||@atlas_featurenumber" )
, mCoverageLayer( nullptr )
, mSingleFile( false )
, mSortFeatures( false )
, mSortAscending( true )
@@ -73,10 +72,10 @@ void QgsAtlasComposition::removeLayers( const QStringList& layers )

Q_FOREACH ( const QString& layerId, layers )
{
if ( layerId == mCoverageLayer->id() )
if ( layerId == mCoverageLayer.layerId )
{
//current coverage layer removed
mCoverageLayer = nullptr;
mCoverageLayer.setLayer( nullptr );
setEnabled( false );
return;
}
@@ -85,12 +84,12 @@ void QgsAtlasComposition::removeLayers( const QStringList& layers )

void QgsAtlasComposition::setCoverageLayer( QgsVectorLayer* layer )
{
if ( layer == mCoverageLayer )
if ( layer == mCoverageLayer.get() )
{
return;
}

mCoverageLayer = layer;
mCoverageLayer.setLayer( layer );
emit coverageLayerChanged( layer );
}

@@ -644,7 +643,10 @@ void QgsAtlasComposition::writeXML( QDomElement& elem, QDomDocument& doc ) const

if ( mCoverageLayer )
{
atlasElem.setAttribute( "coverageLayer", mCoverageLayer->id() );
atlasElem.setAttribute( "coverageLayer", mCoverageLayer.layerId );
atlasElem.setAttribute( "coverageLayerName", mCoverageLayer.name );
atlasElem.setAttribute( "coverageLayerSource", mCoverageLayer.source );
atlasElem.setAttribute( "coverageLayerProvider", mCoverageLayer.provider );
}
else
{
@@ -682,16 +684,13 @@ void QgsAtlasComposition::readXML( const QDomElement& atlasElem, const QDomDocum
}

// look for stored layer name
mCoverageLayer = nullptr;
QMap<QString, QgsMapLayer*> layers = QgsMapLayerRegistry::instance()->mapLayers();
for ( QMap<QString, QgsMapLayer*>::const_iterator it = layers.begin(); it != layers.end(); ++it )
{
if ( it.key() == atlasElem.attribute( "coverageLayer" ) )
{
mCoverageLayer = dynamic_cast<QgsVectorLayer*>( it.value() );
break;
}
}
QString layerId = atlasElem.attribute( "coverageLayer" );
QString layerName = atlasElem.attribute( "coverageLayerName" );
QString layerSource = atlasElem.attribute( "coverageLayerSource" );
QString layerProvider = atlasElem.attribute( "coverageLayerProvider" );

mCoverageLayer = QgsVectorLayerRef( layerId, layerName, layerSource, layerProvider );
mCoverageLayer.resolveWeakly();

mPageNameExpression = atlasElem.attribute( "pageNameExpression", QString() );
mSingleFile = atlasElem.attribute( "singleFile", "false" ) == "true" ? true : false;
@@ -19,6 +19,7 @@
#include "qgscoordinatetransform.h"
#include "qgsfeature.h"
#include "qgsgeometry.h"
#include "qgsvectorlayerref.h"

#include <memory>
#include <QString>
@@ -101,7 +102,7 @@ class CORE_EXPORT QgsAtlasComposition : public QObject
* @returns atlas coverage layer
* @see setCoverageLayer
*/
QgsVectorLayer* coverageLayer() const { return mCoverageLayer; }
QgsVectorLayer* coverageLayer() const { return mCoverageLayer.get(); }

/** Sets the coverage layer to use for the atlas features
* @param layer vector coverage layer
@@ -355,7 +356,7 @@ class CORE_EXPORT QgsAtlasComposition : public QObject
bool mEnabled;
bool mHideCoverage;
QString mFilenamePattern;
QgsVectorLayer* mCoverageLayer;
QgsVectorLayerRef mCoverageLayer;
bool mSingleFile;

QString mCurrentFilename;
@@ -49,7 +49,6 @@ bool QgsComposerAttributeTableCompareV2::operator()( const QgsComposerTableRow&
QgsComposerAttributeTableV2::QgsComposerAttributeTableV2( QgsComposition* composition, bool createUndoCommands )
: QgsComposerTableV2( composition, createUndoCommands )
, mSource( LayerAttributes )
, mVectorLayer( nullptr )
, mCurrentAtlasLayer( nullptr )
, mComposerMap( nullptr )
, mMaximumNumberOfFeatures( 30 )
@@ -67,15 +66,15 @@ QgsComposerAttributeTableV2::QgsComposerAttributeTableV2( QgsComposition* compos
QgsVectorLayer* vl = dynamic_cast<QgsVectorLayer*>( mapIt.value() );
if ( vl )
{
mVectorLayer = vl;
mVectorLayer.setLayer( vl );
break;
}
}
if ( mVectorLayer )
{
resetColumns();
//listen for modifications to layer and refresh table when they occur
connect( mVectorLayer, SIGNAL( layerModified() ), this, SLOT( refreshAttributes() ) );
connect( mVectorLayer.get(), SIGNAL( layerModified() ), this, SLOT( refreshAttributes() ) );
}
connect( QgsMapLayerRegistry::instance(), SIGNAL( layerWillBeRemoved( QString ) ), this, SLOT( removeLayer( const QString& ) ) );

@@ -104,14 +103,14 @@ QString QgsComposerAttributeTableV2::displayName() const

void QgsComposerAttributeTableV2::setVectorLayer( QgsVectorLayer* layer )
{
if ( layer == mVectorLayer )
if ( layer == mVectorLayer.get() )
{
//no change
return;
}

QgsVectorLayer* prevLayer = sourceLayer();
mVectorLayer = layer;
mVectorLayer.setLayer( layer );

if ( mSource == QgsComposerAttributeTableV2::LayerAttributes && layer != prevLayer )
{
@@ -125,7 +124,7 @@ void QgsComposerAttributeTableV2::setVectorLayer( QgsVectorLayer* layer )
resetColumns();

//listen for modifications to layer and refresh table when they occur
connect( mVectorLayer, SIGNAL( layerModified() ), this, SLOT( refreshAttributes() ) );
connect( mVectorLayer.get(), SIGNAL( layerModified() ), this, SLOT( refreshAttributes() ) );
}

refreshAttributes();
@@ -591,7 +590,7 @@ QgsExpressionContext *QgsComposerAttributeTableV2::createExpressionContext() con

if ( mSource == LayerAttributes )
{
context->appendScope( QgsExpressionContextUtils::layerScope( mVectorLayer ) );
context->appendScope( QgsExpressionContextUtils::layerScope( mVectorLayer.get() ) );
}

return context;
@@ -615,7 +614,7 @@ QgsVectorLayer *QgsComposerAttributeTableV2::sourceLayer()
case QgsComposerAttributeTableV2::AtlasFeature:
return mComposition->atlasComposition().coverageLayer();
case QgsComposerAttributeTableV2::LayerAttributes:
return mVectorLayer;
return mVectorLayer.get();
case QgsComposerAttributeTableV2::RelationChildren:
{
QgsRelation relation = QgsProject::instance()->relationManager()->relation( mRelationId );
@@ -631,7 +630,7 @@ void QgsComposerAttributeTableV2::removeLayer( const QString& layerId )
{
if ( layerId == mVectorLayer->id() )
{
mVectorLayer = nullptr;
mVectorLayer.setLayer( nullptr );
//remove existing columns
qDeleteAll( mColumns );
mColumns.clear();
@@ -709,7 +708,10 @@ bool QgsComposerAttributeTableV2::writeXML( QDomElement& elem, QDomDocument & do
}
if ( mVectorLayer )
{
composerTableElem.setAttribute( "vectorLayer", mVectorLayer->id() );
composerTableElem.setAttribute( "vectorLayer", mVectorLayer.layerId );
composerTableElem.setAttribute( "vectorLayerName", mVectorLayer.name );
composerTableElem.setAttribute( "vectorLayerSource", mVectorLayer.source );
composerTableElem.setAttribute( "vectorLayerProvider", mVectorLayer.provider );
}

bool ok = QgsComposerTableV2::writeXML( composerTableElem, doc, ignoreFrames );
@@ -778,19 +780,12 @@ bool QgsComposerAttributeTableV2::readXML( const QDomElement& itemElem, const QD
}

//vector layer
QString layerId = itemElem.attribute( "vectorLayer", "not_existing" );
if ( layerId == "not_existing" )
{
mVectorLayer = nullptr;
}
else
{
QgsMapLayer* ml = QgsMapLayerRegistry::instance()->mapLayer( layerId );
if ( ml )
{
mVectorLayer = dynamic_cast<QgsVectorLayer*>( ml );
}
}
QString layerId = itemElem.attribute( "vectorLayer" );
QString layerName = itemElem.attribute( "vectorLayerName" );
QString layerSource = itemElem.attribute( "vectorLayerSource" );
QString layerProvider = itemElem.attribute( "vectorLayerProvider" );
mVectorLayer = QgsVectorLayerRef( layerId, layerName, layerSource, layerProvider );
mVectorLayer.resolveWeakly();

//connect to new layer
connect( sourceLayer(), SIGNAL( layerModified() ), this, SLOT( refreshAttributes() ) );
@@ -20,6 +20,7 @@

#include "qgscomposertablev2.h"
#include "qgscomposerattributetable.h"
#include "qgsvectorlayerref.h"

class QgsComposerMap;
class QgsVectorLayer;
@@ -120,7 +121,7 @@ class CORE_EXPORT QgsComposerAttributeTableV2: public QgsComposerTableV2
* @returns attribute table's current vector layer
* @see setVectorLayer
*/
QgsVectorLayer* vectorLayer() const { return mVectorLayer; }
QgsVectorLayer* vectorLayer() const { return mVectorLayer.get(); }

/** Sets the relation id from which to display child features
* @param relationId id for relation to display child features from
@@ -314,7 +315,7 @@ class CORE_EXPORT QgsComposerAttributeTableV2: public QgsComposerTableV2
/** Attribute source*/
ContentSource mSource;
/** Associated vector layer*/
QgsVectorLayer* mVectorLayer;
QgsVectorLayerRef mVectorLayer;
/** Relation id, if in relation children mode*/
QString mRelationId;

@@ -35,6 +35,7 @@
#include "qgsexpression.h"
#include "qgsvisibilitypresetcollection.h"
#include "qgsannotation.h"
#include "qgsvectorlayerref.h"

#include "qgslabel.h"
#include "qgslabelattributes.h"
@@ -1323,12 +1324,21 @@ bool QgsComposerMap::writeXML( QDomElement& elem, QDomDocument & doc ) const

//layer set
QDomElement layerSetElem = doc.createElement( "LayerSet" );
QStringList::const_iterator layerIt = mLayerSet.constBegin();
for ( ; layerIt != mLayerSet.constEnd(); ++layerIt )
Q_FOREACH ( const QString &layerId, mLayerSet )
{
QgsVectorLayerRef layerRef( layerId );
layerRef.resolve();

if ( !layerRef )
continue;

QDomElement layerElem = doc.createElement( "Layer" );
QDomText layerIdText = doc.createTextNode( *layerIt );
QDomText layerIdText = doc.createTextNode( layerRef.layerId );
layerElem.appendChild( layerIdText );
layerElem.setAttribute( "name", layerRef.name );
layerElem.setAttribute( "source", layerRef.source );
layerElem.setAttribute( "provider", layerRef.provider );

layerSetElem.appendChild( layerElem );
}
composerMapElem.appendChild( layerSetElem );
@@ -1341,7 +1351,15 @@ bool QgsComposerMap::writeXML( QDomElement& elem, QDomDocument & doc ) const
for ( ; styleIt != mLayerStyleOverrides.constEnd(); ++styleIt )
{
QDomElement styleElem = doc.createElement( "LayerStyle" );
styleElem.setAttribute( "layerid", styleIt.key() );

QgsMapLayerRef ref( styleIt.key() );
ref.resolve();

styleElem.setAttribute( "layerid", ref.layerId );
styleElem.setAttribute( "name", ref.name );
styleElem.setAttribute( "source", ref.source );
styleElem.setAttribute( "provider", ref.provider );

QgsMapLayerStyle style( styleIt.value() );
style.writeXml( styleElem );
stylesElem.appendChild( styleElem );
@@ -1456,8 +1474,15 @@ bool QgsComposerMap::readXML( const QDomElement& itemElem, const QDomDocument& d
layerSet.reserve( layerIdNodeList.size() );
for ( int i = 0; i < layerIdNodeList.size(); ++i )
{
const QDomElement& layerIdElement = layerIdNodeList.at( i ).toElement();
layerSet << layerIdElement.text();
QDomElement layerElem = layerIdNodeList.at( i ).toElement();
QString layerId = layerElem.text();
QString layerName = layerElem.attribute( "name" );
QString layerSource = layerElem.attribute( "source" );
QString layerProvider = layerElem.attribute( "provider" );

QgsMapLayerRef ref( layerId, layerName, layerSource, layerProvider );
ref.resolveWeakly();
layerSet << ref.layerId;
}
}
mLayerSet = layerSet;
@@ -1473,9 +1498,15 @@ bool QgsComposerMap::readXML( const QDomElement& itemElem, const QDomDocument& d
{
const QDomElement& layerStyleElement = layerStyleNodeList.at( i ).toElement();
QString layerId = layerStyleElement.attribute( "layerid" );
QString layerName = layerStyleElement.attribute( "name" );
QString layerSource = layerStyleElement.attribute( "source" );
QString layerProvider = layerStyleElement.attribute( "provider" );
QgsMapLayerRef ref( layerId, layerName, layerSource, layerProvider );
ref.resolveWeakly();

QgsMapLayerStyle style;
style.readXml( layerStyleElement );
mLayerStyleOverrides.insert( layerId, style.xmlData() );
mLayerStyleOverrides.insert( ref.layerId, style.xmlData() );
}
}

Loading

0 comments on commit b3d0515

Please sign in to comment.