Skip to content

Commit 99faf06

Browse files
committedMar 22, 2018
Make project title getters/setters a shortcut to metadata title field
And silently upgrade old project titles to metadata titles
1 parent bba3f6c commit 99faf06

File tree

4 files changed

+32
-10
lines changed

4 files changed

+32
-10
lines changed
 

‎python/core/qgsproject.sip.in

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,10 @@ Sets the project's title.
5454

5555
.. versionadded:: 2.4
5656

57+
.. note::
58+
59+
Since QGIS 3.2 this is just a shortcut to setting the title in the project's metadata().
60+
5761
.. seealso:: :py:func:`title`
5862
%End
5963

@@ -62,6 +66,10 @@ Sets the project's title.
6266
Returns the project's title.
6367

6468
.. seealso:: :py:func:`setTitle`
69+
70+
.. note::
71+
72+
Since QGIS 3.2 this is just a shortcut to retrieving the title from the project's metadata().
6573
%End
6674

6775
bool isDirty() const;

‎src/core/qgsproject.cpp

Lines changed: 12 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -391,21 +391,20 @@ QgsProject *QgsProject::instance()
391391

392392
void QgsProject::setTitle( const QString &title )
393393
{
394-
if ( title == mTitle )
394+
if ( title == mMetadata.title() )
395395
return;
396396

397-
mTitle = title;
397+
mMetadata.setTitle( title );
398+
emit metadataChanged();
398399

399400
setDirty( true );
400401
}
401402

402-
403403
QString QgsProject::title() const
404404
{
405-
return mTitle;
405+
return mMetadata.title();
406406
}
407407

408-
409408
bool QgsProject::isDirty() const
410409
{
411410
return mDirty;
@@ -504,7 +503,6 @@ void QgsProject::clear()
504503
{
505504
mFile.setFileName( QString() );
506505
mProperties.clearKeys();
507-
mTitle.clear();
508506
mHomePath.clear();
509507
mAutoTransaction = false;
510508
mEvaluateDefaultValues = false;
@@ -876,7 +874,6 @@ bool QgsProject::readProjectFile( const QString &filename )
876874

877875

878876
QgsDebugMsg( "Opened document " + projectFile.fileName() );
879-
QgsDebugMsg( "Project title: " + mTitle );
880877

881878
// get project version string, if any
882879
QgsProjectVersion fileVersion = getVersion( *doc );
@@ -912,8 +909,9 @@ bool QgsProject::readProjectFile( const QString &filename )
912909

913910
dump_( mProperties );
914911

915-
// now get project title
916-
_getTitle( *doc, mTitle );
912+
// get older style project title
913+
QString oldTitle;
914+
_getTitle( *doc, oldTitle );
917915

918916
QDomNodeList homePathNl = doc->elementsByTagName( QStringLiteral( "homePath" ) );
919917
if ( homePathNl.count() > 0 )
@@ -981,6 +979,11 @@ bool QgsProject::readProjectFile( const QString &filename )
981979
QDomElement metadataElement = nl.at( 0 ).toElement();
982980
mMetadata.readMetadataXml( metadataElement );
983981
}
982+
if ( mMetadata.title().isEmpty() && !oldTitle.isEmpty() )
983+
{
984+
// upgrade older title storage to storing within project metadata.
985+
mMetadata.setTitle( oldTitle );
986+
}
984987
emit metadataChanged();
985988

986989
nl = doc->elementsByTagName( QStringLiteral( "autotransaction" ) );

‎src/core/qgsproject.h

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -112,13 +112,18 @@ class CORE_EXPORT QgsProject : public QObject, public QgsExpressionContextGenera
112112
* Sets the project's title.
113113
* \param title new title
114114
* \since QGIS 2.4
115+
*
116+
* \note Since QGIS 3.2 this is just a shortcut to setting the title in the project's metadata().
117+
*
115118
* \see title()
116119
*/
117120
void setTitle( const QString &title );
118121

119122
/**
120123
* Returns the project's title.
121124
* \see setTitle()
125+
*
126+
* \note Since QGIS 3.2 this is just a shortcut to retrieving the title from the project's metadata().
122127
*/
123128
QString title() const;
124129

@@ -1285,7 +1290,6 @@ class CORE_EXPORT QgsProject : public QObject, public QgsExpressionContextGenera
12851290
*/
12861291
QString mHomePath;
12871292
mutable QgsProjectPropertyKey mProperties; // property hierarchy, TODO: this shouldn't be mutable
1288-
QString mTitle; // project title
12891293
bool mAutoTransaction = false; // transaction grouped editing
12901294
bool mEvaluateDefaultValues = false; // evaluate default values immediately
12911295
QgsCoordinateReferenceSystem mCrs;

‎tests/src/python/test_qgsprojectmetadata.py

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -342,6 +342,13 @@ def testProject(self):
342342
self.assertEqual(len(metadata_changed_spy), 2)
343343
self.assertEqual(p.metadata().author(), '')
344344

345+
# test that the project title is just a shortcut to the metadata title field
346+
p.setTitle('my title')
347+
self.assertEqual(p.metadata().title(), 'my title')
348+
m.setTitle('my title 2')
349+
p.setMetadata(m)
350+
self.assertEqual(p.title(), 'my title 2')
351+
345352

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

0 commit comments

Comments
 (0)
Please sign in to comment.