diff options
author | Sybren A. Stüvel <sybren@blender.org> | 2021-09-28 17:07:18 +0300 |
---|---|---|
committer | Sybren A. Stüvel <sybren@blender.org> | 2021-09-28 17:09:12 +0300 |
commit | 52a702468a59f1945ecfcf6dde6bccf648a27d36 (patch) | |
tree | 4bd067c0c0435a0f6d155f671119eea1acc142a3 /source/blender/blenkernel/intern/asset_catalog.cc | |
parent | d2004326a1f96f85cb1b6f7c57712de8998ecca0 (diff) |
Asset Catalog Service: add function to change catalog path
Add `AssetCatalogService::update_catalog_path()` to change the catalog
path of the given catalog, and also change the path of all the catalogs
contained within the given catalog.
Rebuilds the tree structure for the UI, but does not save the new catalog
definitions to disk.
No user-facing changes, just backend preparation for UI work.
Diffstat (limited to 'source/blender/blenkernel/intern/asset_catalog.cc')
-rw-r--r-- | source/blender/blenkernel/intern/asset_catalog.cc | 41 |
1 files changed, 41 insertions, 0 deletions
diff --git a/source/blender/blenkernel/intern/asset_catalog.cc b/source/blender/blenkernel/intern/asset_catalog.cc index 8f57f96e771..05e27a93621 100644 --- a/source/blender/blenkernel/intern/asset_catalog.cc +++ b/source/blender/blenkernel/intern/asset_catalog.cc @@ -99,6 +99,25 @@ void AssetCatalogService::delete_catalog(CatalogID catalog_id) this->rebuild_tree(); } +void AssetCatalogService::update_catalog_path(CatalogID catalog_id, + const CatalogPath &new_catalog_path) +{ + AssetCatalog *renamed_cat = this->find_catalog(catalog_id); + const CatalogPath old_cat_path = renamed_cat->path; + + for (auto &catalog_uptr : catalogs_.values()) { + AssetCatalog *cat = catalog_uptr.get(); + if (!cat->is_contained_in(old_cat_path)) { + continue; + } + + const CatalogPath path_suffix = cat->path.substr(old_cat_path.length()); + cat->path = new_catalog_path + path_suffix; + } + + this->rebuild_tree(); +} + AssetCatalog *AssetCatalogService::create_catalog(const CatalogPath &catalog_path) { std::unique_ptr<AssetCatalog> catalog = AssetCatalog::from_path(catalog_path); @@ -756,4 +775,26 @@ CatalogPath AssetCatalog::cleanup_path(const CatalogPath &path) return clean_path; } +bool AssetCatalog::is_contained_in(const CatalogPath &other_path) const +{ + if (other_path.empty()) { + return true; + } + + if (this->path == other_path) { + return true; + } + + /* To be a child path of 'other_path', our path must be at least a separator and another + * character longer. */ + if (this->path.length() < other_path.length() + 2) { + return false; + } + + const StringRef this_path(this->path); + const bool prefix_ok = this_path.startswith(other_path); + const char next_char = this_path[other_path.length()]; + return prefix_ok && next_char == AssetCatalogService::PATH_SEPARATOR; +} + } // namespace blender::bke |