Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
Expose QgsProject::setInstance to API
Fixes #38755

Needs forward porting to all active branches
  • Loading branch information
elpaso authored and nyalldawson committed Sep 16, 2020
1 parent a5a5c4d commit f9e547c
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 9 deletions.
14 changes: 14 additions & 0 deletions python/core/auto_generated/qgsproject.sip.in
Expand Up @@ -38,6 +38,20 @@ open within the main QGIS application.
Returns the QgsProject singleton instance
%End

static void setInstance( QgsProject *project );
%Docstring
Set the current project instance to ``project``

.. note::

this is used mainly by the server, which caches the projects and (potentially) needs to switch the current instance on every request

.. seealso:: :py:func:`instance`

.. versionadded:: 3.10
%End


explicit QgsProject( QObject *parent /TransferThis/ = 0 );
%Docstring
Create a new QgsProject.
Expand Down
19 changes: 10 additions & 9 deletions src/core/qgsproject.h
Expand Up @@ -108,6 +108,16 @@ class CORE_EXPORT QgsProject : public QObject, public QgsExpressionContextGenera
//! Returns the QgsProject singleton instance
static QgsProject *instance();

/**
* Set the current project instance to \a project
*
* \note this is used mainly by the server, which caches the projects and (potentially) needs to switch the current instance on every request
* \see instance()
* \since QGIS 3.10.11
*/
static void setInstance( QgsProject *project ) ;


/**
* Create a new QgsProject.
*
Expand Down Expand Up @@ -1651,15 +1661,6 @@ class CORE_EXPORT QgsProject : public QObject, public QgsExpressionContextGenera

static QgsProject *sProject;

/**
* Set the current project instance to \a project
*
* \note this is used mainly by the server, which caches the projects and (potentially) needs to switch the current instance on every request
* \see instance()
* \note not available in Python bindings
* \since QGIS 3.2
*/
static void setInstance( QgsProject *project ) SIP_SKIP;

/**
* Read map layers from project file.
Expand Down
8 changes: 8 additions & 0 deletions tests/src/python/test_qgsproject.py
Expand Up @@ -1360,6 +1360,14 @@ def testMapScales(self):
p.setUseProjectScales(False)
self.assertEqual(len(spy), 4)

def testSetInstance(self):
"""Test singleton API"""

p = QgsProject()
self.assertNotEqual(p, QgsProject.instance())
QgsProject.setInstance(p)
self.assertEqual(p, QgsProject.instance())


if __name__ == '__main__':
unittest.main()

0 comments on commit f9e547c

Please sign in to comment.