Skip to content

Commit

Permalink
Add API to retrieve QGIS version used to save a QgsProject
Browse files Browse the repository at this point in the history
   project.lastSaveVersion()

now returns the version number used to save the project.
Also cleanup QgsProjectVersion code

Fixes #37288
  • Loading branch information
nyalldawson committed Jun 19, 2020
1 parent 0ac73f4 commit 4dc9b38
Show file tree
Hide file tree
Showing 7 changed files with 97 additions and 30 deletions.
7 changes: 7 additions & 0 deletions python/core/auto_generated/qgsproject.sip.in
Expand Up @@ -124,6 +124,13 @@ Returns the full user name that did the last save.
%Docstring
Returns the date and time when the project was last saved.

.. versionadded:: 3.14
%End

QgsProjectVersion lastSaveVersion() const;
%Docstring
Returns the QGIS version which the project was last saved using.

.. versionadded:: 3.14
%End

Expand Down
35 changes: 28 additions & 7 deletions python/core/auto_generated/qgsprojectversion.sip.in
Expand Up @@ -9,7 +9,6 @@




class QgsProjectVersion
{
%Docstring
Expand All @@ -28,12 +27,35 @@ of QGIS is greater than the one used to create a project file.
Creates a new NULL version
%End

QgsProjectVersion( int major, int minor, int sub, const QString &name = "" );
QgsProjectVersion( int major, int minor, int sub, const QString &name = QString() );
%Docstring
Constructor for QgsProjectVersion, with the specified ``major``, ``minor`` and ``sub`` version numbers.
%End

QgsProjectVersion( const QString &string );
int majorVersion();
int minorVersion();
int subVersion();
QString text();
%Docstring
Constructor for QgsProjectVersion, which parses the version number from a ``string``.
%End

int majorVersion() const;
%Docstring
Returns the major version number.
%End

int minorVersion() const;
%Docstring
Returns the minor version number.
%End

int subVersion() const;
%Docstring
Returns the sub version number.
%End

QString text() const;
%Docstring
Returns a string representation of the version.
%End

bool isNull() const;
%Docstring
Expand All @@ -50,7 +72,6 @@ Returns ``True`` if this is a NULL project version.

};


/************************************************************************
* This file has been generated automatically from *
* *
Expand Down
12 changes: 10 additions & 2 deletions src/core/qgsproject.cpp
Expand Up @@ -496,6 +496,11 @@ QDateTime QgsProject::lastSaveDateTime() const
return mSaveDateTime;
}

QgsProjectVersion QgsProject::lastSaveVersion() const
{
return mSaveVersion;
}

bool QgsProject::isDirty() const
{
return mDirty;
Expand Down Expand Up @@ -771,6 +776,7 @@ void QgsProject::clear()
mSaveUser.clear();
mSaveUserFull.clear();
mSaveDateTime = QDateTime();
mSaveVersion = QgsProjectVersion();
mHomePath.clear();
mCachedHomePath.clear();
mAutoTransaction = false;
Expand Down Expand Up @@ -1296,7 +1302,7 @@ bool QgsProject::readProjectFile( const QString &filename, QgsProject::ReadFlags

// get project version string, if any
QgsProjectVersion fileVersion = getVersion( *doc );
QgsProjectVersion thisVersion( Qgis::version() );
const QgsProjectVersion thisVersion( Qgis::version() );

if ( thisVersion > fileVersion )
{
Expand All @@ -1321,6 +1327,7 @@ bool QgsProject::readProjectFile( const QString &filename, QgsProject::ReadFlags
mFile.setFileName( fileName );
mCachedHomePath.clear();
mProjectScope.reset();
mSaveVersion = fileVersion;

// now get any properties
_getProperties( *doc, mProperties );
Expand Down Expand Up @@ -1542,7 +1549,7 @@ bool QgsProject::readProjectFile( const QString &filename, QgsProject::ReadFlags
}

// Convert pre 3.4 to create layers flags
if ( QgsProjectVersion( 3, 4, 0 ) > fileVersion )
if ( QgsProjectVersion( 3, 4, 0 ) > mSaveVersion )
{
const QStringList requiredLayerIds = readListEntry( QStringLiteral( "RequiredLayers" ), QStringLiteral( "Layers" ) );
for ( const QString &layerId : requiredLayerIds )
Expand Down Expand Up @@ -2114,6 +2121,7 @@ bool QgsProject::writeProjectFile( const QString &filename )
mSaveDateTime = QDateTime();
}
doc->appendChild( qgisNode );
mSaveVersion = QgsProjectVersion( Qgis::version() );

QDomElement homePathNode = doc->createElement( QStringLiteral( "homePath" ) );
homePathNode.setAttribute( QStringLiteral( "path" ), mHomePath );
Expand Down
8 changes: 8 additions & 0 deletions src/core/qgsproject.h
Expand Up @@ -215,6 +215,13 @@ class CORE_EXPORT QgsProject : public QObject, public QgsExpressionContextGenera
*/
QDateTime lastSaveDateTime() const;

/**
* Returns the QGIS version which the project was last saved using.
*
* \since QGIS 3.14
*/
QgsProjectVersion lastSaveVersion() const;

/**
* Returns TRUE if the project has been modified since the last write()
*/
Expand Down Expand Up @@ -1939,6 +1946,7 @@ class CORE_EXPORT QgsProject : public QObject, public QgsExpressionContextGenera
QString mSaveUser; // last saved user.
QString mSaveUserFull; // last saved user full name.
QDateTime mSaveDateTime;
QgsProjectVersion mSaveVersion;

/**
* Manual override for project home path - if empty, home path is automatically
Expand Down
22 changes: 9 additions & 13 deletions src/core/qgsprojectversion.cpp
Expand Up @@ -22,32 +22,28 @@
#include "qgsprojectversion.h"

QgsProjectVersion::QgsProjectVersion( int major, int minor, int sub, const QString &name )
: mMajor( major )
, mMinor( minor )
, mSub( sub )
, mName( name )
{
mMajor = major;
mMinor = minor;
mSub = sub;
mName = name;
}

QgsProjectVersion::QgsProjectVersion( const QString &string )
{
QString pre = string.section( '-', 0, 0 );
const QString pre = string.section( '-', 0, 0 );
const QStringList fileVersionParts = pre.section( '-', 0 ).split( '.' );

QStringList fileVersionParts = pre.section( '-', 0 ).split( '.' );

mMinor = 0;
mSub = 0;
mMajor = fileVersionParts.at( 0 ).toInt();

if ( fileVersionParts.size() > 1 )
{
mMinor = fileVersionParts.at( 1 ).toInt();
}
if ( fileVersionParts.size() > 2 )
{
mSub = fileVersionParts.at( 2 ).toInt();
mSub = fileVersionParts.at( 2 ).toInt();
}
mName = string.section( '-', 1 );
mName = string.section( '-', 1 );

QgsDebugMsgLevel( QStringLiteral( "Version is set to " ) + text(), 4 );
}
Expand Down Expand Up @@ -78,7 +74,7 @@ bool QgsProjectVersion::operator>( const QgsProjectVersion &other ) const
( ( mMajor == other.mMajor ) && ( mMinor == other.mMinor ) && ( mSub > other.mSub ) ) );
}

QString QgsProjectVersion::text()
QString QgsProjectVersion::text() const
{
if ( mName.isEmpty() )
{
Expand Down
36 changes: 28 additions & 8 deletions src/core/qgsprojectversion.h
Expand Up @@ -28,7 +28,6 @@
* Used in places where you need to check if the current version
* of QGIS is greater than the one used to create a project file.
*/

class CORE_EXPORT QgsProjectVersion
{

Expand All @@ -39,12 +38,35 @@ class CORE_EXPORT QgsProjectVersion
*/
QgsProjectVersion() = default;

QgsProjectVersion( int major, int minor, int sub, const QString &name = "" );
/**
* Constructor for QgsProjectVersion, with the specified \a major, \a minor and \a sub version numbers.
*/
QgsProjectVersion( int major, int minor, int sub, const QString &name = QString() );

/**
* Constructor for QgsProjectVersion, which parses the version number from a \a string.
*/
QgsProjectVersion( const QString &string );
int majorVersion() { return mMajor;}
int minorVersion() { return mMinor;}
int subVersion() { return mSub;}
QString text();

/**
* Returns the major version number.
*/
int majorVersion() const { return mMajor;}

/**
* Returns the minor version number.
*/
int minorVersion() const { return mMinor;}

/**
* Returns the sub version number.
*/
int subVersion() const { return mSub;}

/**
* Returns a string representation of the version.
*/
QString text() const;

/**
* Returns TRUE if this is a NULL project version.
Expand Down Expand Up @@ -78,6 +100,4 @@ class CORE_EXPORT QgsProjectVersion
QString mName;
};

// clazy:excludeall=qstring-allocations

#endif // QGSPROJECTVERSION_H
7 changes: 7 additions & 0 deletions tests/src/core/testqgsproject.cpp
Expand Up @@ -517,6 +517,7 @@ void TestQgsProject::projectSaveUser()
QCOMPARE( p.saveUser(), QgsApplication::userLoginName() );
QCOMPARE( p.saveUserFullName(), QgsApplication::userFullName() );
QCOMPARE( p.lastSaveDateTime().date(), QDateTime::currentDateTime().date() );
QCOMPARE( p.lastSaveVersion().text(), QgsProjectVersion( Qgis::version() ).text() );

QgsSettings s;
s.setValue( QStringLiteral( "projects/anonymize_saved_projects" ), true, QgsSettings::Core );
Expand All @@ -533,6 +534,12 @@ void TestQgsProject::projectSaveUser()
QCOMPARE( p.saveUser(), QgsApplication::userLoginName() );
QCOMPARE( p.saveUserFullName(), QgsApplication::userFullName() );
QCOMPARE( p.lastSaveDateTime().date(), QDateTime::currentDateTime().date() );

QgsProject p2;
QVERIFY( p2.read( QString( TEST_DATA_DIR ) + QStringLiteral( "/embedded_groups/project1.qgs" ) ) );
QCOMPARE( p2.lastSaveVersion().text(), QStringLiteral( "2.99.0-Master" ) );
p2.clear();
QVERIFY( p2.lastSaveVersion().isNull() );
}

void TestQgsProject::testSetGetCrs()
Expand Down

0 comments on commit 4dc9b38

Please sign in to comment.