/dev/Code/Sandbox/Plugins/MaglevControlPanel/CreateResourceDialog.cpp

https://github.com/aws/lumberyard · C++ · 180 lines · 139 code · 30 blank · 11 comment · 10 complexity · bda9274bdcb9c3760f40e4d5d348cc75 MD5 · raw file

  1. /*
  2. * All or portions of this file Copyright (c) Amazon.com, Inc. or its affiliates or
  3. * its licensors.
  4. *
  5. * For complete copyright and license terms please see the LICENSE at the root of this
  6. * distribution (the "License"). All use of this software is governed by the License,
  7. * or, if provided, by the license below or the license accompanying this file. Do not
  8. * remove or modify any license notices. This file is distributed on an "AS IS" BASIS,
  9. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  10. *
  11. */
  12. #include <stdafx.h>
  13. #include <CreateResourceDialog.h>
  14. #include <QDesktopServices>
  15. #include <QMessageBox>
  16. #include <QUrl>
  17. #include <QRegExp>
  18. #include <QLineEdit>
  19. #include <QRegExpValidator>
  20. #include <ResourceManagementView.h>
  21. #include <AWSResourceManager.h>
  22. #include <FileSourceControlModel.h>
  23. #include "CreateResourceDialog.moc"
  24. CreateResourceDialog::CreateResourceDialog(QWidget* parent, QSharedPointer<ICloudFormationTemplateModel> templateModel, ResourceManagementView* view)
  25. : QDialog(parent, Qt::CustomizeWindowHint | Qt::WindowTitleHint | Qt::Dialog | Qt::WindowCloseButtonHint)
  26. , m_templateModel{templateModel}
  27. , m_sourceStatus{new SourceControlStatusModel}
  28. , m_view{view}
  29. {
  30. }
  31. void CreateResourceDialog::accept()
  32. {
  33. if (!ValidateResourceName(GetResourceName()))
  34. {
  35. return;
  36. }
  37. ValidateSourceSaveResource();
  38. }
  39. void CreateResourceDialog::ValidateSourceSaveResource()
  40. {
  41. QObject::connect(&*m_sourceStatus, &IFileSourceControlModel::SourceControlStatusUpdated, this, &CreateResourceDialog::OnSourceStatusUpdated);
  42. AWSResourceManager::RequestUpdateSourceModel(m_sourceStatus, m_templateModel);
  43. }
  44. void CreateResourceDialog::OnSourceStatusUpdated()
  45. {
  46. QObject::disconnect(&*m_sourceStatus, &IFileSourceControlModel::SourceControlStatusUpdated, this, 0);
  47. if (m_sourceStatus->FileNeedsCheckout())
  48. {
  49. QString fileName = m_templateModel->Path();
  50. QString checkoutString = fileName + ResourceManagementView::GetSourceCheckoutString();
  51. auto reply = QMessageBox::question(
  52. this,
  53. "File needs checkout",
  54. checkoutString,
  55. QMessageBox::Yes | QMessageBox::No);
  56. if (reply == QMessageBox::Yes)
  57. {
  58. QObject::connect(&*m_sourceStatus, &IFileSourceControlModel::SourceControlStatusUpdated, this, &CreateResourceDialog::OnSourceStatusChanged);
  59. AWSResourceManager::RequestEditSourceModel(m_sourceStatus, m_templateModel);
  60. return;
  61. }
  62. CloseDialog();
  63. return;
  64. }
  65. SaveAndClose();
  66. }
  67. void CreateResourceDialog::SaveAndClose()
  68. {
  69. BeginSaveResource();
  70. }
  71. void CreateResourceDialog::CloseDialog()
  72. {
  73. hide();
  74. }
  75. void CreateResourceDialog::OnSourceStatusChanged()
  76. {
  77. QObject::disconnect(&*m_sourceStatus, &IFileSourceControlModel::SourceControlStatusChanged, this, 0);
  78. SaveAndClose();
  79. }
  80. void CreateResourceDialog::BeginSaveResource()
  81. {
  82. bool result = SaveResource();
  83. FinishSaveResource(result);
  84. }
  85. bool CreateResourceDialog::SaveResource()
  86. {
  87. return true;
  88. }
  89. void CreateResourceDialog::FinishSaveResource(bool result)
  90. {
  91. if (result)
  92. {
  93. m_templateModel->AddResource(GetResourceName(), GetVariantMap());
  94. m_templateModel->AddParameters(GetParameterVariantMap());
  95. if (!m_templateModel->Save())
  96. {
  97. QString saveString = m_templateModel->Path() + " failed to save. Check to be sure the file is writable.";
  98. auto warningBox = QMessageBox::warning(
  99. this,
  100. "Check out file",
  101. saveString, QMessageBox::Ok);
  102. result = false;
  103. }
  104. }
  105. if (result)
  106. {
  107. CloseDialog();
  108. }
  109. }
  110. void CreateResourceDialog::reject()
  111. {
  112. CloseDialog();
  113. }
  114. void CreateResourceDialog::OnResourceHelpActivated(const QString& linkString)
  115. {
  116. QDesktopServices::openUrl(QUrl { linkString });
  117. }
  118. bool CreateResourceDialog::ValidateResourceName(const QString& resourceName)
  119. {
  120. if (!resourceName.length())
  121. {
  122. QMessageBox::critical(this, tr("Invalid resource name"), tr("A resource name must be provided."));
  123. return false;
  124. }
  125. if (m_templateModel->HasResource(resourceName))
  126. {
  127. QMessageBox::critical(this, "Invalid resource name", QString{tr("The resource group already has a resource named %1.")}.arg(resourceName));
  128. return false;
  129. }
  130. if (!TypeSpecificValidateResource(resourceName))
  131. {
  132. return false;
  133. }
  134. return true;
  135. }
  136. QRegExpValidator* CreateResourceDialog::GetValidator(const QString& regEx)
  137. {
  138. QRegExp rx(regEx);
  139. return new QRegExpValidator(rx, this);
  140. }
  141. int CreateResourceDialog::SetValidatorOnLineEdit(QLineEdit* target, const QString& resourceName, const QString& fieldName)
  142. {
  143. QString regex;
  144. QString help;
  145. int minLen;
  146. m_view->GetResourceValidationData(resourceName, fieldName, regex, help, minLen);
  147. target->setValidator(GetValidator(regex));
  148. target->setToolTip(help);
  149. return minLen;
  150. }