Compare last saved date before saving to prevent overwrite

This commit is contained in:
Aurore LAFAURIE 2024-05-27 16:43:00 +02:00
parent bc139d13e7
commit e63c2df2a8
3 changed files with 56 additions and 1 deletions

View file

@ -223,6 +223,7 @@ class Graph(BaseObject):
self._compatibilityNodes = DictModel(keyAttrName='name', parent=self) self._compatibilityNodes = DictModel(keyAttrName='name', parent=self)
self.cacheDir = meshroom.core.defaultCacheFolder self.cacheDir = meshroom.core.defaultCacheFolder
self._filepath = '' self._filepath = ''
self._fileDateVersion = 0
self.header = {} self.header = {}
def clear(self): def clear(self):
@ -267,6 +268,8 @@ class Graph(BaseObject):
if not isinstance(graphData, dict): if not isinstance(graphData, dict):
raise RuntimeError('loadGraph error: Graph is not a dict. File: {}'.format(filepath)) raise RuntimeError('loadGraph error: Graph is not a dict. File: {}'.format(filepath))
self._fileDateVersion = os.path.getmtime(filepath)
self.header = fileData.get(Graph.IO.Keys.Header, {}) self.header = fileData.get(Graph.IO.Keys.Header, {})
nodesVersions = self.header.get(Graph.IO.Keys.NodesVersions, {}) nodesVersions = self.header.get(Graph.IO.Keys.NodesVersions, {})
@ -1539,6 +1542,18 @@ class Graph(BaseObject):
self.updateStatusFromCache(force=True) self.updateStatusFromCache(force=True)
self.cacheDirChanged.emit() self.cacheDirChanged.emit()
@property
def fileDateVersion(self):
return self._fileDateVersion
@fileDateVersion.setter
def fileDateVersion(self, value):
self._fileDateVersion = value
@Slot(str, result=float)
def getFileDateVersionFromPath(self, value):
return os.path.getmtime(value)
def setVerbose(self, v): def setVerbose(self, v):
with GraphModification(self): with GraphModification(self):
for node in self._nodes: for node in self._nodes:
@ -1553,6 +1568,7 @@ class Graph(BaseObject):
filepathChanged = Signal() filepathChanged = Signal()
filepath = Property(str, lambda self: self._filepath, notify=filepathChanged) filepath = Property(str, lambda self: self._filepath, notify=filepathChanged)
fileReleaseVersion = Property(str, lambda self: self.header.get(Graph.IO.Keys.ReleaseVersion, "0.0"), notify=filepathChanged) fileReleaseVersion = Property(str, lambda self: self.header.get(Graph.IO.Keys.ReleaseVersion, "0.0"), notify=filepathChanged)
fileDateVersion = Property(float, fileDateVersion.fget, fileDateVersion.fset, notify=filepathChanged)
cacheDirChanged = Signal() cacheDirChanged = Signal()
cacheDir = Property(str, cacheDir.fget, cacheDir.fset, notify=cacheDirChanged) cacheDir = Property(str, cacheDir.fget, cacheDir.fset, notify=cacheDirChanged)
updated = Signal() updated = Signal()

View file

@ -368,6 +368,36 @@ ApplicationWindow {
onDiscarded: close() onDiscarded: close()
onAccepted: saveAsAction.trigger() onAccepted: saveAsAction.trigger()
} }
MessageDialog {
id: fileModifiedDialog
canCopy: false
icon.text: MaterialIcons.warning
parent: Overlay.overlay
preset: "Warning"
title: "File Modified"
text: "The file has been modified by another instance."
detailedText: "Do you want to overwrite the file?"
// Add a reload file button next to the save button
footer: DialogButtonBox {
position: DialogButtonBox.Footer
standardButtons: Dialog.Save | Dialog.Cancel
Button {
text: "Reload File"
onClicked: {
_reconstruction.loadUrl(_reconstruction.graph.filepath)
fileModifiedDialog.close()
}
}
}
onAccepted: _reconstruction.save()
onDiscarded: close()
}
} }
FileDialog { FileDialog {
@ -715,7 +745,14 @@ ApplicationWindow {
enabled: _reconstruction ? (_reconstruction.graph && !_reconstruction.graph.filepath) || !_reconstruction.undoStack.clean : false enabled: _reconstruction ? (_reconstruction.graph && !_reconstruction.graph.filepath) || !_reconstruction.undoStack.clean : false
onTriggered: { onTriggered: {
if (_reconstruction.graph.filepath) { if (_reconstruction.graph.filepath) {
_reconstruction.save() // get current time date
var date = _reconstruction.graph.getFileDateVersionFromPath(_reconstruction.graph.filepath)
// check if the file has been modified by another instance
if (_reconstruction.graph.fileDateVersion !== date) {
fileModifiedDialog.open()
} else
_reconstruction.save()
} else { } else {
initFileDialogFolder(saveFileDialog) initFileDialogFolder(saveFileDialog)
saveFileDialog.open() saveFileDialog.open()

View file

@ -509,6 +509,8 @@ class Reconstruction(UIGraph):
"Data might have been lost in the process.", "Data might have been lost in the process.",
"Open it with the corresponding version of Meshroom to recover your data." "Open it with the corresponding version of Meshroom to recover your data."
)) ))
self.graph.fileDateVersion = os.path.getmtime(filepath)
return status return status
except FileNotFoundError as e: except FileNotFoundError as e:
self.error.emit( self.error.emit(