Blender V4.5
tree_element_rna.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 "BLI_string.h"
10
11#include "BLT_translation.hh"
12
13#include "DNA_outliner_types.h"
14#include "DNA_space_types.h"
15
16#include "MEM_guardedalloc.h"
17
18#include "RNA_access.hh"
19
20#include "../outliner_intern.hh"
21
22#include "tree_element_rna.hh"
23
24namespace blender::ed::outliner {
25
26/* Don't display arrays larger, weak but index is stored as a short,
27 * also the outliner isn't intended for editing such large data-sets. */
28BLI_STATIC_ASSERT(sizeof(TreeElement::index) == 2, "Index is no longer short!")
29
30/* -------------------------------------------------------------------- */
31/* Common functionality (#TreeElementRNACommon Base Class) */
32
34 : AbstractTreeElement(legacy_te), rna_ptr_(rna_ptr)
35{
36 /* Create an empty tree-element. */
37 if (!is_rna_valid()) {
38 legacy_te_.name = IFACE_("(empty)");
39 return;
40 }
41}
42
44{
45 return rna_ptr_.data != nullptr;
46}
47
48bool TreeElementRNACommon::expand_poll(const SpaceOutliner & /*space_outliner*/) const
49{
50 return is_rna_valid();
51}
52
57
59{
60 return nullptr;
61}
62
63/* -------------------------------------------------------------------- */
64/* RNA Struct */
65
67 : TreeElementRNACommon(legacy_te, rna_ptr)
68{
70
71 if (!is_rna_valid()) {
72 return;
73 }
74
75 legacy_te_.name = RNA_struct_name_get_alloc(&rna_ptr, nullptr, 0, nullptr);
76 if (legacy_te_.name) {
78 }
79 else {
80 legacy_te_.name = RNA_struct_ui_name(rna_ptr.type);
81 }
82}
83
85{
88
89 /* If searching don't expand RNA entries */
90 if (SEARCHING_OUTLINER(&space_outliner) && BLI_strcasecmp("RNA", legacy_te_.name) == 0) {
91 tselem.flag &= ~TSE_CHILDSEARCH;
92 }
93
95 int tot = RNA_property_collection_length(&ptr, iterprop);
96 CLAMP_MAX(tot, max_index);
97
98 TreeElementRNAProperty *parent_prop_te = legacy_te_.parent ?
100 legacy_te_.parent) :
101 nullptr;
102 /* auto open these cases */
103 if (!parent_prop_te || (RNA_property_type(parent_prop_te->get_property_rna()) == PROP_POINTER)) {
104 if (!tselem.used) {
105 tselem.flag &= ~TSE_CLOSED;
106 }
107 }
108
109 if (TSELEM_OPEN(&tselem, &space_outliner)) {
110 for (int index = 0; index < tot; index++) {
111 PointerRNA propptr;
112 RNA_property_collection_lookup_int(&ptr, iterprop, index, &propptr);
113 if (!(RNA_property_flag(static_cast<PropertyRNA *>(propptr.data)) & PROP_HIDDEN)) {
114 add_element(&legacy_te_.subtree, ptr.owner_id, &ptr, &legacy_te_, TSE_RNA_PROPERTY, index);
115 }
116 }
117 }
118 else if (tot) {
120 }
121}
122
123/* -------------------------------------------------------------------- */
124/* RNA Property */
125
127 PointerRNA &rna_ptr,
128 const int index)
129 : TreeElementRNACommon(legacy_te, rna_ptr)
130{
132
133 if (!is_rna_valid()) {
134 return;
135 }
136
137 PointerRNA propptr;
138 PropertyRNA *iterprop = RNA_struct_iterator_property(rna_ptr.type);
139 RNA_property_collection_lookup_int(&rna_ptr, iterprop, index, &propptr);
140
141 PropertyRNA *prop = static_cast<PropertyRNA *>(propptr.data);
142
143 legacy_te_.name = RNA_property_ui_name(prop);
144 rna_prop_ = prop;
145}
146
148{
150 PointerRNA rna_ptr = rna_ptr_;
151 PropertyType proptype = RNA_property_type(rna_prop_);
152
153 /* If searching don't expand RNA entries */
154 if (SEARCHING_OUTLINER(&space_outliner) && BLI_strcasecmp("RNA", legacy_te_.name) == 0) {
155 tselem.flag &= ~TSE_CHILDSEARCH;
156 }
157
158 if (proptype == PROP_POINTER) {
159 PointerRNA pptr = RNA_property_pointer_get(&rna_ptr, rna_prop_);
160
161 if (pptr.data) {
162 if (TSELEM_OPEN(&tselem, &space_outliner)) {
163 add_element(&legacy_te_.subtree, pptr.owner_id, &pptr, &legacy_te_, TSE_RNA_STRUCT, -1);
164 }
165 else {
167 }
168 }
169 }
170 else if (proptype == PROP_COLLECTION) {
171 int tot = RNA_property_collection_length(&rna_ptr, rna_prop_);
172 CLAMP_MAX(tot, max_index);
173
174 if (TSELEM_OPEN(&tselem, &space_outliner)) {
175 for (int index = 0; index < tot; index++) {
176 PointerRNA pptr;
177 RNA_property_collection_lookup_int(&rna_ptr, rna_prop_, index, &pptr);
178 add_element(&legacy_te_.subtree, pptr.owner_id, &pptr, &legacy_te_, TSE_RNA_STRUCT, index);
179 }
180 }
181 else if (tot) {
183 }
184 }
185 else if (ELEM(proptype, PROP_BOOLEAN, PROP_INT, PROP_FLOAT)) {
186 int tot = RNA_property_array_length(&rna_ptr, rna_prop_);
187 CLAMP_MAX(tot, max_index);
188
189 if (TSELEM_OPEN(&tselem, &space_outliner)) {
190 for (int index = 0; index < tot; index++) {
191 add_element(&legacy_te_.subtree,
192 rna_ptr.owner_id,
193 &rna_ptr,
194 &legacy_te_,
196 index);
197 }
198 }
199 else if (tot) {
201 }
202 }
203}
204
206{
207 return rna_prop_;
208}
209
210/* -------------------------------------------------------------------- */
211/* RNA Array Element */
212
214 PointerRNA &rna_ptr,
215 const int index)
216 : TreeElementRNACommon(legacy_te, rna_ptr)
217{
219
220 BLI_assert(legacy_te.parent && (legacy_te.parent->store_elem->type == TSE_RNA_PROPERTY));
221 legacy_te_.index = index;
222
224
225 const size_t name_size = sizeof(char[20]);
226 legacy_te_.name = MEM_calloc_arrayN<char>(name_size, "OutlinerRNAArrayName");
227 if (c) {
228 BLI_snprintf((char *)legacy_te_.name, name_size, " %c", c);
229 }
230 else {
231 BLI_snprintf((char *)legacy_te_.name, name_size, " %d", index + 1);
232 }
233 legacy_te_.flag |= TE_FREE_NAME;
234}
235
237{
238 /* Forward query to the parent (which is expected to be a #TreeElementRNAProperty). */
240 legacy_te_.parent);
241 return parent_prop_te ? parent_prop_te->get_property_rna() : nullptr;
242}
243
244} // 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 char char int BLI_strcasecmp(const char *s1, const char *s2) ATTR_WARN_UNUSED_RESULT ATTR_NONNULL(1
size_t BLI_snprintf(char *__restrict dst, size_t dst_maxncpy, const char *__restrict format,...) ATTR_NONNULL(1
#define CLAMP_MAX(a, c)
#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.hh:149
@ PROP_FLOAT
Definition RNA_types.hh:152
@ PROP_BOOLEAN
Definition RNA_types.hh:150
@ PROP_INT
Definition RNA_types.hh:151
@ PROP_POINTER
Definition RNA_types.hh:155
@ PROP_COLLECTION
Definition RNA_types.hh:156
@ PROP_HIDDEN
Definition RNA_types.hh:324
TreeElement * add_element(ListBase *lb, ID *owner_id, void *create_data, TreeElement *parent, short type, short index, const bool expand=true) const
TreeElementRNAArrayElement(TreeElement &legacy_te, PointerRNA &rna_ptr, int index)
bool expand_poll(const SpaceOutliner &) const override
virtual PropertyRNA * get_property_rna() const
TreeElementRNACommon(TreeElement &legacy_te, PointerRNA &rna_ptr)
void expand(SpaceOutliner &space_outliner) const override
TreeElementRNAProperty(TreeElement &legacy_te, PointerRNA &rna_ptr, int index)
PropertyRNA * get_property_rna() const override
TreeElementRNAStruct(TreeElement &legacy_te, PointerRNA &rna_ptr)
void expand(SpaceOutliner &space_outliner) const override
void * MEM_calloc_arrayN(size_t len, size_t size, const char *str)
Definition mallocn.cc:123
TreeElementT * tree_element_cast(const TreeElement *te)
#define SEARCHING_OUTLINER(sov)
#define TREESTORE(a)
#define TSELEM_OPEN(telm, sv)
char * RNA_struct_name_get_alloc(PointerRNA *ptr, char *fixedbuf, int fixedlen, int *r_len)
PropertyType RNA_property_type(PropertyRNA *prop)
PointerRNA RNA_property_pointer_get(PointerRNA *ptr, PropertyRNA *prop)
char RNA_property_array_item_char(PropertyRNA *prop, int index)
int RNA_property_flag(PropertyRNA *prop)
bool RNA_property_collection_lookup_int(PointerRNA *ptr, PropertyRNA *prop, int key, PointerRNA *r_ptr)
const char * RNA_struct_ui_name(const StructRNA *type)
int RNA_property_array_length(PointerRNA *ptr, PropertyRNA *prop)
const char * RNA_property_ui_name(const PropertyRNA *prop)
PropertyRNA * RNA_struct_iterator_property(StructRNA *type)
int RNA_property_collection_length(PointerRNA *ptr, PropertyRNA *prop)
ID * owner_id
Definition RNA_types.hh:51
StructRNA * type
Definition RNA_types.hh:52
void * data
Definition RNA_types.hh:53
PointerRNA * ptr
Definition wm_files.cc:4227