Skip to content

Commit 32a493d

Browse files
committed
[ui] FileSaveDialog: Added Validation to the file save process
Validating the filename to ensure that the file does not gets saved with just the extension
1 parent 648b095 commit 32a493d

File tree

1 file changed

+53
-0
lines changed

1 file changed

+53
-0
lines changed

meshroom/ui/qml/Application.qml

Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,47 @@ Page {
5959
return ""
6060
}
6161

62+
Component {
63+
id: invalidFilepathDialog
64+
65+
MessageDialog {
66+
title: "Invalid Filepath"
67+
68+
required property string filepath
69+
70+
preset: "Warning"
71+
text: "The provided filepath is not valid."
72+
detailedText: "Filepath: " + filepath
73+
helperText: "Please provide a valid filepath to save the file."
74+
75+
standardButtons: Dialog.Ok
76+
onClosed: destroy()
77+
}
78+
}
79+
80+
function validateFilepathForSave(filepath: string, sourceSaveDialog: Dialog): bool {
81+
/**
82+
* Return true if `filepath` is valid for saving a file to disk.
83+
* Otherwise, show a warning dialog and returns false.
84+
* Closing the warning dialog reopens the specified `sourceSaveDialog`, to allow the user to try again.
85+
*/
86+
const emptyFilename = Filepath.basename(filepath).trim() === ".mg";
87+
88+
// Provided filename is valid
89+
if (!emptyFilename) {
90+
return true
91+
}
92+
93+
// Instantiate the Warning Dialog with the provided filepath
94+
const warningDialog = invalidFilepathDialog.createObject(root, {"filepath": Filepath.urlToString(filepath)});
95+
96+
// And open the dialog
97+
warningDialog.closed.connect(sourceSaveDialog.open);
98+
warningDialog.open();
99+
100+
return false
101+
}
102+
62103
// File dialogs
63104
Platform.FileDialog {
64105
id: saveFileDialog
@@ -71,6 +112,12 @@ Page {
71112
defaultSuffix: ".mg"
72113
fileMode: Platform.FileDialog.SaveFile
73114
onAccepted: {
115+
if (!validateFilepathForSave(currentFile, saveFileDialog))
116+
{
117+
return;
118+
}
119+
120+
// Only save a valid file
74121
_reconstruction.saveAs(currentFile)
75122
MeshroomApp.addRecentProjectFile(currentFile.toString())
76123
closed(Platform.Dialog.Accepted)
@@ -89,6 +136,12 @@ Page {
89136
defaultSuffix: ".mg"
90137
fileMode: Platform.FileDialog.SaveFile
91138
onAccepted: {
139+
if (!validateFilepathForSave(currentFile, saveTemplateDialog))
140+
{
141+
return;
142+
}
143+
144+
// Only save a valid template
92145
_reconstruction.saveAsTemplate(currentFile)
93146
closed(Platform.Dialog.Accepted)
94147
MeshroomApp.reloadTemplateList()

0 commit comments

Comments
 (0)