Cleanup: Separate interface & implementation for asset catalog tree-view

Should make the code a bit more organized and help getting an overview
of the interfaces more quickly.
This commit is contained in:
Julian Eisel 2021-10-04 14:58:52 +02:00
parent cc636db8f2
commit 4882208633
1 changed files with 137 additions and 114 deletions

View File

@ -74,6 +74,7 @@ class AssetCatalogTreeView : public ui::AbstractTreeView {
void add_unassigned_item();
bool is_active_catalog(CatalogID catalog_id) const;
};
/* ---------------------------------------------------------------------- */
class AssetCatalogTreeViewItem : public ui::BasicTreeViewItem {
@ -81,111 +82,19 @@ class AssetCatalogTreeViewItem : public ui::BasicTreeViewItem {
AssetCatalogTreeItem &catalog_item_;
public:
AssetCatalogTreeViewItem(AssetCatalogTreeItem *catalog_item)
: BasicTreeViewItem(catalog_item->get_name()), catalog_item_(*catalog_item)
{
}
AssetCatalogTreeViewItem(AssetCatalogTreeItem *catalog_item);
void on_activate() override
{
const AssetCatalogTreeView &tree_view = static_cast<const AssetCatalogTreeView &>(
get_tree_view());
tree_view.params_->asset_catalog_visibility = FILE_SHOW_ASSETS_FROM_CATALOG;
tree_view.params_->catalog_id = catalog_item_.get_catalog_id();
WM_main_add_notifier(NC_SPACE | ND_SPACE_ASSET_PARAMS, nullptr);
}
void on_activate() override;
void build_row(uiLayout &row) override
{
ui::BasicTreeViewItem::build_row(row);
void build_row(uiLayout &row) override;
if (!is_active()) {
return;
}
bool has_droppable_item(const wmDrag &drag) const;
PointerRNA *props;
const CatalogID catalog_id = catalog_item_.get_catalog_id();
props = UI_but_extra_operator_icon_add(
button(), "ASSET_OT_catalog_new", WM_OP_INVOKE_DEFAULT, ICON_ADD);
RNA_string_set(props, "parent_path", catalog_item_.catalog_path().c_str());
/* Tree items without a catalog ID represent components of catalog paths that are not
* associated with an actual catalog. They exist merely by the presence of a child catalog, and
* thus cannot be deleted themselves. */
if (!BLI_uuid_is_nil(catalog_id)) {
char catalog_id_str_buffer[UUID_STRING_LEN] = "";
BLI_uuid_format(catalog_id_str_buffer, catalog_id);
props = UI_but_extra_operator_icon_add(
button(), "ASSET_OT_catalog_delete", WM_OP_INVOKE_DEFAULT, ICON_X);
RNA_string_set(props, "catalog_id", catalog_id_str_buffer);
}
}
bool has_droppable_item(const wmDrag &drag) const
{
const ListBase *asset_drags = WM_drag_asset_list_get(&drag);
/* There needs to be at least one asset from the current file. */
LISTBASE_FOREACH (const wmDragAssetListItem *, asset_item, asset_drags) {
if (!asset_item->is_external) {
return true;
}
}
return false;
}
bool can_drop(const wmDrag &drag) const override
{
if (drag.type != WM_DRAG_ASSET_LIST) {
return false;
}
return has_droppable_item(drag);
}
std::string drop_tooltip(const bContext & /*C*/,
bool can_drop(const wmDrag &drag) const override;
std::string drop_tooltip(const bContext &C,
const wmDrag &drag,
const wmEvent & /*event*/) const override
{
const ListBase *asset_drags = WM_drag_asset_list_get(&drag);
const bool is_multiple_assets = !BLI_listbase_is_single(asset_drags);
/* Don't try to be smart by dynamically adding the 's' for the plural. Just makes translation
* harder, so use full literals. */
std::string basic_tip = is_multiple_assets ? TIP_("Move assets to catalog") :
TIP_("Move asset to catalog");
return basic_tip + ": " + catalog_item_.get_name() + " (" +
catalog_item_.catalog_path().str() + ")";
}
bool on_drop(const wmDrag &drag) override
{
const ListBase *asset_drags = WM_drag_asset_list_get(&drag);
if (!asset_drags) {
return false;
}
const AssetCatalogTreeView &tree_view = static_cast<const AssetCatalogTreeView &>(
get_tree_view());
LISTBASE_FOREACH (wmDragAssetListItem *, asset_item, asset_drags) {
if (asset_item->is_external) {
/* Only internal assets can be modified! */
continue;
}
BKE_asset_metadata_catalog_id_set(asset_item->asset_data.local_id->asset_data,
catalog_item_.get_catalog_id(),
catalog_item_.get_simple_name().c_str());
/* Trigger re-run of filtering to update visible assets. */
filelist_tag_needs_filtering(tree_view.space_file_.files);
file_select_deselect_all(&tree_view.space_file_, FILE_SEL_SELECTED | FILE_SEL_HIGHLIGHTED);
}
return true;
}
const wmEvent &event) const override;
bool on_drop(const wmDrag &drag) override;
};
/** Only reason this isn't just `BasicTreeViewItem` is to add a '+' icon for adding a root level
@ -193,22 +102,11 @@ class AssetCatalogTreeViewItem : public ui::BasicTreeViewItem {
class AssetCatalogTreeViewAllItem : public ui::BasicTreeViewItem {
using BasicTreeViewItem::BasicTreeViewItem;
void build_row(uiLayout &row) override
{
ui::BasicTreeViewItem::build_row(row);
if (!is_active()) {
return;
}
PointerRNA *props;
props = UI_but_extra_operator_icon_add(
button(), "ASSET_OT_catalog_new", WM_OP_INVOKE_DEFAULT, ICON_ADD);
/* No parent path to use the root level. */
RNA_string_set(props, "parent_path", nullptr);
}
void build_row(uiLayout &row) override;
};
/* ---------------------------------------------------------------------- */
AssetCatalogTreeView::AssetCatalogTreeView(::AssetLibrary *library,
FileAssetSelectParams *params,
SpaceFile &space_file)
@ -283,6 +181,131 @@ bool AssetCatalogTreeView::is_active_catalog(CatalogID catalog_id) const
(params_->catalog_id == catalog_id);
}
/* ---------------------------------------------------------------------- */
AssetCatalogTreeViewItem::AssetCatalogTreeViewItem(AssetCatalogTreeItem *catalog_item)
: BasicTreeViewItem(catalog_item->get_name()), catalog_item_(*catalog_item)
{
}
void AssetCatalogTreeViewItem::on_activate()
{
const AssetCatalogTreeView &tree_view = static_cast<const AssetCatalogTreeView &>(
get_tree_view());
tree_view.params_->asset_catalog_visibility = FILE_SHOW_ASSETS_FROM_CATALOG;
tree_view.params_->catalog_id = catalog_item_.get_catalog_id();
WM_main_add_notifier(NC_SPACE | ND_SPACE_ASSET_PARAMS, nullptr);
}
void AssetCatalogTreeViewItem::build_row(uiLayout &row)
{
ui::BasicTreeViewItem::build_row(row);
if (!is_active()) {
return;
}
PointerRNA *props;
const CatalogID catalog_id = catalog_item_.get_catalog_id();
props = UI_but_extra_operator_icon_add(
button(), "ASSET_OT_catalog_new", WM_OP_INVOKE_DEFAULT, ICON_ADD);
RNA_string_set(props, "parent_path", catalog_item_.catalog_path().c_str());
/* Tree items without a catalog ID represent components of catalog paths that are not
* associated with an actual catalog. They exist merely by the presence of a child catalog, and
* thus cannot be deleted themselves. */
if (!BLI_uuid_is_nil(catalog_id)) {
char catalog_id_str_buffer[UUID_STRING_LEN] = "";
BLI_uuid_format(catalog_id_str_buffer, catalog_id);
props = UI_but_extra_operator_icon_add(
button(), "ASSET_OT_catalog_delete", WM_OP_INVOKE_DEFAULT, ICON_X);
RNA_string_set(props, "catalog_id", catalog_id_str_buffer);
}
}
bool AssetCatalogTreeViewItem::has_droppable_item(const wmDrag &drag) const
{
const ListBase *asset_drags = WM_drag_asset_list_get(&drag);
/* There needs to be at least one asset from the current file. */
LISTBASE_FOREACH (const wmDragAssetListItem *, asset_item, asset_drags) {
if (!asset_item->is_external) {
return true;
}
}
return false;
}
bool AssetCatalogTreeViewItem::can_drop(const wmDrag &drag) const
{
if (drag.type != WM_DRAG_ASSET_LIST) {
return false;
}
return has_droppable_item(drag);
}
std::string AssetCatalogTreeViewItem::drop_tooltip(const bContext & /*C*/,
const wmDrag &drag,
const wmEvent & /*event*/) const
{
const ListBase *asset_drags = WM_drag_asset_list_get(&drag);
const bool is_multiple_assets = !BLI_listbase_is_single(asset_drags);
/* Don't try to be smart by dynamically adding the 's' for the plural. Just makes translation
* harder, so use full literals. */
std::string basic_tip = is_multiple_assets ? TIP_("Move assets to catalog") :
TIP_("Move asset to catalog");
return basic_tip + ": " + catalog_item_.get_name() + " (" + catalog_item_.catalog_path().str() +
")";
}
bool AssetCatalogTreeViewItem::on_drop(const wmDrag &drag)
{
const ListBase *asset_drags = WM_drag_asset_list_get(&drag);
if (!asset_drags) {
return false;
}
const AssetCatalogTreeView &tree_view = static_cast<const AssetCatalogTreeView &>(
get_tree_view());
LISTBASE_FOREACH (wmDragAssetListItem *, asset_item, asset_drags) {
if (asset_item->is_external) {
/* Only internal assets can be modified! */
continue;
}
BKE_asset_metadata_catalog_id_set(asset_item->asset_data.local_id->asset_data,
catalog_item_.get_catalog_id(),
catalog_item_.get_simple_name().c_str());
/* Trigger re-run of filtering to update visible assets. */
filelist_tag_needs_filtering(tree_view.space_file_.files);
file_select_deselect_all(&tree_view.space_file_, FILE_SEL_SELECTED | FILE_SEL_HIGHLIGHTED);
}
return true;
}
/* ---------------------------------------------------------------------- */
void AssetCatalogTreeViewAllItem::build_row(uiLayout &row)
{
ui::BasicTreeViewItem::build_row(row);
if (!is_active()) {
return;
}
PointerRNA *props;
props = UI_but_extra_operator_icon_add(
button(), "ASSET_OT_catalog_new", WM_OP_INVOKE_DEFAULT, ICON_ADD);
/* No parent path to use the root level. */
RNA_string_set(props, "parent_path", nullptr);
}
} // namespace blender::ed::asset_browser
/* ---------------------------------------------------------------------- */