Skip to content
Permalink
Browse files

Add API to set an "original path" for QgsProject

This is intended for use with non-qgs/qgz project files
(see QgsCustomProjectOpenHandler) in order to allow custom project open handlers
to specify the original file name of the project. For custom project formats,
it is NOT appropriate to call setFileName() with the original project path, as
this causes the original (non QGIS) project file to be overwritten when the
project is next saved.
  • Loading branch information
nyalldawson committed Jun 4, 2020
1 parent 5b784e5 commit 1d995840ec4a855d2be6611f2f514d5c268660b9
@@ -143,6 +143,34 @@ representation.
.. seealso:: :py:func:`setFileName`

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

void setOriginalPath( const QString &path );
%Docstring
Sets the original ``path`` associated with the project.

This is intended for use with non-qgs/qgz project files (see :py:class:`QgsCustomProjectOpenHandler`) in order to allow
custom project open handlers to specify the original file name of the project. For custom project formats,
it is NOT appropriate to call setFileName() with the original project path, as this causes the original (non
QGIS) project file to be overwritten when the project is next saved.

.. seealso:: :py:func:`originalPath`

.. versionadded:: 3.14
%End

QString originalPath() const;
%Docstring
Returns the original path associated with the project.

This is intended for use with non-qgs/qgz project files (see :py:class:`QgsCustomProjectOpenHandler`) in order to allow
custom project open handlers to specify the original file name of the project. For custom project formats,
it is NOT appropriate to call setFileName() with the original project path, as this causes the original (non
QGIS) project file to be overwritten when the project is next saved.

.. seealso:: :py:func:`setOriginalPath`

.. versionadded:: 3.14
%End

QFileInfo fileInfo() const /Deprecated/;
@@ -4950,10 +4950,10 @@ void QgisApp::saveRecentProjectPath( bool savePreviewImage, const QIcon &iconOve
return;

if ( projectData.path.isEmpty() ) // in case of custom project storage
projectData.path = QgsProject::instance()->fileName();
projectData.path = !QgsProject::instance()->fileName().isEmpty() ? QgsProject::instance()->fileName() : QgsProject::instance()->originalPath();
projectData.title = QgsProject::instance()->title();
if ( projectData.title.isEmpty() )
projectData.title = QgsProject::instance()->baseName();
projectData.title = !QgsProject::instance()->baseName().isEmpty() ? QgsProject::instance()->baseName() : QFileInfo( QgsProject::instance()->originalPath() ).completeBaseName();

projectData.crs = QgsProject::instance()->crs().authid();

@@ -6968,7 +6968,7 @@ bool QgisApp::addProject( const QString &projectFile )
// if a custom handler was used, then we generate a thumbnail
if ( !usedCustomHandler || !customHandlerWantsThumbnail )
saveRecentProjectPath( false );
else if ( !QgsProject::instance()->fileName().isEmpty() )
else if ( !QgsProject::instance()->originalPath().isEmpty() )
{
// we have to delay the thumbnail creation until after the canvas has refreshed for the first time
QMetaObject::Connection *connection = new QMetaObject::Connection();
@@ -246,7 +246,7 @@ QgsProjectProperties::QgsProjectProperties( QgsMapCanvas *mapCanvas, QWidget *pa

mAutoTransaction->setChecked( QgsProject::instance()->autoTransaction() );
title( QgsProject::instance()->title() );
mProjectFileLineEdit->setText( QDir::toNativeSeparators( QgsProject::instance()->fileName() ) );
mProjectFileLineEdit->setText( QDir::toNativeSeparators( !QgsProject::instance()->fileName().isEmpty() ? QgsProject::instance()->fileName() : QgsProject::instance()->originalPath() ) );
mProjectHomeLineEdit->setShowClearButton( true );
mProjectHomeLineEdit->setText( QDir::toNativeSeparators( QgsProject::instance()->presetHomePath() ) );
connect( mButtonSetProjectHome, &QToolButton::clicked, this, [ = ]
@@ -286,7 +286,7 @@ QgsProjectProperties::QgsProjectProperties( QgsMapCanvas *mapCanvas, QWidget *pa
connect( mButtonOpenProjectFolder, &QToolButton::clicked, this, [ = ]
{
QString path;
QString project = QgsProject::instance()->fileName();
QString project = !QgsProject::instance()->fileName().isEmpty() ? QgsProject::instance()->fileName() : QgsProject::instance()->originalPath();
QgsProjectStorage *storage = QgsProject::instance()->projectStorage();
if ( storage )
path = storage->filePath( project );
@@ -628,6 +628,16 @@ QString QgsProject::fileName() const
return mFile.fileName();
}

void QgsProject::setOriginalPath( const QString &path )
{
mOriginalPath = path;
}

QString QgsProject::originalPath() const
{
return mOriginalPath;
}

QFileInfo QgsProject::fileInfo() const
{
return QFileInfo( mFile );
@@ -1805,6 +1815,8 @@ QgsExpressionContextScope *QgsProject::createExpressionContextScope() const
}

QString projectPath = projectStorage() ? fileName() : absoluteFilePath();
if ( projectPath.isEmpty() )
projectPath = mOriginalPath;
QString projectFolder = QFileInfo( projectPath ).path();
QString projectFilename = QFileInfo( projectPath ).fileName();
QString projectBasename = baseName();
@@ -229,6 +229,32 @@ class CORE_EXPORT QgsProject : public QObject, public QgsExpressionContextGenera
*/
QString fileName() const;

/**
* Sets the original \a path associated with the project.
*
* This is intended for use with non-qgs/qgz project files (see QgsCustomProjectOpenHandler) in order to allow
* custom project open handlers to specify the original file name of the project. For custom project formats,
* it is NOT appropriate to call setFileName() with the original project path, as this causes the original (non
* QGIS) project file to be overwritten when the project is next saved.
*
* \see originalPath()
* \since QGIS 3.14
*/
void setOriginalPath( const QString &path );

/**
* Returns the original path associated with the project.
*
* This is intended for use with non-qgs/qgz project files (see QgsCustomProjectOpenHandler) in order to allow
* custom project open handlers to specify the original file name of the project. For custom project formats,
* it is NOT appropriate to call setFileName() with the original project path, as this causes the original (non
* QGIS) project file to be overwritten when the project is next saved.
* \see setOriginalPath()
* \since QGIS 3.14
*/
QString originalPath() const;

/**
* Returns QFileInfo object for the project's associated file.
*
@@ -1901,6 +1927,8 @@ class CORE_EXPORT QgsProject : public QObject, public QgsExpressionContextGenera

QFile mFile; // current physical project file

QString mOriginalPath;

QString mSaveUser; // last saved user.
QString mSaveUserFull; // last saved user full name.

0 comments on commit 1d99584

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