AtomToolsDocumentApplication.cpp 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138
  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/RPI.Edit/Common/AssetUtils.h>
  9. #include <Atom/RPI.Reflect/Asset/AssetUtils.h>
  10. #include <AtomToolsFramework/Document/AtomToolsDocumentApplication.h>
  11. #include <AtomToolsFramework/Document/AtomToolsDocumentSystemRequestBus.h>
  12. #include <AtomToolsFramework/Document/CreateDocumentDialog.h>
  13. #include <AtomToolsFramework/Util/Util.h>
  14. #include <AzCore/Utils/Utils.h>
  15. #include <AzToolsFramework/API/ToolsApplicationAPI.h>
  16. #include <AzToolsFramework/AssetBrowser/AssetBrowserEntry.h>
  17. #include <QDesktopServices>
  18. #include <QDialog>
  19. #include <QMenu>
  20. #include <QUrl>
  21. namespace AtomToolsFramework
  22. {
  23. AtomToolsDocumentApplication::AtomToolsDocumentApplication(const char* targetName, int* argc, char*** argv)
  24. : Base(targetName, argc, argv)
  25. {
  26. }
  27. void AtomToolsDocumentApplication::StartCommon(AZ::Entity* systemEntity)
  28. {
  29. Base::StartCommon(systemEntity);
  30. m_documentSystem.reset(aznew AtomToolsDocumentSystem(m_toolId));
  31. m_assetBrowserInteractions->RegisterContextMenuActions(
  32. [](const AtomToolsAssetBrowserInteractions::AssetBrowserEntryVector& entries)
  33. {
  34. return entries.front()->GetEntryType() == AzToolsFramework::AssetBrowser::AssetBrowserEntry::AssetEntryType::Source;
  35. },
  36. [this]([[maybe_unused]] QWidget* caller, QMenu* menu, const AtomToolsAssetBrowserInteractions::AssetBrowserEntryVector& entries)
  37. {
  38. bool handledOpen = false;
  39. DocumentTypeInfoVector documentTypes;
  40. AtomToolsDocumentSystemRequestBus::EventResult(
  41. documentTypes, m_toolId, &AtomToolsDocumentSystemRequestBus::Events::GetRegisteredDocumentTypes);
  42. for (const auto& documentType : documentTypes)
  43. {
  44. if (documentType.IsSupportedExtensionToOpen(entries.front()->GetFullPath()))
  45. {
  46. menu->addAction(QObject::tr("Open"), [entries, this]() {
  47. AZ::SystemTickBus::QueueFunction([toolId = m_toolId, path = entries.front()->GetFullPath()]() {
  48. AtomToolsDocumentSystemRequestBus::Event(toolId, &AtomToolsDocumentSystemRequestBus::Events::OpenDocument, path);
  49. });
  50. });
  51. handledOpen = true;
  52. break;
  53. }
  54. }
  55. if (!handledOpen)
  56. {
  57. menu->addAction(QObject::tr("Open"), [entries]() {
  58. QDesktopServices::openUrl(QUrl::fromLocalFile(entries.front()->GetFullPath().c_str()));
  59. });
  60. }
  61. for (const auto& documentType : documentTypes)
  62. {
  63. if (documentType.IsSupportedExtensionToCreate(entries.front()->GetFullPath()))
  64. {
  65. const QString createActionName = documentType.IsSupportedExtensionToSave(entries.front()->GetFullPath())
  66. ? QObject::tr("Create Child %1...").arg(documentType.m_documentTypeName.c_str())
  67. : QObject::tr("Create %1...").arg(documentType.m_documentTypeName.c_str());
  68. menu->addAction(createActionName, [entries, documentType, this]() {
  69. const auto& savePath = GetSaveFilePathFromDialog(
  70. {}, documentType.m_supportedExtensionsToSave, documentType.m_documentTypeName);
  71. if (!savePath.empty())
  72. {
  73. AtomToolsDocumentSystemRequestBus::Event(
  74. m_toolId, &AtomToolsDocumentSystemRequestBus::Events::CreateDocumentFromFilePath,
  75. entries.front()->GetFullPath(), savePath);
  76. }
  77. });
  78. }
  79. }
  80. });
  81. m_assetBrowserInteractions->RegisterContextMenuActions(
  82. [](const AtomToolsAssetBrowserInteractions::AssetBrowserEntryVector& entries)
  83. {
  84. return entries.front()->GetEntryType() == AzToolsFramework::AssetBrowser::AssetBrowserEntry::AssetEntryType::Folder;
  85. },
  86. [this](QWidget* caller, QMenu* menu, const AtomToolsAssetBrowserInteractions::AssetBrowserEntryVector& entries)
  87. {
  88. DocumentTypeInfoVector documentTypes;
  89. AtomToolsDocumentSystemRequestBus::EventResult(
  90. documentTypes, m_toolId, &AtomToolsDocumentSystemRequestBus::Events::GetRegisteredDocumentTypes);
  91. for (const auto& documentType : documentTypes)
  92. {
  93. menu->addAction(QObject::tr("Create %1...").arg(documentType.m_documentTypeName.c_str()), [caller, documentType, entries, this]() {
  94. CreateDocumentDialog dialog(documentType, entries.front()->GetFullPath().c_str(), caller);
  95. dialog.adjustSize();
  96. if (dialog.exec() == QDialog::Accepted && !dialog.m_sourcePath.isEmpty() && !dialog.m_targetPath.isEmpty())
  97. {
  98. AtomToolsDocumentSystemRequestBus::Event(
  99. m_toolId, &AtomToolsDocumentSystemRequestBus::Events::CreateDocumentFromFilePath,
  100. dialog.m_sourcePath.toUtf8().constData(), dialog.m_targetPath.toUtf8().constData());
  101. }
  102. });
  103. }
  104. });
  105. }
  106. void AtomToolsDocumentApplication::Destroy()
  107. {
  108. m_documentSystem.reset();
  109. Base::Destroy();
  110. }
  111. void AtomToolsDocumentApplication::ProcessCommandLine(const AZ::CommandLine& commandLine)
  112. {
  113. // Process command line options for opening documents on startup
  114. size_t openDocumentCount = commandLine.GetNumMiscValues();
  115. for (size_t openDocumentIndex = 0; openDocumentIndex < openDocumentCount; ++openDocumentIndex)
  116. {
  117. const AZStd::string openDocumentPath = commandLine.GetMiscValue(openDocumentIndex);
  118. AZ_Printf(m_targetName.c_str(), "Opening document: %s", openDocumentPath.c_str());
  119. AtomToolsDocumentSystemRequestBus::Event(m_toolId, &AtomToolsDocumentSystemRequestBus::Events::OpenDocument, openDocumentPath);
  120. }
  121. Base::ProcessCommandLine(commandLine);
  122. }
  123. } // namespace AtomToolsFramework