Blender  V3.3
tree_element_rna.cc
Go to the documentation of this file.
1 /* SPDX-License-Identifier: GPL-2.0-or-later */
2 
7 #include <climits>
8 #include <iostream>
9 
10 #include "BLI_listbase.h"
11 #include "BLI_string.h"
12 
13 #include "BLT_translation.h"
14 
15 #include "DNA_outliner_types.h"
16 #include "DNA_space_types.h"
17 
18 #include "MEM_guardedalloc.h"
19 
20 #include "RNA_access.h"
21 
22 #include "../outliner_intern.hh"
23 
24 #include "tree_element_rna.hh"
25 
26 namespace blender::ed::outliner {
27 
28 /* Don't display arrays larger, weak but index is stored as a short,
29  * also the outliner isn't intended for editing such large data-sets. */
30 BLI_STATIC_ASSERT(sizeof(TreeElement::index) == 2, "Index is no longer short!")
31 
32 /* -------------------------------------------------------------------- */
33 /* Common functionality (#TreeElementRNACommon Base Class) */
34 
36  : AbstractTreeElement(legacy_te), rna_ptr_(rna_ptr)
37 {
38  /* Create an empty tree-element. */
39  if (!isRNAValid()) {
40  legacy_te_.name = IFACE_("(empty)");
41  return;
42  }
43 }
44 
46 {
47  return true;
48 }
49 
51 {
52  return rna_ptr_.data != nullptr;
53 }
54 
55 bool TreeElementRNACommon::expandPoll(const SpaceOutliner &UNUSED(space_outliner)) const
56 {
57  return isRNAValid();
58 }
59 
61 {
62  return rna_ptr_;
63 }
64 
66 {
67  return nullptr;
68 }
69 
70 /* -------------------------------------------------------------------- */
71 /* RNA Struct */
72 
74  : TreeElementRNACommon(legacy_te, rna_ptr)
75 {
77 
78  if (!isRNAValid()) {
79  return;
80  }
81 
82  legacy_te_.name = RNA_struct_name_get_alloc(&rna_ptr, nullptr, 0, nullptr);
83  if (legacy_te_.name) {
85  }
86  else {
88  }
89 }
90 
91 void TreeElementRNAStruct::expand(SpaceOutliner &space_outliner) const
92 {
93  TreeStoreElem &tselem = *TREESTORE(&legacy_te_);
95 
96  /* If searching don't expand RNA entries */
97  if (SEARCHING_OUTLINER(&space_outliner) && BLI_strcasecmp("RNA", legacy_te_.name) == 0) {
98  tselem.flag &= ~TSE_CHILDSEARCH;
99  }
100 
102  int tot = RNA_property_collection_length(&ptr, iterprop);
103  CLAMP_MAX(tot, max_index);
104 
105  TreeElementRNAProperty *parent_prop_te = legacy_te_.parent ?
106  tree_element_cast<TreeElementRNAProperty>(
107  legacy_te_.parent) :
108  nullptr;
109  /* auto open these cases */
110  if (!parent_prop_te || (RNA_property_type(parent_prop_te->getPropertyRNA()) == PROP_POINTER)) {
111  if (!tselem.used) {
112  tselem.flag &= ~TSE_CLOSED;
113  }
114  }
115 
116  if (TSELEM_OPEN(&tselem, &space_outliner)) {
117  for (int index = 0; index < tot; index++) {
118  PointerRNA propptr;
119  RNA_property_collection_lookup_int(&ptr, iterprop, index, &propptr);
120  if (!(RNA_property_flag(reinterpret_cast<PropertyRNA *>(propptr.data)) & PROP_HIDDEN)) {
122  &space_outliner, &legacy_te_.subtree, &ptr, &legacy_te_, TSE_RNA_PROPERTY, index);
123  }
124  }
125  }
126  else if (tot) {
128  }
129 }
130 
131 /* -------------------------------------------------------------------- */
132 /* RNA Property */
133 
135  PointerRNA &rna_ptr,
136  const int index)
137  : TreeElementRNACommon(legacy_te, rna_ptr)
138 {
140 
141  if (!isRNAValid()) {
142  return;
143  }
144 
145  PointerRNA propptr;
146  PropertyRNA *iterprop = RNA_struct_iterator_property(rna_ptr.type);
147  RNA_property_collection_lookup_int(&rna_ptr, iterprop, index, &propptr);
148 
149  PropertyRNA *prop = reinterpret_cast<PropertyRNA *>(propptr.data);
150 
152  rna_prop_ = prop;
153 }
154 
156 {
157  TreeStoreElem &tselem = *TREESTORE(&legacy_te_);
158  PointerRNA rna_ptr = rna_ptr_;
159  PropertyType proptype = RNA_property_type(rna_prop_);
160 
161  /* If searching don't expand RNA entries */
162  if (SEARCHING_OUTLINER(&space_outliner) && BLI_strcasecmp("RNA", legacy_te_.name) == 0) {
163  tselem.flag &= ~TSE_CHILDSEARCH;
164  }
165 
166  if (proptype == PROP_POINTER) {
167  PointerRNA pptr = RNA_property_pointer_get(&rna_ptr, rna_prop_);
168 
169  if (pptr.data) {
170  if (TSELEM_OPEN(&tselem, &space_outliner)) {
172  &space_outliner, &legacy_te_.subtree, &pptr, &legacy_te_, TSE_RNA_STRUCT, -1);
173  }
174  else {
176  }
177  }
178  }
179  else if (proptype == PROP_COLLECTION) {
180  int tot = RNA_property_collection_length(&rna_ptr, rna_prop_);
181  CLAMP_MAX(tot, max_index);
182 
183  if (TSELEM_OPEN(&tselem, &space_outliner)) {
184  for (int index = 0; index < tot; index++) {
185  PointerRNA pptr;
186  RNA_property_collection_lookup_int(&rna_ptr, rna_prop_, index, &pptr);
188  &space_outliner, &legacy_te_.subtree, &pptr, &legacy_te_, TSE_RNA_STRUCT, index);
189  }
190  }
191  else if (tot) {
193  }
194  }
195  else if (ELEM(proptype, PROP_BOOLEAN, PROP_INT, PROP_FLOAT)) {
196  int tot = RNA_property_array_length(&rna_ptr, rna_prop_);
197  CLAMP_MAX(tot, max_index);
198 
199  if (TSELEM_OPEN(&tselem, &space_outliner)) {
200  for (int index = 0; index < tot; index++) {
201  outliner_add_element(&space_outliner,
203  &rna_ptr,
204  &legacy_te_,
206  index);
207  }
208  }
209  else if (tot) {
211  }
212  }
213 }
214 
216 {
217  return rna_prop_;
218 }
219 
220 /* -------------------------------------------------------------------- */
221 /* RNA Array Element */
222 
224  PointerRNA &rna_ptr,
225  const int index)
226  : TreeElementRNACommon(legacy_te, rna_ptr)
227 {
229 
230  BLI_assert(legacy_te.parent && (legacy_te.parent->store_elem->type == TSE_RNA_PROPERTY));
231  legacy_te_.index = index;
232 
234 
235  legacy_te_.name = reinterpret_cast<char *>(
236  MEM_callocN(sizeof(char[20]), "OutlinerRNAArrayName"));
237  if (c) {
238  sprintf((char *)legacy_te_.name, " %c", c);
239  }
240  else {
241  sprintf((char *)legacy_te_.name, " %d", index + 1);
242  }
244 }
245 
247 {
248  /* Forward query to the parent (which is expected to be a #TreeElementRNAProperty). */
249  const TreeElementRNAProperty *parent_prop_te = tree_element_cast<TreeElementRNAProperty>(
251  return parent_prop_te ? parent_prop_te->getPropertyRNA() : nullptr;
252 }
253 
254 } // namespace blender::ed::outliner
#define BLI_STATIC_ASSERT(a, msg)
Definition: BLI_assert.h:83
#define BLI_assert(a)
Definition: BLI_assert.h:46
int BLI_strcasecmp(const char *s1, const char *s2) ATTR_WARN_UNUSED_RESULT ATTR_NONNULL()
Definition: string.c:623
#define CLAMP_MAX(a, c)
#define UNUSED(x)
#define ELEM(...)
#define IFACE_(msgid)
@ TSE_RNA_ARRAY_ELEM
@ TSE_RNA_PROPERTY
@ TSE_RNA_STRUCT
@ TSE_CHILDSEARCH
@ TSE_CLOSED
Read Guarded memory(de)allocation.
PropertyType
Definition: RNA_types.h:58
@ PROP_FLOAT
Definition: RNA_types.h:61
@ PROP_BOOLEAN
Definition: RNA_types.h:59
@ PROP_INT
Definition: RNA_types.h:60
@ PROP_POINTER
Definition: RNA_types.h:64
@ PROP_COLLECTION
Definition: RNA_types.h:65
@ PROP_HIDDEN
Definition: RNA_types.h:216
TreeElementRNAArrayElement(TreeElement &legacy_te, PointerRNA &rna_ptr, int index)
bool expandPoll(const SpaceOutliner &) const override
virtual PropertyRNA * getPropertyRNA() const
void expand(SpaceOutliner &space_outliner) const override
TreeElementRNAProperty(TreeElement &legacy_te, PointerRNA &rna_ptr, int index)
PropertyRNA * getPropertyRNA() const override
TreeElementRNAStruct(TreeElement &legacy_te, PointerRNA &rna_ptr)
void expand(SpaceOutliner &space_outliner) const override
void *(* MEM_callocN)(size_t len, const char *str)
Definition: mallocn.c:31
static unsigned c
Definition: RandGen.cpp:83
TreeElement * outliner_add_element(SpaceOutliner *space_outliner, ListBase *lb, void *idv, TreeElement *parent, short type, short index, const bool expand)
#define SEARCHING_OUTLINER(sov)
@ TE_FREE_NAME
@ TE_PRETEND_HAS_CHILDREN
#define TREESTORE(a)
#define TSELEM_OPEN(telm, sv)
char * RNA_struct_name_get_alloc(PointerRNA *ptr, char *fixedbuf, int fixedlen, int *r_len)
Definition: rna_access.c:907
const char * RNA_struct_ui_name(const StructRNA *type)
Definition: rna_access.c:591
PropertyType RNA_property_type(PropertyRNA *prop)
Definition: rna_access.c:1010
PointerRNA RNA_property_pointer_get(PointerRNA *ptr, PropertyRNA *prop)
Definition: rna_access.c:3493
int RNA_property_collection_lookup_int(PointerRNA *ptr, PropertyRNA *prop, int key, PointerRNA *r_ptr)
Definition: rna_access.c:4097
char RNA_property_array_item_char(PropertyRNA *prop, int index)
Definition: rna_access.c:1105
int RNA_property_flag(PropertyRNA *prop)
Definition: rna_access.c:1055
int RNA_property_array_length(PointerRNA *ptr, PropertyRNA *prop)
Definition: rna_access.c:1075
PropertyRNA * RNA_struct_iterator_property(StructRNA *type)
Definition: rna_access.c:634
int RNA_property_collection_length(PointerRNA *ptr, PropertyRNA *prop)
Definition: rna_access.c:3762
const char * RNA_property_ui_name(const PropertyRNA *prop)
Definition: rna_access.c:1875
struct StructRNA * type
Definition: RNA_types.h:37
void * data
Definition: RNA_types.h:38
struct TreeElement * parent
ListBase subtree
TreeStoreElem * store_elem
const char * name
PointerRNA * ptr
Definition: wm_files.c:3480