LoggingWindowSession.cpp 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481
  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 <QScrollBar>
  9. #include <QGraphicsItem>
  10. #include <QScopedValueRollback>
  11. #include <GraphCanvas/Components/Nodes/NodeTitleBus.h>
  12. #include <GraphCanvas/Components/SceneBus.h>
  13. #include <GraphCanvas/Components/Slots/SlotBus.h>
  14. #include <GraphCanvas/Components/VisualBus.h>
  15. #include <GraphCanvas/Widgets/StyledItemDelegates/IconDecoratedNameDelegate.h>
  16. #include <GraphCanvas/Utils/GraphUtils.h>
  17. #include <Editor/View/Widgets/AssetGraphSceneDataBus.h>
  18. #include <Editor/View/Widgets/LoggingPanel/LoggingWindowSession.h>
  19. #include <ScriptCanvas/Bus/RequestBus.h>
  20. #include <ScriptCanvas/GraphCanvas/MappingBus.h>
  21. #include <Editor/GraphCanvas/GraphCanvasEditorNotificationBusId.h>
  22. namespace ScriptCanvasEditor
  23. {
  24. /////////////////////////////
  25. // LoggingWindowFilterModel
  26. /////////////////////////////
  27. bool LoggingWindowFilterModel::filterAcceptsRow(int sourceRow, const QModelIndex& sourceParent) const
  28. {
  29. if (m_logFilter.IsEmpty())
  30. {
  31. return true;
  32. }
  33. QAbstractItemModel* model = sourceModel();
  34. QModelIndex index = model->index(sourceRow, 0, sourceParent);
  35. DebugLogTreeItem* treeItem = static_cast<DebugLogTreeItem*>(index.internalPointer());
  36. if (treeItem)
  37. {
  38. return treeItem->MatchesFilter(m_logFilter);
  39. }
  40. return false;
  41. }
  42. void LoggingWindowFilterModel::SetFilter(const QString& filter)
  43. {
  44. m_filter = filter;
  45. m_logFilter.m_filter = QRegExp(m_filter, Qt::CaseInsensitive);
  46. invalidateFilter();
  47. }
  48. void LoggingWindowFilterModel::ClearFilter()
  49. {
  50. SetFilter("");
  51. }
  52. bool LoggingWindowFilterModel::HasFilter() const
  53. {
  54. return !m_filter.isEmpty();
  55. }
  56. /////////////////////////
  57. // LoggingWindowSession
  58. /////////////////////////
  59. LoggingWindowSession::LoggingWindowSession(QWidget* parentWidget)
  60. : QWidget(parentWidget)
  61. , m_ui(new Ui::LoggingWindowSession())
  62. , m_clearSelectionOnSceneSelectionChange(true)
  63. , m_scrollToBottom(true)
  64. , m_debugRoot(nullptr)
  65. , m_treeModel(nullptr)
  66. , m_filterModel(nullptr)
  67. {
  68. m_ui->setupUi(this);
  69. QObject::connect(m_ui->captureButton, &QToolButton::clicked, this, &LoggingWindowSession::OnCaptureButtonPressed);
  70. QObject::connect(m_ui->expandAll, &QToolButton::clicked, this, &LoggingWindowSession::OnExpandAll);
  71. QObject::connect(m_ui->collapseAll, &QToolButton::clicked, this, &LoggingWindowSession::OnCollapseAll);
  72. QObject::connect(m_ui->targetSelector, static_cast<void(QComboBox::*)(int)>(&QComboBox::currentIndexChanged), this, &LoggingWindowSession::OnTargetChanged);
  73. QObject::connect(m_ui->logTree->verticalScrollBar(), &QScrollBar::valueChanged, this, &LoggingWindowSession::OnLogScrolled);
  74. QObject::connect(m_ui->logTree, &QTreeView::expanded, this, &LoggingWindowSession::OnLogItemExpanded);
  75. QObject::connect(m_ui->logTree->verticalScrollBar(), &QScrollBar::rangeChanged, this, &LoggingWindowSession::OnLogRangeChanged);
  76. QObject::connect(m_ui->filterWidget, &AzQtComponents::FilteredSearchWidget::TextFilterChanged, this, &LoggingWindowSession::OnSearchFilterChanged);
  77. m_ui->filterWidget->SetFilterInputInterval(AZStd::chrono::milliseconds(250));
  78. m_ui->logTree->setMouseTracking(true);
  79. QObject::connect(m_ui->logTree, &QTreeView::clicked, this, &LoggingWindowSession::OnLogClicked);
  80. QObject::connect(m_ui->logTree, &QTreeView::doubleClicked, this, &LoggingWindowSession::OnLogDoubleClicked);
  81. m_focusDelayTimer.setInterval(125);
  82. m_focusDelayTimer.setSingleShot(true);
  83. QObject::connect(&m_focusDelayTimer, &QTimer::timeout, this, &LoggingWindowSession::HandleQueuedFocus);
  84. GraphCanvas::AssetEditorNotificationBus::Handler::BusConnect(ScriptCanvasEditor::AssetEditorId);
  85. AZ::EntityId graphCanvasId;
  86. GeneralRequestBus::BroadcastResult(graphCanvasId, &GeneralRequests::GetActiveGraphCanvasGraphId);
  87. OnActiveGraphChanged(graphCanvasId);
  88. }
  89. LoggingWindowSession::~LoggingWindowSession()
  90. {
  91. }
  92. const LoggingDataId& LoggingWindowSession::GetDataId() const
  93. {
  94. return m_loggingDataId;
  95. }
  96. void LoggingWindowSession::ClearFilter()
  97. {
  98. m_ui->filterWidget->ClearTextFilter();
  99. }
  100. void LoggingWindowSession::OnActiveGraphChanged(const AZ::EntityId& graphId)
  101. {
  102. ClearLoggingSelection();
  103. if (GraphCanvas::SceneNotificationBus::Handler::BusIsConnected())
  104. {
  105. GraphCanvas::SceneNotificationBus::Handler::BusDisconnect();
  106. }
  107. if (graphId.IsValid())
  108. {
  109. GraphCanvas::SceneNotificationBus::Handler::BusConnect(graphId);
  110. }
  111. }
  112. void LoggingWindowSession::OnSelectionChanged()
  113. {
  114. ClearLoggingSelection();
  115. }
  116. void LoggingWindowSession::RegisterTreeRoot(DebugLogRootItem* debugRoot)
  117. {
  118. m_debugRoot = debugRoot;
  119. m_treeModel = aznew GraphCanvas::GraphCanvasTreeModel(debugRoot, this);
  120. m_filterModel = aznew LoggingWindowFilterModel();
  121. m_filterModel->setSourceModel(m_treeModel);
  122. m_ui->logTree->setModel(m_filterModel);
  123. m_ui->logTree->header()->setStretchLastSection(false);
  124. m_ui->logTree->header()->setSectionResizeMode(DebugLogTreeItem::Column::NodeName, QHeaderView::ResizeMode::Stretch);
  125. m_ui->logTree->header()->setSectionResizeMode(DebugLogTreeItem::Column::Input, QHeaderView::ResizeMode::Stretch);
  126. m_ui->logTree->header()->setSectionResizeMode(DebugLogTreeItem::Column::Output, QHeaderView::ResizeMode::Stretch);
  127. m_ui->logTree->header()->setSectionResizeMode(DebugLogTreeItem::Column::TimeStep, QHeaderView::ResizeMode::Fixed);
  128. m_ui->logTree->header()->resizeSection(DebugLogTreeItem::Column::TimeStep, 75);
  129. m_ui->logTree->header()->setSectionResizeMode(DebugLogTreeItem::Column::ScriptName, QHeaderView::ResizeMode::Fixed);
  130. m_ui->logTree->header()->resizeSection(DebugLogTreeItem::Column::ScriptName, 150);
  131. m_ui->logTree->header()->setSectionResizeMode(DebugLogTreeItem::Column::SourceEntity, QHeaderView::ResizeMode::Fixed);
  132. m_ui->logTree->header()->resizeSection(DebugLogTreeItem::Column::SourceEntity, 200);
  133. m_ui->logTree->setItemDelegateForColumn(DebugLogTreeItem::Column::NodeName, aznew GraphCanvas::IconDecoratedNameDelegate(this));
  134. QObject::connect(m_ui->logTree->selectionModel(), &QItemSelectionModel::selectionChanged, this, &LoggingWindowSession::OnLogSelectionChanged);
  135. }
  136. void LoggingWindowSession::SetDataId(const LoggingDataId& loggingDataId)
  137. {
  138. if (!m_loggingDataId.IsValid())
  139. {
  140. m_loggingDataId = loggingDataId;
  141. }
  142. }
  143. void LoggingWindowSession::OnExpandAll()
  144. {
  145. m_ui->logTree->expandAll();
  146. ScrollToSelection();
  147. }
  148. void LoggingWindowSession::OnCollapseAll()
  149. {
  150. m_ui->logTree->collapseAll();
  151. ScrollToSelection();
  152. }
  153. void LoggingWindowSession::OnSearchFilterChanged(const QString& filterString)
  154. {
  155. m_filterModel->SetFilter(filterString);
  156. }
  157. void LoggingWindowSession::OnLogScrolled(int value)
  158. {
  159. if (m_ui->logTree->verticalScrollBar()->isEnabled())
  160. {
  161. if (m_ui->logTree->verticalScrollBar()->maximum() == value)
  162. {
  163. m_scrollToBottom = true;
  164. }
  165. else
  166. {
  167. m_scrollToBottom = false;
  168. }
  169. }
  170. else
  171. {
  172. m_scrollToBottom = true;
  173. }
  174. }
  175. void LoggingWindowSession::OnLogItemExpanded([[maybe_unused]] const QModelIndex& modelIndex)
  176. {
  177. m_scrollToBottom = false;
  178. }
  179. void LoggingWindowSession::OnLogRangeChanged([[maybe_unused]] int min, int max)
  180. {
  181. if (m_scrollToBottom)
  182. {
  183. m_ui->logTree->verticalScrollBar()->setValue(max);
  184. }
  185. if (!m_ui->logTree->verticalScrollBar()->isEnabled())
  186. {
  187. m_scrollToBottom = true;
  188. }
  189. }
  190. void LoggingWindowSession::OnLogClicked(const QModelIndex& modelIndex)
  191. {
  192. if (modelIndex.column() == DebugLogTreeItem::Column::ScriptName)
  193. {
  194. QModelIndex sourceIndex = m_filterModel->mapToSource(modelIndex);
  195. DebugLogTreeItem* treeItem = static_cast<DebugLogTreeItem*>(sourceIndex.internalPointer());
  196. if (ExecutionLogTreeItem* executionItem = azrtti_cast<ExecutionLogTreeItem*>(treeItem))
  197. {
  198. QScopedValueRollback<bool> valueRollback(m_clearSelectionOnSceneSelectionChange, false);
  199. const AZ::Data::AssetId& assetId = executionItem->GetAssetId();
  200. GeneralRequestBus::Broadcast(&GeneralRequests::OpenScriptCanvasAssetId
  201. , SourceHandle(nullptr, assetId.m_guid), Tracker::ScriptCanvasFileState::UNMODIFIED);
  202. }
  203. }
  204. }
  205. void LoggingWindowSession::OnLogDoubleClicked(const QModelIndex& modelIndex)
  206. {
  207. ExecutionLogTreeItem* executionItem = ResolveExecutionItem(modelIndex);
  208. if (executionItem)
  209. {
  210. const AZ::Data::AssetId& assetId = executionItem->GetAssetId();
  211. bool isAssetOpen = false;
  212. GeneralRequestBus::BroadcastResult(isAssetOpen, &GeneralRequests::IsScriptCanvasAssetOpen, SourceHandle(nullptr, assetId.m_guid));
  213. GeneralRequestBus::Broadcast(&GeneralRequests::OpenScriptCanvasAssetId
  214. , SourceHandle(nullptr, assetId.m_guid), Tracker::ScriptCanvasFileState::UNMODIFIED);
  215. AZ::EntityId graphCanvasGraphId;
  216. GeneralRequestBus::BroadcastResult(graphCanvasGraphId, &GeneralRequests::FindGraphCanvasGraphIdByAssetId
  217. , SourceHandle(nullptr, assetId.m_guid));
  218. if (isAssetOpen)
  219. {
  220. FocusOnElement(assetId, executionItem->GetScriptCanvasAssetNodeId());
  221. }
  222. else
  223. {
  224. m_assetId = assetId;
  225. m_assetNodeId = executionItem->GetScriptCanvasAssetNodeId();
  226. m_focusDelayTimer.stop();
  227. m_focusDelayTimer.start();
  228. }
  229. }
  230. }
  231. void LoggingWindowSession::OnLogSelectionChanged(const QItemSelection& selected, const QItemSelection& deselected)
  232. {
  233. for (const QModelIndex& deselectedIndex : deselected.indexes())
  234. {
  235. if (deselectedIndex.column() != 0)
  236. {
  237. continue;
  238. }
  239. ExecutionLogTreeItem* executionItem = ResolveExecutionItem(deselectedIndex);
  240. if (executionItem)
  241. {
  242. RemoveHighlight(executionItem->GetAssetId(), executionItem->GetScriptCanvasAssetNodeId());
  243. }
  244. }
  245. for (const QModelIndex& selectedIndex : selected.indexes())
  246. {
  247. if (selectedIndex.column() != 0)
  248. {
  249. continue;
  250. }
  251. ExecutionLogTreeItem* executionItem = ResolveExecutionItem(selectedIndex);
  252. if (executionItem)
  253. {
  254. HighlightElement(executionItem->GetAssetId(), executionItem->GetScriptCanvasAssetNodeId());
  255. }
  256. }
  257. }
  258. ExecutionLogTreeItem* LoggingWindowSession::ResolveExecutionItem(const QModelIndex& proxyModelIndex)
  259. {
  260. QModelIndex sourceIndex = m_filterModel->mapToSource(proxyModelIndex);
  261. DebugLogTreeItem* treeItem = static_cast<DebugLogTreeItem*>(sourceIndex.internalPointer());
  262. DebugLogTreeItem* parentItem = static_cast<DebugLogTreeItem*>(treeItem->GetParent());
  263. ExecutionLogTreeItem* executionItem = azrtti_cast<ExecutionLogTreeItem*>(treeItem);
  264. while (executionItem == nullptr && parentItem != nullptr)
  265. {
  266. executionItem = azrtti_cast<ExecutionLogTreeItem*>(parentItem);
  267. parentItem = static_cast<DebugLogTreeItem*>(parentItem->GetParent());
  268. }
  269. return executionItem;
  270. }
  271. void LoggingWindowSession::HandleQueuedFocus()
  272. {
  273. AZ::EntityId activeGraphCanvasGraphId;
  274. GeneralRequestBus::BroadcastResult(activeGraphCanvasGraphId, &GeneralRequests::GetActiveGraphCanvasGraphId);
  275. AZ::EntityId graphCanvasGraphId;
  276. GeneralRequestBus::BroadcastResult(graphCanvasGraphId, &GeneralRequests::FindGraphCanvasGraphIdByAssetId, SourceHandle(nullptr, m_assetId.m_guid));
  277. if (activeGraphCanvasGraphId == graphCanvasGraphId)
  278. {
  279. FocusOnElement(m_assetId, m_assetNodeId);
  280. m_focusDelayTimer.stop();
  281. m_assetId.SetInvalid();
  282. m_assetNodeId.SetInvalid();
  283. }
  284. }
  285. void LoggingWindowSession::FocusOnElement(const AZ::Data::AssetId& assetId, const AZ::EntityId& assetNodeId)
  286. {
  287. GraphCanvas::FocusConfig focusConfig;
  288. AZ::EntityId scriptCanvasNodeId;
  289. AssetGraphSceneBus::BroadcastResult(scriptCanvasNodeId, &AssetGraphScene::FindEditorNodeIdByAssetNodeId, SourceHandle(nullptr, assetId.m_guid), assetNodeId);
  290. GraphCanvas::NodeId graphCanvasNodeId;
  291. SceneMemberMappingRequestBus::EventResult(graphCanvasNodeId, scriptCanvasNodeId, &SceneMemberMappingRequests::GetGraphCanvasEntityId);
  292. if (GraphCanvas::GraphUtils::IsNodeGroup(graphCanvasNodeId))
  293. {
  294. focusConfig.m_spacingType = GraphCanvas::FocusConfig::SpacingType::GridStep;
  295. focusConfig.m_spacingAmount = 1;
  296. }
  297. else
  298. {
  299. focusConfig.m_spacingType = GraphCanvas::FocusConfig::SpacingType::Scalar;
  300. focusConfig.m_spacingAmount = 2;
  301. }
  302. AZStd::vector< AZ::EntityId > memberIds = { graphCanvasNodeId };
  303. GraphCanvas::GraphUtils::FocusOnElements(memberIds, focusConfig);
  304. {
  305. QScopedValueRollback<bool> maintainSelection(m_clearSelectionOnSceneSelectionChange, false);
  306. RemoveHighlight(assetId, assetNodeId);
  307. GraphCanvas::GraphId graphId;
  308. GraphCanvas::SceneMemberRequestBus::EventResult(graphId, graphCanvasNodeId, &GraphCanvas::SceneMemberRequests::GetScene);
  309. GraphCanvas::SceneRequestBus::Event(graphId, &GraphCanvas::SceneRequests::ClearSelection);
  310. GraphCanvas::SceneMemberUIRequestBus::Event(graphCanvasNodeId, &GraphCanvas::SceneMemberUIRequests::SetSelected, true);
  311. }
  312. }
  313. void LoggingWindowSession::HighlightElement(const AZ::Data::AssetId& assetId, const AZ::EntityId& assetNodeId)
  314. {
  315. AZ::EntityId graphCanvasGraphId;
  316. GeneralRequestBus::BroadcastResult(graphCanvasGraphId, &GeneralRequests::FindGraphCanvasGraphIdByAssetId, SourceHandle(nullptr, assetId.m_guid));
  317. if (graphCanvasGraphId.IsValid())
  318. {
  319. AZ::EntityId scriptCanvasNodeId;
  320. AssetGraphSceneBus::BroadcastResult(scriptCanvasNodeId, &AssetGraphScene::FindEditorNodeIdByAssetNodeId, SourceHandle(nullptr, assetId.m_guid), assetNodeId);
  321. GraphCanvas::NodeId graphCanvasNodeId;
  322. SceneMemberMappingRequestBus::EventResult(graphCanvasNodeId, scriptCanvasNodeId, &SceneMemberMappingRequests::GetGraphCanvasEntityId);
  323. GraphCanvas::SceneMemberGlowOutlineConfiguration glowConfiguration;
  324. glowConfiguration.m_sceneMember = graphCanvasNodeId;
  325. glowConfiguration.m_blurRadius = 0; // #17174 using blur degrades performance
  326. glowConfiguration.m_pen = QPen();
  327. glowConfiguration.m_pen.setBrush(QColor(243, 129, 29));
  328. glowConfiguration.m_pen.setWidth(5);
  329. glowConfiguration.m_pulseRate = AZStd::chrono::milliseconds(2500);
  330. glowConfiguration.m_zValue = 0;
  331. GraphCanvas::GraphicsEffectId effectId;
  332. GraphCanvas::SceneRequestBus::EventResult(effectId, graphCanvasGraphId, &GraphCanvas::SceneRequests::CreateGlowOnSceneMember, glowConfiguration);
  333. auto effectIter = m_highlightEffects.find(assetNodeId);
  334. if (effectIter != m_highlightEffects.end())
  335. {
  336. GraphCanvas::SceneRequestBus::Event(graphCanvasGraphId, &GraphCanvas::SceneRequests::CancelGraphicsEffect, effectIter->second);
  337. }
  338. m_highlightEffects[assetNodeId] = effectId;
  339. }
  340. }
  341. void LoggingWindowSession::RemoveHighlight(const AZ::Data::AssetId& assetId, const AZ::EntityId& assetNodeId)
  342. {
  343. auto effectIter = m_highlightEffects.find(assetNodeId);
  344. if (effectIter != m_highlightEffects.end())
  345. {
  346. AZ::EntityId graphCanvasGraphId;
  347. GeneralRequestBus::BroadcastResult(graphCanvasGraphId, &GeneralRequests::FindGraphCanvasGraphIdByAssetId, SourceHandle(nullptr, assetId.m_guid));
  348. if (graphCanvasGraphId.IsValid())
  349. {
  350. GraphCanvas::SceneRequestBus::Event(graphCanvasGraphId, &GraphCanvas::SceneRequests::CancelGraphicsEffect, effectIter->second);
  351. }
  352. m_highlightEffects.erase(effectIter);
  353. }
  354. }
  355. void LoggingWindowSession::ScrollToSelection()
  356. {
  357. for (auto selectedIndex : m_ui->logTree->selectionModel()->selectedIndexes())
  358. {
  359. m_ui->logTree->scrollTo(selectedIndex);
  360. }
  361. }
  362. void LoggingWindowSession::ClearLoggingSelection()
  363. {
  364. if (m_clearSelectionOnSceneSelectionChange)
  365. {
  366. m_ui->logTree->clearSelection();
  367. }
  368. }
  369. #include <Editor/View/Widgets/LoggingPanel/moc_LoggingWindowSession.cpp>
  370. }