EditorPreferencesTreeWidgetItem.cpp 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115
  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 "EditorDefs.h"
  9. #include "EditorPreferencesTreeWidgetItem.h"
  10. // AzCore
  11. #include <AzCore/Component/ComponentApplicationBus.h>
  12. // AzToolsFramework
  13. #include <AzToolsFramework/UI/PropertyEditor/ReflectedPropertyEditor.hxx>
  14. // Editor
  15. #include "IPreferencesPage.h"
  16. #include <AzQtComponents/Components/StyleManager.h>
  17. EditorPreferencesTreeWidgetItem::EditorPreferencesTreeWidgetItem(IPreferencesPage* page, const QPixmap& selectedImage, QPixmap& unselectedImage)
  18. : QTreeWidgetItem(EditorPreferencesPage)
  19. , m_preferencesPage(page)
  20. , m_selectedImage(selectedImage)
  21. , m_unselectedImage(unselectedImage)
  22. , m_entirePageMatchesFilter(true)
  23. {
  24. Setup(page);
  25. }
  26. EditorPreferencesTreeWidgetItem::EditorPreferencesTreeWidgetItem(IPreferencesPage* page, const QIcon& icon)
  27. : QTreeWidgetItem(EditorPreferencesPage)
  28. , m_preferencesPage(page)
  29. , m_entirePageMatchesFilter(true)
  30. {
  31. setIcon(0, icon);
  32. setData(0, Qt::DecorationRole, icon);
  33. Setup(page);
  34. }
  35. void EditorPreferencesTreeWidgetItem::Setup(IPreferencesPage* page)
  36. {
  37. setData(0, Qt::DisplayRole, m_preferencesPage->GetTitle());
  38. AZ::SerializeContext* serializeContext = nullptr;
  39. AZ::ComponentApplicationBus::BroadcastResult(serializeContext, &AZ::ComponentApplicationBus::Events::GetSerializeContext);
  40. AZ_Assert(serializeContext, "Serialization context not available");
  41. // Grab all property names on the page so we can filter by property text by recursing through the hierarchy
  42. AzToolsFramework::InstanceDataHierarchy hierarchy;
  43. hierarchy.AddRootInstance(page);
  44. hierarchy.Build(serializeContext, AZ::SerializeContext::ENUM_ACCESS_FOR_READ);
  45. std::function<void(AzToolsFramework::InstanceDataNode&)> visitNode = [&](AzToolsFramework::InstanceDataNode& node)
  46. {
  47. const AZ::Edit::ElementData* data = node.GetElementEditMetadata();
  48. QString text;
  49. if (data)
  50. {
  51. text = data->m_name;
  52. }
  53. if (!text.isEmpty() && !m_propertyNames.contains(text))
  54. {
  55. m_propertyNames.append(text);
  56. }
  57. for (AzToolsFramework::InstanceDataNode& child : node.GetChildren())
  58. {
  59. visitNode(child);
  60. }
  61. };
  62. visitNode(hierarchy);
  63. }
  64. EditorPreferencesTreeWidgetItem::~EditorPreferencesTreeWidgetItem()
  65. {
  66. }
  67. IPreferencesPage* EditorPreferencesTreeWidgetItem::GetPreferencesPage() const
  68. {
  69. return m_preferencesPage;
  70. }
  71. void EditorPreferencesTreeWidgetItem::Filter(const QString& filter)
  72. {
  73. // Everything on a page matches the filter if its or any of its parents' text matches
  74. m_entirePageMatchesFilter = false;
  75. QTreeWidgetItem* item = this;
  76. while (item && !m_entirePageMatchesFilter)
  77. {
  78. m_entirePageMatchesFilter = item->text(0).contains(filter, Qt::CaseInsensitive);
  79. item = item->parent();
  80. }
  81. // Otherwise, just check the contents to see if there's a match
  82. bool filtered = !m_entirePageMatchesFilter;
  83. for (auto it = m_propertyNames.begin(), end = m_propertyNames.end(); it != end && filtered; ++it)
  84. {
  85. filtered = !(*it).contains(filter, Qt::CaseInsensitive);
  86. }
  87. setHidden(filtered);
  88. }
  89. void EditorPreferencesTreeWidgetItem::UpdateEditorFilter(AzToolsFramework::ReflectedPropertyEditor* editor, const QString& filter)
  90. {
  91. QString filterText = m_entirePageMatchesFilter ? QString() : filter;
  92. editor->InvalidateAll(filterText.toUtf8().constData());
  93. editor->ExpandAll();
  94. }