AssetCatalogModel.h 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154
  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. #pragma once
  9. #if !defined(Q_MOC_RUN)
  10. #include <QtGui/qstandarditemmodel.h>
  11. #include <QFileIconProvider>
  12. #include <QThread>
  13. #include <AzCore/Asset/AssetCommon.h>
  14. #include <AzCore/std/containers/unordered_map.h>
  15. #include <AzFramework/Asset/AssetCatalogBus.h>
  16. #include <AzToolsFramework/UI/SearchWidget/SearchCriteriaWidget.hxx>
  17. #endif
  18. ///////////////////////////////////////////////////////////////////////////////
  19. struct DatabaseEntry
  20. {
  21. public:
  22. DatabaseEntry(AZ::Data::AssetId assetID, const char *assetPath)
  23. : m_id(assetID)
  24. , m_path(assetPath)
  25. {}
  26. AZ::Data::AssetId m_id;
  27. QString m_path;
  28. };
  29. ///////////////////////////////////////////////////////////////////////////////
  30. class AssetCatalogEntry
  31. : public QStandardItem
  32. {
  33. public:
  34. // This will be easier to store in the data, so that filters don't have to cast the item to get to it.
  35. enum Roles
  36. {
  37. FileIconRole = Qt::DecorationRole,
  38. FilePathRole = Qt::UserRole + 1,
  39. VisibilityRole = Qt::UserRole + 2,
  40. FolderRole = Qt::UserRole + 3
  41. };
  42. AssetCatalogEntry() {}
  43. AZ_CLASS_ALLOCATOR(AssetCatalogEntry, AZ::SystemAllocator);
  44. bool operator<(const QStandardItem& other) const override;
  45. public:
  46. AZ::Data::AssetId m_assetId; ///< The unique ID of the asset in the asset database.
  47. AZ::Data::AssetType m_assetType; ///< The type of the asset is used to validate on certain drop targets, like the PropertyAssetCtrl.
  48. AZ::Uuid m_classId; ///< If valid, the component that should be created when this asset is dragged onto creation-capable windows.
  49. };
  50. ///////////////////////////////////////////////////////////////////////////////
  51. class AssetCatalogModel
  52. : public QStandardItemModel
  53. , public AzFramework::AssetCatalogEventBus::Handler
  54. {
  55. Q_OBJECT
  56. public:
  57. AZ_CLASS_ALLOCATOR(AssetCatalogModel, AZ::SystemAllocator);
  58. AssetCatalogModel(QObject* parent = 0);
  59. ~AssetCatalogModel() override;
  60. QString RootPath() const { return m_rootPath; }
  61. void LoadDatabase();
  62. QString FileName(const QModelIndex& index) const;
  63. QString FilePath(const QModelIndex& index) const;
  64. AssetCatalogEntry* AssetData(const QModelIndex& index) const;
  65. //! Finds an asset. On success, returns a pointer to the item.
  66. //! \retrun A valid pointer on success, nullptr on fail.
  67. AssetCatalogEntry* FindAsset(QString assetPath);
  68. // QAbstractItemModel overrides
  69. QVariant data(const QModelIndex& index, int role = Qt::DisplayRole) const override;
  70. QMimeData* mimeData(const QModelIndexList& indexes) const override;
  71. QVariant headerData(int section, Qt::Orientation orientation, int role = Qt::DisplayRole) const override;
  72. // AzFramework::AssetCatalogEventBus::Handler
  73. void OnCatalogAssetAdded(const AZ::Data::AssetId& assetId) override;
  74. void OnCatalogAssetRemoved(const AZ::Data::AssetId& assetId, const AZ::Data::AssetInfo& assetInfo) override;
  75. Q_SIGNALS:
  76. void LoadComplete();
  77. void SetTotalProgress(int value);
  78. void UpdateProgress(int value);
  79. public Q_SLOTS:
  80. void SearchCriteriaChanged(QStringList& criteriaList, AzToolsFramework::FilterOperatorType filterOperator);
  81. void ProcessAssets();
  82. void StartProcessingAssets();
  83. void StopProcessingAssets();
  84. protected:
  85. //! Adds an asset and returns a pointer to that new asset.
  86. AssetCatalogEntry* AddAsset(QString assetPath, AZ::Data::AssetId id);
  87. //! Removes an asset. On success, returns a pointer to the parent item. On failure, returns nullptr.
  88. AssetCatalogEntry* RemoveAsset(QString assetPath);
  89. void BuildFilter(QStringList& criteriaList, AzToolsFramework::FilterOperatorType filterOperator);
  90. void InvalidateFilter();
  91. void SetFilterRegExp(const AZStd::string& filterType, const QRegExp& regExp);
  92. void ClearFilterRegExp(const AZStd::string& filterType = AZStd::string());
  93. AZ::Data::AssetType GetAssetType(const QString &filename) const;
  94. QStandardItem* GetPath(QString& path, bool createIfNeeded, QStandardItem* parent = nullptr);
  95. void ApplyFilter(QStandardItem* parent);
  96. AZStd::unordered_map<AZ::Data::AssetType, QIcon> m_assetTypeToIcon;
  97. AZStd::unordered_map<AZ::Uuid, AZ::Uuid> m_assetTypeToComponent;
  98. AZStd::unordered_map<AZStd::string, AZStd::vector<AZ::Uuid>> m_extensionToAssetType;
  99. QFileIconProvider m_iconProvider;
  100. QString m_rootPath;
  101. AzToolsFramework::FilterByCategoryMap m_filtersRegExp;
  102. static const int ASSET_CATALOG_BATCH_SIZE = 50;
  103. AZStd::vector<DatabaseEntry*> m_fileCache; // scratch space to get the registry data out of the AssetDatabase in quick fashion.
  104. int m_fileCacheCurrentIndex;
  105. bool m_canProcessAssets;
  106. };
  107. ///////////////////////////////////////////////////////////////////////////////
  108. class AssetCatalogModelWorkerThread
  109. : public QThread
  110. {
  111. Q_OBJECT
  112. public:
  113. AssetCatalogModelWorkerThread(AssetCatalogModel* catalog, QThread* returnThread);
  114. void startJob();
  115. public Q_SLOTS:
  116. void ReturnToThread();
  117. protected:
  118. void run() override;
  119. // These are pointers that this object will not own.
  120. QThread* m_returnThread;
  121. AssetCatalogModel* m_catalog;
  122. };