AddRemoteProjectDialog.cpp 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287
  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. m_applyButton->setProperty("primary", true);
  124. connect(cancelButton, &QPushButton::clicked, this, &QDialog::reject);
  125. connect(m_applyButton, &QPushButton::clicked, this, &AddRemoteProjectDialog::DownloadObject);
  126. m_inputTimer = new QTimer(this);
  127. m_inputTimer->setSingleShot(true);
  128. connect(m_inputTimer, &QTimer::timeout, this, &AddRemoteProjectDialog::ValidateURI);
  129. connect(
  130. m_autoBuild, &QCheckBox::clicked, [this](bool checked)
  131. {
  132. if (checked)
  133. {
  134. m_applyButton->setText(tr("Download && Build"));
  135. }
  136. else
  137. {
  138. m_applyButton->setText(tr("Download"));
  139. }
  140. }
  141. );
  142. connect(
  143. m_repoPath->lineEdit(), &QLineEdit::textEdited,
  144. [this]([[maybe_unused]] const QString& text)
  145. {
  146. // wait for a second before attempting to validate so we're less likely to do it per keypress
  147. m_inputTimer->start(1000);
  148. m_repoPath->SetValidationState(FormLineEditWidget::ValidationState::Validating);
  149. });
  150. SetDialogReady(false);
  151. }
  152. void AddRemoteProjectDialog::ValidateURI()
  153. {
  154. // validate URI, if it's a valid repository, get the project info and set the dialog as ready
  155. bool validRepository = PythonBindingsInterface::Get()->ValidateRepository(m_repoPath->lineEdit()->text());
  156. bool containsProjects = false;
  157. if (validRepository)
  158. {
  159. auto repoProjectsResult = PythonBindingsInterface::Get()->GetProjectsForRepo(m_repoPath->lineEdit()->text());
  160. if (repoProjectsResult.IsSuccess())
  161. {
  162. const auto repoProjects = repoProjectsResult.GetValue();
  163. if (!repoProjects.isEmpty())
  164. {
  165. // only get the first one for now
  166. const ProjectInfo& project = repoProjects.at(0);
  167. SetCurrentProject(project);
  168. containsProjects = true;
  169. }
  170. }
  171. }
  172. const bool isValidProjectRepo = validRepository && containsProjects;
  173. m_repoPath->SetValidationState(
  174. isValidProjectRepo ? FormLineEditWidget::ValidationState::ValidationSuccess
  175. : FormLineEditWidget::ValidationState::ValidationFailed);
  176. m_repoPath->setErrorLabelVisible(!isValidProjectRepo);
  177. SetDialogReady(isValidProjectRepo);
  178. }
  179. void AddRemoteProjectDialog::DownloadObject()
  180. {
  181. // Add Repo:
  182. const QString repoUri = m_repoPath->lineEdit()->text();
  183. auto addGemRepoResult = PythonBindingsInterface::Get()->AddGemRepo(repoUri);
  184. if (addGemRepoResult.IsSuccess())
  185. {
  186. // Send download to project screen to initiate download
  187. emit StartObjectDownload(m_currentProject.m_projectName, GetInstallPath(), ShouldBuild());
  188. emit QDialog::accept();
  189. }
  190. else
  191. {
  192. QString failureMessage = tr("Failed to add gem repo: %1.").arg(repoUri);
  193. ProjectUtils::DisplayDetailedError(failureMessage, addGemRepoResult, this);
  194. AZ_Error("Project Manager", false, failureMessage.toUtf8().constData());
  195. }
  196. }
  197. QString AddRemoteProjectDialog::GetRepoPath()
  198. {
  199. return m_repoPath->lineEdit()->text();
  200. }
  201. QString AddRemoteProjectDialog::GetInstallPath()
  202. {
  203. return m_installPath->lineEdit()->text();
  204. }
  205. bool AddRemoteProjectDialog::ShouldBuild()
  206. {
  207. return m_autoBuild->isChecked();
  208. }
  209. void AddRemoteProjectDialog::SetCurrentProject(const ProjectInfo& projectInfo)
  210. {
  211. m_currentProject = projectInfo;
  212. m_downloadProjectLabel->setText(tr("Download Project %1").arg(projectInfo.m_displayName));
  213. m_installPath->lineEdit()->setText(QDir::toNativeSeparators(ProjectUtils::GetDefaultProjectPath() + "/" + projectInfo.m_projectName));
  214. m_requirementsContentLabel->setText(projectInfo.m_requirements);
  215. m_licensesContentLabel->setText(projectInfo.m_license);
  216. }
  217. void AddRemoteProjectDialog::SetDialogReady(bool isReady)
  218. {
  219. // Reset
  220. if (!isReady)
  221. {
  222. m_downloadProjectLabel->setText(tr("Download Project..."));
  223. m_installPath->setText("");
  224. }
  225. m_downloadProjectLabel->setEnabled(isReady);
  226. m_installPath->setEnabled(isReady);
  227. m_autoBuild->setEnabled(isReady);
  228. m_buildToggleLabel->setEnabled(isReady);
  229. m_requirementsTitleLabel->setEnabled(isReady);
  230. m_licensesTitleLabel->setEnabled(isReady);
  231. m_requirementsContentLabel->setEnabled(isReady);
  232. m_licensesContentLabel->setEnabled(isReady);
  233. m_applyButton->setEnabled(isReady);
  234. }
  235. } // namespace O3DE::ProjectManager