AddRemoteProjectDialog.cpp 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272
  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 <AddRemoteProjectDialog.h>
  9. #include <FormFolderBrowseEditWidget.h>
  10. #include <TextOverflowWidget.h>
  11. #include <AzQtComponents/Components/Widgets/CheckBox.h>
  12. #include <ProjectUtils.h>
  13. #include <PythonBindingsInterface.h>
  14. #include <QVBoxLayout>
  15. #include <QGridLayout>
  16. #include <QLabel>
  17. #include <QLineEdit>
  18. #include <QCheckBox>
  19. #include <QDialogButtonBox>
  20. #include <QPushButton>
  21. #include <QDir>
  22. #include <QTimer>
  23. namespace O3DE::ProjectManager
  24. {
  25. AddRemoteProjectDialog::AddRemoteProjectDialog(QWidget* parent)
  26. : QDialog(parent)
  27. {
  28. setWindowTitle(tr("Add a remote project"));
  29. setModal(true);
  30. setObjectName("addRemoteProjectDialog");
  31. setFixedSize(QSize(760, 600));
  32. QVBoxLayout* vLayout = new QVBoxLayout();
  33. vLayout->setContentsMargins(30, 30, 25, 10);
  34. vLayout->setSpacing(0);
  35. vLayout->setAlignment(Qt::AlignTop);
  36. setLayout(vLayout);
  37. QLabel* instructionTitleLabel = new QLabel(tr("Please enter a remote URL for your project"), this);
  38. instructionTitleLabel->setObjectName("remoteProjectDialogInstructionTitleLabel");
  39. instructionTitleLabel->setAlignment(Qt::AlignLeft);
  40. vLayout->addWidget(instructionTitleLabel);
  41. vLayout->addSpacing(10);
  42. m_repoPath = new FormLineEditWidget(tr("Remote URL"), "", this);
  43. m_repoPath->setMinimumSize(QSize(600, 0));
  44. m_repoPath->setErrorLabelText(tr("Not a valid remote source."));
  45. vLayout->addWidget(m_repoPath);
  46. vLayout->addSpacing(10);
  47. QLabel* warningLabel = new QLabel(tr("Online repositories may contain files that could potentially harm your computer,"
  48. " please ensure you understand the risks before downloading from third-party sources."), this);
  49. warningLabel->setObjectName("remoteProjectDialogWarningLabel");
  50. warningLabel->setWordWrap(true);
  51. warningLabel->setAlignment(Qt::AlignLeft);
  52. vLayout->addWidget(warningLabel);
  53. vLayout->addSpacing(10);
  54. QFrame* hLine = new QFrame();
  55. hLine->setFrameShape(QFrame::HLine);
  56. hLine->setObjectName("horizontalSeparatingLine");
  57. vLayout->addWidget(hLine);
  58. vLayout->addSpacing(10);
  59. m_downloadProjectLabel = new QLabel(tr("Download Project..."), this);
  60. m_downloadProjectLabel->setObjectName("remoteProjectDialogDownloadProjectLabel");
  61. m_downloadProjectLabel->setAlignment(Qt::AlignLeft);
  62. vLayout->addWidget(m_downloadProjectLabel);
  63. m_installPath = new FormFolderBrowseEditWidget(tr("Local project directory"));
  64. m_installPath->setMinimumSize(QSize(600, 0));
  65. vLayout->addWidget(m_installPath);
  66. vLayout->addSpacing(10);
  67. QHBoxLayout* buildHLayout = new QHBoxLayout(this);
  68. buildHLayout->setContentsMargins(0, 0, 0, 0);
  69. buildHLayout->setAlignment(Qt::AlignLeft);
  70. m_autoBuild = new QCheckBox(this);
  71. m_autoBuild->setChecked(true);
  72. AzQtComponents::CheckBox::applyToggleSwitchStyle(m_autoBuild);
  73. buildHLayout->addWidget(m_autoBuild);
  74. buildHLayout->addSpacing(10);
  75. m_buildToggleLabel = new QLabel(tr("Automatically build project"), this);
  76. m_buildToggleLabel->setAlignment(Qt::AlignLeft);
  77. buildHLayout->addWidget(m_buildToggleLabel);
  78. vLayout->addLayout(buildHLayout);
  79. vLayout->addSpacing(20);
  80. QGridLayout* extraInfoGridLayout = new QGridLayout(this);
  81. extraInfoGridLayout->setContentsMargins(0, 0, 0, 0);
  82. extraInfoGridLayout->setHorizontalSpacing(5);
  83. extraInfoGridLayout->setVerticalSpacing(15);
  84. extraInfoGridLayout->setAlignment(Qt::AlignLeft);
  85. m_requirementsTitleLabel = new QLabel(tr("Requirements"), this);
  86. m_requirementsTitleLabel->setObjectName("remoteProjectDialogRequirementsTitleLabel");
  87. m_requirementsTitleLabel->setAlignment(Qt::AlignLeft);
  88. extraInfoGridLayout->addWidget(m_requirementsTitleLabel, 0, 0);
  89. m_licensesTitleLabel = new QLabel(tr("Licenses"), this);
  90. m_licensesTitleLabel->setObjectName("remoteProjectDialogLicensesTitleLabel");
  91. m_licensesTitleLabel->setAlignment(Qt::AlignLeft);
  92. extraInfoGridLayout->addWidget(m_licensesTitleLabel, 0, 1);
  93. extraInfoGridLayout->setVerticalSpacing(15);
  94. m_requirementsContentLabel = new TextOverflowLabel(tr("Requirements"));
  95. m_requirementsContentLabel->setObjectName("remoteProjectDialogRequirementsContentLabel");
  96. m_requirementsContentLabel->setWordWrap(true);
  97. m_requirementsContentLabel->setAlignment(Qt::AlignLeft);
  98. m_requirementsContentLabel->setFixedWidth(350);
  99. extraInfoGridLayout->addWidget(m_requirementsContentLabel, 1, 0);
  100. m_licensesContentLabel = new TextOverflowLabel(tr("Licenses"));
  101. m_licensesContentLabel->setObjectName("remoteProjectDialogLicensesContentLabel");
  102. m_licensesContentLabel->setWordWrap(true);
  103. m_licensesContentLabel->setAlignment(Qt::AlignLeft);
  104. m_licensesContentLabel->setFixedWidth(350);
  105. extraInfoGridLayout->addWidget(m_licensesContentLabel, 1, 1);
  106. vLayout->addLayout(extraInfoGridLayout);
  107. vLayout->addStretch();
  108. m_dialogButtons = new QDialogButtonBox();
  109. m_dialogButtons->setObjectName("footer");
  110. vLayout->addWidget(m_dialogButtons);
  111. QPushButton* cancelButton = m_dialogButtons->addButton(tr("Cancel"), QDialogButtonBox::RejectRole);
  112. cancelButton->setProperty("secondary", true);
  113. m_applyButton = m_dialogButtons->addButton(tr("Download && Build"), QDialogButtonBox::ApplyRole);
  114. connect(cancelButton, &QPushButton::clicked, this, &QDialog::reject);
  115. connect(m_applyButton, &QPushButton::clicked, this, &AddRemoteProjectDialog::DownloadObject);
  116. m_inputTimer = new QTimer(this);
  117. m_inputTimer->setSingleShot(true);
  118. connect(m_inputTimer, &QTimer::timeout, this, &AddRemoteProjectDialog::ValidateURI);
  119. connect(
  120. m_autoBuild, &QCheckBox::clicked, [this](bool checked)
  121. {
  122. if (checked)
  123. {
  124. m_applyButton->setText(tr("Download && Build"));
  125. }
  126. else
  127. {
  128. m_applyButton->setText(tr("Download"));
  129. }
  130. }
  131. );
  132. connect(
  133. m_repoPath->lineEdit(), &QLineEdit::textEdited,
  134. [this]([[maybe_unused]] const QString& text)
  135. {
  136. // wait for a second before attempting to validate so we're less likely to do it per keypress
  137. m_inputTimer->start(1000);
  138. m_repoPath->SetValidationState(FormLineEditWidget::ValidationState::Validating);
  139. });
  140. SetDialogReady(false);
  141. }
  142. void AddRemoteProjectDialog::ValidateURI()
  143. {
  144. // validate URI, if it's a valid repository, get the project info and set the dialog as ready
  145. bool validRepository = PythonBindingsInterface::Get()->ValidateRepository(m_repoPath->lineEdit()->text());
  146. bool containsProjects = false;
  147. if (validRepository)
  148. {
  149. auto repoProjectsResult = PythonBindingsInterface::Get()->GetProjectsForRepo(m_repoPath->lineEdit()->text());
  150. if (repoProjectsResult.IsSuccess())
  151. {
  152. const auto repoProjects = repoProjectsResult.GetValue();
  153. if (!repoProjects.isEmpty())
  154. {
  155. // only get the first one for now
  156. const ProjectInfo& project = repoProjects.at(0);
  157. SetCurrentProject(project);
  158. containsProjects = true;
  159. }
  160. }
  161. }
  162. m_repoPath->SetValidationState(
  163. (validRepository && containsProjects) ? FormLineEditWidget::ValidationState::ValidationSuccess
  164. : FormLineEditWidget::ValidationState::ValidationFailed);
  165. m_repoPath->setErrorLabelVisible(!(validRepository && containsProjects));
  166. SetDialogReady(validRepository && containsProjects);
  167. }
  168. void AddRemoteProjectDialog::DownloadObject()
  169. {
  170. // Add Repo:
  171. const QString repoUri = m_repoPath->lineEdit()->text();
  172. auto addGemRepoResult = PythonBindingsInterface::Get()->AddGemRepo(repoUri);
  173. if (addGemRepoResult.IsSuccess())
  174. {
  175. // Send download to project screen to initiate download
  176. emit StartObjectDownload(m_currentProject.m_projectName, ShouldBuild());
  177. emit QDialog::accept();
  178. }
  179. else
  180. {
  181. QString failureMessage = tr("Failed to add gem repo: %1.").arg(repoUri);
  182. ProjectUtils::DisplayDetailedError(failureMessage, addGemRepoResult, this);
  183. AZ_Error("Project Manager", false, failureMessage.toUtf8().constData());
  184. }
  185. }
  186. QString AddRemoteProjectDialog::GetRepoPath()
  187. {
  188. return m_repoPath->lineEdit()->text();
  189. }
  190. QString AddRemoteProjectDialog::GetInstallPath()
  191. {
  192. return m_installPath->lineEdit()->text();
  193. }
  194. bool AddRemoteProjectDialog::ShouldBuild()
  195. {
  196. return m_autoBuild->isChecked();
  197. }
  198. void AddRemoteProjectDialog::SetCurrentProject(const ProjectInfo& projectInfo)
  199. {
  200. m_currentProject = projectInfo;
  201. m_downloadProjectLabel->setText(tr("Download Project %1").arg(projectInfo.m_displayName));
  202. m_installPath->lineEdit()->setText(QDir::toNativeSeparators(ProjectUtils::GetDefaultProjectPath() + "/" + projectInfo.m_projectName));
  203. m_requirementsContentLabel->setText(projectInfo.m_requirements);
  204. m_licensesContentLabel->setText(projectInfo.m_license);
  205. }
  206. void AddRemoteProjectDialog::SetDialogReady(bool isReady)
  207. {
  208. // Reset
  209. if (!isReady)
  210. {
  211. m_downloadProjectLabel->setText(tr("Download Project..."));
  212. m_installPath->setText("");
  213. }
  214. m_downloadProjectLabel->setEnabled(isReady);
  215. m_installPath->setEnabled(isReady);
  216. m_autoBuild->setEnabled(isReady);
  217. m_buildToggleLabel->setEnabled(isReady);
  218. m_requirementsTitleLabel->setEnabled(isReady);
  219. m_licensesTitleLabel->setEnabled(isReady);
  220. m_requirementsContentLabel->setEnabled(isReady);
  221. m_licensesContentLabel->setEnabled(isReady);
  222. m_applyButton->setEnabled(isReady);
  223. }
  224. } // namespace O3DE::ProjectManager