3
0

PivotTreeWidget.cpp 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422
  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 <AzCore/Asset/AssetManagerBus.h>
  9. #include <ScriptCanvas/Bus/RequestBus.h>
  10. #include <Editor/View/Widgets/LoggingPanel/PivotTree/PivotTreeWidget.h>
  11. // Disable warnings in moc code
  12. AZ_PUSH_DISABLE_WARNING(4251 4800, "-Wunknown-warning-option")
  13. #include <Editor/View/Widgets/LoggingPanel/PivotTree/ui_PivotTreeWidget.h>
  14. AZ_POP_DISABLE_WARNING
  15. namespace ScriptCanvasEditor
  16. {
  17. //////////////////
  18. // PivotTreeItem
  19. //////////////////
  20. PivotTreeItem::PivotTreeItem()
  21. : m_isPivotElement(false)
  22. {
  23. }
  24. PivotTreeItem::~PivotTreeItem()
  25. {
  26. }
  27. const LoggingDataId& PivotTreeItem::GetLoggingDataId() const
  28. {
  29. return m_loggingDataId;
  30. }
  31. int PivotTreeItem::GetColumnCount() const
  32. {
  33. return Column::Count;
  34. }
  35. Qt::ItemFlags PivotTreeItem::Flags([[maybe_unused]] const QModelIndex& index) const
  36. {
  37. Qt::ItemFlags flags = Qt::ItemFlag::ItemIsEnabled | Qt::ItemFlag::ItemIsSelectable | Qt::ItemFlag::ItemIsUserCheckable;
  38. if (m_isPivotElement)
  39. {
  40. flags |= Qt::ItemFlag::ItemIsAutoTristate;
  41. }
  42. return flags;
  43. }
  44. QVariant PivotTreeItem::Data(const QModelIndex& index, int role) const
  45. {
  46. switch (index.column())
  47. {
  48. case Column::Name:
  49. {
  50. if (role == Qt::DisplayRole
  51. || role == Qt::ToolTip)
  52. {
  53. return QString(GetDisplayName().c_str());
  54. }
  55. else if (role == Qt::CheckStateRole)
  56. {
  57. return GetCheckState();
  58. }
  59. }
  60. break;
  61. default:
  62. break;
  63. }
  64. return QVariant();
  65. }
  66. bool PivotTreeItem::SetData(const QModelIndex& index, const QVariant& value, int role)
  67. {
  68. switch (index.column())
  69. {
  70. case Column::Name:
  71. {
  72. if (role == Qt::CheckStateRole)
  73. {
  74. Qt::CheckState checkState = value.value<Qt::CheckState>();
  75. // Never want to let the user interaction set it to
  76. if (checkState == Qt::CheckState::PartiallyChecked)
  77. {
  78. if (GetCheckState() == Qt::CheckState::Unchecked)
  79. {
  80. checkState = Qt::CheckState::Checked;
  81. }
  82. else
  83. {
  84. checkState = Qt::CheckState::Unchecked;
  85. }
  86. }
  87. SetCheckState(checkState);
  88. }
  89. }
  90. break;
  91. default:
  92. break;
  93. }
  94. return false;
  95. }
  96. void PivotTreeItem::OnChildAdded(GraphCanvasTreeItem* treeItem)
  97. {
  98. if (m_loggingDataId.IsValid())
  99. {
  100. static_cast<PivotTreeItem*>(treeItem)->SetLoggingDataId(m_loggingDataId);
  101. }
  102. }
  103. void PivotTreeItem::OnLoggingDataIdSet()
  104. {
  105. }
  106. void PivotTreeItem::SetLoggingDataId(const LoggingDataId& dataId)
  107. {
  108. if (dataId != m_loggingDataId)
  109. {
  110. m_loggingDataId = dataId;
  111. for (int i = 0; i < GetChildCount(); ++i)
  112. {
  113. PivotTreeItem* treeItem = static_cast<PivotTreeItem*>(FindChildByRow(i));
  114. treeItem->SetLoggingDataId(dataId);
  115. }
  116. OnLoggingDataIdSet();
  117. }
  118. }
  119. void PivotTreeItem::SetIsPivotedElement(bool isPivotElement)
  120. {
  121. m_isPivotElement = isPivotElement;
  122. }
  123. ////////////////////////
  124. // PivotTreeEntityItem
  125. ////////////////////////
  126. PivotTreeEntityItem::PivotTreeEntityItem(const AZ::NamedEntityId& namedEntityId)
  127. : m_namedEntityId(namedEntityId)
  128. {
  129. }
  130. const AZ::NamedEntityId& PivotTreeEntityItem::GetNamedEntityId() const
  131. {
  132. return m_namedEntityId;
  133. }
  134. AZStd::string PivotTreeEntityItem::GetDisplayName() const
  135. {
  136. return m_namedEntityId.ToString();
  137. }
  138. const AZ::EntityId& PivotTreeEntityItem::GetEntityId() const
  139. {
  140. return m_namedEntityId;
  141. }
  142. ///////////////////////
  143. // PivotTreeGraphItem
  144. ///////////////////////
  145. PivotTreeGraphItem::PivotTreeGraphItem(const AZ::Data::AssetId& assetId)
  146. : m_assetId(assetId)
  147. {
  148. // Determine the file name for our asset
  149. AZStd::string fullPath;
  150. AZ::Data::AssetCatalogRequestBus::BroadcastResult(fullPath, &AZ::Data::AssetCatalogRequests::GetAssetPathById, m_assetId);
  151. AZStd::size_t indexOf = fullPath.find_last_of('/');
  152. if (indexOf == AZStd::string::npos)
  153. {
  154. m_assetName = fullPath;
  155. m_assetPath = "";
  156. }
  157. else
  158. {
  159. m_assetPath = fullPath.substr(0, indexOf);
  160. m_assetName = fullPath.substr(indexOf + 1);
  161. }
  162. }
  163. AZStd::string PivotTreeGraphItem::GetDisplayName() const
  164. {
  165. return m_assetName;
  166. }
  167. const AZ::Data::AssetId& PivotTreeGraphItem::GetAssetId() const
  168. {
  169. return m_assetId;
  170. }
  171. AZStd::string_view PivotTreeGraphItem::GetAssetPath() const
  172. {
  173. return m_assetPath;
  174. }
  175. //////////////////
  176. // PivotTreeRoot
  177. //////////////////
  178. void PivotTreeRoot::SwitchDataSource(const LoggingDataId& aggregateDataSource)
  179. {
  180. SetLoggingDataId(aggregateDataSource);
  181. OnDataSourceChanged(aggregateDataSource);
  182. }
  183. Qt::CheckState PivotTreeRoot::GetCheckState() const
  184. {
  185. return Qt::CheckState::Unchecked;
  186. }
  187. void PivotTreeRoot::SetCheckState(Qt::CheckState checkState)
  188. {
  189. AZ_UNUSED(checkState);
  190. }
  191. AZStd::string PivotTreeRoot::GetDisplayName() const
  192. {
  193. return "";
  194. }
  195. ////////////////////////////
  196. // PivotTreeSortProxyModel
  197. ////////////////////////////
  198. bool PivotTreeSortProxyModel::filterAcceptsRow(int sourceRow, const QModelIndex& sourceParent) const
  199. {
  200. if (m_filter.isEmpty())
  201. {
  202. return true;
  203. }
  204. QAbstractItemModel* model = sourceModel();
  205. QModelIndex index = model->index(sourceRow, PivotTreeItem::Column::Name, sourceParent);
  206. QString test = model->data(index, Qt::DisplayRole).toString();
  207. bool showRow = test.lastIndexOf(m_filterRegex) >= 0;
  208. // Handle showing ourselves if a child is being displayed
  209. if (!showRow && sourceModel()->hasChildren(index))
  210. {
  211. for (int i = 0; i < sourceModel()->rowCount(index); ++i)
  212. {
  213. if (filterAcceptsRow(i, index))
  214. {
  215. showRow = true;
  216. break;
  217. }
  218. }
  219. }
  220. // We also want to display ourselves if any of our parents match the filter
  221. QModelIndex parentIndex = sourceModel()->parent(index);
  222. while (!showRow && parentIndex.isValid())
  223. {
  224. QString test2 = model->data(parentIndex).toString();
  225. showRow = test2.contains(m_filterRegex);
  226. parentIndex = sourceModel()->parent(parentIndex);
  227. }
  228. return showRow;
  229. }
  230. bool PivotTreeSortProxyModel::HasFilter() const
  231. {
  232. return !m_filter.isEmpty();
  233. }
  234. void PivotTreeSortProxyModel::SetFilter(const QString& filter)
  235. {
  236. m_filter = filter;
  237. m_filterRegex = QRegExp(m_filter, Qt::CaseInsensitive);
  238. invalidateFilter();
  239. }
  240. void PivotTreeSortProxyModel::ClearFilter()
  241. {
  242. if (HasFilter())
  243. {
  244. SetFilter("");
  245. }
  246. }
  247. ////////////////////
  248. // PivotTreeWidget
  249. ////////////////////
  250. PivotTreeWidget::PivotTreeWidget(PivotTreeRoot* pivotRoot, const AZ::Crc32& savingId, QWidget* parent)
  251. : QWidget(parent)
  252. , m_ui(new Ui::PivotTreeWidget())
  253. {
  254. m_ui->setupUi(this);
  255. m_pivotRoot = pivotRoot;
  256. m_treeModel = aznew GraphCanvas::GraphCanvasTreeModel(pivotRoot);
  257. m_proxyModel = aznew PivotTreeSortProxyModel();
  258. m_proxyModel->ClearFilter();
  259. m_proxyModel->setSourceModel(m_treeModel);
  260. m_proxyModel->setSortCaseSensitivity(Qt::CaseInsensitive);
  261. m_ui->pivotTreeView->setModel(m_proxyModel);
  262. m_ui->pivotTreeView->sortByColumn(PivotTreeItem::Column::Name, Qt::SortOrder::AscendingOrder);
  263. m_ui->pivotTreeView->header()->setHidden(true);
  264. m_ui->pivotTreeView->header()->setStretchLastSection(false);
  265. m_ui->pivotTreeView->header()->setSectionResizeMode(PivotTreeItem::Column::Name, QHeaderView::ResizeMode::Stretch);
  266. m_ui->pivotTreeView->header()->setSectionResizeMode(PivotTreeItem::Column::QT_NEEDS_A_SECOND_COLUMN_FOR_THIS_MODEL_TO_WORK_FOR_SOME_REASON, QHeaderView::ResizeMode::Fixed);
  267. m_ui->pivotTreeView->header()->resizeSection(PivotTreeItem::Column::QT_NEEDS_A_SECOND_COLUMN_FOR_THIS_MODEL_TO_WORK_FOR_SOME_REASON, 1);
  268. QObject::connect(m_ui->filterWidget, &AzQtComponents::FilteredSearchWidget::TextFilterChanged, this, &PivotTreeWidget::OnFilterChanged);
  269. m_ui->filterWidget->SetFilterInputInterval(AZStd::chrono::milliseconds(250));
  270. m_ui->pivotTreeView->InitializeTreeViewSaving(savingId);
  271. m_ui->pivotTreeView->PauseTreeViewSaving();
  272. QObject::connect(m_ui->pivotTreeView, &QTreeView::doubleClicked, this, &PivotTreeWidget::OnItemDoubleClicked);
  273. }
  274. PivotTreeWidget::~PivotTreeWidget()
  275. {
  276. }
  277. void PivotTreeWidget::DisplayTree()
  278. {
  279. OnTreeDisplayed();
  280. }
  281. void PivotTreeWidget::SwitchDataSource(const LoggingDataId& aggregateDataSource)
  282. {
  283. {
  284. QSignalBlocker signalBlocker(m_ui->filterWidget);
  285. m_ui->filterWidget->ClearTextFilter();
  286. OnFilterChanged("");
  287. }
  288. m_pivotRoot->SwitchDataSource(aggregateDataSource);
  289. }
  290. void PivotTreeWidget::OnFilterChanged(const QString& activeTextFilter)
  291. {
  292. bool hadFilter = m_proxyModel->HasFilter();
  293. if (!m_proxyModel->HasFilter() && !activeTextFilter.isEmpty())
  294. {
  295. m_ui->pivotTreeView->UnpauseTreeViewSaving();
  296. m_ui->pivotTreeView->CaptureTreeViewSnapshot();
  297. m_ui->pivotTreeView->PauseTreeViewSaving();
  298. }
  299. m_proxyModel->SetFilter(activeTextFilter);
  300. if (hadFilter && !m_proxyModel->HasFilter())
  301. {
  302. m_ui->pivotTreeView->UnpauseTreeViewSaving();
  303. m_ui->pivotTreeView->ApplyTreeViewSnapshot();
  304. m_ui->pivotTreeView->PauseTreeViewSaving();
  305. }
  306. else if (m_proxyModel->HasFilter())
  307. {
  308. m_ui->pivotTreeView->expandAll();
  309. }
  310. }
  311. PivotTreeRoot* PivotTreeWidget::GetTreeRoot()
  312. {
  313. return m_pivotRoot;
  314. }
  315. void PivotTreeWidget::OnTreeDisplayed()
  316. {
  317. }
  318. void PivotTreeWidget::OnItemDoubleClicked(const QModelIndex& modelIndex)
  319. {
  320. QModelIndex sourceIndex = modelIndex;
  321. QSortFilterProxyModel* proxyModel = qobject_cast<QSortFilterProxyModel*>(m_ui->pivotTreeView->model());
  322. if (proxyModel)
  323. {
  324. sourceIndex = proxyModel->mapToSource(modelIndex);
  325. }
  326. if (PivotTreeItem* pivotTreeItem = static_cast<PivotTreeItem*>(sourceIndex.internalPointer()))
  327. {
  328. if (PivotTreeGraphItem* graphItem = azrtti_cast<PivotTreeGraphItem*>(pivotTreeItem))
  329. {
  330. GeneralRequestBus::Broadcast
  331. ( &GeneralRequests::OpenScriptCanvasAssetId
  332. , SourceHandle(nullptr, graphItem->GetAssetId().m_guid)
  333. , Tracker::ScriptCanvasFileState::UNMODIFIED);
  334. }
  335. }
  336. }
  337. #include <Editor/View/Widgets/LoggingPanel/PivotTree/moc_PivotTreeWidget.cpp>
  338. }