PathValidator.cpp 1.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162
  1. /*
  2. * Copyright (c) Contributors to the Open 3D Engine Project.
  3. * For complete copyright and license terms please see the LICENSE at the root of this distribution.
  4. *
  5. * SPDX-License-Identifier: Apache-2.0 OR MIT
  6. *
  7. */
  8. #include "PathValidator.h"
  9. #include <QWidget>
  10. #include <QFileInfo>
  11. #include <QDir>
  12. namespace O3DE::ProjectManager
  13. {
  14. PathValidator::PathValidator(PathMode pathMode, QWidget* parent)
  15. : QValidator(parent)
  16. , m_pathMode(pathMode)
  17. {
  18. }
  19. void PathValidator::setAllowEmpty(bool allowEmpty)
  20. {
  21. m_allowEmpty = allowEmpty;
  22. }
  23. void PathValidator::setPathMode(PathMode pathMode)
  24. {
  25. m_pathMode = pathMode;
  26. }
  27. QValidator::State PathValidator::validate(QString &text, int &) const
  28. {
  29. if(text.isEmpty())
  30. {
  31. return m_allowEmpty ? QValidator::Acceptable : QValidator::Intermediate;
  32. }
  33. QFileInfo pathInfo(text);
  34. if(!pathInfo.dir().exists())
  35. {
  36. return QValidator::Intermediate;
  37. }
  38. switch(m_pathMode)
  39. {
  40. case PathMode::AnyFile://acceptable, as long as it's not an directoy
  41. return pathInfo.isDir() ? QValidator::Intermediate : QValidator::Acceptable;
  42. case PathMode::ExistingFile://must be an existing file
  43. return pathInfo.exists() && pathInfo.isFile() ? QValidator::Acceptable : QValidator::Intermediate;
  44. case PathMode::ExistingFolder://must be an existing folder
  45. return pathInfo.exists() && pathInfo.isDir() ? QValidator::Acceptable : QValidator::Intermediate;
  46. default:
  47. Q_UNREACHABLE();
  48. }
  49. return QValidator::Invalid;
  50. }
  51. } // namespace O3DE::ProjectManager