AtomToolsAssetBrowserInteractions.cpp 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262
  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 <AtomToolsFramework/AssetBrowser/AtomToolsAssetBrowserInteractions.h>
  10. #include <AtomToolsFramework/Util/Util.h>
  11. #include <AzCore/Utils/Utils.h>
  12. #include <AzCore/std/string/wildcard.h>
  13. #include <AzQtComponents/Utilities/DesktopUtilities.h>
  14. #include <AzToolsFramework/API/EditorPythonRunnerRequestsBus.h>
  15. #include <AzToolsFramework/AssetBrowser/AssetBrowserBus.h>
  16. #include <AzToolsFramework/AssetBrowser/AssetBrowserEntry.h>
  17. #include <AzToolsFramework/AssetBrowser/AssetSelectionModel.h>
  18. #include <AzToolsFramework/Thumbnails/SourceControlThumbnail.h>
  19. #include <QApplication>
  20. #include <QClipboard>
  21. #include <QDesktopServices>
  22. #include <QDir>
  23. #include <QFileDialog>
  24. #include <QFileInfo>
  25. #include <QInputDialog>
  26. #include <QMenu>
  27. #include <QMessageBox>
  28. namespace AtomToolsFramework
  29. {
  30. AtomToolsAssetBrowserInteractions::AtomToolsAssetBrowserInteractions()
  31. {
  32. AssetBrowserInteractionNotificationBus::Handler::BusConnect();
  33. }
  34. AtomToolsAssetBrowserInteractions::~AtomToolsAssetBrowserInteractions()
  35. {
  36. AssetBrowserInteractionNotificationBus::Handler::BusDisconnect();
  37. }
  38. void AtomToolsAssetBrowserInteractions::RegisterContextMenuActions(
  39. const FilterCallback& filterCallback, const ActionCallback& actionCallback)
  40. {
  41. m_contextMenuCallbacks.emplace_back(filterCallback, actionCallback);
  42. }
  43. void AtomToolsAssetBrowserInteractions::AddContextMenuActions(
  44. QWidget* caller, QMenu* menu, const AssetBrowserEntryVector& entries)
  45. {
  46. const AssetBrowserEntry* entry = entries.empty() ? nullptr : entries.front();
  47. if (!entry)
  48. {
  49. return;
  50. }
  51. m_caller = caller;
  52. QObject::connect(m_caller, &QObject::destroyed, [this]() { m_caller = nullptr; });
  53. // Add all of the custom context menu entries first
  54. for (const auto& contextMenuCallbackPair : m_contextMenuCallbacks)
  55. {
  56. if (contextMenuCallbackPair.first(entries))
  57. {
  58. contextMenuCallbackPair.second(caller, menu, entries);
  59. }
  60. }
  61. if (entry->GetEntryType() == AssetBrowserEntry::AssetEntryType::Source)
  62. {
  63. AddContextMenuActionsForSourceEntries(caller, menu, entry);
  64. }
  65. else if (entry->GetEntryType() == AssetBrowserEntry::AssetEntryType::Folder)
  66. {
  67. AddContextMenuActionsForFolderEntries(caller, menu, entry);
  68. }
  69. AddContextMenuActionsForAllEntries(caller, menu, entry);
  70. AddContextMenuActionsForSourceControl(caller, menu, entry);
  71. }
  72. void AtomToolsAssetBrowserInteractions::AddContextMenuActionsForSourceEntries(
  73. [[maybe_unused]] QWidget* caller, QMenu* menu, const AzToolsFramework::AssetBrowser::AssetBrowserEntry* entry)
  74. {
  75. menu->addAction("Duplicate", [entry]()
  76. {
  77. const auto& duplicateFilePath = GetUniqueFilePath(entry->GetFullPath());
  78. if (QFile::copy(entry->GetFullPath().c_str(), duplicateFilePath.c_str()))
  79. {
  80. QFile::setPermissions(duplicateFilePath.c_str(), QFile::ReadOther | QFile::WriteOther);
  81. // Auto add file to source control
  82. AzToolsFramework::SourceControlCommandBus::Broadcast(&AzToolsFramework::SourceControlCommandBus::Events::RequestEdit,
  83. duplicateFilePath.c_str(), true, [](bool, const AzToolsFramework::SourceControlFileInfo&) {});
  84. }
  85. });
  86. QMenu* scriptsMenu = menu->addMenu(QObject::tr("Python Scripts"));
  87. const AZStd::vector<AZStd::string> arguments{ entry->GetFullPath() };
  88. AddRegisteredScriptToMenu(scriptsMenu, "/O3DE/AtomToolsFramework/AssetBrowser/ContextMenuScripts", arguments);
  89. }
  90. void AtomToolsAssetBrowserInteractions::AddContextMenuActionsForFolderEntries(
  91. QWidget* caller, QMenu* menu, const AzToolsFramework::AssetBrowser::AssetBrowserEntry* entry)
  92. {
  93. menu->addAction(QObject::tr("Create new sub folder..."), [caller, entry]()
  94. {
  95. bool ok = false;
  96. QString newFolderName = QInputDialog::getText(caller, "Enter new folder name", "name:", QLineEdit::Normal, "NewFolder", &ok);
  97. if (ok)
  98. {
  99. if (newFolderName.isEmpty())
  100. {
  101. QMessageBox msgBox(QMessageBox::Icon::Critical, "Error", "Folder name can't be empty", QMessageBox::Ok, caller);
  102. msgBox.exec();
  103. }
  104. else
  105. {
  106. AZStd::string newFolderPath;
  107. AzFramework::StringFunc::Path::Join(entry->GetFullPath().c_str(), newFolderName.toUtf8().constData(), newFolderPath);
  108. QDir dir(newFolderPath.c_str());
  109. if (dir.exists())
  110. {
  111. QMessageBox::critical(caller, "Error", "Folder with this name already exists");
  112. return;
  113. }
  114. auto result = dir.mkdir(newFolderPath.c_str());
  115. if (!result)
  116. {
  117. AZ_Error("MaterialBrowser", false, "Failed to make new folder");
  118. return;
  119. }
  120. }
  121. }
  122. });
  123. }
  124. void AtomToolsAssetBrowserInteractions::AddContextMenuActionsForAllEntries(
  125. [[maybe_unused]] QWidget* caller, QMenu* menu, const AzToolsFramework::AssetBrowser::AssetBrowserEntry* entry)
  126. {
  127. menu->addAction(AzQtComponents::fileBrowserActionName(), [entry]()
  128. {
  129. AzQtComponents::ShowFileOnDesktop(entry->GetFullPath().c_str());
  130. });
  131. menu->addSeparator();
  132. menu->addAction(QObject::tr("Copy Name To Clipboard"), [=]()
  133. {
  134. QApplication::clipboard()->setText(entry->GetName().c_str());
  135. });
  136. menu->addAction(QObject::tr("Copy Path To Clipboard"), [=]()
  137. {
  138. QApplication::clipboard()->setText(entry->GetFullPath().c_str());
  139. });
  140. }
  141. void AtomToolsAssetBrowserInteractions::AddContextMenuActionsForSourceControl(
  142. [[maybe_unused]] QWidget* caller, QMenu* menu, const AzToolsFramework::AssetBrowser::AssetBrowserEntry* entry)
  143. {
  144. using namespace AzToolsFramework;
  145. bool isActive = false;
  146. SourceControlConnectionRequestBus::BroadcastResult(isActive, &SourceControlConnectionRequests::IsActive);
  147. AZStd::string path = entry->GetFullPath();
  148. if (isActive && ValidateDocumentPath(path))
  149. {
  150. menu->addSeparator();
  151. QMenu* sourceControlMenu = menu->addMenu("Source Control");
  152. // Update the enabled state of source control menu actions only if menu is shown
  153. QMenu::connect(sourceControlMenu, &QMenu::aboutToShow, [this, path]()
  154. {
  155. SourceControlCommandBus::Broadcast(&SourceControlCommandBus::Events::GetFileInfo, path.c_str(),
  156. [this](bool success, const SourceControlFileInfo& info) { UpdateContextMenuActionsForSourceControl(success, info); });
  157. });
  158. // add get latest action
  159. m_getLatestAction = sourceControlMenu->addAction("Get Latest", [path]()
  160. {
  161. SourceControlCommandBus::Broadcast(&SourceControlCommandBus::Events::RequestLatest, path.c_str(),
  162. [](bool, const SourceControlFileInfo&) {});
  163. });
  164. QObject::connect(m_getLatestAction, &QObject::destroyed, [this]()
  165. {
  166. m_getLatestAction = nullptr;
  167. });
  168. m_getLatestAction->setEnabled(false);
  169. // add add action
  170. m_addAction = sourceControlMenu->addAction("Add", [path]()
  171. {
  172. SourceControlCommandBus::Broadcast(&SourceControlCommandBus::Events::RequestEdit, path.c_str(), true,
  173. [path](bool, const SourceControlFileInfo&)
  174. {
  175. SourceControlThumbnailRequestBus::Broadcast(&SourceControlThumbnailRequests::FileStatusChanged, path.c_str());
  176. });
  177. });
  178. QObject::connect(m_addAction, &QObject::destroyed, [this]()
  179. {
  180. m_addAction = nullptr;
  181. });
  182. m_addAction->setEnabled(false);
  183. // add checkout action
  184. m_checkOutAction = sourceControlMenu->addAction("Check Out", [path]()
  185. {
  186. SourceControlCommandBus::Broadcast(&SourceControlCommandBus::Events::RequestEdit, path.c_str(), true,
  187. [path](bool, const SourceControlFileInfo&)
  188. {
  189. SourceControlThumbnailRequestBus::Broadcast(&SourceControlThumbnailRequests::FileStatusChanged, path.c_str());
  190. });
  191. });
  192. QObject::connect(m_checkOutAction, &QObject::destroyed, [this]()
  193. {
  194. m_checkOutAction = nullptr;
  195. });
  196. m_checkOutAction->setEnabled(false);
  197. // add undo checkout action
  198. m_undoCheckOutAction = sourceControlMenu->addAction("Undo Check Out", [path]()
  199. {
  200. SourceControlCommandBus::Broadcast(&SourceControlCommandBus::Events::RequestRevert, path.c_str(),
  201. [path](bool, const SourceControlFileInfo&)
  202. {
  203. SourceControlThumbnailRequestBus::Broadcast(&SourceControlThumbnailRequests::FileStatusChanged, path.c_str());
  204. });
  205. });
  206. QObject::connect(m_undoCheckOutAction, &QObject::destroyed, [this]()
  207. {
  208. m_undoCheckOutAction = nullptr;
  209. });
  210. m_undoCheckOutAction->setEnabled(false);
  211. }
  212. }
  213. void AtomToolsAssetBrowserInteractions::UpdateContextMenuActionsForSourceControl(bool success, AzToolsFramework::SourceControlFileInfo info)
  214. {
  215. if (!success && m_caller)
  216. {
  217. QMessageBox::critical(m_caller, "Error", "Source control operation failed.");
  218. }
  219. if (m_getLatestAction)
  220. {
  221. m_getLatestAction->setEnabled(info.IsManaged() && info.HasFlag(AzToolsFramework::SCF_OutOfDate));
  222. }
  223. if (m_addAction)
  224. {
  225. m_addAction->setEnabled(!info.IsManaged());
  226. }
  227. if (m_checkOutAction)
  228. {
  229. m_checkOutAction->setEnabled(info.IsManaged() && info.IsReadOnly() && !info.IsLockedByOther());
  230. }
  231. if (m_undoCheckOutAction)
  232. {
  233. m_undoCheckOutAction->setEnabled(info.IsManaged() && !info.IsReadOnly());
  234. }
  235. }
  236. } // namespace AtomToolsFramework