Skip to content

Commit

Permalink
[ui] FileSaveDialog: Added Validation to the file save process
Browse files Browse the repository at this point in the history
Validating the filename to ensure that the file does not gets saved with just the extension
  • Loading branch information
waaake committed Dec 2, 2024
1 parent 648b095 commit 32a493d
Showing 1 changed file with 53 additions and 0 deletions.
53 changes: 53 additions & 0 deletions meshroom/ui/qml/Application.qml
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,47 @@ Page {
return ""
}

Component {
id: invalidFilepathDialog

MessageDialog {
title: "Invalid Filepath"

required property string filepath

preset: "Warning"
text: "The provided filepath is not valid."
detailedText: "Filepath: " + filepath
helperText: "Please provide a valid filepath to save the file."

standardButtons: Dialog.Ok
onClosed: destroy()
}
}

function validateFilepathForSave(filepath: string, sourceSaveDialog: Dialog): bool {
/**
* Return true if `filepath` is valid for saving a file to disk.
* Otherwise, show a warning dialog and returns false.
* Closing the warning dialog reopens the specified `sourceSaveDialog`, to allow the user to try again.
*/
const emptyFilename = Filepath.basename(filepath).trim() === ".mg";

// Provided filename is valid
if (!emptyFilename) {
return true
}

// Instantiate the Warning Dialog with the provided filepath
const warningDialog = invalidFilepathDialog.createObject(root, {"filepath": Filepath.urlToString(filepath)});

// And open the dialog
warningDialog.closed.connect(sourceSaveDialog.open);
warningDialog.open();

return false
}

// File dialogs
Platform.FileDialog {
id: saveFileDialog
Expand All @@ -71,6 +112,12 @@ Page {
defaultSuffix: ".mg"
fileMode: Platform.FileDialog.SaveFile
onAccepted: {
if (!validateFilepathForSave(currentFile, saveFileDialog))
{
return;
}

// Only save a valid file
_reconstruction.saveAs(currentFile)
MeshroomApp.addRecentProjectFile(currentFile.toString())
closed(Platform.Dialog.Accepted)
Expand All @@ -89,6 +136,12 @@ Page {
defaultSuffix: ".mg"
fileMode: Platform.FileDialog.SaveFile
onAccepted: {
if (!validateFilepathForSave(currentFile, saveTemplateDialog))
{
return;
}

// Only save a valid template
_reconstruction.saveAsTemplate(currentFile)
closed(Platform.Dialog.Accepted)
MeshroomApp.reloadTemplateList()
Expand Down

0 comments on commit 32a493d

Please sign in to comment.