| 1 |
/*************************************************************************** |
| 2 |
* Copyright (c) 2009 Werner Mayer <wmayer[at]users.sourceforge.net> * |
| 3 |
* * |
| 4 |
* This file is part of the FreeCAD CAx development system. * |
| 5 |
* * |
| 6 |
* This library is free software; you can redistribute it and/or * |
| 7 |
* modify it under the terms of the GNU Library General Public * |
| 8 |
* License as published by the Free Software Foundation; either * |
| 9 |
* version 2 of the License, or (at your option) any later version. * |
| 10 |
* * |
| 11 |
* This library is distributed in the hope that it will be useful, * |
| 12 |
* but WITHOUT ANY WARRANTY; without even the implied warranty of * |
| 13 |
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * |
| 14 |
* GNU Library General Public License for more details. * |
| 15 |
* * |
| 16 |
* You should have received a copy of the GNU Library General Public * |
| 17 |
* License along with this library; see the file COPYING.LIB. If not, * |
| 18 |
* write to the Free Software Foundation, Inc., 59 Temple Place, * |
| 19 |
* Suite 330, Boston, MA 02111-1307, USA * |
| 20 |
* * |
| 21 |
***************************************************************************/ |
| 22 |
|
| 23 |
#include <map> |
| 24 |
#include <vector> |
| 25 |
#include <string> |
| 26 |
|
| 27 |
#include <Base/Reader.h> |
| 28 |
#include <Base/Tools.h> |
| 29 |
#include <Base/UniqueNameManager.h> |
| 30 |
#include <Base/Writer.h> |
| 31 |
|
| 32 |
#include "DynamicProperty.h" |
| 33 |
#include "Application.h" |
| 34 |
#include "Property.h" |
| 35 |
#include "PropertyContainer.h" |
| 36 |
|
| 37 |
|
| 38 |
FC_LOG_LEVEL_INIT("Property", true, true) |
| 39 |
|
| 40 |
|
| 41 |
using namespace App; |
| 42 |
|
| 43 |
|
| 44 |
DynamicProperty::DynamicProperty() = default; |
| 45 |
|
| 46 |
DynamicProperty::~DynamicProperty() |
| 47 |
{ |
| 48 |
clear(); |
| 49 |
} |
| 50 |
|
| 51 |
void DynamicProperty::clear() |
| 52 |
{ |
| 53 |
auto& index = props.get<0>(); |
| 54 |
for (auto& v : index) { |
| 55 |
delete v.property; |
| 56 |
} |
| 57 |
index.clear(); |
| 58 |
} |
| 59 |
|
| 60 |
void DynamicProperty::getPropertyList(std::vector<Property*>& List) const |
| 61 |
{ |
| 62 |
for (auto& v : props.get<0>()) { |
| 63 |
List.push_back(v.property); |
| 64 |
} |
| 65 |
} |
| 66 |
|
| 67 |
void DynamicProperty::getPropertyNamedList( |
| 68 |
std::vector<std::pair<const char*, Property*>>& List) const |
| 69 |
{ |
| 70 |
for (auto& v : props.get<0>()) { |
| 71 |
List.emplace_back(v.getName(), v.property); |
| 72 |
} |
| 73 |
} |
| 74 |
|
| 75 |
void DynamicProperty::visitProperties(const std::function<void(Property*)>& visitor) const { |
| 76 |
for (auto& v : props.get<0>()) { |
| 77 |
visitor(v.property); |
| 78 |
} |
| 79 |
} |
| 80 |
|
| 81 |
void DynamicProperty::getPropertyMap(std::map<std::string,Property*>& Map) const |
| 82 |
{ |
| 83 |
for (auto& v : props.get<0>()) { |
| 84 |
Map[v.name] = v.property; |
| 85 |
} |
| 86 |
} |
| 87 |
|
| 88 |
Property* DynamicProperty::getDynamicPropertyByName(const char* name) const |
| 89 |
{ |
| 90 |
auto& index = props.get<0>(); |
| 91 |
auto it = index.find(name); |
| 92 |
if (it != index.end()) { |
| 93 |
return it->property; |
| 94 |
} |
| 95 |
return nullptr; |
| 96 |
} |
| 97 |
|
| 98 |
std::vector<std::string> DynamicProperty::getDynamicPropertyNames() const |
| 99 |
{ |
| 100 |
std::vector<std::string> names; |
| 101 |
auto& index = props.get<0>(); |
| 102 |
names.reserve(index.size()); |
| 103 |
for (auto& v : index) { |
| 104 |
names.push_back(v.name); |
| 105 |
} |
| 106 |
return names; |
| 107 |
} |
| 108 |
|
| 109 |
short DynamicProperty::getPropertyType(const Property* prop) const |
| 110 |
{ |
| 111 |
return prop ? prop->getType() : 0; |
| 112 |
} |
| 113 |
|
| 114 |
short DynamicProperty::getPropertyType(const char* name) const |
| 115 |
{ |
| 116 |
auto& index = props.get<0>(); |
| 117 |
auto it = index.find(name); |
| 118 |
if (it != index.end()) { |
| 119 |
short attr = it->attr; |
| 120 |
if (it->hidden) { |
| 121 |
attr |= Prop_Hidden; |
| 122 |
} |
| 123 |
if (it->readonly) { |
| 124 |
attr |= Prop_ReadOnly; |
| 125 |
} |
| 126 |
return attr; |
| 127 |
} |
| 128 |
return 0; |
| 129 |
} |
| 130 |
|
| 131 |
const char* DynamicProperty::getPropertyGroup(const Property* prop) const |
| 132 |
{ |
| 133 |
auto& index = props.get<1>(); |
| 134 |
auto it = index.find(const_cast<Property*>(prop)); |
| 135 |
if (it != index.end()) { |
| 136 |
return it->group.c_str(); |
| 137 |
} |
| 138 |
return nullptr; |
| 139 |
} |
| 140 |
|
| 141 |
const char* DynamicProperty::getPropertyGroup(const char* name) const |
| 142 |
{ |
| 143 |
auto& index = props.get<0>(); |
| 144 |
auto it = index.find(name); |
| 145 |
if (it != index.end()) { |
| 146 |
return it->group.c_str(); |
| 147 |
} |
| 148 |
return nullptr; |
| 149 |
} |
| 150 |
|
| 151 |
const char* DynamicProperty::getPropertyDocumentation(const Property* prop) const |
| 152 |
{ |
| 153 |
auto& index = props.get<1>(); |
| 154 |
auto it = index.find(const_cast<Property*>(prop)); |
| 155 |
if (it != index.end()) { |
| 156 |
return it->doc.c_str(); |
| 157 |
} |
| 158 |
return nullptr; |
| 159 |
} |
| 160 |
|
| 161 |
const char* DynamicProperty::getPropertyDocumentation(const char* name) const |
| 162 |
{ |
| 163 |
auto& index = props.get<0>(); |
| 164 |
auto it = index.find(name); |
| 165 |
if (it != index.end()) { |
| 166 |
return it->doc.c_str(); |
| 167 |
} |
| 168 |
return nullptr; |
| 169 |
} |
| 170 |
|
| 171 |
Property* DynamicProperty::addDynamicProperty(PropertyContainer& pc, |
| 172 |
const char* type, |
| 173 |
const char* name, |
| 174 |
const char* group, |
| 175 |
const char* doc, |
| 176 |
short attr, |
| 177 |
bool ro, |
| 178 |
bool hidden) |
| 179 |
{ |
| 180 |
if (!type) { |
| 181 |
type = "<null>"; |
| 182 |
} |
| 183 |
|
| 184 |
std::string _name; |
| 185 |
|
| 186 |
static ParameterGrp::handle hGrp = |
| 187 |
GetApplication().GetParameterGroupByPath("User parameter:BaseApp/Preferences/Document"); |
| 188 |
if (hGrp->GetBool("AutoNameDynamicProperty", false)) { |
| 189 |
if (!name || !name[0]) { |
| 190 |
name = type; |
| 191 |
} |
| 192 |
_name = getUniquePropertyName(pc, name); |
| 193 |
if (_name != name) { |
| 194 |
FC_WARN(pc.getFullName() |
| 195 |
<< " rename dynamic property from '" << name << "' to '" << _name << "'"); |
| 196 |
} |
| 197 |
name = _name.c_str(); |
| 198 |
} |
| 199 |
else if (!name) { |
| 200 |
name = "<null>"; // setting a bad name to trigger exception |
| 201 |
} |
| 202 |
|
| 203 |
auto prop = pc.getPropertyByName(name); |
| 204 |
if (prop && prop->getContainer() == &pc) { |
| 205 |
FC_THROWM(Base::NameError, |
| 206 |
"Property " << pc.getFullName() << '.' << name << " already exists"); |
| 207 |
} |
| 208 |
|
| 209 |
if (Base::Tools::getIdentifier(name) != name) { |
| 210 |
FC_THROWM(Base::NameError, "Invalid property name '" << name << "'"); |
| 211 |
} |
| 212 |
|
| 213 |
Base::Type propType = |
| 214 |
Base::Type::getTypeIfDerivedFrom(type, App::Property::getClassTypeId(), true); |
| 215 |
if (propType.isBad()) { |
| 216 |
FC_THROWM(Base::TypeError, |
| 217 |
"Invalid type " << type << " for property " << pc.getFullName() << '.' << name); |
| 218 |
} |
| 219 |
|
| 220 |
void* propInstance = propType.createInstance(); |
| 221 |
if (!propInstance) { |
| 222 |
FC_THROWM(Base::RuntimeError, |
| 223 |
"Failed to create property " << pc.getFullName() << '.' << name << " of type " |
| 224 |
<< type); |
| 225 |
} |
| 226 |
|
| 227 |
Property* pcProperty = static_cast<Property*>(propInstance); |
| 228 |
|
| 229 |
auto res = props.get<0>().emplace(pcProperty, name, nullptr, group, doc, attr, ro, hidden); |
| 230 |
|
| 231 |
pcProperty->setContainer(&pc); |
| 232 |
pcProperty->myName = res.first->name.c_str(); |
| 233 |
|
| 234 |
if (ro) { |
| 235 |
attr |= Prop_ReadOnly; |
| 236 |
} |
| 237 |
if (hidden) { |
| 238 |
attr |= Prop_Hidden; |
| 239 |
} |
| 240 |
|
| 241 |
pcProperty->syncType(attr); |
| 242 |
pcProperty->StatusBits.set((size_t)Property::PropDynamic); |
| 243 |
|
| 244 |
GetApplication().signalAppendDynamicProperty(*pcProperty); |
| 245 |
|
| 246 |
return pcProperty; |
| 247 |
} |
| 248 |
|
| 249 |
bool DynamicProperty::addProperty(Property* prop) |
| 250 |
{ |
| 251 |
if (!prop || !prop->hasName()) { |
| 252 |
return false; |
| 253 |
} |
| 254 |
auto& index = props.get<0>(); |
| 255 |
#if BOOST_VERSION < 107500 |
| 256 |
if (index.count(prop->getName())) { |
| 257 |
return false; |
| 258 |
} |
| 259 |
#else |
| 260 |
if (index.contains(prop->getName())) { |
| 261 |
return false; |
| 262 |
} |
| 263 |
#endif |
| 264 |
index.emplace(prop, |
| 265 |
std::string(), |
| 266 |
prop->getName(), |
| 267 |
prop->getGroup(), |
| 268 |
prop->getDocumentation(), |
| 269 |
prop->getType(), |
| 270 |
false, |
| 271 |
false); |
| 272 |
return true; |
| 273 |
} |
| 274 |
|
| 275 |
bool DynamicProperty::removeProperty(const Property* prop) |
| 276 |
{ |
| 277 |
auto& index = props.get<1>(); |
| 278 |
auto it = index.find(const_cast<Property*>(prop)); |
| 279 |
if (it != index.end()) { |
| 280 |
index.erase(it); |
| 281 |
return true; |
| 282 |
} |
| 283 |
return false; |
| 284 |
} |
| 285 |
|
| 286 |
bool DynamicProperty::removeDynamicProperty(const char* name) |
| 287 |
{ |
| 288 |
auto& index = props.get<0>(); |
| 289 |
auto it = index.find(name); |
| 290 |
if (it != index.end()) { |
| 291 |
if (it->property->testStatus(Property::LockDynamic)) { |
| 292 |
throw Base::RuntimeError("property is locked"); |
| 293 |
} |
| 294 |
else if (!it->property->testStatus(Property::PropDynamic)) { |
| 295 |
throw Base::RuntimeError("property is not dynamic"); |
| 296 |
} |
| 297 |
Property* prop = it->property; |
| 298 |
GetApplication().signalRemoveDynamicProperty(*prop); |
| 299 |
|
| 300 |
// Handle possible recursive calls of removeDynamicProperty |
| 301 |
if (prop->myName) { |
| 302 |
Property::destroy(prop); |
| 303 |
index.erase(it); |
| 304 |
// memory of myName has been freed |
| 305 |
prop->myName = nullptr; |
| 306 |
} |
| 307 |
return true; |
| 308 |
} |
| 309 |
|
| 310 |
return false; |
| 311 |
} |
| 312 |
|
| 313 |
std::string DynamicProperty::getUniquePropertyName(const PropertyContainer& pc, const char* Name) const |
| 314 |
{ |
| 315 |
std::string cleanName = Base::Tools::getIdentifier(Name); |
| 316 |
|
| 317 |
// We test if the property already exists by finding it, which is not much more expensive than |
| 318 |
// having a separate propertyExists(name) method. This avoids building the UniqueNameManager |
| 319 |
// (which could also tell if the name exists) except in the relatively rare condition of |
| 320 |
// the name already existing. |
| 321 |
if (pc.getPropertyByName(cleanName.c_str()) == nullptr) { |
| 322 |
return cleanName; |
| 323 |
} |
| 324 |
Base::UniqueNameManager names; |
| 325 |
// Build the index of existing names. |
| 326 |
pc.visitProperties([&](Property* prop) { |
| 327 |
names.addExactName(prop->getName()); |
| 328 |
}); |
| 329 |
return names.makeUniqueName(cleanName); |
| 330 |
} |
| 331 |
|
| 332 |
void DynamicProperty::save(const Property* prop, Base::Writer& writer) const |
| 333 |
{ |
| 334 |
auto& index = props.get<1>(); |
| 335 |
auto it = index.find(const_cast<Property*>(prop)); |
| 336 |
if (it != index.end()) { |
| 337 |
auto& data = *it; |
| 338 |
writer.Stream() << "\" group=\"" << Base::Persistence::encodeAttribute(data.group) |
| 339 |
<< "\" doc=\"" << Base::Persistence::encodeAttribute(data.doc) |
| 340 |
<< "\" attr=\"" << data.attr << "\" ro=\"" << data.readonly << "\" hide=\"" |
| 341 |
<< data.hidden; |
| 342 |
} |
| 343 |
} |
| 344 |
|
| 345 |
Property* DynamicProperty::restore(PropertyContainer& pc, |
| 346 |
const char* PropName, |
| 347 |
const char* TypeName, |
| 348 |
const Base::XMLReader& reader) |
| 349 |
{ |
| 350 |
if (!reader.hasAttribute("group")) { |
| 351 |
return nullptr; |
| 352 |
} |
| 353 |
|
| 354 |
short attribute = 0; |
| 355 |
bool readonly = false, hidden = false; |
| 356 |
const char *group = nullptr, *doc = nullptr, *attr = nullptr, *ro = nullptr, *hide = nullptr; |
| 357 |
group = reader.getAttribute<const char*>("group"); |
| 358 |
if (reader.hasAttribute("doc")) { |
| 359 |
doc = reader.getAttribute<const char*>("doc"); |
| 360 |
} |
| 361 |
if (reader.hasAttribute("attr")) { |
| 362 |
attr = reader.getAttribute<const char*>("attr"); |
| 363 |
if (attr) { |
| 364 |
std::istringstream str(attr); |
| 365 |
str >> attribute; |
| 366 |
} |
| 367 |
} |
| 368 |
if (reader.hasAttribute("ro")) { |
| 369 |
ro = reader.getAttribute<const char*>("ro"); |
| 370 |
if (ro) { |
| 371 |
readonly = (ro[0] - 48) != 0; |
| 372 |
} |
| 373 |
} |
| 374 |
if (reader.hasAttribute("hide")) { |
| 375 |
hide = reader.getAttribute<const char*>("hide"); |
| 376 |
if (hide) { |
| 377 |
hidden = (hide[0] - 48) != 0; |
| 378 |
} |
| 379 |
} |
| 380 |
|
| 381 |
return addDynamicProperty(pc, TypeName, PropName, group, doc, attribute, readonly, hidden); |
| 382 |
} |
| 383 |
|
| 384 |
DynamicProperty::PropData DynamicProperty::getDynamicPropertyData(const Property* prop) const |
| 385 |
{ |
| 386 |
auto& index = props.get<1>(); |
| 387 |
auto it = index.find(const_cast<Property*>(prop)); |
| 388 |
if (it != index.end()) { |
| 389 |
return *it; |
| 390 |
} |
| 391 |
return {}; |
| 392 |
} |
| 393 |
|
| 394 |
bool DynamicProperty::changeDynamicProperty(const Property* prop, |
| 395 |
const char* group, |
| 396 |
const char* doc) |
| 397 |
{ |
| 398 |
auto& index = props.get<1>(); |
| 399 |
auto it = index.find(const_cast<Property*>(prop)); |
| 400 |
if (it == index.end()) { |
| 401 |
return false; |
| 402 |
} |
| 403 |
if (group) { |
| 404 |
it->group = group; |
| 405 |
} |
| 406 |
if (doc) { |
| 407 |
it->doc = doc; |
| 408 |
} |
| 409 |
return true; |
| 410 |
} |
| 411 |
|
| 412 |
bool DynamicProperty::renameDynamicProperty(Property* prop, |
| 413 |
const char* newName) |
| 414 |
{ |
| 415 |
auto& propIndex = props.get<1>(); |
| 416 |
auto propIt = propIndex.find(prop); |
| 417 |
if (propIt == propIndex.end()) { |
| 418 |
return false; |
| 419 |
} |
| 420 |
const PropData& data = *propIt; |
| 421 |
|
| 422 |
if (propIt->property->testStatus(Property::LockDynamic)) { |
| 423 |
FC_THROWM(Base::RuntimeError, "Property " << prop->getName() << " is locked"); |
| 424 |
} |
| 425 |
|
| 426 |
PropertyContainer* container = prop->getContainer(); |
| 427 |
if (container->getPropertyByName(newName) != nullptr) { |
| 428 |
FC_THROWM(Base::NameError, |
| 429 |
"Property " << container->getFullName() << '.' << newName << " already exists"); |
| 430 |
} |
| 431 |
|
| 432 |
if (Base::Tools::getIdentifier(newName) != newName) { |
| 433 |
FC_THROWM(Base::NameError, "Invalid property name '" << newName << "'"); |
| 434 |
} |
| 435 |
|
| 436 |
std::string oldName{data.getName()}; |
| 437 |
auto& nameIndex = props.get<0>(); |
| 438 |
auto nameIt = nameIndex.find(data.getName()); |
| 439 |
if (nameIt == nameIndex.end()) { |
| 440 |
// This should never happen |
| 441 |
FC_THROWM(Base::RuntimeError, |
| 442 |
"Property " << data.getName() << " not found in index"); |
| 443 |
} |
| 444 |
nameIndex.modify(nameIt, [&](PropData& d) { |
| 445 |
d.name = newName; |
| 446 |
d.pName = nullptr; |
| 447 |
// make sure that the property's name points to PropData.name that |
| 448 |
// manages the memory. |
| 449 |
d.property->myName = d.name.c_str(); |
| 450 |
}); |
| 451 |
|
| 452 |
GetApplication().signalRenameDynamicProperty(*prop, oldName.c_str()); |
| 453 |
|
| 454 |
return true; |
| 455 |
} |
| 456 |
|
| 457 |
const char* DynamicProperty::getPropertyName(const Property* prop) const |
| 458 |
{ |
| 459 |
auto& index = props.get<1>(); |
| 460 |
auto it = index.find(const_cast<Property*>(prop)); |
| 461 |
if (it != index.end()) { |
| 462 |
return it->getName(); |
| 463 |
} |
| 464 |
return nullptr; |
| 465 |
} |