Skip to content

Commit 68b38b2

Browse files
committed
Add a signal when a project is cleared
1 parent 93e5902 commit 68b38b2

File tree

4 files changed

+57
-14
lines changed

4 files changed

+57
-14
lines changed

python/core/auto_generated/qgsproject.sip.in

+20-7
Original file line numberDiff line numberDiff line change
@@ -221,6 +221,8 @@ from a source to destination coordinate reference system.
221221
Clear the project - removes all settings and resets it back to an empty, default state.
222222

223223
.. versionadded:: 2.4
224+
225+
.. seealso:: :py:func:`cleared`
224226
%End
225227

226228
bool read( const QString &filename );
@@ -984,19 +986,30 @@ in the project. The removeMapLayer(), removeMapLayers() calls do not block remov
984986
%End
985987

986988
signals:
989+
990+
void cleared();
991+
%Docstring
992+
Emitted when the project is cleared (and additionally when an open project is cleared
993+
just before a new project is read).
994+
995+
.. seealso:: :py:func:`clear`
996+
997+
.. versionadded:: 3.2
998+
%End
999+
9871000
void readProject( const QDomDocument & );
9881001
%Docstring
989-
emitted when project is being read
1002+
Emitted when a project is being read.
9901003
%End
9911004

9921005
void writeProject( QDomDocument & );
9931006
%Docstring
994-
emitted when project is being written
1007+
Emitted when the project is being written.
9951008
%End
9961009

9971010
void readMapLayer( QgsMapLayer *mapLayer, const QDomElement &layerNode );
9981011
%Docstring
999-
Emitted, after the basic initialization of a layer from the project
1012+
Emitted after the basic initialization of a layer from the project
10001013
file is done. You can use this signal to read additional information
10011014
from the project file.
10021015

@@ -1006,7 +1019,7 @@ from the project file.
10061019

10071020
void writeMapLayer( QgsMapLayer *mapLayer, QDomElement &layerElem, QDomDocument &doc );
10081021
%Docstring
1009-
Emitted, when a layer is being saved. You can use this method to save
1022+
Emitted when a layer is being saved. You can use this method to save
10101023
additional information to the layer.
10111024

10121025
:param mapLayer: The map layer which is being initialized
@@ -1016,12 +1029,12 @@ additional information to the layer.
10161029

10171030
void projectSaved();
10181031
%Docstring
1019-
emitted when the project file has been written and closed
1032+
Emitted when the project file has been written and closed.
10201033
%End
10211034

10221035
void oldProjectVersionWarning( const QString & );
10231036
%Docstring
1024-
emitted when an old project file is read.
1037+
Emitted when an old project file is read.
10251038
%End
10261039

10271040
void layerLoaded( int i, int n );
@@ -1070,7 +1083,7 @@ Emitted when the home path of the project changes.
10701083

10711084
void snappingConfigChanged( const QgsSnappingConfig &config );
10721085
%Docstring
1073-
emitted whenever the configuration for snapping has changed
1086+
Emitted whenever the configuration for snapping has changed.
10741087
%End
10751088

10761089
void customVariablesChanged();

src/core/qgsproject.cpp

+1
Original file line numberDiff line numberDiff line change
@@ -606,6 +606,7 @@ void QgsProject::clear()
606606
mRootGroup->clear();
607607

608608
setDirty( false );
609+
emit cleared();
609610
}
610611

611612
// basically a debugging tool to dump property list values

src/core/qgsproject.h

+28-7
Original file line numberDiff line numberDiff line change
@@ -247,6 +247,7 @@ class CORE_EXPORT QgsProject : public QObject, public QgsExpressionContextGenera
247247
/**
248248
* Clear the project - removes all settings and resets it back to an empty, default state.
249249
* \since QGIS 2.4
250+
* \see cleared()
250251
*/
251252
void clear();
252253

@@ -955,14 +956,28 @@ class CORE_EXPORT QgsProject : public QObject, public QgsExpressionContextGenera
955956
void setRequiredLayers( const QSet<QgsMapLayer *> &layers );
956957

957958
signals:
958-
//! emitted when project is being read
959+
960+
/**
961+
* Emitted when the project is cleared (and additionally when an open project is cleared
962+
* just before a new project is read).
963+
*
964+
* \see clear()
965+
* \since QGIS 3.2
966+
*/
967+
void cleared();
968+
969+
/**
970+
* Emitted when a project is being read.
971+
*/
959972
void readProject( const QDomDocument & );
960973

961-
//! emitted when project is being written
974+
/**
975+
* Emitted when the project is being written.
976+
*/
962977
void writeProject( QDomDocument & );
963978

964979
/**
965-
* Emitted, after the basic initialization of a layer from the project
980+
* Emitted after the basic initialization of a layer from the project
966981
* file is done. You can use this signal to read additional information
967982
* from the project file.
968983
*
@@ -972,7 +987,7 @@ class CORE_EXPORT QgsProject : public QObject, public QgsExpressionContextGenera
972987
void readMapLayer( QgsMapLayer *mapLayer, const QDomElement &layerNode );
973988

974989
/**
975-
* Emitted, when a layer is being saved. You can use this method to save
990+
* Emitted when a layer is being saved. You can use this method to save
976991
* additional information to the layer.
977992
*
978993
* \param mapLayer The map layer which is being initialized
@@ -981,10 +996,14 @@ class CORE_EXPORT QgsProject : public QObject, public QgsExpressionContextGenera
981996
*/
982997
void writeMapLayer( QgsMapLayer *mapLayer, QDomElement &layerElem, QDomDocument &doc );
983998

984-
//! emitted when the project file has been written and closed
999+
/**
1000+
* Emitted when the project file has been written and closed.
1001+
*/
9851002
void projectSaved();
9861003

987-
//! emitted when an old project file is read.
1004+
/**
1005+
* Emitted when an old project file is read.
1006+
*/
9881007
void oldProjectVersionWarning( const QString & );
9891008

9901009
/**
@@ -1019,7 +1038,9 @@ class CORE_EXPORT QgsProject : public QObject, public QgsExpressionContextGenera
10191038
*/
10201039
void homePathChanged();
10211040

1022-
//! emitted whenever the configuration for snapping has changed
1041+
/**
1042+
* Emitted whenever the configuration for snapping has changed.
1043+
*/
10231044
void snappingConfigChanged( const QgsSnappingConfig &config );
10241045

10251046
/**

tests/src/python/test_qgsproject.py

+8
Original file line numberDiff line numberDiff line change
@@ -135,6 +135,14 @@ def test_makeKeyTokens_(self):
135135
def catchMessage(self):
136136
self.messageCaught = True
137137

138+
def testClear(self):
139+
prj = QgsProject.instance()
140+
prj.setTitle('xxx')
141+
spy = QSignalSpy(prj.cleared)
142+
prj.clear()
143+
self.assertEqual(len(spy), 1)
144+
self.assertFalse(prj.title())
145+
138146
def testCrs(self):
139147
prj = QgsProject.instance()
140148
prj.clear()

0 commit comments

Comments
 (0)