@@ -34,6 +34,9 @@ along with sofaqtquick. If not, see <http://www.gnu.org/licenses/>.
34
34
#include < sofa/core/sptr.h>
35
35
#include < sofa/helper/Factory.h>
36
36
#include < sofa/core/objectmodel/Data.h>
37
+ #include < sofa/core/objectmodel/Base.h>
38
+ #include < sofa/core/objectmodel/BaseObject.h>
39
+ #include < sofa/core/objectmodel/BaseNode.h>
37
40
38
41
#include " config.h"
39
42
@@ -44,10 +47,112 @@ namespace sofa {
44
47
}
45
48
namespace core {
46
49
namespace objectmodel {
47
- class Base ;
48
50
class BaseData ;
49
- class BaseNode ;
50
- class BaseObject ;
51
+
52
+
53
+ class SOFAPYTHON3_API PrefabLink
54
+ {
55
+ public:
56
+ PrefabLink () {}
57
+ PrefabLink (const Base::SPtr& targetBase) { m_targetBase = targetBase; }
58
+ PrefabLink (BaseLink* targetLink) { m_targetBase = targetLink->getLinkedBase (); }
59
+ PrefabLink (const std::string& targetPath) { m_targetPath = targetPath; }
60
+
61
+ const Base::SPtr& getTargetBase () const { return m_targetBase; }
62
+ void setTargetBase (const Base::SPtr& targetBase) { m_targetBase = targetBase; }
63
+
64
+ const std::string& getTargetPath () const { return m_targetPath; }
65
+ void setTargetPath (const std::string& targetPath) { m_targetPath = targetPath; }
66
+
67
+ friend std::ostream& operator << ( std::ostream& out, const PrefabLink& l)
68
+ {
69
+ if (l.getTargetBase ())
70
+ {
71
+ auto bn = l.getTargetBase ()->toBaseNode ();
72
+ auto bo = l.getTargetBase ()->toBaseObject ();
73
+ out << " @" + (bn ? bn->getPathName () : bo->getPathName ());
74
+ }
75
+ out << l.getTargetPath ();
76
+ return out;
77
+ }
78
+
79
+ friend std::istream& operator >> ( std::istream& in, PrefabLink& l)
80
+ {
81
+ std::string s;
82
+ in >> s;
83
+ l.setTargetPath (s);
84
+ return in;
85
+ }
86
+
87
+ private:
88
+ Base::SPtr m_targetBase { nullptr };
89
+ std::string m_targetPath {" " };
90
+ };
91
+
92
+ class SOFAPYTHON3_API DataLink : public Data<PrefabLink>
93
+ {
94
+ typedef Data<PrefabLink> Inherit;
95
+
96
+ DataLink ( const std::string& helpMsg=" " , bool isDisplayed=true , bool isReadOnly=false )
97
+ : Inherit(helpMsg, isDisplayed, isReadOnly)
98
+ {
99
+ }
100
+
101
+ DataLink ( const std::string& value, const std::string& helpMsg=" " , bool isDisplayed=true , bool isReadOnly=false )
102
+ : Inherit(value, helpMsg, isDisplayed, isReadOnly)
103
+ {
104
+ }
105
+
106
+ explicit DataLink (const BaseData::BaseInitData& init)
107
+ : Inherit(init)
108
+ {
109
+ }
110
+
111
+ const PrefabLink& getValue () const
112
+ {
113
+ updateIfDirty ();
114
+ if (m_value.getValue ().getTargetBase ()) return m_value.getValue ();
115
+
116
+ auto self = const_cast <DataLink*>(this );
117
+
118
+ Base* dst = nullptr ;
119
+ this ->getOwner ()->findLinkDest (dst, self->m_value .getValue ().getTargetPath (), nullptr );
120
+ if (dst) {
121
+ auto edit = self->m_value .beginEdit ();
122
+ edit->setTargetBase (dst);
123
+ edit->setTargetPath (" " );
124
+ self->m_value .endEdit ();
125
+ }
126
+ return m_value.getValue ();
127
+ }
128
+
129
+ std::string getValueString () const
130
+ {
131
+ const auto & ptr = getValue ();
132
+ if (ptr.getTargetBase ())
133
+ {
134
+ auto bn = ptr.getTargetBase ()->toBaseNode ();
135
+ auto bo = ptr.getTargetBase ()->toBaseObject ();
136
+ return " @" + (bn ? bn->getPathName () : bo->getPathName ());
137
+ }
138
+ return ptr.getTargetPath ();
139
+ }
140
+
141
+
142
+ bool read (const std::string& value)
143
+ {
144
+ Base* dst;
145
+ auto data = m_value.beginEdit ();
146
+ if (this ->getOwner ()->findLinkDest (dst, value, nullptr ) && dst != nullptr )
147
+ data->setTargetBase (dst);
148
+ else {
149
+ data->setTargetBase (nullptr );
150
+ data->setTargetPath (value);
151
+ }
152
+ return true ;
153
+ }
154
+ };
155
+
51
156
}
52
157
}
53
158
}
@@ -189,7 +294,7 @@ class scoped_writeonly_access
189
294
~scoped_writeonly_access (){ data->endEditVoidPtr (); }
190
295
};
191
296
192
- SOFAPYTHON3_API BaseData* addData (py::object py_self, const std::string& name, py::object value = py::object (), py::object defaultValue = py::object (), const std::string& help = "", const std::string& group = "Property", std::string type = "");
297
+ SOFAPYTHON3_API BaseData* addData (py::object py_self, const std::string& name, py::object value = py::none (), py::object defaultValue = py::none (), const std::string& help = "", const std::string& group = "Property", std::string type = "");
193
298
SOFAPYTHON3_API BaseLink* addLink (py::object py_self, const std::string& name, py::object value, const std::string& help);
194
299
SOFAPYTHON3_API bool isProtectedKeyword (const std::string& name);
195
300
0 commit comments