Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
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 1d99584
Show file tree
Hide file tree
Showing 5 changed files with 73 additions and 5 deletions.
28 changes: 28 additions & 0 deletions python/core/auto_generated/qgsproject.sip.in
Expand Up @@ -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/;
Expand Down
6 changes: 3 additions & 3 deletions src/app/qgisapp.cpp
Expand Up @@ -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();

Expand Down Expand Up @@ -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();
Expand Down
4 changes: 2 additions & 2 deletions src/app/qgsprojectproperties.cpp
Expand Up @@ -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, [ = ]
Expand Down Expand Up @@ -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 );
Expand Down
12 changes: 12 additions & 0 deletions src/core/qgsproject.cpp
Expand Up @@ -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 );
Expand Down Expand Up @@ -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();
Expand Down
28 changes: 28 additions & 0 deletions src/core/qgsproject.h
Expand Up @@ -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.
*
Expand Down Expand Up @@ -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.

Expand Down

0 comments on commit 1d99584

Please sign in to comment.