Blender V5.3
tree_display_orphaned.cc
Go to the documentation of this file.
1/* SPDX-FileCopyrightText: 2023 Blender Authors
2 *
3 * SPDX-License-Identifier: GPL-2.0-or-later */
4
8
9#include "DNA_ID.h"
10#include "DNA_space_types.h"
11
12#include "BLI_listbase.hh"
14
15#include "BKE_idtype.hh"
16#include "BKE_main.hh"
17
18#include "../outliner_intern.hh"
19#include "common.hh"
20#include "tree_display.hh"
22
23namespace blender::ed::outliner {
24
25template<typename T> using List = ListBaseWrapper<T>;
26
31
32ListBaseT<TreeElement> TreeDisplayIDOrphans::build_tree(const TreeSourceData &source_data)
33{
34 ListBaseT<TreeElement> tree = {nullptr};
35 short filter_id_type = (space_outliner_.filter & SO_FILTER_ID_TYPE) ?
37 0;
38
39 Vector<ListBaseT<ID> *> lbarray;
40 if (filter_id_type) {
41 lbarray.append(which_libbase(source_data.bmain, filter_id_type));
42 }
43 else {
44 lbarray.extend(BKE_main_lists_get(*source_data.bmain));
45 }
46
47 for (int a = 0; a < lbarray.size(); a++) {
48 if (BLI_listbase_is_empty(lbarray[a])) {
49 continue;
50 }
51 if (!datablock_has_orphans(*lbarray[a])) {
52 continue;
53 }
54
55 /* Header for this type of data-block. */
56 TreeElement *te = nullptr;
57 if (!filter_id_type) {
58 ID *id = lbarray[a]->first();
59 te = add_element<TreeElementIDBase>({.lb = &tree, .persistent_ptr = lbarray[a]});
60 te->name = outliner_idcode_to_plural(GS(id->name));
61 }
62
63 /* Add the orphaned data-blocks - these will not be added with any subtrees attached. */
64 for (ID *id : List<ID>(lbarray[a])) {
65 if (ID_REFCOUNTING_USERS(id) <= 0) {
66 add_id_element({.lb = (te) ? &te->subtree : &tree, .parent = te, .expand = false}, id);
67 }
68 }
69 }
70
71 return tree;
72}
73
74bool TreeDisplayIDOrphans::datablock_has_orphans(ListBaseT<ID> &lb) const
75{
76 if (lb.is_empty()) {
77 return false;
78 }
79 const IDTypeInfo *id_type = BKE_idtype_get_info_from_id(lb.first());
80 if (id_type->flags & IDTYPE_FLAGS_NEVER_UNUSED) {
81 /* These ID types are never unused. */
82 return false;
83 }
84
85 for (ID *id : List<ID>(lb)) {
86 if (ID_REFCOUNTING_USERS(id) <= 0) {
87 return true;
88 }
89 }
90 return false;
91}
92
93} // namespace blender::ed::outliner
ID and Library types, which are fundamental for SDNA.
#define ID_REFCOUNTING_USERS(id)
Definition DNA_ID.h:684
int64_t size() const
void append(const T &value)
void extend(Span< T > array)
const T & first() const
TreeElement * add_id_element(const TreeElementAddParams &params, ID *id)
AbstractTreeDisplay(SpaceOutliner &space_outliner)
TreeElement * add_element(const TreeElementAddParams &params, Args &&...args)
TreeDisplayIDOrphans(SpaceOutliner &space_outliner)
ListBaseT< TreeElement > build_tree(const TreeSourceData &source_data) override
const char * outliner_idcode_to_plural(short idcode)
BLI_INLINE bool BLI_listbase_is_empty(const ListBase *lb)
MainListsArray BKE_main_lists_get(Main &bmain)
Definition main.cc:1158
const IDTypeInfo * BKE_idtype_get_info_from_id(const ID *id)
Definition idtype.cc:232
ListBaseWrapperTemplate< ListBase, T > ListBaseWrapper
ID_Type GS(const char *name)
Definition DNA_ID.h:410
ListBaseT< ID > * which_libbase(Main *bmain, short type)
Definition main.cc:1073
@ IDTYPE_FLAGS_NEVER_UNUSED
Definition BKE_idtype.hh:73
eSpaceOutliner_Filter filter
The data to build the tree from.
Establish and manage Outliner trees for different display modes.