AddRemoteProjectDialog.cpp 11 KB

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