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 Nov 26, 2024
1 parent 648b095 commit af2c19e
Showing 1 changed file with 60 additions and 0 deletions.
60 changes: 60 additions & 0 deletions meshroom/ui/qml/Application.qml
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,48 @@ Page {
return ""
}

Dialog {
id: invalidFileDialog
title: "Invalid File"

// The dialog to reopen when user clicks 'OK'
property var redial: null

x: parent.width / 2 - width / 2
y: parent.height / 2 - height / 2
height: 120

// What appears in the dialog
Row {
MaterialLabel { text: MaterialIcons.warning; color: "#FF9800"; font.pointSize: 30 }
Label { width: 20 } // A kind of spacer

Column {
Label { text: "The filepath provided is not valid. Please provide a valid file path/name to save the file." }
Label { id: message; text: ""; color: "#FF9800" }
Label { text: "Click OK to return to File Dialog, Cancel to cancel the save process." }
}
}

// Standard buttons for the Dialog
standardButtons: Dialog.Ok | Dialog.Cancel

onAccepted: {
if (redial) redial.open()
}
onRejected: {
if (redial) redial.closed(Platform.Dialog.Rejected)
}

function show(filepath) {
// Update the message which is displayed on the error dialog
message.text = "Provided Path: " + filepath

// open the dialog
this.open()
}
}

// File dialogs
Platform.FileDialog {
id: saveFileDialog
Expand All @@ -71,6 +113,15 @@ Page {
defaultSuffix: ".mg"
fileMode: Platform.FileDialog.SaveFile
onAccepted: {
// Validate if the filename without any spaces is just .mg ?
if (Filepath.basename(currentFile).trim() === ".mg") {
// If that's the case then show an error to the user about the filepath being wrong
invalidFileDialog.redial = saveFileDialog
invalidFileDialog.show(Filepath.urlToString(currentFile))
return
}

// Only save a valid file
_reconstruction.saveAs(currentFile)
MeshroomApp.addRecentProjectFile(currentFile.toString())
closed(Platform.Dialog.Accepted)
Expand All @@ -89,6 +140,15 @@ Page {
defaultSuffix: ".mg"
fileMode: Platform.FileDialog.SaveFile
onAccepted: {
// Validate if the filename without any spaces is just .mg ?
if (Filepath.basename(currentFile).trim() === ".mg") {
// If that's the case then show an error to the user about the filepath being wrong
invalidFileDialog.redial = saveTemplateDialog
invalidFileDialog.show(Filepath.urlToString(currentFile))
return
}

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

0 comments on commit af2c19e

Please sign in to comment.