Blender V5.0
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#include "BLI_string_utf8.h"
11
12#include "BLT_translation.hh"
13
14#include "DNA_outliner_types.h"
15#include "DNA_space_types.h"
16
17#include "MEM_guardedalloc.h"
18
19#include "RNA_access.hh"
20
21#include "../outliner_intern.hh"
22
23#include "tree_element_rna.hh"
24
25namespace blender::ed::outliner {
26
27/* Don't display arrays larger, weak but index is stored as a short,
28 * also the outliner isn't intended for editing such large data-sets. */
29BLI_STATIC_ASSERT(sizeof(TreeElement::index) == 2, "Index is no longer short!")
30
31/* -------------------------------------------------------------------- */
32/* Common functionality (#TreeElementRNACommon Base Class) */
33
35 : AbstractTreeElement(legacy_te), rna_ptr_(rna_ptr)
36{
37 /* Create an empty tree-element. */
38 if (!is_rna_valid()) {
39 legacy_te_.name = IFACE_("(empty)");
40 return;
41 }
42}
43
45{
46 return rna_ptr_.data != nullptr;
47}
48
49bool TreeElementRNACommon::expand_poll(const SpaceOutliner & /*space_outliner*/) const
50{
51 return is_rna_valid();
52}
53
58
60{
61 return nullptr;
62}
63
64/* -------------------------------------------------------------------- */
65/* RNA Struct */
66
68 : TreeElementRNACommon(legacy_te, rna_ptr)
69{
71
72 if (!is_rna_valid()) {
73 return;
74 }
75
76 legacy_te_.name = RNA_struct_name_get_alloc(&rna_ptr, nullptr, 0, nullptr);
77 if (legacy_te_.name) {
79 }
80 else {
81 legacy_te_.name = RNA_struct_ui_name(rna_ptr.type);
82 }
83}
84
86{
89
90 /* If searching don't expand RNA entries */
91 if (SEARCHING_OUTLINER(&space_outliner) && BLI_strcasecmp("RNA", legacy_te_.name) == 0) {
92 tselem.flag &= ~TSE_CHILDSEARCH;
93 }
94
96 int tot = RNA_property_collection_length(&ptr, iterprop);
97 CLAMP_MAX(tot, max_index);
98
99 TreeElementRNAProperty *parent_prop_te = legacy_te_.parent ?
101 legacy_te_.parent) :
102 nullptr;
103 /* auto open these cases */
104 if (!parent_prop_te || (RNA_property_type(parent_prop_te->get_property_rna()) == PROP_POINTER)) {
105 if (!tselem.used) {
106 tselem.flag &= ~TSE_CLOSED;
107 }
108 }
109
110 if (TSELEM_OPEN(&tselem, &space_outliner)) {
111 for (int index = 0; index < tot; index++) {
112 PointerRNA propptr;
113 RNA_property_collection_lookup_int(&ptr, iterprop, index, &propptr);
114 if (!(RNA_property_flag(static_cast<PropertyRNA *>(propptr.data)) & PROP_HIDDEN)) {
115 add_element(&legacy_te_.subtree, ptr.owner_id, &ptr, &legacy_te_, TSE_RNA_PROPERTY, index);
116 }
117 }
118 }
119 else if (tot) {
121 }
122}
123
124/* -------------------------------------------------------------------- */
125/* RNA Property */
126
128 PointerRNA &rna_ptr,
129 const int index)
130 : TreeElementRNACommon(legacy_te, rna_ptr)
131{
133
134 if (!is_rna_valid()) {
135 return;
136 }
137
138 PointerRNA propptr;
139 PropertyRNA *iterprop = RNA_struct_iterator_property(rna_ptr.type);
140 RNA_property_collection_lookup_int(&rna_ptr, iterprop, index, &propptr);
141
142 PropertyRNA *prop = static_cast<PropertyRNA *>(propptr.data);
143
144 legacy_te_.name = RNA_property_ui_name(prop);
145 rna_prop_ = prop;
146}
147
149{
151 PointerRNA rna_ptr = rna_ptr_;
152 PropertyType proptype = RNA_property_type(rna_prop_);
153
154 /* If searching don't expand RNA entries */
155 if (SEARCHING_OUTLINER(&space_outliner) && BLI_strcasecmp("RNA", legacy_te_.name) == 0) {
156 tselem.flag &= ~TSE_CHILDSEARCH;
157 }
158
159 if (proptype == PROP_POINTER) {
160 PointerRNA pptr = RNA_property_pointer_get(&rna_ptr, rna_prop_);
161
162 if (pptr.data) {
163 if (TSELEM_OPEN(&tselem, &space_outliner)) {
164 add_element(&legacy_te_.subtree, pptr.owner_id, &pptr, &legacy_te_, TSE_RNA_STRUCT, -1);
165 }
166 else {
168 }
169 }
170 }
171 else if (proptype == PROP_COLLECTION) {
172 int tot = RNA_property_collection_length(&rna_ptr, rna_prop_);
173 CLAMP_MAX(tot, max_index);
174
175 if (TSELEM_OPEN(&tselem, &space_outliner)) {
176 for (int index = 0; index < tot; index++) {
177 PointerRNA pptr;
178 RNA_property_collection_lookup_int(&rna_ptr, rna_prop_, index, &pptr);
179 add_element(&legacy_te_.subtree, pptr.owner_id, &pptr, &legacy_te_, TSE_RNA_STRUCT, index);
180 }
181 }
182 else if (tot) {
184 }
185 }
186 else if (ELEM(proptype, PROP_BOOLEAN, PROP_INT, PROP_FLOAT)) {
187 int tot = RNA_property_array_length(&rna_ptr, rna_prop_);
188 CLAMP_MAX(tot, max_index);
189
190 if (TSELEM_OPEN(&tselem, &space_outliner)) {
191 for (int index = 0; index < tot; index++) {
192 add_element(&legacy_te_.subtree,
193 rna_ptr.owner_id,
194 &rna_ptr,
195 &legacy_te_,
197 index);
198 }
199 }
200 else if (tot) {
202 }
203 }
204}
205
207{
208 return rna_prop_;
209}
210
211/* -------------------------------------------------------------------- */
212/* RNA Array Element */
213
215 PointerRNA &rna_ptr,
216 const int index)
217 : TreeElementRNACommon(legacy_te, rna_ptr)
218{
220
221 BLI_assert(legacy_te.parent && (legacy_te.parent->store_elem->type == TSE_RNA_PROPERTY));
222 legacy_te_.index = index;
223
225
226 const size_t name_size = sizeof(char[20]);
227 char *name = MEM_calloc_arrayN<char>(name_size, "OutlinerRNAArrayName");
228 if (c) {
229 BLI_snprintf_utf8(name, name_size, " %c", c);
230 }
231 else {
232 BLI_snprintf_utf8(name, name_size, " %d", index + 1);
233 }
234 legacy_te_.name = name;
235 legacy_te_.flag |= TE_FREE_NAME;
236}
237
239{
240 /* Forward query to the parent (which is expected to be a #TreeElementRNAProperty). */
242 legacy_te_.parent);
243 return parent_prop_te ? parent_prop_te->get_property_rna() : nullptr;
244}
245
246} // 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 size_t size_t BLI_snprintf_utf8(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:161
@ PROP_FLOAT
Definition RNA_types.hh:164
@ PROP_BOOLEAN
Definition RNA_types.hh:162
@ PROP_INT
Definition RNA_types.hh:163
@ PROP_POINTER
Definition RNA_types.hh:167
@ PROP_COLLECTION
Definition RNA_types.hh:168
@ PROP_HIDDEN
Definition RNA_types.hh:338
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)
const char * name
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)
const char * RNA_property_ui_name(const PropertyRNA *prop, const PointerRNA *ptr)
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)
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:4238