2
0

AssetBundlerAbstractFileTableModel.h 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120
  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. #include <AzCore/std/containers/unordered_set.h>
  10. #include <AzCore/std/containers/vector.h>
  11. #include <AzCore/std/smart_ptr/unique_ptr.h>
  12. #include <AzCore/std/string/string.h>
  13. #include <AzCore/Outcome/Outcome.h>
  14. #include <AzFramework/Platform/PlatformDefaults.h>
  15. #include <QAbstractTableModel>
  16. #include <QModelIndex>
  17. #include <QSet>
  18. #include <QString>
  19. namespace AssetBundler
  20. {
  21. extern const char* DateTimeFormat;
  22. extern const char* ReadOnlyFileErrorMessage;
  23. //! Provides an abstract model that can be subclassed to create table models used to store information about files found on-disk.
  24. class AssetBundlerAbstractFileTableModel
  25. : public QAbstractTableModel
  26. {
  27. public:
  28. enum DataRoles
  29. {
  30. SortRole = Qt::UserRole + 1,
  31. };
  32. explicit AssetBundlerAbstractFileTableModel(QObject* parent = nullptr);
  33. virtual ~AssetBundlerAbstractFileTableModel() {}
  34. //////////////////////////////////////////////////////////////////////////
  35. // Pure virtual functions
  36. virtual AZStd::vector<AZStd::string> CreateNewFiles(
  37. const AZStd::string& absoluteFilePath,
  38. const AzFramework::PlatformFlags& platforms,
  39. const QString& project = QString()) = 0;
  40. virtual bool DeleteFile(const QModelIndex& index) = 0;
  41. virtual void LoadFile(
  42. const AZStd::string& absoluteFilePath,
  43. const AZStd::string& projectName = "",
  44. bool isDefaultFile = false) = 0;
  45. virtual bool WriteToDisk(const AZStd::string& key) = 0;
  46. //! Returns the absolute path of the file at the given index on success, returns an empty string on failure.
  47. virtual AZStd::string GetFileAbsolutePath(const QModelIndex& index) const = 0;
  48. virtual int GetFileNameColumnIndex() const = 0;
  49. virtual int GetTimeStampColumnIndex() const = 0;
  50. //////////////////////////////////////////////////////////////////////////
  51. //! Reload all the data based on the watched folders and files
  52. virtual void Reload(
  53. const char* fileExtension,
  54. const QSet<QString>& watchedFolders,
  55. const QSet<QString>& watchedFiles = QSet<QString>(),
  56. const AZStd::unordered_map<AZStd::string, AZStd::string>& pathToProjectNameMap = AZStd::unordered_map<AZStd::string, AZStd::string>());
  57. virtual void ReloadFiles(
  58. const AZStd::vector<AZStd::string>& absoluteFilePathList,
  59. AZStd::unordered_map<AZStd::string, AZStd::string> pathToProjectNameMap = AZStd::unordered_map<AZStd::string, AZStd::string>());
  60. bool Save(const QModelIndex& selectedIndex);
  61. bool SaveAll();
  62. bool HasUnsavedChanges() const;
  63. //////////////////////////////////////////////////////////////////////////
  64. // QAbstractTableModel overrides
  65. int rowCount(const QModelIndex& parent = QModelIndex()) const override;
  66. //////////////////////////////////////////////////////////////////////////
  67. protected:
  68. //! Verifies input index is in range and returns the associated key.
  69. //! Throws an error and returns an empty string if the input index is out of range.
  70. AZStd::string GetFileKey(const QModelIndex& index) const;
  71. //! Returns an ordered list of every file key in the model.
  72. //! If a proxy model is not used, the order of this list will also be the display order.
  73. const AZStd::vector<AZStd::string>& GetAllFileKeys() const;
  74. //! Get the index row if the file info is stored in the model
  75. //! Returns -1 if the file key doesn't exist.
  76. int GetIndexRowByKey(const AZStd::string& key) const;
  77. //! Adds input key to the end of the list of all keys and notifies the view that a row has been added.
  78. //! When subclassing, instantiate all data associated with this key so the view can update properly.
  79. void AddFileKey(const AZStd::string& key);
  80. //! Verifies input index, signals to the view that rows will be removed, and removes the key found at the input index.
  81. //! When subclassing, be sure to remove all data associated with the key at this index before calling this function.
  82. //! Returns true if the index is successfully removed, and false on failure
  83. bool RemoveFileKey(const QModelIndex& index);
  84. //! Delete a file from the disk and remove it from the model by its key
  85. bool DeleteFileByKey(const AZStd::string& key);
  86. AZStd::unordered_set<AZStd::string> m_keysWithUnsavedChanges;
  87. private:
  88. //! When subclassing: store file information in a map, and add the keys to this vector.
  89. //! Provides a 1:1 mapping between a QModelIndex::row value and a key.
  90. AZStd::vector<AZStd::string> m_fileListKeys;
  91. };
  92. } // namespace AssetBundler