Debian Codegraph @

src/App/GroupExtension.cpp

raw
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
25 #include <Base/Tools.h>
26
27 #include "Document.h"
28 #include "GeoFeatureGroupExtension.h"
29 #include "GroupExtensionPy.h"
30
31
32 using namespace App;
33 namespace sp = std::placeholders;
34
35 EXTENSION_PROPERTY_SOURCE(App::GroupExtension, App::DocumentObjectExtension)
36
37 namespace App
38 {
39 EXTENSION_PROPERTY_SOURCE_TEMPLATE(App::GroupExtensionPython, App::GroupExtension)
40
41 // explicit template instantiation
42 template class AppExport ExtensionPythonT<GroupExtensionPythonT<GroupExtension>>;
43 } // namespace App
44
45 GroupExtension::GroupExtension()
46 {
47 initExtensionType(GroupExtension::getExtensionClassTypeId());
48
49 EXTENSION_ADD_PROPERTY_TYPE(Group,
50 (nullptr),
51 "Base",
52 (App::PropertyType)(Prop_None),
53 "List of referenced objects");
54
55 EXTENSION_ADD_PROPERTY_TYPE(_GroupTouched,
56 (false),
57 "Base",
58 PropertyType(Prop_Hidden | Prop_Transient),
59 0);
60 }
61
62 GroupExtension::~GroupExtension() = default;
63
64 DocumentObject* GroupExtension::addObject(const char* sType, const char* pObjectName)
65 {
66 DocumentObject* obj = getExtendedObject()->getDocument()->addObject(sType, pObjectName);
67 if (!allowObject(obj)) {
68 getExtendedObject()->getDocument()->removeObject(obj->getNameInDocument());
69 return nullptr;
70 }
71 addObject(obj);
72 return obj;
73 }
74
75 std::vector<DocumentObject*> GroupExtension::addObject(DocumentObject* obj)
76 {
77 std::vector<DocumentObject*> vec = {obj};
78 return addObjects(vec);
79 }
80
81 std::vector<DocumentObject*> GroupExtension::addObjects(std::vector<DocumentObject*> objs)
82 {
83
84 std::vector<DocumentObject*> added;
85 std::vector<DocumentObject*> grp = Group.getValues();
86 for (auto obj : objs) {
87
88 if (!allowObject(obj)) {
89 continue;
90 }
91
92 if (hasObject(obj)) {
93 continue;
94 }
95
96 // only one group per object. Note that it is allowed to be in a group and geofeaturegroup.
97 // However, getGroupOfObject() returns only normal groups, no GeoFeatureGroups. Hence this
98 // works.
99 auto* group = App::GroupExtension::getGroupOfObject(obj);
100 if (group && group != getExtendedObject()) {
101 group->getExtensionByType<App::GroupExtension>()->removeObject(obj);
102 }
103
104 // if we are in a geofeaturegroup we need to ensure the object is too
105 auto geogrp = GeoFeatureGroupExtension::getGroupOfObject(getExtendedObject());
106 auto objgrp = GeoFeatureGroupExtension::getGroupOfObject(obj);
107 if (geogrp != objgrp) {
108 // what to do depends on if we are in geofeature group or not
109 if (geogrp) {
110 geogrp->getExtensionByType<GeoFeatureGroupExtension>()->addObject(obj);
111 }
112 else {
113 objgrp->getExtensionByType<GeoFeatureGroupExtension>()->removeObject(obj);
114 }
115 }
116
117 grp.push_back(obj);
118 added.push_back(obj);
119 }
120
121 Group.setValues(grp);
122
123 return added;
124 }
125
126 std::vector<DocumentObject*> GroupExtension::setObjects(std::vector<DocumentObject*> obj)
127 {
128
129 Group.setValues(std::vector<DocumentObject*>());
130 return addObjects(obj);
131 }
132
133 std::vector<DocumentObject*> GroupExtension::removeObject(DocumentObject* obj)
134 {
135 std::vector<DocumentObject*> vec = {obj};
136 return removeObjects(vec);
137 }
138
139 std::vector<DocumentObject*> GroupExtension::removeObjects(std::vector<DocumentObject*> objs)
140 {
141
142 const std::vector<DocumentObject*>& grp = Group.getValues();
143 std::vector<DocumentObject*> newGrp = grp;
144 std::vector<DocumentObject*> removed;
145
146 std::vector<DocumentObject*>::iterator end = newGrp.end();
147 for (auto obj : objs) {
148 auto res = std::remove(newGrp.begin(), end, obj);
149 if (res != end) {
150 end = res;
151 removed.push_back(obj);
152 }
153 }
154
155 newGrp.erase(end, newGrp.end());
156 if (grp.size() != newGrp.size()) {
157 Group.setValues(newGrp);
158 }
159
160 return removed;
161 }
162
163 void GroupExtension::removeObjectsFromDocument()
164 {
165 while (Group.getSize() > 0) {
166 // Remove the objects step by step because it can happen
167 // that an object is part of several groups and thus a
168 // double destruction could be possible
169 const std::vector<DocumentObject*>& grp = Group.getValues();
170 removeObjectFromDocument(grp.front());
171 }
172 }
173
174 void GroupExtension::removeObjectFromDocument(DocumentObject* obj)
175 {
176 // check that object is not invalid
177 if (!obj || !obj->isAttachedToDocument()) {
178 return;
179 }
180
181 // remove all children
182 if (obj->hasExtension(GroupExtension::getExtensionClassTypeId())) {
183 GroupExtension* grp = static_cast<GroupExtension*>(
184 obj->getExtension(GroupExtension::getExtensionClassTypeId()));
185
186 // recursive call to remove all subgroups
187 grp->removeObjectsFromDocument();
188 }
189
190 getExtendedObject()->getDocument()->removeObject(obj->getNameInDocument());
191 }
192
193 DocumentObject* GroupExtension::getObject(const char* Name) const
194 {
195 DocumentObject* obj = getExtendedObject()->getDocument()->getObject(Name);
196 if (obj && hasObject(obj)) {
197 return obj;
198 }
199 return nullptr;
200 }
201
202 bool GroupExtension::hasObject(const DocumentObject* obj, bool recursive) const
203 {
204 if (obj == getExtendedObject()) {
205 return false;
206 }
207
208 try {
209 const std::vector<DocumentObject*>& grp = Group.getValues();
210 for (auto child : grp) {
211
212 if (!child) {
213 continue;
214 }
215
216 if (child == obj) {
217 return true;
218 }
219 else if (child == getExtendedObject()) {
220 throw Base::RuntimeError(
221 "Cyclic dependencies detected: Search cannot be performed");
222 }
223 else if (recursive && child->hasExtension(GroupExtension::getExtensionClassTypeId())) {
224 App::GroupExtension* subGroup = static_cast<App::GroupExtension*>(
225 child->getExtension(GroupExtension::getExtensionClassTypeId()));
226 std::vector<const GroupExtension*> history;
227 history.push_back(this);
228
229 if (subGroup->recursiveHasObject(obj, subGroup, history)) {
230 return true;
231 }
232 }
233 }
234
235 return false;
236 }
237 catch (const Base::RuntimeError& e) {
238 e.reportException();
239 return false;
240 }
241 }
242
243 bool GroupExtension::recursiveHasObject(const DocumentObject* obj,
244 const GroupExtension* group,
245 std::vector<const GroupExtension*> history) const
246 {
247
248 // the purpose is to prevent infinite recursion when groups form a cyclic graph. To do this
249 // we store every group we processed on the current leave of the tree, and if we reach an
250 // already processed group we know that it not really is a tree but a cycle.
251 history.push_back(this);
252
253 // we use hasObject with out recursion to allow override in derived classes
254 if (group->hasObject(obj, false)) {
255 return true;
256 }
257
258 // we checked for the searched object already with hasObject and did not find it, now we need to
259 // do the same for all subgroups
260 for (auto child : group->Group.getValues()) {
261
262 if (!child) {
263 continue;
264 }
265
266 if (child->hasExtension(GroupExtension::getExtensionClassTypeId())) {
267
268 auto ext = child->getExtensionByType<GroupExtension>();
269
270 if (std::ranges::find(history, ext) != history.end()) {
271 throw Base::RuntimeError(
272 "Cyclic dependencies detected: Search cannot be performed");
273 }
274
275 if (recursiveHasObject(obj, ext, history)) {
276 return true;
277 }
278 }
279 }
280 return false;
281 }
282
283 bool GroupExtension::isChildOf(const GroupExtension* group, bool recursive) const
284 {
285 return group->hasObject(getExtendedObject(), recursive);
286 }
287
288 const std::vector<DocumentObject*>& GroupExtension::getObjects() const
289 {
290 return Group.getValues();
291 }
292
293 std::vector<DocumentObject*> GroupExtension::getObjectsOfType(const Base::Type& typeId) const
294 {
295 std::vector<DocumentObject*> type;
296 const std::vector<DocumentObject*>& grp = Group.getValues();
297 for (auto it : grp) {
298 if (it->isDerivedFrom(typeId)) {
299 type.push_back(it);
300 }
301 }
302
303 return type;
304 }
305
306 int GroupExtension::countObjectsOfType(const Base::Type& typeId) const
307 {
308 int type = 0;
309 const std::vector<DocumentObject*>& grp = Group.getValues();
310 for (auto it : grp) {
311 if (it->isDerivedFrom(typeId)) {
312 type++;
313 }
314 }
315
316 return type;
317 }
318
319 DocumentObject* GroupExtension::getGroupOfObject(const DocumentObject* obj)
320 {
321 // note that we return here only Groups, but nothing derived from it, e.g. no GeoFeatureGroups.
322 // That is important as there are clear differences between groups/geofeature groups (e.g. an
323 // object can be in only one group, and only one geofeaturegroup, however, it can be in both at
324 // the same time)
325 for (auto o : obj->getInList()) {
326 Extension* ext = o->getExtension(GroupExtension::getExtensionClassTypeId(), false, true);
327 if (!ext) {
328 ext = o->getExtension(GroupExtensionPython::getExtensionClassTypeId(), false, true);
329 }
330 if (ext) {
331 auto grp = static_cast<GroupExtension*>(ext)->Group.getValues();
332 if (std::find(grp.begin(), grp.end(), obj) != grp.end()) {
333 return o;
334 }
335 }
336 }
337
338 return nullptr;
339 }
340
341 PyObject* GroupExtension::getExtensionPyObject()
342 {
343
344 if (ExtensionPythonObject.is(Py::_None())) {
345 // ref counter is set to 1
346 auto grp = new GroupExtensionPy(this);
347 ExtensionPythonObject = Py::Object(grp, true);
348 }
349 return Py::new_reference_to(ExtensionPythonObject);
350 }
351
352 void GroupExtension::extensionOnChanged(const Property* p)
353 {
354
355 // objects are only allowed in a single group. Note that this check must only be done for normal
356 // groups, not any derived classes
357 if ((this->getExtensionTypeId() == GroupExtension::getExtensionClassTypeId()) && p == &Group
358 && !Group.testStatus(Property::User3)) {
359 if (!getExtendedObject()->isRestoring()
360 && !getExtendedObject()->getDocument()->isPerformingTransaction()) {
361
362 bool error = false;
363 auto corrected = Group.getValues();
364 for (auto obj : Group.getValues()) {
365
366 // we have already set the obj into the group, so in a case of multiple groups
367 // getGroupOfObject would return anyone of it and hence it is possible that we miss
368 // an error. We need a custom check
369 auto list = obj->getInList();
370 for (auto in : list) {
371 auto ext = in->getExtension(GroupExtension::getExtensionClassTypeId(), false, true);
372 if (ext && (in != getExtendedObject())) {
373 auto grp = static_cast<GroupExtension*>(ext)->Group.getValues();
374 if (std::find(grp.begin(), grp.end(), obj) != grp.end()) {
375 error = true;
376 corrected.erase(std::remove(corrected.begin(), corrected.end(), obj),
377 corrected.end());
378 }
379 }
380 }
381 }
382
383 // if an error was found we need to correct the values and inform the user
384 if (error) {
385 Base::ObjectStatusLocker<Property::Status, Property> guard(Property::User3, &Group);
386 Group.setValues(corrected);
387 throw Base::RuntimeError("Object can only be in a single Group");
388 }
389 }
390 }
391
392 if (p == &Group) {
393 _Conns.clear();
394 for (auto obj : Group.getValue()) {
395 if (obj && obj->isAttachedToDocument()) {
396 // NOLINTBEGIN
397 _Conns[obj] = obj->signalChanged.connect(
398 std::bind(&GroupExtension::slotChildChanged, this, sp::_1, sp::_2));
399 // NOLINTEND
400 }
401 }
402 }
403
404 App::Extension::extensionOnChanged(p);
405 }
406
407 void GroupExtension::slotChildChanged(const DocumentObject& obj, const Property& prop)
408 {
409 if (&prop == &obj.Visibility) {
410 _GroupTouched.touch();
411 }
412 }
413
414 bool GroupExtension::extensionGetSubObject(DocumentObject*& ret,
415 const char* subname,
416 PyObject** pyObj,
417 Base::Matrix4D* mat,
418 bool /*transform*/,
419 int depth) const
420 {
421 const char* dot;
422 if (!subname || *subname == 0) {
423 auto obj = freecad_cast<const DocumentObject*>(getExtendedContainer());
424 ret = const_cast<DocumentObject*>(obj);
425 return true;
426 }
427 dot = strchr(subname, '.');
428 if (!dot) {
429 return false;
430 }
431 if (subname[0] != '$') {
432 ret = Group.findUsingMap(std::string(subname, dot));
433 }
434 else {
435 std::string name = std::string(subname + 1, dot);
436 for (auto child : Group.getValues()) {
437 if (name == child->Label.getStrValue()) {
438 ret = child;
439 break;
440 }
441 }
442 }
443 if (!ret) {
444 return false;
445 }
446 return ret->getSubObject(dot + 1, pyObj, mat, true, depth + 1);
447 }
448
449 bool GroupExtension::extensionGetSubObjects(std::vector<std::string>& ret, int) const
450 {
451 for (auto obj : Group.getValues()) {
452 if (obj && obj->isAttachedToDocument()) {
453 ret.push_back(std::string(obj->getNameInDocument()) + '.');
454 }
455 }
456 return true;
457 }
458
459 App::DocumentObjectExecReturn* GroupExtension::extensionExecute()
460 {
461 // This touch property is for propagating changes to upper group
462 _GroupTouched.touch();
463 return inherited::extensionExecute();
464 }
465
466 std::vector<App::DocumentObject*> GroupExtension::getAllChildren() const
467 {
468 std::vector<DocumentObject*> res;
469 std::set<DocumentObject*> rset;
470 getAllChildren(res, rset);
471 return res;
472 }
473
474 void GroupExtension::getAllChildren(std::vector<App::DocumentObject*>& res,
475 std::set<App::DocumentObject*>& rset) const
476 {
477 for (auto obj : Group.getValues()) {
478 if (!obj || !obj->isAttachedToDocument()) {
479 continue;
480 }
481 if (!rset.insert(obj).second) {
482 continue;
483 }
484 res.push_back(obj);
485 auto ext = obj->getExtensionByType<GroupExtension>(true, false);
486 if (ext) {
487 ext->getAllChildren(res, rset);
488 }
489 }
490 }