forked from FreeCAD/FreeCAD
/
Property.cpp
332 lines (285 loc) · 9.59 KB
/
Property.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
/***************************************************************************
* Copyright (c) 2002 Jürgen Riegel <juergen.riegel@web.de> *
* *
* This file is part of the FreeCAD CAx development system. *
* *
* This library is free software; you can redistribute it and/or *
* modify it under the terms of the GNU Library General Public *
* License as published by the Free Software Foundation; either *
* version 2 of the License, or (at your option) any later version. *
* *
* This library is distributed in the hope that it will be useful, *
* but WITHOUT ANY WARRANTY; without even the implied warranty of *
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
* GNU Library General Public License for more details. *
* *
* You should have received a copy of the GNU Library General Public *
* License along with this library; see the file COPYING.LIB. If not, *
* write to the Free Software Foundation, Inc., 59 Temple Place, *
* Suite 330, Boston, MA 02111-1307, USA *
* *
***************************************************************************/
#include "PreCompiled.h"
#ifndef _PreComp_
# include <cassert>
#endif
/// Here the FreeCAD includes sorted by Base,App,Gui......
#include "Property.h"
#include "ObjectIdentifier.h"
#include "PropertyContainer.h"
#include <Base/Exception.h>
#include "Application.h"
#include "DocumentObject.h"
using namespace App;
//**************************************************************************
//**************************************************************************
// Property
//++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
TYPESYSTEM_SOURCE_ABSTRACT(App::Property , Base::Persistence)
//**************************************************************************
// Construction/Destruction
// Here is the implementation! Description should take place in the header file!
Property::Property()
:father(0), myName(0)
{
}
Property::~Property()
{
}
const char* Property::getName(void) const
{
if (myName == NULL)
return "?";
return myName;
}
std::string Property::getFullName() const {
std::string name;
if(myName) {
if(father)
name = father->getFullName() + ".";
name += myName;
}else
return "?";
return name;
}
short Property::getType(void) const
{
short type = 0;
#define GET_PTYPE(_name) do {\
if(testStatus(App::Property::Prop##_name)) type|=Prop_##_name;\
}while(0)
GET_PTYPE(ReadOnly);
GET_PTYPE(Hidden);
GET_PTYPE(Output);
GET_PTYPE(Transient);
GET_PTYPE(NoRecompute);
GET_PTYPE(NoPersist);
return type;
}
void Property::syncType(unsigned type) {
#define SYNC_PTYPE(_name) do{\
if(type & Prop_##_name) StatusBits.set((size_t)Prop##_name);\
}while(0)
SYNC_PTYPE(ReadOnly);
SYNC_PTYPE(Transient);
SYNC_PTYPE(Hidden);
SYNC_PTYPE(Output);
SYNC_PTYPE(NoRecompute);
SYNC_PTYPE(NoPersist);
}
const char* Property::getGroup(void) const
{
return father->getPropertyGroup(this);
}
const char* Property::getDocumentation(void) const
{
return father->getPropertyDocumentation(this);
}
void Property::setContainer(PropertyContainer *Father)
{
father = Father;
}
void Property::setPathValue(const ObjectIdentifier &path, const boost::any &value)
{
path.setValue(value);
}
const boost::any Property::getPathValue(const ObjectIdentifier &path) const
{
return path.getValue();
}
void Property::getPaths(std::vector<ObjectIdentifier> &paths) const
{
paths.emplace_back(getContainer(), getName());
}
ObjectIdentifier Property::canonicalPath(const ObjectIdentifier &p) const
{
return p;
}
namespace App {
/*!
* \brief The PropertyCleaner struct
* Make deleting dynamic property safer by postponing its destruction.
*
* Dynamic property can be removed at any time, even during triggering of
* onChanged() signal of the removing property. This patch introduced
* static function Property::destroy() to make it safer by queueing any
* removed property, and only deleting them when no onChanged() call is
* active.
*/
struct PropertyCleaner {
PropertyCleaner(Property *p)
: prop(p)
{
++_PropCleanerCounter;
}
~PropertyCleaner() {
if(--_PropCleanerCounter)
return;
bool found = false;
while (_RemovedProps.size()) {
auto p = _RemovedProps.back();
_RemovedProps.pop_back();
if(p != prop)
delete p;
else
found = true;
}
if (found)
_RemovedProps.push_back(prop);
}
static void add(Property *prop) {
_RemovedProps.push_back(prop);
}
Property *prop;
static std::vector<Property*> _RemovedProps;
static int _PropCleanerCounter;
};
}
std::vector<Property*> PropertyCleaner::_RemovedProps;
int PropertyCleaner::_PropCleanerCounter = 0;
void Property::destroy(Property *p) {
if (p) {
// Is it necessary to nullify the container? May cause crash if any
// onChanged() caller assumes a non-null container.
//
// p->setContainer(0);
PropertyCleaner::add(p);
}
}
void Property::touch()
{
PropertyCleaner guard(this);
if (father)
father->onChanged(this);
StatusBits.set(Touched);
}
void Property::setReadOnly(bool readOnly)
{
this->setStatus(App::Property::ReadOnly, readOnly);
}
void Property::hasSetValue(void)
{
PropertyCleaner guard(this);
if (father)
father->onChanged(this);
StatusBits.set(Touched);
}
void Property::aboutToSetValue(void)
{
if (father)
father->onBeforeChange(this);
}
void Property::verifyPath(const ObjectIdentifier &p) const
{
p.verify(*this);
}
Property *Property::Copy(void) const
{
// have to be reimplemented by a subclass!
assert(0);
return 0;
}
void Property::Paste(const Property& /*from*/)
{
// have to be reimplemented by a subclass!
assert(0);
}
void Property::setStatusValue(unsigned long status) {
static const unsigned long mask =
(1<<PropDynamic)
|(1<<PropNoRecompute)
|(1<<PropReadOnly)
|(1<<PropTransient)
|(1<<PropOutput)
|(1<<PropHidden);
status &= ~mask;
status |= StatusBits.to_ulong() & mask;
unsigned long oldStatus = StatusBits.to_ulong();
StatusBits = decltype(StatusBits)(status);
if(father) {
static unsigned long _signalMask = (1<<ReadOnly) | (1<<Hidden);
if((status & _signalMask) != (oldStatus & _signalMask))
father->onPropertyStatusChanged(*this,oldStatus);
}
}
void Property::setStatus(Status pos, bool on) {
auto bits = StatusBits;
bits.set(pos,on);
setStatusValue(bits.to_ulong());
}
//**************************************************************************
//**************************************************************************
// PropertyListsBase
//++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
void PropertyListsBase::_setPyObject(PyObject *value) {
std::vector<int> indices;
std::vector<PyObject *> vals;
Py::Object pySeq;
if (PyDict_Check(value)) {
Py::Dict dict(value);
auto size = dict.size();
vals.reserve(size);
indices.reserve(size);
int listSize = getSize();
for(auto it=dict.begin();it!=dict.end();++it) {
const auto &item = *it;
PyObject *key = item.first.ptr();
if(!PyLong_Check(key))
throw Base::TypeError("expect key type to be integer");
long idx = PyLong_AsLong(key);
if(idx<-1 || idx>listSize)
throw Base::ValueError("index out of bound");
if(idx==-1 || idx==listSize) {
idx = listSize;
++listSize;
}
indices.push_back(idx);
vals.push_back(item.second.ptr());
}
} else {
if (PySequence_Check(value))
pySeq = value;
else {
PyObject *iter = PyObject_GetIter(value);
if(iter) {
Py::Object pyIter(iter,true);
pySeq = Py::asObject(PySequence_Fast(iter,""));
} else {
PyErr_Clear();
vals.push_back(value);
}
}
if(!pySeq.isNone()) {
Py::Sequence seq(pySeq);
vals.reserve(seq.size());
for(auto it=seq.begin();it!=seq.end();++it)
vals.push_back((*it).ptr());
}
}
setPyValues(vals,indices);
}
//**************************************************************************
//**************************************************************************
// PropertyLists
//++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
TYPESYSTEM_SOURCE_ABSTRACT(App::PropertyLists , App::Property)