Blender  V2.93
tree_display_override_library.cc
Go to the documentation of this file.
1 /*
2  * This program is free software; you can redistribute it and/or
3  * modify it under the terms of the GNU General Public License
4  * as published by the Free Software Foundation; either version 2
5  * of the License, or (at your option) any later version.
6  *
7  * This program is distributed in the hope that it will be useful,
8  * but WITHOUT ANY WARRANTY; without even the implied warranty of
9  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
10  * GNU General Public License for more details.
11  *
12  * You should have received a copy of the GNU General Public License
13  * along with this program; if not, write to the Free Software Foundation,
14  * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
15  */
16 
21 #include "BLI_listbase.h"
22 #include "BLI_listbase_wrapper.hh"
23 
24 #include "BKE_collection.h"
25 #include "BKE_main.h"
26 
27 #include "DNA_collection_types.h"
28 
29 #include "BLT_translation.h"
30 
31 #include "../outliner_intern.h"
32 #include "tree_display.hh"
33 
34 namespace blender::ed::outliner {
35 
36 /* Convenience/readability. */
37 template<typename T> using List = ListBaseWrapper<T>;
38 
40  : AbstractTreeDisplay(space_outliner)
41 {
42 }
43 
45 {
46  ListBase tree = {nullptr};
47 
48  {
49  /* current file first - mainvar provides tselem with unique pointer - not used */
50  TreeElement *ten = add_library_contents(*source_data.bmain, tree, nullptr);
51  TreeStoreElem *tselem;
52 
53  if (ten) {
54  tselem = TREESTORE(ten);
55  if (!tselem->used) {
56  tselem->flag &= ~TSE_CLOSED;
57  }
58  }
59  }
60 
61  for (ID *id : List<ID>(source_data.bmain->libraries)) {
62  Library *lib = reinterpret_cast<Library *>(id);
63  TreeElement *ten = add_library_contents(*source_data.bmain, tree, lib);
64  /* NULL-check matters, due to filtering there may not be a new element. */
65  if (ten) {
66  lib->id.newid = (ID *)ten;
67  }
68  }
69 
70  /* make hierarchy */
71  for (TreeElement *ten : List<TreeElement>(tree)) {
72  if (ten == tree.first) {
73  /* First item is main, skip. */
74  continue;
75  }
76 
77  TreeStoreElem *tselem = TREESTORE(ten);
78  Library *lib = (Library *)tselem->id;
79  BLI_assert(!lib || (GS(lib->id.name) == ID_LI));
80  if (!lib || !lib->parent) {
81  continue;
82  }
83 
84  TreeElement *parent = (TreeElement *)lib->parent->id.newid;
85 
86  if (tselem->id->tag & LIB_TAG_INDIRECT) {
87  /* Only remove from 'first level' if lib is not also directly used. */
88  BLI_remlink(&tree, ten);
89  BLI_addtail(&parent->subtree, ten);
90  ten->parent = parent;
91  }
92  else {
93  /* Else, make a new copy of the libtree for our parent. */
94  TreeElement *dupten = add_library_contents(*source_data.bmain, parent->subtree, lib);
95  if (dupten) {
96  dupten->parent = parent;
97  }
98  }
99  }
100  /* restore newid pointers */
101  for (ID *library_id : List<ID>(source_data.bmain->libraries)) {
102  library_id->newid = nullptr;
103  }
104 
105  return tree;
106 }
107 
108 TreeElement *TreeDisplayOverrideLibrary::add_library_contents(Main &mainvar,
109  ListBase &lb,
110  Library *lib) const
111 {
112  const short filter_id_type = id_filter_get();
113 
114  ListBase *lbarray[INDEX_ID_MAX];
115  int tot;
116  if (filter_id_type) {
117  lbarray[0] = which_libbase(&mainvar, space_outliner_.filter_id_type);
118  tot = 1;
119  }
120  else {
121  tot = set_listbasepointers(&mainvar, lbarray);
122  }
123 
124  TreeElement *tenlib = nullptr;
125  for (int a = 0; a < tot; a++) {
126  if (!lbarray[a] || !lbarray[a]->first) {
127  continue;
128  }
129 
130  ID *id = nullptr;
131 
132  /* check if there's data in current lib */
133  for (ID *id_iter : List<ID>(lbarray[a])) {
134  if (id_iter->lib == lib && ID_IS_OVERRIDE_LIBRARY_REAL(id_iter)) {
135  id = id_iter;
136  break;
137  }
138  }
139 
140  if (id != nullptr) {
141  if (!tenlib) {
142  /* Create library tree element on demand, depending if there are any data-blocks. */
143  if (lib) {
144  tenlib = outliner_add_element(&space_outliner_, &lb, lib, nullptr, TSE_SOME_ID, 0);
145  }
146  else {
147  tenlib = outliner_add_element(&space_outliner_, &lb, &mainvar, nullptr, TSE_ID_BASE, 0);
148  tenlib->name = IFACE_("Current File");
149  }
150  }
151 
152  /* Create data-block list parent element on demand. */
153  if (id != nullptr) {
154  TreeElement *ten;
155 
156  if (filter_id_type) {
157  ten = tenlib;
158  }
159  else {
160  ten = outliner_add_element(
161  &space_outliner_, &tenlib->subtree, lbarray[a], nullptr, TSE_ID_BASE, 0);
162  ten->directdata = lbarray[a];
164  }
165 
166  for (ID *id : List<ID>(lbarray[a])) {
167  if (override_library_id_filter_poll(lib, id)) {
168  TreeElement *override_tree_element = outliner_add_element(
169  &space_outliner_, &ten->subtree, id, ten, TSE_LIBRARY_OVERRIDE_BASE, 0);
170 
171  if (BLI_listbase_is_empty(&override_tree_element->subtree)) {
172  outliner_free_tree_element(override_tree_element, &ten->subtree);
173  }
174  }
175  }
176  }
177  }
178  }
179 
180  return tenlib;
181 }
182 
183 short TreeDisplayOverrideLibrary::id_filter_get() const
184 {
187  }
188  return 0;
189 }
190 
191 bool TreeDisplayOverrideLibrary::override_library_id_filter_poll(Library *lib, ID *id) const
192 {
193  if (id->lib != lib) {
194  return false;
195  }
196 
197  if (!ID_IS_OVERRIDE_LIBRARY_REAL(id)) {
198  return false;
199  }
200 
201  return true;
202 }
203 
204 } // namespace blender::ed::outliner
int set_listbasepointers(struct Main *main, struct ListBase *lb[])
struct ListBase * which_libbase(struct Main *bmain, short type)
Definition: main.c:447
#define BLI_assert(a)
Definition: BLI_assert.h:58
BLI_INLINE bool BLI_listbase_is_empty(const struct ListBase *lb)
Definition: BLI_listbase.h:124
void BLI_addtail(struct ListBase *listbase, void *vlink) ATTR_NONNULL(1)
Definition: listbase.c:110
void BLI_remlink(struct ListBase *listbase, void *vlink) ATTR_NONNULL(1)
Definition: listbase.c:133
#define IFACE_(msgid)
#define ID_IS_OVERRIDE_LIBRARY_REAL(_id)
Definition: DNA_ID.h:438
@ LIB_TAG_INDIRECT
Definition: DNA_ID.h:524
@ INDEX_ID_MAX
Definition: DNA_ID.h:859
@ ID_LI
Definition: DNA_ID_enums.h:58
Object groups, one object can be in many groups at once.
@ TSE_LIBRARY_OVERRIDE_BASE
@ TSE_ID_BASE
@ TSE_SOME_ID
@ TSE_CLOSED
@ SO_FILTER_ID_TYPE
Base Class For Tree-Displays.
Definition: tree_display.hh:55
ListBase buildTree(const TreeSourceData &source_data) override
const char * outliner_idcode_to_plural(short idcode)
Definition: common.cc:33
void * tree
DRWShaderLibrary * lib
#define GS(x)
Definition: iris.c:241
static unsigned a[3]
Definition: RandGen.cpp:92
ListBaseWrapper< T > List
#define TREESTORE(a)
void outliner_free_tree_element(TreeElement *element, ListBase *parent_subtree)
TreeElement * outliner_add_element(SpaceOutliner *space_outliner, ListBase *lb, void *idv, TreeElement *parent, short type, short index)
Definition: DNA_ID.h:273
int tag
Definition: DNA_ID.h:292
struct Library * lib
Definition: DNA_ID.h:277
char name[66]
Definition: DNA_ID.h:283
Definition: BKE_main.h:116
ListBase libraries
Definition: BKE_main.h:147
struct TreeElement * parent
ListBase subtree
void * directdata
const char * name
The data to build the tree from.
Definition: tree_display.h:39
struct Main * bmain
Definition: tree_display.h:40
Establish and manage Outliner trees for different display modes.