Skip to content

Commit 57ff36b

Browse files
committedDec 11, 2016
Make it possible to use project as non-singleton
This is just a first step - it will be a long way to get rid of all the usages of singleton instance in QGIS code.
1 parent 899ea38 commit 57ff36b

File tree

4 files changed

+28
-28
lines changed

4 files changed

+28
-28
lines changed
 

‎python/core/qgsproject.sip

Lines changed: 7 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,13 @@ class QgsProject : QObject, QgsExpressionContextGenerator
2323
//! Returns the QgsProject singleton instance
2424
static QgsProject* instance();
2525

26+
/**
27+
* Create a new QgsProject.
28+
*
29+
* Most of the time you want to use QgsProject::instance() instead as many components of QGIS work with the singleton.
30+
*/
31+
explicit QgsProject( QObject* parent /TransferThis/ = nullptr );
32+
2633
~QgsProject();
2734

2835
/** Sets the project's title.
@@ -810,12 +817,4 @@ class QgsProject : QObject, QgsExpressionContextGenerator
810817
*/
811818
void setDirty( bool b = true );
812819

813-
private:
814-
/**
815-
* Create a new QgsProject.
816-
* Private since it's (still) a singleton.
817-
* You want to use QgsProject::instance() instead.
818-
*/
819-
explicit QgsProject( QObject* parent = nullptr );
820-
821820
};

‎src/core/qgsproject.cpp

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2060,18 +2060,18 @@ void QgsProject::setEvaluateDefaultValues( bool evaluateDefaultValues )
20602060

20612061
void QgsProject::setTopologicalEditing( bool enabled )
20622062
{
2063-
QgsProject::instance()->writeEntry( QStringLiteral( "Digitizing" ), QStringLiteral( "/TopologicalEditing" ), ( enabled ? 1 : 0 ) );
2063+
writeEntry( QStringLiteral( "Digitizing" ), QStringLiteral( "/TopologicalEditing" ), ( enabled ? 1 : 0 ) );
20642064
emit topologicalEditingChanged();
20652065
}
20662066

20672067
bool QgsProject::topologicalEditing() const
20682068
{
2069-
return QgsProject::instance()->readNumEntry( QStringLiteral( "Digitizing" ), QStringLiteral( "/TopologicalEditing" ), 0 );
2069+
return readNumEntry( QStringLiteral( "Digitizing" ), QStringLiteral( "/TopologicalEditing" ), 0 );
20702070
}
20712071

20722072
QgsUnitTypes::DistanceUnit QgsProject::distanceUnits() const
20732073
{
2074-
QString distanceUnitString = QgsProject::instance()->readEntry( QStringLiteral( "Measurement" ), QStringLiteral( "/DistanceUnits" ), QString() );
2074+
QString distanceUnitString = readEntry( QStringLiteral( "Measurement" ), QStringLiteral( "/DistanceUnits" ), QString() );
20752075
if ( !distanceUnitString.isEmpty() )
20762076
return QgsUnitTypes::decodeDistanceUnit( distanceUnitString );
20772077

@@ -2089,7 +2089,7 @@ void QgsProject::setDistanceUnits( QgsUnitTypes::DistanceUnit unit )
20892089

20902090
QgsUnitTypes::AreaUnit QgsProject::areaUnits() const
20912091
{
2092-
QString areaUnitString = QgsProject::instance()->readEntry( QStringLiteral( "Measurement" ), QStringLiteral( "/AreaUnits" ), QString() );
2092+
QString areaUnitString = readEntry( QStringLiteral( "Measurement" ), QStringLiteral( "/AreaUnits" ), QString() );
20932093
if ( !areaUnitString.isEmpty() )
20942094
return QgsUnitTypes::decodeAreaUnit( areaUnitString );
20952095

@@ -2161,7 +2161,7 @@ void QgsProject::setNonIdentifiableLayers( const QStringList& layerIds )
21612161

21622162
QStringList QgsProject::nonIdentifiableLayers() const
21632163
{
2164-
return QgsProject::instance()->readListEntry( QStringLiteral( "Identify" ), QStringLiteral( "/disabledLayers" ) );
2164+
return readListEntry( QStringLiteral( "Identify" ), QStringLiteral( "/disabledLayers" ) );
21652165
}
21662166

21672167
bool QgsProject::autoTransaction() const

‎src/core/qgsproject.h

Lines changed: 7 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -68,10 +68,6 @@ class QgsVectorLayer;
6868
6969
*/
7070

71-
// TODO Might want to consider moving from Singleton; i.e., allowing more than one
72-
// project. Just as the GIMP can have simultaneous multiple images, perhaps
73-
// QGIS can one day have simultaneous multiple projects.
74-
7571
class CORE_EXPORT QgsProject : public QObject, public QgsExpressionContextGenerator
7672
{
7773
Q_OBJECT
@@ -87,6 +83,13 @@ class CORE_EXPORT QgsProject : public QObject, public QgsExpressionContextGenera
8783
//! Returns the QgsProject singleton instance
8884
static QgsProject* instance();
8985

86+
/**
87+
* Create a new QgsProject.
88+
*
89+
* Most of the time you want to use QgsProject::instance() instead as many components of QGIS work with the singleton.
90+
*/
91+
explicit QgsProject( QObject* parent = nullptr );
92+
9093
~QgsProject();
9194

9295
/** Sets the project's title.
@@ -920,13 +923,6 @@ class CORE_EXPORT QgsProject : public QObject, public QgsExpressionContextGenera
920923

921924
private:
922925

923-
/**
924-
* Create a new QgsProject.
925-
* Private since it's (still) a singleton.
926-
* You want to use QgsProject::instance() instead.
927-
*/
928-
explicit QgsProject( QObject* parent = nullptr );
929-
930926
static QgsProject* sProject;
931927

932928
/** Read map layers from project file.

‎tests/src/core/testqgsproject.cpp

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,7 @@ void TestQgsProject::cleanupTestCase()
6161

6262
void TestQgsProject::testReadPath()
6363
{
64-
QgsProject* prj = QgsProject::instance();
64+
QgsProject* prj = new QgsProject;
6565
// this is a bit hacky as we do not really load such project
6666
QString prefix;
6767
#if defined(Q_OS_WIN)
@@ -82,6 +82,7 @@ void TestQgsProject::testReadPath()
8282
QCOMPARE( prj->readPath( "./x.gz" ), QString( prefix + "/home/qgis/x.gz" ) );
8383
QCOMPARE( prj->readPath( "/vsigzip/./x.gz" ), QString( "/vsigzip/%1/home/qgis/x.gz" ).arg( prefix ) ); // not sure how useful this really is...
8484

85+
delete prj;
8586
}
8687

8788
void TestQgsProject::testProjectUnits()
@@ -94,7 +95,7 @@ void TestQgsProject::testProjectUnits()
9495
QSettings s;
9596
s.setValue( QStringLiteral( "/qgis/measure/displayunits" ), QgsUnitTypes::encodeUnit( QgsUnitTypes::DistanceFeet ) );
9697

97-
QgsProject* prj = QgsProject::instance();
98+
QgsProject* prj = new QgsProject;
9899
// new project should inherit QGIS default distance unit
99100
prj->clear();
100101
QCOMPARE( prj->distanceUnits(), QgsUnitTypes::DistanceFeet );
@@ -123,15 +124,19 @@ void TestQgsProject::testProjectUnits()
123124
//test setting new units for project
124125
prj->setAreaUnits( QgsUnitTypes::AreaAcres );
125126
QCOMPARE( prj->areaUnits(), QgsUnitTypes::AreaAcres );
127+
128+
delete prj;
126129
}
127130

128131
void TestQgsProject::variablesChanged()
129132
{
130-
QSignalSpy spyVariablesChanged( QgsProject::instance(), SIGNAL( variablesChanged() ) );
133+
QgsProject* prj = new QgsProject;
134+
QSignalSpy spyVariablesChanged( prj, SIGNAL( variablesChanged() ) );
131135
QgsStringMap vars;
132136
vars.insert( QStringLiteral( "variable" ), QStringLiteral( "1" ) );
133-
QgsProject::instance()->setVariables( vars );
137+
prj->setVariables( vars );
134138
QVERIFY( spyVariablesChanged.count() == 1 );
139+
delete prj;
135140
}
136141

137142

0 commit comments

Comments
 (0)
Please sign in to comment.