| 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 |
|
| 24 |
#ifndef SRC_APP_DYNAMICPROPERTY_H_ |
| 25 |
#define SRC_APP_DYNAMICPROPERTY_H_ |
| 26 |
|
| 27 |
#include <map> |
| 28 |
#include <string> |
| 29 |
#include <vector> |
| 30 |
#include <utility> |
| 31 |
|
| 32 |
#include <boost/multi_index_container.hpp> |
| 33 |
#include <boost/multi_index/hashed_index.hpp> |
| 34 |
#include <boost/multi_index/sequenced_index.hpp> |
| 35 |
#include <boost/multi_index/member.hpp> |
| 36 |
#include <boost/multi_index/mem_fun.hpp> |
| 37 |
#include <FCGlobal.h> |
| 38 |
|
| 39 |
|
| 40 |
namespace Base |
| 41 |
{ |
| 42 |
class Writer; |
| 43 |
class XMLReader; |
| 44 |
class XMLWriter; |
| 45 |
} // namespace Base |
| 46 |
|
| 47 |
namespace App |
| 48 |
{ |
| 49 |
class Property; |
| 50 |
class PropertyContainer; |
| 51 |
|
| 52 |
namespace bmi = boost::multi_index; |
| 53 |
|
| 54 |
struct CStringHasher |
| 55 |
{ |
| 56 |
inline std::size_t operator()(const char* s) const |
| 57 |
{ |
| 58 |
if (!s) { |
| 59 |
return 0; |
| 60 |
} |
| 61 |
return boost::hash_range(s, s + std::strlen(s)); |
| 62 |
} |
| 63 |
inline bool operator()(const char* a, const char* b) const |
| 64 |
{ |
| 65 |
if (!a) { |
| 66 |
return !b; |
| 67 |
} |
| 68 |
if (!b) { |
| 69 |
return false; |
| 70 |
} |
| 71 |
return std::strcmp(a, b) == 0; |
| 72 |
} |
| 73 |
}; |
| 74 |
|
| 75 |
/** This class implements an interface to add properties at run-time to an object |
| 76 |
* derived from PropertyContainer. The additional properties are made persistent. |
| 77 |
* @author Werner Mayer |
| 78 |
*/ |
| 79 |
class AppExport DynamicProperty |
| 80 |
{ |
| 81 |
public: |
| 82 |
DynamicProperty(); |
| 83 |
virtual ~DynamicProperty(); |
| 84 |
|
| 85 |
/** @name Access properties */ |
| 86 |
//@{ |
| 87 |
/// Get all properties of the class (including parent) |
| 88 |
void getPropertyList(std::vector<Property*>& List) const; |
| 89 |
/// get all properties with their names |
| 90 |
void getPropertyNamedList(std::vector<std::pair<const char*, Property*>>& List) const; |
| 91 |
/// See PropertyContainer::visitProperties for semantics |
| 92 |
void visitProperties(const std::function<void(Property*)>& visitor) const; |
| 93 |
/// Get all properties of the class (including parent) |
| 94 |
void getPropertyMap(std::map<std::string, Property*>& Map) const; |
| 95 |
/// Find a dynamic property by its name |
| 96 |
Property* getDynamicPropertyByName(const char* name) const; |
| 97 |
/*! |
| 98 |
Add a dynamic property of the type @a type and with the name @a name. |
| 99 |
@a Group gives the grouping name which appears in the property editor and |
| 100 |
@a doc shows the tooltip there. |
| 101 |
With @a attr, @a ro and @a hidden the behaviour of the property can be controlled. |
| 102 |
@a attr is an OR'ed value of the PropertyType enumeration. |
| 103 |
If no special attribute should be set Prop_None can be set (or leave the default of 0). |
| 104 |
For convenience the attributes for 'Read-Only' and 'Hidden' can also be controlled with |
| 105 |
the values @a ro or @a hidden. This means, |
| 106 |
@code |
| 107 |
addDynamicProperty(..., ..., "Base","blah", Prop_ReadOnly | Prop_Hidden); |
| 108 |
@endcode |
| 109 |
is equivalent to |
| 110 |
@code |
| 111 |
addDynamicProperty(..., ..., "Base","blah", Prop_None, true, true); |
| 112 |
@endcode |
| 113 |
*/ |
| 114 |
Property* addDynamicProperty(PropertyContainer& pc, |
| 115 |
const char* type, |
| 116 |
const char* name = nullptr, |
| 117 |
const char* group = nullptr, |
| 118 |
const char* doc = nullptr, |
| 119 |
short attr = 0, |
| 120 |
bool ro = false, |
| 121 |
bool hidden = false); |
| 122 |
/** Add a pre-existing property |
| 123 |
* |
| 124 |
* The property is not treated as dynamic, and will not trigger signal. |
| 125 |
* |
| 126 |
* @return Return false if there is a property exist with the same name. |
| 127 |
*/ |
| 128 |
bool addProperty(Property* prop); |
| 129 |
/*! |
| 130 |
Removes a dynamic property by name. Returns true if the property is part of the container, |
| 131 |
otherwise false is returned. |
| 132 |
*/ |
| 133 |
bool removeDynamicProperty(const char* name); |
| 134 |
/// Remove pre-existing property, which will not be deleted. |
| 135 |
bool removeProperty(const Property* prop); |
| 136 |
/// Get a list of all dynamic properties. |
| 137 |
std::vector<std::string> getDynamicPropertyNames() const; |
| 138 |
/// Get the name of a property |
| 139 |
const char* getPropertyName(const Property* prop) const; |
| 140 |
//@} |
| 141 |
|
| 142 |
/** @name Property attributes */ |
| 143 |
//@{ |
| 144 |
/// Get the attributes of a property |
| 145 |
short getPropertyType(const Property* prop) const; |
| 146 |
/// Get the attributes of a named property |
| 147 |
short getPropertyType(const char* name) const; |
| 148 |
/// Get the group name of a property |
| 149 |
const char* getPropertyGroup(const Property* prop) const; |
| 150 |
/// Get the group name of a named property |
| 151 |
const char* getPropertyGroup(const char* name) const; |
| 152 |
/// Get the documentation of a property |
| 153 |
const char* getPropertyDocumentation(const Property* prop) const; |
| 154 |
/// Get the documentation of a named property |
| 155 |
const char* getPropertyDocumentation(const char* name) const; |
| 156 |
//@} |
| 157 |
|
| 158 |
/// Remove all properties |
| 159 |
void clear(); |
| 160 |
|
| 161 |
/// Get property count |
| 162 |
size_t size() const |
| 163 |
{ |
| 164 |
return props.size(); |
| 165 |
} |
| 166 |
|
| 167 |
void save(const Property* prop, Base::Writer& writer) const; |
| 168 |
|
| 169 |
Property* restore(PropertyContainer& pc, |
| 170 |
const char* PropName, |
| 171 |
const char* TypeName, |
| 172 |
const Base::XMLReader& reader); |
| 173 |
|
| 174 |
struct PropData |
| 175 |
{ |
| 176 |
Property* property; |
| 177 |
std::string name; |
| 178 |
const char* pName; |
| 179 |
mutable std::string group; |
| 180 |
mutable std::string doc; |
| 181 |
short attr; |
| 182 |
bool readonly; |
| 183 |
bool hidden; |
| 184 |
|
| 185 |
PropData(Property* prop = nullptr, |
| 186 |
std::string&& n = std::string(), |
| 187 |
const char* pn = nullptr, |
| 188 |
const char* g = nullptr, |
| 189 |
const char* d = nullptr, |
| 190 |
short a = 0, |
| 191 |
bool ro = false, |
| 192 |
bool h = false) |
| 193 |
: property(prop) |
| 194 |
, name(std::move(n)) |
| 195 |
, pName(pn) |
| 196 |
, group(g ? g : "") |
| 197 |
, doc(d ? d : "") |
| 198 |
, attr(a) |
| 199 |
, readonly(ro) |
| 200 |
, hidden(h) |
| 201 |
{} |
| 202 |
|
| 203 |
const char* getName() const |
| 204 |
{ |
| 205 |
return pName ? pName : name.c_str(); |
| 206 |
} |
| 207 |
}; |
| 208 |
|
| 209 |
PropData getDynamicPropertyData(const Property* prop) const; |
| 210 |
|
| 211 |
bool changeDynamicProperty(const Property* prop, const char* group, const char* doc); |
| 212 |
|
| 213 |
bool renameDynamicProperty(Property* prop, const char* newName); |
| 214 |
|
| 215 |
private: |
| 216 |
std::string getUniquePropertyName(const PropertyContainer& pc, const char* Name) const; |
| 217 |
|
| 218 |
private: |
| 219 |
bmi::multi_index_container< |
| 220 |
PropData, |
| 221 |
bmi::indexed_by< |
| 222 |
bmi::hashed_unique<bmi::const_mem_fun<PropData, const char*, &PropData::getName>, |
| 223 |
CStringHasher, |
| 224 |
CStringHasher>, |
| 225 |
bmi::hashed_unique<bmi::member<PropData, Property*, &PropData::property>>>> |
| 226 |
props; |
| 227 |
}; |
| 228 |
|
| 229 |
} // namespace App |
| 230 |
|
| 231 |
#endif // SRC_APP_DYNAMICPROPERTY_H_ |