Skip to content
Permalink
Browse files

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.
  • Loading branch information
wonder-sk committed Dec 11, 2016
1 parent 899ea38 commit 57ff36bc016c70a3abb99ebb66764f01868edd9f
Showing with 28 additions and 28 deletions.
  1. +7 −8 python/core/qgsproject.sip
  2. +5 −5 src/core/qgsproject.cpp
  3. +7 −11 src/core/qgsproject.h
  4. +9 −4 tests/src/core/testqgsproject.cpp
@@ -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.
@@ -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 );

};
@@ -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 );

@@ -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 );

@@ -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
@@ -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
@@ -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.
@@ -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.
@@ -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)
@@ -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()
@@ -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 );
@@ -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;
}


0 comments on commit 57ff36b

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