Skip to content

Commit 8c340f7

Browse files
authored
Make composition associated with QgsProject (#3957)
... rather than using project singleton within compositions
1 parent 062de6a commit 8c340f7

60 files changed

Lines changed: 188 additions & 139 deletions

File tree

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

‎doc/api_break.dox‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -615,6 +615,7 @@ QgsComposerTableV2 {#qgis_api_break_3_0_QgsComposerTableV2}
615615
QgsComposition {#qgis_api_break_3_0_QgsComposition}
616616
--------------
617617

618+
- constructor requires QgsProject instance as the second argument
618619
- addItemsFromXML() has been renamed to addItemsFromXml()
619620
- Constructor with QgsMapRenderer parameter has been removed. Use the variant with QgsMapSettings parameter.
620621
- mapRenderer() has been removed. Use mapSettings() instead.

‎python/core/composer/qgscomposition.sip‎

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ class QgsComposition : QGraphicsScene, QgsExpressionContextGenerator
4040
Landscape
4141
};
4242

43-
explicit QgsComposition( const QgsMapSettings& mapSettings );
43+
explicit QgsComposition( const QgsMapSettings& mapSettings, QgsProject* project );
4444

4545
/** Composition atlas modes*/
4646
enum AtlasMode
@@ -52,6 +52,14 @@ class QgsComposition : QGraphicsScene, QgsExpressionContextGenerator
5252

5353
~QgsComposition();
5454

55+
/**
56+
* The project associated with the composition. Used to get access to layers, map themes,
57+
* relations and various other bits. It is never null.
58+
*
59+
* \note Added in QGIS 3.0
60+
*/
61+
QgsProject* project() const;
62+
5563
/** Changes size of paper item.
5664
* @param width page width in mm
5765
* @param height page height in mm

‎src/app/composer/qgscomposer.cpp‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -547,7 +547,7 @@ QgsComposer::QgsComposer( QgisApp *qgis, const QString& title )
547547
connect( mActionShowRulers, SIGNAL( triggered( bool ) ), this, SLOT( toggleRulers( bool ) ) );
548548

549549
//init undo/redo buttons
550-
mComposition = new QgsComposition( mQgis->mapCanvas()->mapSettings() );
550+
mComposition = new QgsComposition( mQgis->mapCanvas()->mapSettings(), QgsProject::instance() );
551551

552552
mActionUndo->setEnabled( false );
553553
mActionRedo->setEnabled( false );
@@ -3543,7 +3543,7 @@ void QgsComposer::readXml( const QDomElement& composerElem, const QDomDocument&
35433543
createComposerView();
35443544

35453545
//read composition settings
3546-
mComposition = new QgsComposition( mQgis->mapCanvas()->mapSettings() );
3546+
mComposition = new QgsComposition( mQgis->mapCanvas()->mapSettings(), QgsProject::instance() );
35473547
QDomNodeList compositionNodeList = composerElem.elementsByTagName( QStringLiteral( "Composition" ) );
35483548
if ( compositionNodeList.size() > 0 )
35493549
{

‎src/core/composer/qgsaddremoveitemcommand.cpp‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -80,5 +80,5 @@ void QgsAddRemoveItemCommand::switchState()
8080
emit itemAdded( mItem );
8181
mState = Added;
8282
}
83-
QgsProject::instance()->setDirty( true );
83+
mComposition->project()->setDirty( true );
8484
}

‎src/core/composer/qgsaddremovemultiframecommand.cpp‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -78,7 +78,7 @@ void QgsAddRemoveMultiFrameCommand::switchState()
7878
mComposition->addMultiFrame( mMultiFrame );
7979
mState = Added;
8080
}
81-
QgsProject::instance()->setDirty( true );
81+
mComposition->project()->setDirty( true );
8282
}
8383
}
8484

‎src/core/composer/qgsatlascomposition.cpp‎

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@ QgsAtlasComposition::QgsAtlasComposition( QgsComposition* composition )
4545
{
4646

4747
//listen out for layer removal
48-
connect( QgsProject::instance(), SIGNAL( layersWillBeRemoved( QStringList ) ), this, SLOT( removeLayers( QStringList ) ) );
48+
connect( mComposition->project(), SIGNAL( layersWillBeRemoved( QStringList ) ), this, SLOT( removeLayers( QStringList ) ) );
4949
}
5050

5151
void QgsAtlasComposition::setEnabled( bool enabled )
@@ -625,7 +625,7 @@ void QgsAtlasComposition::readXml( const QDomElement& atlasElem, const QDomDocum
625625

626626
// look for stored layer name
627627
QString coverageLayerId = atlasElem.attribute( QStringLiteral( "coverageLayer" ) );
628-
mCoverageLayer = qobject_cast<QgsVectorLayer*>( QgsProject::instance()->mapLayer( coverageLayerId ) );
628+
mCoverageLayer = qobject_cast<QgsVectorLayer*>( mComposition->project()->mapLayer( coverageLayerId ) );
629629

630630
mPageNameExpression = atlasElem.attribute( QStringLiteral( "pageNameExpression" ), QString() );
631631
mSingleFile = atlasElem.attribute( QStringLiteral( "singleFile" ), QStringLiteral( "false" ) ) == QLatin1String( "true" ) ? true : false;
@@ -718,7 +718,7 @@ QgsExpressionContext QgsAtlasComposition::createExpressionContext()
718718
{
719719
QgsExpressionContext expressionContext;
720720
expressionContext << QgsExpressionContextUtils::globalScope()
721-
<< QgsExpressionContextUtils::projectScope( QgsProject::instance() );
721+
<< QgsExpressionContextUtils::projectScope( mComposition->project() );
722722
if ( mComposition )
723723
expressionContext << QgsExpressionContextUtils::compositionScope( mComposition );
724724

‎src/core/composer/qgscomposerattributetablev2.cpp‎

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,7 @@ QgsComposerAttributeTableV2::QgsComposerAttributeTableV2( QgsComposition* compos
6262
, mFeatureFilter( QLatin1String( "" ) )
6363
{
6464
//set first vector layer from layer registry as default one
65-
QMap<QString, QgsMapLayer*> layerMap = QgsProject::instance()->mapLayers();
65+
QMap<QString, QgsMapLayer*> layerMap = mComposition->project()->mapLayers();
6666
QMap<QString, QgsMapLayer*>::const_iterator mapIt = layerMap.constBegin();
6767
for ( ; mapIt != layerMap.constEnd(); ++mapIt )
6868
{
@@ -79,10 +79,11 @@ QgsComposerAttributeTableV2::QgsComposerAttributeTableV2( QgsComposition* compos
7979
//listen for modifications to layer and refresh table when they occur
8080
connect( mVectorLayer, SIGNAL( layerModified() ), this, SLOT( refreshAttributes() ) );
8181
}
82-
connect( QgsProject::instance(), SIGNAL( layerWillBeRemoved( QString ) ), this, SLOT( removeLayer( const QString& ) ) );
8382

8483
if ( mComposition )
8584
{
85+
connect( mComposition->project(), SIGNAL( layerWillBeRemoved( QString ) ), this, SLOT( removeLayer( const QString& ) ) );
86+
8687
//refresh table attributes when composition is refreshed
8788
connect( mComposition, SIGNAL( refreshItemsTriggered() ), this, SLOT( refreshAttributes() ) );
8889

@@ -140,7 +141,7 @@ void QgsComposerAttributeTableV2::setRelationId( const QString& relationId )
140141

141142
QgsVectorLayer* prevLayer = sourceLayer();
142143
mRelationId = relationId;
143-
QgsRelation relation = QgsProject::instance()->relationManager()->relation( mRelationId );
144+
QgsRelation relation = mComposition->project()->relationManager()->relation( mRelationId );
144145
QgsVectorLayer* newLayer = relation.referencingLayer();
145146

146147
if ( mSource == QgsComposerAttributeTableV2::RelationChildren && newLayer != prevLayer )
@@ -438,7 +439,7 @@ bool QgsComposerAttributeTableV2::getTableContents( QgsComposerTableContents &co
438439

439440
if ( mSource == QgsComposerAttributeTableV2::RelationChildren )
440441
{
441-
QgsRelation relation = QgsProject::instance()->relationManager()->relation( mRelationId );
442+
QgsRelation relation = mComposition->project()->relationManager()->relation( mRelationId );
442443
QgsFeature atlasFeature = mComposition->atlasComposition().feature();
443444
req = relation.getRelatedFeaturesRequest( atlasFeature );
444445
}
@@ -564,7 +565,7 @@ QgsVectorLayer *QgsComposerAttributeTableV2::sourceLayer()
564565
return mVectorLayer;
565566
case QgsComposerAttributeTableV2::RelationChildren:
566567
{
567-
QgsRelation relation = QgsProject::instance()->relationManager()->relation( mRelationId );
568+
QgsRelation relation = mComposition->project()->relationManager()->relation( mRelationId );
568569
return relation.referencingLayer();
569570
}
570571
}
@@ -731,7 +732,7 @@ bool QgsComposerAttributeTableV2::readXml( const QDomElement& itemElem, const QD
731732
}
732733
else
733734
{
734-
QgsMapLayer* ml = QgsProject::instance()->mapLayer( layerId );
735+
QgsMapLayer* ml = mComposition->project()->mapLayer( layerId );
735736
if ( ml )
736737
{
737738
mVectorLayer = dynamic_cast<QgsVectorLayer*>( ml );

‎src/core/composer/qgscomposerhtml.cpp‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -185,7 +185,7 @@ void QgsComposerHtml::loadHtml( const bool useCache, const QgsExpressionContext
185185
//set html, using the specified url as base if in Url mode or the project file if in manual mode
186186
const QUrl baseUrl = mContentMode == QgsComposerHtml::Url ?
187187
QUrl( mActualFetchedUrl ) :
188-
QUrl::fromLocalFile( QgsProject::instance()->fileInfo().absoluteFilePath() );
188+
QUrl::fromLocalFile( mComposition->project()->fileInfo().absoluteFilePath() );
189189
mWebPage->mainFrame()->setHtml( loadedHtml, baseUrl );
190190

191191
//set user stylesheet
@@ -547,8 +547,8 @@ void QgsComposerHtml::setExpressionContext( const QgsFeature &feature, QgsVector
547547
if ( mComposition )
548548
{
549549
mDistanceArea->setEllipsoidalMode( mComposition->mapSettings().hasCrsTransformEnabled() );
550+
mDistanceArea->setEllipsoid( mComposition->project()->ellipsoid() );
550551
}
551-
mDistanceArea->setEllipsoid( QgsProject::instance()->ellipsoid() );
552552

553553
// create JSON representation of feature
554554
QgsJSONExporter exporter( layer );

‎src/core/composer/qgscomposeritemcommand.cpp‎

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
#include "qgscomposeritem.h"
2020
#include "qgscomposerframe.h"
2121
#include "qgscomposermultiframe.h"
22+
#include "qgscomposition.h"
2223
#include "qgsproject.h"
2324
#include "qgslogger.h"
2425

@@ -114,7 +115,7 @@ void QgsComposerItemCommand::restoreState( QDomDocument& stateDoc ) const
114115

115116
destItem->readXml( stateDoc.documentElement().firstChild().toElement(), stateDoc );
116117
destItem->repaint();
117-
QgsProject::instance()->setDirty( true );
118+
destItem->composition()->project()->setDirty( true );
118119
}
119120

120121
//

‎src/core/composer/qgscomposerlabel.cpp‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -165,7 +165,7 @@ void QgsComposerLabel::contentChanged()
165165
//set this to true after html is loaded.
166166
mHtmlLoaded = false;
167167

168-
const QUrl baseUrl = QUrl::fromLocalFile( QgsProject::instance()->fileInfo().absoluteFilePath() );
168+
const QUrl baseUrl = QUrl::fromLocalFile( mComposition->project()->fileInfo().absoluteFilePath() );
169169
mWebPage->mainFrame()->setHtml( textToDraw, baseUrl );
170170

171171
//For very basic html labels with no external assets, the html load will already be
@@ -265,7 +265,7 @@ void QgsComposerLabel::refreshExpressionContext()
265265
mDistanceArea->setSourceCrs( mComposition->mapSettings().destinationCrs().srsid() );
266266
}
267267
mDistanceArea->setEllipsoidalMode( mComposition->mapSettings().hasCrsTransformEnabled() );
268-
mDistanceArea->setEllipsoid( QgsProject::instance()->ellipsoid() );
268+
mDistanceArea->setEllipsoid( mComposition->project()->ellipsoid() );
269269
contentChanged();
270270

271271
update();

0 commit comments

Comments
 (0)