mirror of
https://github.com/AxioDL/PrimeWorldEditor.git
synced 2025-12-17 17:05:37 +00:00
50 lines
1.3 KiB
C++
50 lines
1.3 KiB
C++
#ifndef CFILENAMEVALIDATOR_H
|
|
#define CFILENAMEVALIDATOR_H
|
|
|
|
#include <QValidator>
|
|
|
|
#include "Editor/UICommon.h"
|
|
#include <Common/FileUtil.h>
|
|
|
|
class CFileNameValidator : public QValidator
|
|
{
|
|
bool mIsDirectory;
|
|
|
|
public:
|
|
explicit CFileNameValidator(bool IsDirectory, QObject *pParent = nullptr)
|
|
: QValidator(pParent)
|
|
, mIsDirectory(IsDirectory)
|
|
{}
|
|
|
|
QValidator::State validate(QString& rInput, int&) const override
|
|
{
|
|
auto Out = QValidator::Acceptable;
|
|
|
|
if (!FileUtil::IsValidName(TO_TSTRING(rInput), mIsDirectory))
|
|
{
|
|
// Uh oh, the input is invalid. Only invalid characters will be considered entirely
|
|
// invalid; other errors will be considered intermediate.
|
|
Out = QValidator::Intermediate;
|
|
|
|
for (const auto Chr : rInput)
|
|
{
|
|
if (!FileUtil::IsValidFileNameCharacter(Chr.toLatin1()))
|
|
{
|
|
Out = QValidator::Invalid;
|
|
break;
|
|
}
|
|
}
|
|
}
|
|
|
|
return Out;
|
|
}
|
|
|
|
void fixup(QString& rInput) const override
|
|
{
|
|
const TString Sanitized = FileUtil::SanitizeName(TO_TSTRING(rInput), mIsDirectory);
|
|
rInput = TO_QSTRING(Sanitized);
|
|
}
|
|
};
|
|
|
|
#endif // CFILENAMEVALIDATOR_H
|