ProjectSettingsScreen.cpp 5.6 KB

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