Skip to content
Permalink
Browse files

Add API to retrieve QGIS version used to save a QgsProject

   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 4dc9b38142315dcec2cda68cf0b311891b1896a1
@@ -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

@@ -9,7 +9,6 @@




class QgsProjectVersion
{
%Docstring
@@ -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
@@ -50,7 +72,6 @@ Returns ``True`` if this is a NULL project version.

};


/************************************************************************
* This file has been generated automatically from *
* *
@@ -496,6 +496,11 @@ QDateTime QgsProject::lastSaveDateTime() const
return mSaveDateTime;
}

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

bool QgsProject::isDirty() const
{
return mDirty;
@@ -771,6 +776,7 @@ void QgsProject::clear()
mSaveUser.clear();
mSaveUserFull.clear();
mSaveDateTime = QDateTime();
mSaveVersion = QgsProjectVersion();
mHomePath.clear();
mCachedHomePath.clear();
mAutoTransaction = false;
@@ -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 )
{
@@ -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 );
@@ -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 )
@@ -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 );
@@ -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()
*/
@@ -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
@@ -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 );
}
@@ -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() )
{
@@ -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
{

@@ -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.
@@ -78,6 +100,4 @@ class CORE_EXPORT QgsProjectVersion
QString mName;
};

// clazy:excludeall=qstring-allocations

#endif // QGSPROJECTVERSION_H
@@ -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 );
@@ -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()

0 comments on commit 4dc9b38

Please sign in to comment.
You can’t perform that action at this time.