Util.cpp 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177
  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 <Atom/ImageProcessing/ImageObject.h>
  9. #include <Atom/ImageProcessing/ImageProcessingBus.h>
  10. #include <AtomToolsFramework/Util/Util.h>
  11. #include <AzCore/IO/SystemFile.h>
  12. #include <AzCore/Jobs/JobFunction.h>
  13. #include <AzCore/StringFunc/StringFunc.h>
  14. #include <AzCore/Utils/Utils.h>
  15. #include <AzFramework/API/ApplicationAPI.h>
  16. #include <AzQtComponents/Components/Widgets/FileDialog.h>
  17. #include <AzToolsFramework/AssetBrowser/AssetBrowserBus.h>
  18. #include <AzToolsFramework/AssetBrowser/AssetBrowserEntry.h>
  19. #include <AzToolsFramework/AssetBrowser/AssetSelectionModel.h>
  20. AZ_PUSH_DISABLE_WARNING(4251 4800, "-Wunknown-warning-option") // disable warnings spawned by QT
  21. #include <QApplication>
  22. #include <QMessageBox>
  23. #include <QProcess>
  24. AZ_POP_DISABLE_WARNING
  25. namespace AtomToolsFramework
  26. {
  27. void LoadImageAsync(const AZStd::string& path, LoadImageAsyncCallback callback)
  28. {
  29. AZ::Job* job = AZ::CreateJobFunction(
  30. [path, callback]()
  31. {
  32. ImageProcessingAtom::IImageObjectPtr imageObject;
  33. ImageProcessingAtom::ImageProcessingRequestBus::BroadcastResult(
  34. imageObject, &ImageProcessingAtom::ImageProcessingRequests::LoadImagePreview, path);
  35. if (imageObject)
  36. {
  37. AZ::u8* imageBuf = nullptr;
  38. AZ::u32 pitch = 0;
  39. AZ::u32 mip = 0;
  40. imageObject->GetImagePointer(mip, imageBuf, pitch);
  41. const AZ::u32 width = imageObject->GetWidth(mip);
  42. const AZ::u32 height = imageObject->GetHeight(mip);
  43. QImage image(imageBuf, width, height, pitch, QImage::Format_RGBA8888);
  44. if (callback)
  45. {
  46. callback(image);
  47. }
  48. }
  49. },
  50. true);
  51. job->Start();
  52. }
  53. QFileInfo GetSaveFileInfo(const QString& initialPath)
  54. {
  55. const QFileInfo initialFileInfo(initialPath);
  56. const QString initialExt(initialFileInfo.completeSuffix());
  57. const QFileInfo selectedFileInfo(AzQtComponents::FileDialog::GetSaveFileName(
  58. QApplication::activeWindow(),
  59. "Save File",
  60. initialFileInfo.absolutePath() +
  61. AZ_CORRECT_FILESYSTEM_SEPARATOR_STRING +
  62. initialFileInfo.baseName(),
  63. QString("Files (*.%1)").arg(initialExt)));
  64. if (selectedFileInfo.absoluteFilePath().isEmpty())
  65. {
  66. // Cancelled operation
  67. return QFileInfo();
  68. }
  69. if (!selectedFileInfo.absoluteFilePath().endsWith(initialExt))
  70. {
  71. QMessageBox::critical(QApplication::activeWindow(), "Error", QString("File name must have .%1 extension.").arg(initialExt));
  72. return QFileInfo();
  73. }
  74. return selectedFileInfo;
  75. }
  76. QFileInfo GetOpenFileInfo(const AZStd::vector<AZ::Data::AssetType>& assetTypes)
  77. {
  78. using namespace AZ::Data;
  79. using namespace AzToolsFramework::AssetBrowser;
  80. // [GFX TODO] Should this just be an open file dialog filtered to supported source data extensions?
  81. auto selection = AssetSelectionModel::AssetTypesSelection(assetTypes);
  82. // [GFX TODO] This is functional but UI is not as designed
  83. AssetBrowserComponentRequestBus::Broadcast(&AssetBrowserComponentRequests::PickAssets, selection, QApplication::activeWindow());
  84. if (!selection.IsValid())
  85. {
  86. return QFileInfo();
  87. }
  88. auto entry = selection.GetResult();
  89. const SourceAssetBrowserEntry* sourceEntry = azrtti_cast<const SourceAssetBrowserEntry*>(entry);
  90. if (!sourceEntry)
  91. {
  92. const ProductAssetBrowserEntry* productEntry = azrtti_cast<const ProductAssetBrowserEntry*>(entry);
  93. if (productEntry)
  94. {
  95. sourceEntry = azrtti_cast<const SourceAssetBrowserEntry*>(productEntry->GetParent());
  96. }
  97. }
  98. if (!sourceEntry)
  99. {
  100. return QFileInfo();
  101. }
  102. return QFileInfo(sourceEntry->GetFullPath().c_str());
  103. }
  104. QFileInfo GetUniqueFileInfo(const QString& initialPath)
  105. {
  106. int counter = 0;
  107. QFileInfo fileInfo = initialPath;
  108. const QString extension = "." + fileInfo.completeSuffix();
  109. const QString basePathAndName = fileInfo.absoluteFilePath().left(fileInfo.absoluteFilePath().size() - extension.size());
  110. while (fileInfo.exists())
  111. {
  112. fileInfo = QString("%1_%2%3").arg(basePathAndName).arg(++counter).arg(extension);
  113. }
  114. return fileInfo;
  115. }
  116. QFileInfo GetDuplicationFileInfo(const QString& initialPath)
  117. {
  118. const QFileInfo initialFileInfo(initialPath);
  119. const QString initialExt(initialFileInfo.completeSuffix());
  120. const QFileInfo duplicateFileInfo(AzQtComponents::FileDialog::GetSaveFileName(
  121. QApplication::activeWindow(),
  122. "Duplicate File",
  123. GetUniqueFileInfo(initialPath).absoluteFilePath(),
  124. QString("Files (*.%1)").arg(initialExt)));
  125. if (duplicateFileInfo == initialFileInfo)
  126. {
  127. // Cancelled operation or selected same path
  128. return QFileInfo();
  129. }
  130. if (duplicateFileInfo.absoluteFilePath().isEmpty())
  131. {
  132. // Cancelled operation or selected same path
  133. return QFileInfo();
  134. }
  135. if (!duplicateFileInfo.absoluteFilePath().endsWith(initialExt))
  136. {
  137. QMessageBox::critical(QApplication::activeWindow(), "Error", QString("File name must have .%1 extension.").arg(initialExt));
  138. return QFileInfo();
  139. }
  140. return duplicateFileInfo;
  141. }
  142. bool LaunchTool(const QString& baseName, const QString& extension, const QStringList& arguments)
  143. {
  144. AZ::IO::FixedMaxPath engineRoot = AZ::Utils::GetEnginePath();
  145. AZ_Assert(!engineRoot.empty(), "Cannot query Engine Path");
  146. AZ::IO::FixedMaxPath launchPath = AZ::IO::FixedMaxPath(AZ::Utils::GetExecutableDirectory())
  147. / (baseName + extension).toUtf8().constData();
  148. return QProcess::startDetached(launchPath.c_str(), arguments, engineRoot.c_str());
  149. }
  150. }