FindEntityItemModel.h 2.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485
  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. #ifndef FIND_ENTITY_ITEM_MODEL_H
  9. #define FIND_ENTITY_ITEM_MODEL_H
  10. #if !defined(Q_MOC_RUN)
  11. #include <AzCore/base.h>
  12. #include <AzCore/Memory/SystemAllocator.h>
  13. #include <AzCore/Component/Component.h>
  14. #include <QAbstractItemModel>
  15. #endif
  16. #pragma once
  17. //! Model for items in the "Find Entity" tree view.
  18. //! Each item represents an Entity.
  19. class FindEntityItemModel
  20. : public QAbstractItemModel
  21. {
  22. Q_OBJECT;
  23. public:
  24. AZ_CLASS_ALLOCATOR(FindEntityItemModel, AZ::SystemAllocator);
  25. //! Columns of data to display about each Entity.
  26. enum Column
  27. {
  28. ColumnName, //!< Entity name
  29. ColumnCount //!< Total number of columns
  30. };
  31. enum Roles
  32. {
  33. VisibilityRole = Qt::UserRole + 1,
  34. RoleCount
  35. };
  36. FindEntityItemModel(QObject* parent = nullptr);
  37. void Initialize(AZ::EntityId canvasEntityId);
  38. // Qt overrides.
  39. int rowCount(const QModelIndex& parent = QModelIndex()) const override;
  40. int columnCount(const QModelIndex&) const override;
  41. QVariant data(const QModelIndex& index, int role) const override;
  42. QModelIndex index(int row, int column, const QModelIndex& parent = QModelIndex()) const override;
  43. QModelIndex parent(const QModelIndex& index) const override;
  44. QModelIndex GetIndexFromEntity(const AZ::EntityId& entityId, int column = 0) const;
  45. AZ::EntityId GetEntityFromIndex(const QModelIndex& index) const;
  46. void SearchStringChanged(const AZStd::string& filter);
  47. void SearchFilterChanged(const AZStd::vector<AZ::Uuid>& componentFilters);
  48. protected:
  49. QVariant DataForName(const QModelIndex& index, int role) const;
  50. //! Use the current filter setting and re-evaluate the filter.
  51. void InvalidateFilter();
  52. bool FilterEntity(const AZ::EntityId& entityId);
  53. bool IsFiltered(const AZ::EntityId& entityId) const;
  54. bool IsMatch(const AZ::EntityId& entityId) const;
  55. AZStd::string m_filterString;
  56. AZStd::vector<AZ::Uuid> m_componentFilters;
  57. AZStd::unordered_map<AZ::EntityId, bool> m_entityFilteredState;
  58. AZStd::unordered_map<AZ::EntityId, bool> m_entityMatchState;
  59. AZ::EntityId m_canvasEntityId;
  60. };
  61. Q_DECLARE_METATYPE(AZ::ComponentTypeList); // allows type to be stored by QVariable
  62. #endif