Implemented helper method of QFileDialog::getSaveFileName to prevent user from saving files with invalid names.
Signed-off-by: Chris Galvan <chgalvan@amazon.com>monroegm-disable-blank-issue-2
parent
9e5ef08229
commit
d590a91fe7
@ -0,0 +1,53 @@
|
||||
/*
|
||||
* Copyright (c) Contributors to the Open 3D Engine Project.
|
||||
* For complete copyright and license terms please see the LICENSE at the root of this distribution.
|
||||
*
|
||||
* SPDX-License-Identifier: Apache-2.0 OR MIT
|
||||
*
|
||||
*/
|
||||
|
||||
#include <AzQtComponents/Components/Widgets/FileDialog.h>
|
||||
|
||||
#include <QMessageBox>
|
||||
#include <QRegExp>
|
||||
|
||||
namespace AzQtComponents
|
||||
{
|
||||
QString FileDialog::GetSaveFileName(QWidget* parent, const QString& caption, const QString& dir,
|
||||
const QString& filter, QString* selectedFilter, QFileDialog::Options options)
|
||||
{
|
||||
bool shouldPromptAgain = false;
|
||||
QString filePath;
|
||||
|
||||
do
|
||||
{
|
||||
// Trigger Qt's save filename dialog
|
||||
// If filePath isn't empty, it means we are prompting again because the filename was invalid,
|
||||
// so pass it instead of the directory so the filename is pre-filled in for the user
|
||||
filePath = QFileDialog::getSaveFileName(parent, caption, (filePath.isEmpty()) ? dir : filePath, filter, selectedFilter, options);
|
||||
|
||||
if (!filePath.isEmpty())
|
||||
{
|
||||
QFileInfo fileInfo(filePath);
|
||||
QString fileName = fileInfo.fileName();
|
||||
|
||||
// Check if the filename has any invalid characters
|
||||
QRegExp validFileNameRegex("^[a-zA-Z0-9_\\-./]*$");
|
||||
shouldPromptAgain = !validFileNameRegex.exactMatch(fileName);
|
||||
|
||||
// If the filename had invalid characters, then show a warning message and then we will re-prompt the save filename dialog
|
||||
if (shouldPromptAgain)
|
||||
{
|
||||
QMessageBox::warning(parent, QObject::tr("Invalid filename"), QObject::tr("The filename contains invalid characters\n\n%1").arg(fileName));
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
// If the filePath is empty, then the user cancelled the dialog so we don't need to prompt again
|
||||
shouldPromptAgain = false;
|
||||
}
|
||||
} while (shouldPromptAgain);
|
||||
|
||||
return filePath;
|
||||
}
|
||||
} // namespace AzQtComponents
|
||||
@ -0,0 +1,29 @@
|
||||
/*
|
||||
* Copyright (c) Contributors to the Open 3D Engine Project.
|
||||
* For complete copyright and license terms please see the LICENSE at the root of this distribution.
|
||||
*
|
||||
* SPDX-License-Identifier: Apache-2.0 OR MIT
|
||||
*
|
||||
*/
|
||||
|
||||
#pragma once
|
||||
|
||||
#include <AzQtComponents/AzQtComponentsAPI.h>
|
||||
|
||||
#if !defined(Q_MOC_RUN)
|
||||
#include <QFileDialog>
|
||||
#endif
|
||||
|
||||
namespace AzQtComponents
|
||||
{
|
||||
class AZ_QT_COMPONENTS_API FileDialog
|
||||
{
|
||||
public:
|
||||
//! Helper method that extends QFileDialog::getSaveFileName to prevent the user from
|
||||
//! saving a filename with invalid characters (e.g. AP doesn't allow @ characters because they are used for aliases)
|
||||
static QString GetSaveFileName(QWidget* parent = nullptr, const QString& caption = QString(),
|
||||
const QString& dir = QString(), const QString& filter = QString(),
|
||||
QString* selectedFilter = nullptr, QFileDialog::Options options = QFileDialog::Options());
|
||||
};
|
||||
|
||||
} // namespace AzQtComponents
|
||||
Loading…
Reference in New Issue