AddRemoteTemplateDialog.cpp 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136
  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 <AddRemoteTemplateDialog.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. AddRemoteTemplateDialog::AddRemoteTemplateDialog(QWidget* parent)
  26. : QDialog(parent)
  27. {
  28. setWindowTitle(tr("Add a remote template"));
  29. setModal(true);
  30. setObjectName("addRemoteTemplateDialog");
  31. setFixedSize(QSize(760, 270));
  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 template"), this);
  38. instructionTitleLabel->setObjectName("remoteTemplateDialogInstructionTitleLabel");
  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 template source."));
  45. m_repoPath->lineEdit()->setPlaceholderText("https://github.com/o3de/example.git");
  46. vLayout->addWidget(m_repoPath);
  47. vLayout->addSpacing(10);
  48. QLabel* warningLabel = new QLabel(tr("Online repositories may contain files that could potentially harm your computer,"
  49. " please ensure you understand the risks before downloading from third-party sources."), this);
  50. warningLabel->setObjectName("remoteProjectDialogWarningLabel");
  51. warningLabel->setWordWrap(true);
  52. warningLabel->setAlignment(Qt::AlignLeft);
  53. vLayout->addWidget(warningLabel);
  54. vLayout->addSpacing(20);
  55. vLayout->addStretch();
  56. m_dialogButtons = new QDialogButtonBox();
  57. m_dialogButtons->setObjectName("footer");
  58. vLayout->addWidget(m_dialogButtons);
  59. QPushButton* cancelButton = m_dialogButtons->addButton(tr("Cancel"), QDialogButtonBox::RejectRole);
  60. cancelButton->setProperty("secondary", true);
  61. m_applyButton = m_dialogButtons->addButton(tr("Add"), QDialogButtonBox::ApplyRole);
  62. m_applyButton->setProperty("primary", true);
  63. connect(cancelButton, &QPushButton::clicked, this, &QDialog::reject);
  64. connect(m_applyButton, &QPushButton::clicked, this, &AddRemoteTemplateDialog::AddTemplateSource);
  65. m_inputTimer = new QTimer(this);
  66. m_inputTimer->setSingleShot(true);
  67. connect(m_inputTimer, &QTimer::timeout, this, &AddRemoteTemplateDialog::ValidateURI);
  68. connect(
  69. m_repoPath->lineEdit(), &QLineEdit::textEdited,
  70. [this]([[maybe_unused]] const QString& text)
  71. {
  72. // wait for a second before attempting to validate so we're less likely to do it per keypress
  73. m_inputTimer->start(1000);
  74. m_repoPath->SetValidationState(FormLineEditWidget::ValidationState::Validating);
  75. });
  76. SetDialogReady(false);
  77. }
  78. void AddRemoteTemplateDialog::ValidateURI()
  79. {
  80. // validate URI, if it's a valid repository set the add button as active
  81. bool validRepository = PythonBindingsInterface::Get()->ValidateRepository(m_repoPath->lineEdit()->text());
  82. SetDialogReady(validRepository);
  83. m_repoPath->SetValidationState(
  84. validRepository ? FormLineEditWidget::ValidationState::ValidationSuccess
  85. : FormLineEditWidget::ValidationState::ValidationFailed);
  86. m_repoPath->setErrorLabelVisible(!validRepository);
  87. }
  88. void AddRemoteTemplateDialog::AddTemplateSource()
  89. {
  90. // Add the remote source
  91. const QString repoUri = m_repoPath->lineEdit()->text();
  92. auto addGemRepoResult = PythonBindingsInterface::Get()->AddGemRepo(repoUri);
  93. if (addGemRepoResult.IsSuccess())
  94. {
  95. emit QDialog::accept();
  96. }
  97. else
  98. {
  99. QString failureMessage = tr("Failed to add template source: %1.").arg(repoUri);
  100. ProjectUtils::DisplayDetailedError(failureMessage, addGemRepoResult, this);
  101. AZ_Error("Project Manager", false, failureMessage.toUtf8().constData());
  102. }
  103. }
  104. QString AddRemoteTemplateDialog::GetRepoPath()
  105. {
  106. return m_repoPath->lineEdit()->text();
  107. }
  108. void AddRemoteTemplateDialog::SetDialogReady(bool isReady)
  109. {
  110. // Reset
  111. m_applyButton->setEnabled(isReady);
  112. }
  113. } // namespace O3DE::ProjectManager