ProjectSettingsScreen.cpp 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163
  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 <ProjectSettingsScreen.h>
  9. #include <FormFolderBrowseEditWidget.h>
  10. #include <FormLineEditWidget.h>
  11. #include <PathValidator.h>
  12. #include <PythonBindingsInterface.h>
  13. #include <QFileDialog>
  14. #include <QFrame>
  15. #include <QHBoxLayout>
  16. #include <QVBoxLayout>
  17. #include <QLineEdit>
  18. #include <QStandardPaths>
  19. #include <QScrollArea>
  20. namespace O3DE::ProjectManager
  21. {
  22. ProjectSettingsScreen::ProjectSettingsScreen(QWidget* parent)
  23. : ScreenWidget(parent)
  24. {
  25. m_horizontalLayout = new QHBoxLayout(this);
  26. m_horizontalLayout->setAlignment(Qt::AlignLeft);
  27. m_horizontalLayout->setContentsMargins(0, 0, 0, 0);
  28. // if we don't provide a parent for this box layout the stylesheet doesn't take
  29. // if we don't set this in a frame (just use a sub-layout) all the content will align incorrectly horizontally
  30. QFrame* projectSettingsFrame = new QFrame(this);
  31. projectSettingsFrame->setObjectName("projectSettings");
  32. QScrollArea* scrollArea = new QScrollArea(this);
  33. scrollArea->setWidgetResizable(true);
  34. QWidget* scrollWidget = new QWidget(this);
  35. scrollArea->setWidget(scrollWidget);
  36. m_verticalLayout = new QVBoxLayout();
  37. m_verticalLayout->setMargin(0);
  38. m_verticalLayout->setAlignment(Qt::AlignTop);
  39. scrollWidget->setLayout(m_verticalLayout);
  40. m_projectName = new FormLineEditWidget(tr("Project name"), "", this);
  41. connect(m_projectName->lineEdit(), &QLineEdit::textChanged, this, &ProjectSettingsScreen::OnProjectNameUpdated);
  42. m_verticalLayout->addWidget(m_projectName);
  43. m_projectVersion = new FormLineEditWidget(tr("Project version"), "1.0.0", this);
  44. m_verticalLayout->addWidget(m_projectVersion);
  45. m_projectPath = new FormFolderBrowseEditWidget(tr("Project Location"), "", this);
  46. connect(m_projectPath->lineEdit(), &QLineEdit::textChanged, this, &ProjectSettingsScreen::OnProjectPathUpdated);
  47. m_verticalLayout->addWidget(m_projectPath);
  48. projectSettingsFrame->setLayout(m_verticalLayout);
  49. m_horizontalLayout->addWidget(projectSettingsFrame);
  50. setLayout(m_horizontalLayout);
  51. }
  52. ProjectManagerScreen ProjectSettingsScreen::GetScreenEnum()
  53. {
  54. return ProjectManagerScreen::Invalid;
  55. }
  56. QString ProjectSettingsScreen::GetDefaultProjectPath()
  57. {
  58. QString defaultPath = QStandardPaths::writableLocation(QStandardPaths::DocumentsLocation);
  59. AZ::Outcome<EngineInfo> engineInfoResult = PythonBindingsInterface::Get()->GetEngineInfo();
  60. if (engineInfoResult.IsSuccess())
  61. {
  62. QDir path(QDir::toNativeSeparators(engineInfoResult.GetValue().m_defaultProjectsFolder));
  63. if (path.exists())
  64. {
  65. defaultPath = path.absolutePath();
  66. }
  67. }
  68. return defaultPath;
  69. }
  70. ProjectInfo ProjectSettingsScreen::GetProjectInfo()
  71. {
  72. ProjectInfo projectInfo;
  73. projectInfo.m_projectName = m_projectName->lineEdit()->text();
  74. projectInfo.m_version = m_projectVersion->lineEdit()->text();
  75. // currently we don't have separate fields for changing the project name and display name
  76. projectInfo.m_displayName = projectInfo.m_projectName;
  77. projectInfo.m_path = m_projectPath->lineEdit()->text();
  78. return projectInfo;
  79. }
  80. bool ProjectSettingsScreen::ValidateProjectName() const
  81. {
  82. bool projectNameIsValid = true;
  83. if (m_projectName->lineEdit()->text().isEmpty())
  84. {
  85. projectNameIsValid = false;
  86. m_projectName->setErrorLabelText(tr("Please provide a project name."));
  87. }
  88. else
  89. {
  90. // this validation should roughly match the utils.validate_identifier which the cli
  91. // uses to validate project names
  92. QRegExp validProjectNameRegex("[A-Za-z][A-Za-z0-9_-]{0,63}");
  93. const bool result = validProjectNameRegex.exactMatch(m_projectName->lineEdit()->text());
  94. if (!result)
  95. {
  96. projectNameIsValid = false;
  97. m_projectName->setErrorLabelText(
  98. tr("Project names must start with a letter and consist of up to 64 letter, number, '_' or '-' characters"));
  99. }
  100. }
  101. m_projectName->setErrorLabelVisible(!projectNameIsValid);
  102. return projectNameIsValid;
  103. }
  104. bool ProjectSettingsScreen::ValidateProjectPath() const
  105. {
  106. bool projectPathIsValid = true;
  107. QDir path(m_projectPath->lineEdit()->text());
  108. if (!path.isAbsolute())
  109. {
  110. projectPathIsValid = false;
  111. m_projectPath->setErrorLabelText(tr("Please provide an absolute path for the project location."));
  112. }
  113. else if (path.exists() && !path.isEmpty())
  114. {
  115. projectPathIsValid = false;
  116. m_projectPath->setErrorLabelText(tr("This folder exists and isn't empty. Please choose a different location."));
  117. }
  118. m_projectPath->setErrorLabelVisible(!projectPathIsValid);
  119. return projectPathIsValid;
  120. }
  121. void ProjectSettingsScreen::OnProjectNameUpdated()
  122. {
  123. ValidateProjectName();
  124. }
  125. void ProjectSettingsScreen::OnProjectPathUpdated()
  126. {
  127. ValidateProjectName() && ValidateProjectPath();
  128. }
  129. AZ::Outcome<void, QString> ProjectSettingsScreen::Validate() const
  130. {
  131. if (ValidateProjectName() && ValidateProjectPath())
  132. {
  133. return AZ::Success();
  134. }
  135. // Returning empty string to use the default error message
  136. return AZ::Failure<QString>("");
  137. }
  138. } // namespace O3DE::ProjectManager