NewFileDialog.cpp 9.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235
  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/NewFileDialog.h>
  9. #include <source/ui/ui_NewFileDialog.h>
  10. #include <source/ui/PlatformSelectionWidget.h>
  11. #include <source/utils/utils.h>
  12. #include <AzCore/IO/FileIO.h>
  13. #include <AzFramework/StringFunc/StringFunc.h>
  14. #include <AzToolsFramework/Asset/AssetBundler.h>
  15. #include <QLineEdit>
  16. #include <QMessageBox>
  17. #include <QPushButton>
  18. namespace AssetBundler
  19. {
  20. NewFileDialog::NewFileDialog(
  21. QWidget* parent,
  22. const QString& dialogTitle,
  23. const QString& startingPath,
  24. const char* fileExtension,
  25. const QString& fileNameFilter,
  26. const AzFramework::PlatformFlags& enabledPlatforms,
  27. bool isRunningRule)
  28. : QDialog(parent)
  29. , m_startingPath(startingPath)
  30. , m_fileExtension(fileExtension)
  31. {
  32. m_ui.reset(new Ui::NewFileDialog);
  33. m_ui->setupUi(this);
  34. setWindowTitle(dialogTitle);
  35. // Set up File Name section
  36. m_ui->fileNameLineEdit->setEnabled(false);
  37. connect(m_ui->browseButton, &QPushButton::clicked, this, &NewFileDialog::OnBrowseButtonPressed);
  38. m_newFileDialog.setFileMode(QFileDialog::AnyFile);
  39. m_newFileDialog.setNameFilter(fileNameFilter);
  40. m_newFileDialog.setViewMode(QFileDialog::Detail);
  41. m_newFileDialog.setDirectory(m_startingPath);
  42. // We are not creating a new file when Qt thinks we are, so we need to block signals or else the file watcher will be
  43. // triggered too soon
  44. m_newFileDialog.blockSignals(true);
  45. // Set up Platform selection
  46. QString disabledPatformMessageOverride;
  47. if (isRunningRule)
  48. {
  49. disabledPatformMessageOverride = tr("This platform is not valid for all input Asset Lists.");
  50. }
  51. m_ui->platformSelectionWidget->Init(enabledPlatforms, disabledPatformMessageOverride);
  52. connect(m_ui->platformSelectionWidget,
  53. &PlatformSelectionWidget::PlatformsSelected,
  54. this,
  55. &NewFileDialog::OnPlatformSelectionChanged);
  56. // Set up Cancel and Create New File buttons
  57. m_ui->createFileButton->setEnabled(false);
  58. connect(m_ui->cancelButton, &QPushButton::clicked, this, &QDialog::reject);
  59. connect(m_ui->createFileButton, &QPushButton::clicked, this, &NewFileDialog::OnCreateFileButtonPressed);
  60. }
  61. AZStd::string NewFileDialog::GetAbsoluteFilePath()
  62. {
  63. return m_absoluteFilePath;
  64. }
  65. AzFramework::PlatformFlags NewFileDialog::GetPlatformFlags()
  66. {
  67. return m_ui->platformSelectionWidget->GetSelectedPlatforms();
  68. }
  69. void NewFileDialog::OnBrowseButtonPressed()
  70. {
  71. int result = m_newFileDialog.exec();
  72. if (result == QDialog::DialogCode::Accepted)
  73. {
  74. m_absoluteFilePath = m_newFileDialog.selectedFiles()[0].toUtf8().data();
  75. AzToolsFramework::RemovePlatformIdentifier(m_absoluteFilePath);
  76. if (m_fileExtension && !AzFramework::StringFunc::Path::HasExtension(m_absoluteFilePath.c_str()))
  77. {
  78. m_absoluteFilePath.append(".");
  79. m_absoluteFilePath.append(m_fileExtension);
  80. }
  81. m_ui->fileNameLineEdit->setEnabled(true);
  82. m_ui->fileNameLineEdit->setText(m_absoluteFilePath.c_str());
  83. }
  84. m_fileNameIsValid = !m_absoluteFilePath.empty();
  85. m_ui->createFileButton->setEnabled(m_platformIsValid && m_fileNameIsValid);
  86. }
  87. void NewFileDialog::OnPlatformSelectionChanged(const AzFramework::PlatformFlags& selectedPlatforms)
  88. {
  89. // Hide the "Create File" button if no platforms are selected
  90. m_platformIsValid = selectedPlatforms != AzFramework::PlatformFlags::Platform_NONE;
  91. m_ui->createFileButton->setEnabled(m_platformIsValid && m_fileNameIsValid);
  92. }
  93. void NewFileDialog::OnCreateFileButtonPressed()
  94. {
  95. // Check to see if any of the selected platform-specific files already exist on-disk
  96. QString overwriteExistingFilesList;
  97. AZStd::fixed_vector<AZStd::string, AzFramework::NumPlatforms> selectedPlatformNames{ AZStd::from_range,
  98. AzFramework::PlatformHelper::GetPlatforms(GetPlatformFlags()) };
  99. for (const AZStd::string& platformName : selectedPlatformNames)
  100. {
  101. FilePath platformSpecificFilePath(GetAbsoluteFilePath(), platformName);
  102. const char* platformSpecificFileAbsolutePath = platformSpecificFilePath.AbsolutePath().c_str();
  103. if (AZ::IO::FileIOBase::GetInstance()->Exists(platformSpecificFileAbsolutePath))
  104. {
  105. overwriteExistingFilesList.append(QString("%1\n").arg(platformSpecificFileAbsolutePath));
  106. }
  107. }
  108. // Ask the user if they are sure they want to overwrite existing files
  109. if (!overwriteExistingFilesList.isEmpty())
  110. {
  111. QString messageBoxText = QString(tr(
  112. "The following files already exist on-disk. Generating new files will overwrite the existing ones.\n\n%1\n\nDo you wish to permanently delete the existing files?")).arg(overwriteExistingFilesList);
  113. QMessageBox::StandardButton confirmDeleteFileResult =
  114. QMessageBox::question(this, QString(tr("Replace Existing Files")), messageBoxText);
  115. if (confirmDeleteFileResult != QMessageBox::StandardButton::Yes)
  116. {
  117. // User canceled out of the operation
  118. return;
  119. }
  120. }
  121. emit QDialog::accept();
  122. }
  123. AZStd::string NewFileDialog::OSNewFileDialog(
  124. QWidget* parent,
  125. const char* fileExtension,
  126. const char* fileTypeDisplayName,
  127. const AZStd::string& startingDirectory)
  128. {
  129. QFileDialog filePathDialog(parent);
  130. filePathDialog.setFileMode(QFileDialog::AnyFile);
  131. filePathDialog.setNameFilter(QString(tr("%1 (*.%2)")).arg(fileTypeDisplayName).arg(fileExtension));
  132. filePathDialog.setViewMode(QFileDialog::Detail);
  133. filePathDialog.setDirectory(startingDirectory.c_str());
  134. // Since we handle file creation instead of the OS, we have to block signals or else the model will be reloaded,
  135. // and our in-memory changes will be lost.
  136. filePathDialog.blockSignals(true);
  137. int result = filePathDialog.exec();
  138. if (result == QDialog::DialogCode::Rejected || filePathDialog.selectedFiles().empty())
  139. {
  140. // User canceled out of the file dialog, cancel operation
  141. return "";
  142. }
  143. AZStd::string absoluteFilePath(filePathDialog.selectedFiles()[0].toUtf8().data());
  144. if (!AzFramework::StringFunc::Path::HasExtension(absoluteFilePath.c_str()))
  145. {
  146. absoluteFilePath =
  147. AZStd::string::format("%s%c%s", absoluteFilePath.c_str(), AZ_FILESYSTEM_EXTENSION_SEPARATOR, fileExtension);
  148. }
  149. return absoluteFilePath;
  150. }
  151. int NewFileDialog::FileGenerationResultMessageBox(
  152. QWidget* parent,
  153. const AZStd::vector<AZStd::string>& generatedFiles,
  154. bool generatedWithErrors)
  155. {
  156. QMessageBox messageBox(parent);
  157. messageBox.setStandardButtons(QMessageBox::Ok);
  158. messageBox.setDefaultButton(QMessageBox::Ok);
  159. if (generatedFiles.empty())
  160. {
  161. messageBox.setText(QString("No files were generated. Please refer to the console for more information."));
  162. messageBox.setIcon(QMessageBox::Critical);
  163. return messageBox.exec();
  164. }
  165. QString messageText;
  166. if (generatedWithErrors)
  167. {
  168. messageText = QString("The following files were generated with errors. Please refer to the console for more information.\n\n");
  169. messageBox.setIcon(QMessageBox::Warning);
  170. }
  171. else
  172. {
  173. messageText = QString("You have successfully generated:\n\n");
  174. messageBox.setIcon(QMessageBox::NoIcon);
  175. }
  176. AZStd::string fileName;
  177. for (const AZStd::string& filePath : generatedFiles)
  178. {
  179. AzFramework::StringFunc::Path::GetFullFileName(filePath.c_str(), fileName);
  180. messageText.append(QString("%1\n").arg(fileName.c_str()));
  181. }
  182. AZStd::string extension;
  183. AzFramework::StringFunc::Path::GetExtension(fileName.c_str(), extension, false);
  184. if (extension == AzToolsFramework::AssetSeedManager::GetAssetListFileExtension())
  185. {
  186. messageText.append("\nVisit the Asset Lists tab to see the lists.");
  187. }
  188. else if (extension == AzToolsFramework::AssetBundleSettings::GetBundleFileExtension())
  189. {
  190. messageText.append("\nVisit the Completed Bundles tab to see the bundles.");
  191. }
  192. messageBox.setText(messageText);
  193. // QMessageBoxes try to shrink to the smallest size possible, so we need to make a spacer
  194. QSpacerItem* horizontalSpacer = new QSpacerItem(550, 0, QSizePolicy::Minimum, QSizePolicy::Expanding);
  195. QGridLayout* layout = static_cast<QGridLayout*>(messageBox.layout());
  196. layout->addItem(horizontalSpacer, layout->rowCount(), 0, 1, layout->columnCount());
  197. return messageBox.exec();
  198. }
  199. } //namespace AssetBundler
  200. #include <source/ui/moc_NewFileDialog.cpp>