Blender  V2.93
tree_display_libraries.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 *TreeDisplayLibraries::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 = static_cast<ID *>(lbarray[a]->first);
131  const bool is_library = (GS(id->name) == ID_LI) && (lib != nullptr);
132 
133  /* check if there's data in current lib */
134  for (ID *id_iter : List<ID>(lbarray[a])) {
135  if (id_iter->lib == lib) {
136  id = id_iter;
137  break;
138  }
139  }
140 
141  /* We always want to create an entry for libraries, even if/when we have no more IDs from
142  * them. This invalid state is important to show to user as well.*/
143  if (id != nullptr || is_library) {
144  if (!tenlib) {
145  /* Create library tree element on demand, depending if there are any data-blocks. */
146  if (lib) {
147  tenlib = outliner_add_element(&space_outliner_, &lb, lib, nullptr, TSE_SOME_ID, 0);
148  }
149  else {
150  tenlib = outliner_add_element(&space_outliner_, &lb, &mainvar, nullptr, TSE_ID_BASE, 0);
151  tenlib->name = IFACE_("Current File");
152  }
153  }
154 
155  /* Create data-block list parent element on demand. */
156  if (id != nullptr) {
157  TreeElement *ten;
158 
159  if (filter_id_type) {
160  ten = tenlib;
161  }
162  else {
163  ten = outliner_add_element(
164  &space_outliner_, &tenlib->subtree, lbarray[a], nullptr, TSE_ID_BASE, 0);
165  ten->directdata = lbarray[a];
167  }
168 
169  for (ID *id : List<ID>(lbarray[a])) {
170  if (library_id_filter_poll(lib, id)) {
172  }
173  }
174  }
175  }
176  }
177 
178  return tenlib;
179 }
180 
181 short TreeDisplayLibraries::id_filter_get() const
182 {
185  }
186  return 0;
187 }
188 
189 bool TreeDisplayLibraries::library_id_filter_poll(Library *lib, ID *id) const
190 {
191  if (id->lib != lib) {
192  return false;
193  }
194 
195  if (id_filter_get() == ID_GR) {
196  /* Don't show child collections of non-scene master collection,
197  * they are already shown as children. */
198  Collection *collection = (Collection *)id;
199  bool has_non_scene_parent = false;
200 
201  for (CollectionParent *cparent : List<CollectionParent>(collection->parents)) {
202  if (!(cparent->collection->flag & COLLECTION_IS_MASTER)) {
203  has_non_scene_parent = true;
204  }
205  }
206 
207  if (has_non_scene_parent) {
208  return false;
209  }
210  }
211 
212  return true;
213 }
214 
215 } // 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
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)
@ LIB_TAG_INDIRECT
Definition: DNA_ID.h:524
@ INDEX_ID_MAX
Definition: DNA_ID.h:859
@ ID_LI
Definition: DNA_ID_enums.h:58
@ ID_GR
Definition: DNA_ID_enums.h:77
Object groups, one object can be in many groups at once.
@ COLLECTION_IS_MASTER
@ 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
TreeDisplayLibraries(SpaceOutliner &space_outliner)
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
#define TREESTORE(a)
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
void * first
Definition: DNA_listBase.h:47
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.