/
DynamicProperty.cpp
430 lines (383 loc) · 16.1 KB
/
DynamicProperty.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
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
/***************************************************************************
* Copyright (c) 2009 Werner Mayer <wmayer[at]users.sourceforge.net> *
* *
* 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 <algorithm>
#endif
#include "DynamicProperty.h"
#include "Property.h"
#include "PropertyContainer.h"
#include "Application.h"
#include <Base/Reader.h>
#include <Base/Writer.h>
#include <Base/Console.h>
#include <Base/Exception.h>
#include <Base/Tools.h>
using namespace App;
DynamicProperty::DynamicProperty(PropertyContainer* p) : pc(p)
{
}
DynamicProperty::~DynamicProperty()
{
}
void DynamicProperty::getPropertyList(std::vector<Property*> &List) const
{
// get the properties of the base class first and insert the dynamic properties afterwards
this->pc->PropertyContainer::getPropertyList(List);
for (std::map<std::string,PropData>::const_iterator it = props.begin(); it != props.end(); ++it)
List.push_back(it->second.property);
}
void DynamicProperty::getPropertyMap(std::map<std::string,Property*> &Map) const
{
// get the properties of the base class first and insert the dynamic properties afterwards
this->pc->PropertyContainer::getPropertyMap(Map);
for (std::map<std::string,PropData>::const_iterator it = props.begin(); it != props.end(); ++it)
Map[it->first] = it->second.property;
}
Property *DynamicProperty::getPropertyByName(const char* name) const
{
std::map<std::string,PropData>::const_iterator it = props.find(name);
if (it != props.end())
return it->second.property;
return this->pc->PropertyContainer::getPropertyByName(name);
}
Property *DynamicProperty::getDynamicPropertyByName(const char* name) const
{
std::map<std::string,PropData>::const_iterator it = props.find(name);
if (it != props.end())
return it->second.property;
return 0;
}
std::vector<std::string> DynamicProperty::getDynamicPropertyNames() const
{
std::vector<std::string> names;
for (std::map<std::string,PropData>::const_iterator it = props.begin(); it != props.end(); ++it) {
names.push_back(it->first);
}
return names;
}
void DynamicProperty::addDynamicProperties(const PropertyContainer* cont)
{
std::vector<std::string> names = cont->getDynamicPropertyNames();
for (std::vector<std::string>::iterator it = names.begin(); it != names.end(); ++it) {
App::Property* prop = cont->getDynamicPropertyByName(it->c_str());
if (prop) {
addDynamicProperty(
prop->getTypeId().getName(),
prop->getName(),
prop->getGroup(),
prop->getDocumentation(),
prop->getType(),
cont->isReadOnly(prop),
cont->isHidden(prop));
}
}
}
const char* DynamicProperty::getPropertyName(const Property* prop) const
{
for (std::map<std::string,PropData>::const_iterator it = props.begin(); it != props.end(); ++it) {
if (it->second.property == prop)
return it->first.c_str();
}
return this->pc->PropertyContainer::getPropertyName(prop);
}
unsigned int DynamicProperty::getMemSize (void) const
{
std::map<std::string,Property*> Map;
getPropertyMap(Map);
std::map<std::string,Property*>::const_iterator It;
unsigned int size = 0;
for (It = Map.begin(); It != Map.end();++It)
size += It->second->getMemSize();
return size;
}
short DynamicProperty::getPropertyType(const Property* prop) const
{
for (std::map<std::string,PropData>::const_iterator it = props.begin(); it != props.end(); ++it) {
if (it->second.property == prop)
return it->second.attr;
}
return this->pc->PropertyContainer::getPropertyType(prop);
}
short DynamicProperty::getPropertyType(const char *name) const
{
std::map<std::string,PropData>::const_iterator it = props.find(name);
if (it != props.end())
return it->second.attr;
return this->pc->PropertyContainer::getPropertyType(name);
}
const char* DynamicProperty::getPropertyGroup(const Property* prop) const
{
for (std::map<std::string,PropData>::const_iterator it = props.begin(); it != props.end(); ++it) {
if (it->second.property == prop)
return it->second.group.c_str();
}
return this->pc->PropertyContainer::getPropertyGroup(prop);
}
const char* DynamicProperty::getPropertyGroup(const char *name) const
{
std::map<std::string,PropData>::const_iterator it = props.find(name);
if (it != props.end())
return it->second.group.c_str();
return this->pc->PropertyContainer::getPropertyGroup(name);
}
const char* DynamicProperty::getPropertyDocumentation(const Property* prop) const
{
for (std::map<std::string,PropData>::const_iterator it = props.begin(); it != props.end(); ++it) {
if (it->second.property == prop)
return it->second.doc.c_str();
}
return this->pc->PropertyContainer::getPropertyDocumentation(prop);
}
const char* DynamicProperty::getPropertyDocumentation(const char *name) const
{
std::map<std::string,PropData>::const_iterator it = props.find(name);
if (it != props.end())
return it->second.doc.c_str();
return this->pc->PropertyContainer::getPropertyDocumentation(name);
}
bool DynamicProperty::isReadOnly(const Property* prop) const
{
for (std::map<std::string,PropData>::const_iterator it = props.begin(); it != props.end(); ++it) {
if (it->second.property == prop)
return it->second.readonly;
}
return this->pc->PropertyContainer::isReadOnly(prop);
}
bool DynamicProperty::isReadOnly(const char *name) const
{
std::map<std::string,PropData>::const_iterator it = props.find(name);
if (it != props.end())
return it->second.readonly;
return this->pc->PropertyContainer::isReadOnly(name);
}
bool DynamicProperty::isHidden(const Property* prop) const
{
for (std::map<std::string,PropData>::const_iterator it = props.begin(); it != props.end(); ++it) {
if (it->second.property == prop)
return it->second.hidden;
}
return this->pc->PropertyContainer::isHidden(prop);
}
bool DynamicProperty::isHidden(const char *name) const
{
std::map<std::string,PropData>::const_iterator it = props.find(name);
if (it != props.end())
return it->second.hidden;
return this->pc->PropertyContainer::isHidden(name);
}
Property* DynamicProperty::addDynamicProperty(const char* type, const char* name, const char* group,
const char* doc, short attr, bool ro, bool hidden)
{
Base::BaseClass* base = static_cast<Base::BaseClass*>(Base::Type::createInstanceByName(type,true));
if (!base)
return 0;
if (!base->getTypeId().isDerivedFrom(Property::getClassTypeId())) {
delete base;
std::stringstream str;
str << "'" << type << "' is not a property type";
throw Base::Exception(str.str());
}
// get unique name
Property* pcProperty = static_cast<Property*>(base);
std::string ObjectName;
if (name && *name != '\0')
ObjectName = getUniquePropertyName(name);
else
ObjectName = getUniquePropertyName(type);
pcProperty->setContainer(this->pc);
PropData data;
data.property = pcProperty;
data.group = (group ? group : "");
data.doc = (doc ? doc : "");
data.attr = attr;
data.readonly = ro;
data.hidden = hidden;
props[ObjectName] = data;
GetApplication().signalAppendDynamicProperty(*pcProperty);
return pcProperty;
}
bool DynamicProperty::removeDynamicProperty(const char* name)
{
std::map<std::string,PropData>::iterator it = props.find(name);
if (it != props.end()) {
GetApplication().signalRemoveDynamicProperty(*it->second.property);
delete it->second.property;
props.erase(it);
return true;
}
return false;
}
std::string DynamicProperty::getUniquePropertyName(const char *Name) const
{
std::string CleanName = Base::Tools::getIdentifier(Name);
// name in use?
std::map<std::string,Property*> objectProps;
getPropertyMap(objectProps);
std::map<std::string,Property*>::const_iterator pos;
pos = objectProps.find(CleanName);
if (pos == objectProps.end()) {
// if not, name is OK
return CleanName;
}
else {
std::vector<std::string> names;
names.reserve(objectProps.size());
for (pos = objectProps.begin();pos != objectProps.end();++pos) {
names.push_back(pos->first);
}
return Base::Tools::getUniqueName(CleanName, names);
}
}
std::string DynamicProperty::encodeAttribute(const std::string& str) const
{
std::string tmp;
for (std::string::const_iterator it = str.begin(); it != str.end(); ++it) {
if (*it == '<')
tmp += "<";
else if (*it == '"')
tmp += """;
else if (*it == '\'')
tmp += "'";
else if (*it == '&')
tmp += "&";
else if (*it == '>')
tmp += ">";
else if (*it == '\r')
tmp += "
";
else if (*it == '\n')
tmp += "
";
else
tmp += *it;
}
return tmp;
}
void DynamicProperty::Save (Base::Writer &writer) const
{
std::map<std::string,Property*> Map;
getPropertyMap(Map);
writer.incInd(); // indentation for 'Properties Count'
writer.Stream() << writer.ind() << "<Properties Count=\"" << Map.size() << "\">" << std::endl;
std::map<std::string,Property*>::iterator it;
for (it = Map.begin(); it != Map.end(); ++it)
{
writer.incInd(); // indentation for 'Property name'
// check whether a static or dynamic property
std::map<std::string,PropData>::const_iterator pt = props.find(it->first);
if (pt == props.end()) {
writer.Stream() << writer.ind() << "<Property name=\"" << it->first << "\" type=\""
<< it->second->getTypeId().getName() << "\">" << std::endl;
}
else {
writer.Stream() << writer.ind() << "<Property name=\"" << it->first
<< "\" type=\"" << it->second->getTypeId().getName()
<< "\" group=\"" << encodeAttribute(pt->second.group)
<< "\" doc=\"" << encodeAttribute(pt->second.doc)
<< "\" attr=\"" << pt->second.attr << "\" ro=\"" << pt->second.readonly
<< "\" hide=\"" << pt->second.hidden << "\">" << std::endl;
}
writer.incInd(); // indentation for the actual property
try {
// We must make sure to handle all exceptions accordingly so that
// the project file doesn't get invalidated. In the error case this
// means to proceed instead of aborting the write operation.
// Don't write transient properties
if (!(getPropertyType(it->second) & Prop_Transient))
it->second->Save(writer);
}
catch (const Base::Exception &e) {
Base::Console().Error("%s\n", e.what());
}
catch (const std::exception &e) {
Base::Console().Error("%s\n", e.what());
}
catch (const char* e) {
Base::Console().Error("%s\n", e);
}
#ifndef FC_DEBUG
catch (...) {
Base::Console().Error("DynamicProperty::Save: Unknown C++ exception thrown. Try to continue...\n");
}
#endif
writer.decInd(); // indentation for the actual property
writer.Stream() << writer.ind() << "</Property>" << std::endl;
writer.decInd(); // indentation for 'Property name'
}
writer.Stream() << writer.ind() << "</Properties>" << std::endl;
writer.decInd(); // indentation for 'Properties Count'
}
void DynamicProperty::Restore(Base::XMLReader &reader)
{
reader.readElement("Properties");
int Cnt = reader.getAttributeAsInteger("Count");
for (int i=0 ;i<Cnt ;i++) {
reader.readElement("Property");
const char* PropName = reader.getAttribute("name");
const char* TypeName = reader.getAttribute("type");
Property* prop = getPropertyByName(PropName);
try {
if (!prop) {
short attribute = 0;
bool readonly = false, hidden = false;
const char *group=0, *doc=0, *attr=0, *ro=0, *hide=0;
if (reader.hasAttribute("group"))
group = reader.getAttribute("group");
if (reader.hasAttribute("doc"))
doc = reader.getAttribute("doc");
if (reader.hasAttribute("attr")) {
attr = reader.getAttribute("attr");
if (attr) attribute = attr[0]-48;
}
if (reader.hasAttribute("ro")) {
ro = reader.getAttribute("ro");
if (ro) readonly = (ro[0]-48) != 0;
}
if (reader.hasAttribute("hide")) {
hide = reader.getAttribute("hide");
if (hide) hidden = (hide[0]-48) != 0;
}
prop = addDynamicProperty(TypeName, PropName, group, doc, attribute, readonly, hidden);
}
}
catch(const Base::Exception& e) {
// only handle this exception type
Base::Console().Warning(e.what());
}
//NOTE: We must also check the type of the current property because a subclass of
//PropertyContainer might change the type of a property but not its name. In this
//case we would force to read-in a wrong property type and the behaviour would be
//undefined.
// Don't read transient properties
if (!(getPropertyType(prop) & Prop_Transient)) {
if (prop && strcmp(prop->getTypeId().getName(), TypeName) == 0)
prop->Restore(reader);
else if (prop)
Base::Console().Warning("%s: Overread data for property %s of type %s, expected type is %s\n",
pc->getTypeId().getName(), prop->getName(), prop->getTypeId().getName(), TypeName);
else
Base::Console().Warning("%s: No property found with name %s and type %s\n",
pc->getTypeId().getName(), PropName, TypeName);
}
reader.readEndElement("Property");
}
reader.readEndElement("Properties");
}