From 0649d7f50c0d46445f4294f17c1b65fdaa30eb8a Mon Sep 17 00:00:00 2001 From: Mathieu Pellerin Date: Sun, 1 Dec 2024 14:14:42 +0700 Subject: [PATCH 1/2] When renaming files, allow for overwriting preexisting destination file --- src/core/platforms/platformutilities.cpp | 25 ++++++++++++++++++------ src/core/platforms/platformutilities.h | 2 +- 2 files changed, 20 insertions(+), 7 deletions(-) diff --git a/src/core/platforms/platformutilities.cpp b/src/core/platforms/platformutilities.cpp index f9a1456e51..283fb00916 100644 --- a/src/core/platforms/platformutilities.cpp +++ b/src/core/platforms/platformutilities.cpp @@ -183,14 +183,27 @@ bool PlatformUtilities::rmFile( const QString &filename ) const return file.remove( filename ); } -bool PlatformUtilities::renameFile( const QString &filename, const QString &newname ) const +bool PlatformUtilities::renameFile( const QString &oldFilePath, const QString &newFilePath, bool overwrite ) const { - QFileInfo fi( newname ); - QDir dir( fi.absolutePath() ); - dir.mkpath( fi.absolutePath() ); + QFileInfo oldFi( oldFilePath ); + QFileInfo newFi( newFilePath ); + if ( oldFi.absoluteFilePath() == newFi.absoluteFilePath() ) + { + return true; + } - QFile file( filename ); - return file.rename( newname ); + // Insure the path exists + QDir dir( newFi.absolutePath() ); + dir.mkpath( newFi.absolutePath() ); + + // If the renamed file exists, overwrite + if ( newFi.exists() && overwrite ) + { + QFile newfile( newFilePath ); + newfile.remove(); + } + + return QFile::rename( oldFilePath, newFilePath ); } QString PlatformUtilities::applicationDirectory() const diff --git a/src/core/platforms/platformutilities.h b/src/core/platforms/platformutilities.h index b864be94e1..357e99f7ba 100644 --- a/src/core/platforms/platformutilities.h +++ b/src/core/platforms/platformutilities.h @@ -116,7 +116,7 @@ class QFIELD_CORE_EXPORT PlatformUtilities : public QObject // TODO: move these functions to fileutils. Make sure to adjust any qml code relying on this. Q_INVOKABLE bool createDir( const QString &path, const QString &dirname ) const; Q_INVOKABLE bool rmFile( const QString &filename ) const; - Q_INVOKABLE bool renameFile( const QString &filename, const QString &newname ) const; + Q_INVOKABLE bool renameFile( const QString &oldFilePath, const QString &newFilePath, bool overwrite = true ) const; /** From d94d43551aee05bbb4cb5945ad221d0f9a54441f Mon Sep 17 00:00:00 2001 From: Mathieu Pellerin Date: Sun, 1 Dec 2024 14:15:01 +0700 Subject: [PATCH 2/2] Insure sketched photos are refreshed --- src/qml/editorwidgets/ExternalResource.qml | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/src/qml/editorwidgets/ExternalResource.qml b/src/qml/editorwidgets/ExternalResource.qml index ec7af801ec..5703a4edc4 100644 --- a/src/qml/editorwidgets/ExternalResource.qml +++ b/src/qml/editorwidgets/ExternalResource.qml @@ -347,6 +347,10 @@ EditorWidgetBase { filepath = filepath.replace('{filename}', FileUtils.fileName(path)); filepath = filepath.replace('{extension}', FileUtils.fileSuffix(path)); platformUtilities.renameFile(path, prefixToRelativePath + filepath); + + // In order to insure an edited image gets refreshed in the feature form, reset the source + image.source = ''; + image.source = UrlUtils.fromString(prefixToRelativePath + filepath); valueChangeRequested(filepath, false); enabled = false; }