| 1 |
/*************************************************************************** |
| 2 |
* Copyright (c) 2006 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 APP_GROUPEXTENSION_H |
| 25 |
#define APP_GROUPEXTENSION_H |
| 26 |
|
| 27 |
#include <App/DocumentObject.h> |
| 28 |
#include <App/DocumentObjectExtension.h> |
| 29 |
#include <App/ExtensionPython.h> |
| 30 |
#include <vector> |
| 31 |
|
| 32 |
|
| 33 |
namespace App |
| 34 |
{ |
| 35 |
class DocumentObjectGroup; |
| 36 |
class GroupExtensionPy; |
| 37 |
|
| 38 |
class AppExport GroupExtension: public DocumentObjectExtension |
| 39 |
{ |
| 40 |
EXTENSION_PROPERTY_HEADER_WITH_OVERRIDE(App::GroupExtension); |
| 41 |
using inherited = DocumentObjectExtension; |
| 42 |
|
| 43 |
public: |
| 44 |
/// Constructor |
| 45 |
GroupExtension(); |
| 46 |
~GroupExtension() override; |
| 47 |
|
| 48 |
/** @name Object handling */ |
| 49 |
//@{ |
| 50 |
/** Adds an object of \a sType with \a pObjectName to the document this group belongs to and |
| 51 |
* append it to this group as well. |
| 52 |
*/ |
| 53 |
virtual DocumentObject* addObject(const char* sType, const char* pObjectName); |
| 54 |
/** Adds an object of \a T with \a pObjectName to the document this group belongs to and |
| 55 |
* append it to this group as well. |
| 56 |
*/ |
| 57 |
template<typename T> |
| 58 |
T* addObject(const char* pObjectName); |
| 59 |
/* Adds the object \a obj to this group. Returns all objects that have been added. |
| 60 |
*/ |
| 61 |
virtual std::vector<DocumentObject*> addObject(DocumentObject* obj); |
| 62 |
/* Adds the objects \a objs to this group. Returns all objects that have been added. |
| 63 |
*/ |
| 64 |
virtual std::vector<DocumentObject*> addObjects(std::vector<DocumentObject*> obj); |
| 65 |
|
| 66 |
/* Sets the objects in this group. Everything contained already will be removed first |
| 67 |
*/ |
| 68 |
virtual std::vector<DocumentObject*> setObjects(std::vector<DocumentObject*> obj); |
| 69 |
|
| 70 |
/*override this function if you want only special objects |
| 71 |
*/ |
| 72 |
virtual bool allowObject(DocumentObject*) |
| 73 |
{ |
| 74 |
return true; |
| 75 |
} |
| 76 |
|
| 77 |
/** Removes an object from this group. Returns all objects that have been removed. |
| 78 |
*/ |
| 79 |
virtual std::vector<DocumentObject*> removeObject(DocumentObject* obj); |
| 80 |
/** Removes objects from this group. Returns all objects that have been removed. |
| 81 |
*/ |
| 82 |
virtual std::vector<DocumentObject*> removeObjects(std::vector<DocumentObject*> obj); |
| 83 |
/** Removes all children objects from this group and the document. |
| 84 |
*/ |
| 85 |
virtual void removeObjectsFromDocument(); |
| 86 |
/** Returns the object of this group with \a Name. If the group doesn't have such an object 0 is |
| 87 |
* returned. |
| 88 |
* @note This method might return 0 even if the document this group belongs to contains an |
| 89 |
* object with this name. |
| 90 |
*/ |
| 91 |
DocumentObject* getObject(const char* Name) const; |
| 92 |
/** |
| 93 |
* Checks whether the object \a obj is part of this group. |
| 94 |
* @param obj the object to check for. |
| 95 |
* @param recursive if true check also if the obj is child of some sub group (default is |
| 96 |
* false). |
| 97 |
*/ |
| 98 |
virtual bool hasObject(const DocumentObject* obj, bool recursive = false) const; |
| 99 |
/** |
| 100 |
* Checks whether this group object is a child (or sub-child if enabled) |
| 101 |
* of the given group object. |
| 102 |
*/ |
| 103 |
bool isChildOf(const GroupExtension* group, bool recursive = true) const; |
| 104 |
/** Returns a list of all objects this group does have. |
| 105 |
*/ |
| 106 |
const std::vector<DocumentObject*>& getObjects() const; |
| 107 |
/** Returns a list of all objects of \a typeId this group does have. |
| 108 |
*/ |
| 109 |
std::vector<DocumentObject*> getObjectsOfType(const Base::Type& typeId) const; |
| 110 |
/** Returns the number of objects of \a typeId this group does have. |
| 111 |
*/ |
| 112 |
int countObjectsOfType(const Base::Type& typeId) const; |
| 113 |
/** Returns the object group of the document which the given object \a obj is part of. |
| 114 |
* In case this object is not part of a group 0 is returned. |
| 115 |
* @note This only returns objects that are normal groups, not any special derived type |
| 116 |
* like GeoFeatureGroups or OriginGroups. To retrieve those please use their appropriate |
| 117 |
* functions |
| 118 |
*/ |
| 119 |
static DocumentObject* getGroupOfObject(const DocumentObject* obj); |
| 120 |
//@} |
| 121 |
|
| 122 |
PyObject* getExtensionPyObject() override; |
| 123 |
|
| 124 |
void extensionOnChanged(const Property* p) override; |
| 125 |
|
| 126 |
bool extensionGetSubObject(DocumentObject*& ret, |
| 127 |
const char* subname, |
| 128 |
PyObject** pyObj, |
| 129 |
Base::Matrix4D* mat, |
| 130 |
bool transform, |
| 131 |
int depth) const override; |
| 132 |
|
| 133 |
bool extensionGetSubObjects(std::vector<std::string>& ret, int reason) const override; |
| 134 |
|
| 135 |
App::DocumentObjectExecReturn* extensionExecute() override; |
| 136 |
|
| 137 |
std::vector<DocumentObject*> getAllChildren() const; |
| 138 |
void getAllChildren(std::vector<DocumentObject*>&, std::set<DocumentObject*>&) const; |
| 139 |
|
| 140 |
/// Properties |
| 141 |
PropertyLinkList Group; |
| 142 |
PropertyBool _GroupTouched; |
| 143 |
|
| 144 |
private: |
| 145 |
void removeObjectFromDocument(DocumentObject*); |
| 146 |
// This function stores the already searched objects to prevent infinite recursion in case of a |
| 147 |
// cyclic group graph It throws an exception of type Base::RuntimeError if a cyclic dependency |
| 148 |
// is detected. |
| 149 |
bool recursiveHasObject(const DocumentObject* obj, |
| 150 |
const GroupExtension* group, |
| 151 |
std::vector<const GroupExtension*> history) const; |
| 152 |
|
| 153 |
// for tracking children visibility |
| 154 |
void slotChildChanged(const App::DocumentObject&, const App::Property&); |
| 155 |
std::unordered_map<const App::DocumentObject*, boost::signals2::scoped_connection> _Conns; |
| 156 |
}; |
| 157 |
|
| 158 |
|
| 159 |
template<typename T> |
| 160 |
T* GroupExtension::addObject(const char* pObjectName) |
| 161 |
{ |
| 162 |
static_assert(std::is_base_of<DocumentObject, T>::value, "T must be derived from DocumentObject"); |
| 163 |
return static_cast<T*>(addObject(T::getClassName(), pObjectName)); |
| 164 |
} |
| 165 |
|
| 166 |
template<typename ExtensionT> |
| 167 |
class GroupExtensionPythonT: public ExtensionT |
| 168 |
{ |
| 169 |
|
| 170 |
public: |
| 171 |
GroupExtensionPythonT() = default; |
| 172 |
~GroupExtensionPythonT() override = default; |
| 173 |
|
| 174 |
// override the documentobjectextension functions to make them available in python |
| 175 |
bool allowObject(DocumentObject* obj) override |
| 176 |
{ |
| 177 |
Base::PyGILStateLocker locker; |
| 178 |
Py::Object pyobj = Py::asObject(obj->getPyObject()); |
| 179 |
EXTENSION_PROXY_ONEARG(allowObject, pyobj); |
| 180 |
|
| 181 |
if (result.isNone()) { |
| 182 |
return ExtensionT::allowObject(obj); |
| 183 |
} |
| 184 |
|
| 185 |
if (result.isBoolean()) { |
| 186 |
return result.isTrue(); |
| 187 |
} |
| 188 |
|
| 189 |
return false; |
| 190 |
}; |
| 191 |
}; |
| 192 |
|
| 193 |
using GroupExtensionPython = ExtensionPythonT<GroupExtensionPythonT<GroupExtension>>; |
| 194 |
|
| 195 |
} // namespace App |
| 196 |
|
| 197 |
|
| 198 |
#endif // APP_GROUPEXTENSION_H |