Skip to content

Commit

Permalink
Make it possible to use project as non-singleton
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
wonder-sk committed Dec 11, 2016
1 parent 899ea38 commit 57ff36b
Show file tree
Hide file tree
Showing 4 changed files with 28 additions and 28 deletions.
15 changes: 7 additions & 8 deletions python/core/qgsproject.sip
Expand Up @@ -23,6 +23,13 @@ class QgsProject : QObject, QgsExpressionContextGenerator
//! Returns the QgsProject singleton instance
static QgsProject* instance();

/**
* Create a new QgsProject.
*
* Most of the time you want to use QgsProject::instance() instead as many components of QGIS work with the singleton.
*/
explicit QgsProject( QObject* parent /TransferThis/ = nullptr );

~QgsProject();

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

private:
/**
* Create a new QgsProject.
* Private since it's (still) a singleton.
* You want to use QgsProject::instance() instead.
*/
explicit QgsProject( QObject* parent = nullptr );

};
10 changes: 5 additions & 5 deletions src/core/qgsproject.cpp
Expand Up @@ -2060,18 +2060,18 @@ void QgsProject::setEvaluateDefaultValues( bool evaluateDefaultValues )

void QgsProject::setTopologicalEditing( bool enabled )
{
QgsProject::instance()->writeEntry( QStringLiteral( "Digitizing" ), QStringLiteral( "/TopologicalEditing" ), ( enabled ? 1 : 0 ) );
writeEntry( QStringLiteral( "Digitizing" ), QStringLiteral( "/TopologicalEditing" ), ( enabled ? 1 : 0 ) );
emit topologicalEditingChanged();
}

bool QgsProject::topologicalEditing() const
{
return QgsProject::instance()->readNumEntry( QStringLiteral( "Digitizing" ), QStringLiteral( "/TopologicalEditing" ), 0 );
return readNumEntry( QStringLiteral( "Digitizing" ), QStringLiteral( "/TopologicalEditing" ), 0 );
}

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

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

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

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

QStringList QgsProject::nonIdentifiableLayers() const
{
return QgsProject::instance()->readListEntry( QStringLiteral( "Identify" ), QStringLiteral( "/disabledLayers" ) );
return readListEntry( QStringLiteral( "Identify" ), QStringLiteral( "/disabledLayers" ) );
}

bool QgsProject::autoTransaction() const
Expand Down
18 changes: 7 additions & 11 deletions src/core/qgsproject.h
Expand Up @@ -68,10 +68,6 @@ class QgsVectorLayer;
*/

// TODO Might want to consider moving from Singleton; i.e., allowing more than one
// project. Just as the GIMP can have simultaneous multiple images, perhaps
// QGIS can one day have simultaneous multiple projects.

class CORE_EXPORT QgsProject : public QObject, public QgsExpressionContextGenerator
{
Q_OBJECT
Expand All @@ -87,6 +83,13 @@ class CORE_EXPORT QgsProject : public QObject, public QgsExpressionContextGenera
//! Returns the QgsProject singleton instance
static QgsProject* instance();

/**
* Create a new QgsProject.
*
* Most of the time you want to use QgsProject::instance() instead as many components of QGIS work with the singleton.
*/
explicit QgsProject( QObject* parent = nullptr );

~QgsProject();

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

private:

/**
* Create a new QgsProject.
* Private since it's (still) a singleton.
* You want to use QgsProject::instance() instead.
*/
explicit QgsProject( QObject* parent = nullptr );

static QgsProject* sProject;

/** Read map layers from project file.
Expand Down
13 changes: 9 additions & 4 deletions tests/src/core/testqgsproject.cpp
Expand Up @@ -61,7 +61,7 @@ void TestQgsProject::cleanupTestCase()

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

delete prj;
}

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

QgsProject* prj = QgsProject::instance();
QgsProject* prj = new QgsProject;
// new project should inherit QGIS default distance unit
prj->clear();
QCOMPARE( prj->distanceUnits(), QgsUnitTypes::DistanceFeet );
Expand Down Expand Up @@ -123,15 +124,19 @@ void TestQgsProject::testProjectUnits()
//test setting new units for project
prj->setAreaUnits( QgsUnitTypes::AreaAcres );
QCOMPARE( prj->areaUnits(), QgsUnitTypes::AreaAcres );

delete prj;
}

void TestQgsProject::variablesChanged()
{
QSignalSpy spyVariablesChanged( QgsProject::instance(), SIGNAL( variablesChanged() ) );
QgsProject* prj = new QgsProject;
QSignalSpy spyVariablesChanged( prj, SIGNAL( variablesChanged() ) );
QgsStringMap vars;
vars.insert( QStringLiteral( "variable" ), QStringLiteral( "1" ) );
QgsProject::instance()->setVariables( vars );
prj->setVariables( vars );
QVERIFY( spyVariablesChanged.count() == 1 );
delete prj;
}


Expand Down

0 comments on commit 57ff36b

Please sign in to comment.