Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
Make project title getters/setters a shortcut to metadata title field
And silently upgrade old project titles to metadata titles
  • Loading branch information
nyalldawson committed Mar 22, 2018
1 parent bba3f6c commit 99faf06
Show file tree
Hide file tree
Showing 4 changed files with 32 additions and 10 deletions.
8 changes: 8 additions & 0 deletions python/core/qgsproject.sip.in
Expand Up @@ -54,6 +54,10 @@ Sets the project's title.

.. versionadded:: 2.4

.. note::

Since QGIS 3.2 this is just a shortcut to setting the title in the project's metadata().

.. seealso:: :py:func:`title`
%End

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

.. seealso:: :py:func:`setTitle`

.. note::

Since QGIS 3.2 this is just a shortcut to retrieving the title from the project's metadata().
%End

bool isDirty() const;
Expand Down
21 changes: 12 additions & 9 deletions src/core/qgsproject.cpp
Expand Up @@ -391,21 +391,20 @@ QgsProject *QgsProject::instance()

void QgsProject::setTitle( const QString &title )
{
if ( title == mTitle )
if ( title == mMetadata.title() )
return;

mTitle = title;
mMetadata.setTitle( title );
emit metadataChanged();

setDirty( true );
}


QString QgsProject::title() const
{
return mTitle;
return mMetadata.title();
}


bool QgsProject::isDirty() const
{
return mDirty;
Expand Down Expand Up @@ -504,7 +503,6 @@ void QgsProject::clear()
{
mFile.setFileName( QString() );
mProperties.clearKeys();
mTitle.clear();
mHomePath.clear();
mAutoTransaction = false;
mEvaluateDefaultValues = false;
Expand Down Expand Up @@ -876,7 +874,6 @@ bool QgsProject::readProjectFile( const QString &filename )


QgsDebugMsg( "Opened document " + projectFile.fileName() );
QgsDebugMsg( "Project title: " + mTitle );

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

dump_( mProperties );

// now get project title
_getTitle( *doc, mTitle );
// get older style project title
QString oldTitle;
_getTitle( *doc, oldTitle );

QDomNodeList homePathNl = doc->elementsByTagName( QStringLiteral( "homePath" ) );
if ( homePathNl.count() > 0 )
Expand Down Expand Up @@ -981,6 +979,11 @@ bool QgsProject::readProjectFile( const QString &filename )
QDomElement metadataElement = nl.at( 0 ).toElement();
mMetadata.readMetadataXml( metadataElement );
}
if ( mMetadata.title().isEmpty() && !oldTitle.isEmpty() )
{
// upgrade older title storage to storing within project metadata.
mMetadata.setTitle( oldTitle );
}
emit metadataChanged();

nl = doc->elementsByTagName( QStringLiteral( "autotransaction" ) );
Expand Down
6 changes: 5 additions & 1 deletion src/core/qgsproject.h
Expand Up @@ -112,13 +112,18 @@ class CORE_EXPORT QgsProject : public QObject, public QgsExpressionContextGenera
* Sets the project's title.
* \param title new title
* \since QGIS 2.4
*
* \note Since QGIS 3.2 this is just a shortcut to setting the title in the project's metadata().
*
* \see title()
*/
void setTitle( const QString &title );

/**
* Returns the project's title.
* \see setTitle()
*
* \note Since QGIS 3.2 this is just a shortcut to retrieving the title from the project's metadata().
*/
QString title() const;

Expand Down Expand Up @@ -1285,7 +1290,6 @@ class CORE_EXPORT QgsProject : public QObject, public QgsExpressionContextGenera
*/
QString mHomePath;
mutable QgsProjectPropertyKey mProperties; // property hierarchy, TODO: this shouldn't be mutable
QString mTitle; // project title
bool mAutoTransaction = false; // transaction grouped editing
bool mEvaluateDefaultValues = false; // evaluate default values immediately
QgsCoordinateReferenceSystem mCrs;
Expand Down
7 changes: 7 additions & 0 deletions tests/src/python/test_qgsprojectmetadata.py
Expand Up @@ -342,6 +342,13 @@ def testProject(self):
self.assertEqual(len(metadata_changed_spy), 2)
self.assertEqual(p.metadata().author(), '')

# test that the project title is just a shortcut to the metadata title field
p.setTitle('my title')
self.assertEqual(p.metadata().title(), 'my title')
m.setTitle('my title 2')
p.setMetadata(m)
self.assertEqual(p.title(), 'my title 2')


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

0 comments on commit 99faf06

Please sign in to comment.