RobotImporterWidgetUtils.cpp 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109
  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 "RobotImporterWidgetUtils.h"
  9. #include <QFileDialog>
  10. #include <QMessageBox>
  11. #include <QWidget>
  12. #include <AzCore/Utils/Utils.h>
  13. namespace ROS2::RobotImporterWidgetUtils
  14. {
  15. namespace Internal
  16. {
  17. AZStd::optional<QString> QueryUserForPath(
  18. const AZStd::string& extensionDescription, QFileDialog::FileMode mode, QWidget* parent = nullptr)
  19. {
  20. QFileDialog importFileDialog(parent);
  21. importFileDialog.setDirectory(AZ::Utils::GetProjectPath().c_str());
  22. importFileDialog.setFileMode(mode);
  23. importFileDialog.setNameFilter(QObject::tr(extensionDescription.c_str()));
  24. importFileDialog.setViewMode(QFileDialog::Detail);
  25. int result = importFileDialog.exec();
  26. if (result != QDialog::DialogCode::Accepted)
  27. {
  28. return AZStd::nullopt;
  29. }
  30. return importFileDialog.selectedFiles().first();
  31. }
  32. ExistingPrefabAction QueryUserForExistingPrefabAction(QWidget* parent = nullptr)
  33. {
  34. QMessageBox msgBox(parent);
  35. msgBox.setWindowTitle(QObject::tr("Prefab file exists"));
  36. msgBox.setText(QObject::tr("The prefab file already exists."));
  37. msgBox.setInformativeText(QObject::tr("Do you want to overwrite it or save it with another file name?"));
  38. msgBox.setStandardButtons(QMessageBox::Save | QMessageBox::Discard | QMessageBox::Cancel);
  39. msgBox.setDefaultButton(QMessageBox::Discard);
  40. msgBox.setButtonText(QMessageBox::Save, QObject::tr("Overwrite"));
  41. msgBox.setButtonText(QMessageBox::Discard, QObject::tr("Save As..."));
  42. switch (msgBox.exec())
  43. {
  44. case QMessageBox::Save:
  45. return ExistingPrefabAction::Overwrite;
  46. case QMessageBox::Discard:
  47. return ExistingPrefabAction::CreateWithNewName;
  48. default:
  49. return ExistingPrefabAction::Cancel;
  50. }
  51. }
  52. } // namespace Internal
  53. AZStd::optional<AZStd::string> QueryUserForURDFPath(QWidget* parent)
  54. {
  55. const auto path = Internal::QueryUserForPath("Unified Robot Description Format (*.urdf)", QFileDialog::ExistingFiles);
  56. if (!path)
  57. {
  58. return AZStd::nullopt;
  59. }
  60. if (path->isEmpty())
  61. {
  62. QMessageBox::critical(parent, QObject::tr("Empty path provided"), QObject::tr("No path was provided. Please try again"));
  63. return QueryUserForURDFPath();
  64. }
  65. if (!QFile::exists(path.value()))
  66. {
  67. QMessageBox::critical(parent, QObject::tr("Does not exist"), QObject::tr("File does not exist. Please try again"));
  68. return QueryUserForURDFPath();
  69. }
  70. return path->toUtf8().constData();
  71. }
  72. AZStd::optional<AZ::IO::Path> ValidatePrefabPathExistenceAndQueryUserForNewIfNecessary(const AZ::IO::Path& path, QWidget* parent)
  73. {
  74. if (!QFile::exists(path.c_str()))
  75. {
  76. return path;
  77. }
  78. switch (Internal::QueryUserForExistingPrefabAction(parent))
  79. {
  80. case ExistingPrefabAction::Cancel:
  81. return AZStd::nullopt;
  82. case ExistingPrefabAction::Overwrite:
  83. return path;
  84. case ExistingPrefabAction::CreateWithNewName:
  85. // I am aware that similar functionality might be available by QFileDialog::setAcceptMode
  86. // However, the prompt to confirm the overwrite showed up under the file selection dialog, which made a terrible UX
  87. // TODO: It should be fixed at some point in the future
  88. AZStd::optional<QString> newPathCandidate = Internal::QueryUserForPath("Prefab (*.prefab)", QFileDialog::AnyFile);
  89. if (!newPathCandidate || newPathCandidate->isEmpty())
  90. {
  91. return AZStd::nullopt;
  92. }
  93. return ValidatePrefabPathExistenceAndQueryUserForNewIfNecessary(newPathCandidate.value().toStdString().c_str());
  94. }
  95. }
  96. } // namespace ROS2::RobotImporterWidgetUtils