AddSeedDialog.cpp 3.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798
  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 <source/ui/AddSeedDialog.h>
  9. #include <source/ui/ui_AddSeedDialog.h>
  10. #include <source/ui/PlatformSelectionWidget.h>
  11. #include <QFileDialog>
  12. #include <QPushButton>
  13. const char QtRelativePathPrefix[] = "../";
  14. namespace AssetBundler
  15. {
  16. AddSeedDialog::AddSeedDialog(
  17. QWidget* parent,
  18. const AzFramework::PlatformFlags& enabledPlatforms,
  19. const AZStd::string& platformSpecificCachePath)
  20. : QDialog(parent)
  21. , m_platformSpecificCachePath(platformSpecificCachePath.c_str())
  22. {
  23. m_ui.reset(new Ui::AddSeedDialog);
  24. m_ui->setupUi(this);
  25. // Set up Browse File button
  26. m_ui->fileNameLineEdit->setReadOnly(true);
  27. connect(m_ui->browseFileButton, &QPushButton::clicked, this, &AddSeedDialog::OnBrowseFileButtonPressed);
  28. // Set up Platform selection
  29. m_ui->platformSelectionWidget->Init(enabledPlatforms);
  30. connect(m_ui->platformSelectionWidget,
  31. &PlatformSelectionWidget::PlatformsSelected,
  32. this,
  33. &AddSeedDialog::OnPlatformSelectionChanged);
  34. // Set up Cancel and Create New File buttons
  35. m_ui->addSeedButton->setEnabled(false);
  36. connect(m_ui->cancelButton, &QPushButton::clicked, this, &QDialog::reject);
  37. connect(m_ui->addSeedButton, &QPushButton::clicked, this, &QDialog::accept);
  38. }
  39. AZStd::string AddSeedDialog::GetFileName()
  40. {
  41. return m_fileName;
  42. }
  43. AzFramework::PlatformFlags AddSeedDialog::GetPlatformFlags()
  44. {
  45. return m_ui->platformSelectionWidget->GetSelectedPlatforms();
  46. }
  47. void AddSeedDialog::OnBrowseFileButtonPressed()
  48. {
  49. QString seedAbsolutePath = QFileDialog::getOpenFileName(this, tr("Add New Seed"), m_platformSpecificCachePath);
  50. m_fileNameIsValid = !seedAbsolutePath.isEmpty();
  51. if (!m_fileNameIsValid)
  52. {
  53. // The user canceled out of the window, do not update anything
  54. return;
  55. }
  56. // Make sure the path is relative to the platform-specific cache
  57. QDir platformSpecificCacheDir(m_platformSpecificCachePath);
  58. QString seedRelativePath = platformSpecificCacheDir.relativeFilePath(seedAbsolutePath);
  59. // trim off the "../" from the relative path
  60. FormatRelativePathString(seedRelativePath);
  61. m_fileName = seedRelativePath.toUtf8().data();
  62. // Update the ui
  63. m_ui->fileNameLineEdit->setText(seedRelativePath);
  64. m_ui->addSeedButton->setEnabled(m_platformIsValid && m_fileNameIsValid);
  65. }
  66. void AddSeedDialog::OnPlatformSelectionChanged(const AzFramework::PlatformFlags& selectedPlatforms)
  67. {
  68. // Hide the "Create File" button if no platforms are selected
  69. m_platformIsValid = selectedPlatforms != AzFramework::PlatformFlags::Platform_NONE;
  70. m_ui->addSeedButton->setEnabled(m_platformIsValid && m_fileNameIsValid);
  71. }
  72. void AddSeedDialog::FormatRelativePathString(QString& relativePath)
  73. {
  74. if (relativePath.startsWith(QtRelativePathPrefix))
  75. {
  76. relativePath.remove(0, static_cast<int>(strlen(QtRelativePathPrefix)));
  77. }
  78. }
  79. } //namespace AssetBundler
  80. #include <source/ui/moc_AddSeedDialog.cpp>