1 // Copyright (C) 2014-2020 CEA/DEN, EDF R&D
3 // This library is free software; you can redistribute it and/or
4 // modify it under the terms of the GNU Lesser General Public
5 // License as published by the Free Software Foundation; either
6 // version 2.1 of the License, or (at your option) any later version.
8 // This library is distributed in the hope that it will be useful,
9 // but WITHOUT ANY WARRANTY; without even the implied warranty of
10 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
11 // Lesser General Public License for more details.
13 // You should have received a copy of the GNU Lesser General Public
14 // License along with this library; if not, write to the Free Software
15 // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
17 // See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
20 #include <ModuleBase_WidgetValidated.h>
21 #include <ModuleBase_IModule.h>
22 #include <ModuleBase_IViewer.h>
23 #include <ModuleBase_IWorkshop.h>
24 #include <ModuleBase_ISelection.h>
25 #include <ModuleBase_ISelectionActivate.h>
26 #include <ModuleBase_WidgetSelectorStore.h>
27 #include <ModuleBase_ViewerPrs.h>
29 #include <Events_InfoMessage.h>
31 #include <ModelAPI_Session.h>
32 #include <ModelAPI_Validator.h>
33 #include <ModelAPI_AttributeValidator.h>
34 #include <ModelAPI_Events.h>
35 #include <ModelAPI_Tools.h>
36 #include <ModelAPI_AttributeSelection.h>
38 #include <BuildPlugin_Face.h>
39 #include <BuildPlugin_Wire.h>
40 #include <BuildPlugin_Solid.h>
42 #include <SelectMgr_ListIteratorOfListOfFilter.hxx>
43 #include <SelectMgr_EntityOwner.hxx>
44 #include <StdSelect_BRepOwner.hxx>
46 #include <Events_Loop.h>
50 //#define DEBUG_VALID_STATE
52 ModuleBase_WidgetValidated::ModuleBase_WidgetValidated(QWidget* theParent,
53 ModuleBase_IWorkshop* theWorkshop,
54 const Config_WidgetAPI* theData)
55 : ModuleBase_ModelWidget(theParent, theData),
56 myWorkshop(theWorkshop), myIsInValidate(false)
58 myAttributeStore = new ModuleBase_WidgetSelectorStore();
61 ModuleBase_WidgetValidated::~ModuleBase_WidgetValidated()
63 delete myAttributeStore;
66 //********************************************************************
67 ObjectPtr ModuleBase_WidgetValidated::findPresentedObject(const AISObjectPtr& theAIS) const
69 return myPresentedObject;
72 //********************************************************************
73 void ModuleBase_WidgetValidated::deactivate()
78 //********************************************************************
79 void ModuleBase_WidgetValidated::clearValidatedCash()
81 #ifdef DEBUG_VALID_STATE
82 qDebug("clearValidatedCash");
88 //********************************************************************
89 void ModuleBase_WidgetValidated::storeAttributeValue(const AttributePtr& theAttribute)
91 myIsInValidate = true;
92 myAttributeStore->storeAttributeValue(theAttribute, myWorkshop);
95 //********************************************************************
96 void ModuleBase_WidgetValidated::restoreAttributeValue(const AttributePtr& theAttribute,
99 myIsInValidate = false;
100 myAttributeStore->restoreAttributeValue(theAttribute, myWorkshop);
104 //********************************************************************
105 void ModuleBase_WidgetValidated::collectSubBodies(const ResultBodyPtr& theBody,
106 AIS_NListOfEntityOwner& theList)
109 TopoDS_Shape aTDShape;
110 int aNb = theBody->numberOfSubs();
111 for (int i = 0; i < aNb; i++) {
112 ResultBodyPtr aSub = theBody->subResult(i);
113 if (aSub->numberOfSubs() > 0)
114 collectSubBodies(aSub, theList);
116 aTDShape = aSub->shape()->impl<TopoDS_Shape>();
117 aIOPtr = myWorkshop->findPresentation(aSub);
119 Handle(AIS_InteractiveObject) anIO = aIOPtr->impl<Handle(AIS_InteractiveObject)>();
120 theList.Append(new StdSelect_BRepOwner(aTDShape, anIO));
123 theList.Append(new StdSelect_BRepOwner(aTDShape));
128 //********************************************************************
129 bool ModuleBase_WidgetValidated::isValidInFilters(const ModuleBase_ViewerPrsPtr& thePrs)
132 AIS_NListOfEntityOwner aOwnersList;
133 if (!thePrs->owner().IsNull())
134 aOwnersList.Append(thePrs->owner());
136 // if an owner is null, the selection happens in the Object browser.
137 // creates a selection owner on the base of object shape and the object AIS object
138 if ((aOwnersList.Size() == 0) && thePrs->object().get()) {
139 ResultPtr aResult = myWorkshop->selection()->getResult(thePrs);
140 GeomShapePtr aShape = aResult.get() ? aResult->shape() : GeomShapePtr();
141 // some results have no shape, e.g. the parameter one. So, they should not be validated
143 const TopoDS_Shape aTDShape = aShape->impl<TopoDS_Shape>();
144 Handle(AIS_InteractiveObject) anIO = myWorkshop->selection()->getIO(thePrs);
145 aOwnersList.Append(new StdSelect_BRepOwner(aTDShape, anIO));
146 myPresentedObject = aResult;
149 //FeaturePtr aFeature = ModelAPI_Feature::feature(thePrs->object());
150 FeaturePtr aFeature = std::dynamic_pointer_cast<ModelAPI_Feature>(thePrs->object());
151 if (aFeature.get()) {
152 // Use feature as a reference to all its results
153 myPresentedObject = aFeature;
154 AttributePtr anAttr = attribute();
155 std::string aType = anAttr->attributeType();
157 // Check that results of Feature is acceptable by filters for selection attribute
158 if (aType == ModelAPI_AttributeSelection::typeId()) {
159 AttributeSelectionPtr aSelectAttr =
160 std::dynamic_pointer_cast<ModelAPI_AttributeSelection>(anAttr);
161 aSelectAttr->setValue(myPresentedObject, GeomShapePtr(), true);
162 GeomShapePtr aShapePtr = aSelectAttr->value();
163 if (!aShapePtr.get() && aSelectAttr->contextFeature().get() &&
164 aSelectAttr->contextFeature()->firstResult().get()) {
165 aShapePtr = aSelectAttr->contextFeature()->firstResult()->shape();
167 if (aShapePtr.get()) {
168 const TopoDS_Shape aTDShape = aShapePtr->impl<TopoDS_Shape>();
169 Handle(AIS_InteractiveObject) anIO = myWorkshop->selection()->getIO(thePrs);
170 aOwnersList.Append(new StdSelect_BRepOwner(aTDShape, anIO));
174 ResultPtr aFirstRes = aFeature->firstResult();
175 if (aFirstRes.get()) {
176 ResultBodyPtr aBody = std::dynamic_pointer_cast<ModelAPI_ResultBody>(aFirstRes);
177 if (aBody.get() && (aBody->numberOfSubs() > 0))
178 collectSubBodies(aBody, aOwnersList);
180 GeomShapePtr aShapePtr = ModelAPI_Tools::shape(aFirstRes);
181 if (aShapePtr.get()) {
182 TopoDS_Shape aTDShape = aShapePtr->impl<TopoDS_Shape>();
183 AISObjectPtr aIOPtr = myWorkshop->findPresentation(aFirstRes);
185 Handle(AIS_InteractiveObject) anIO =
186 aIOPtr->impl<Handle(AIS_InteractiveObject)>();
187 aOwnersList.Append(new StdSelect_BRepOwner(aTDShape, anIO));
190 aOwnersList.Append(new StdSelect_BRepOwner(aTDShape));
198 aValid = (aOwnersList.Size() > 0); // only results with a shape can be filtered
200 // checks the owner by the AIS context activated filters
201 if (aOwnersList.Size() > 0) {
202 // the widget validator filter should be active, but during check by preselection
203 // it is not yet activated, so we need to activate/deactivate it manually
204 bool isActivated = isFilterActivated();
206 QIntList aModuleSelectionFilters = myWorkshop->module()->selectionFilters();
207 SelectMgr_ListOfFilter aSelectionFilters;
208 selectionFilters(aModuleSelectionFilters, aSelectionFilters);
209 /// after validation, the selection filters should be restored
210 myWorkshop->selectionActivate()->activateSelectionFilters(aSelectionFilters);
213 Handle(AIS_InteractiveContext) aContext = myWorkshop->viewer()->AISContext();
214 if (!aContext.IsNull()) {
215 const SelectMgr_ListOfFilter& aFilters = aContext->Filters();
216 SelectMgr_ListIteratorOfListOfFilter anIt(aFilters);
217 AIS_NListOfEntityOwner::Iterator aOIt;
218 for (; anIt.More() && aValid; anIt.Next()) {
219 Handle(SelectMgr_Filter) aFilter = anIt.Value();
220 for (aOIt.Init(aOwnersList); aOIt.More(); aOIt.Next()) {
221 aValid = aFilter->IsOk(aOIt.Value());
230 // reset filters set in activateSelectionFilters above
231 myWorkshop->selectionActivate()->updateSelectionFilters();
232 clearValidatedCash();
236 // removes created owner
237 if (aOwnersList.Size() > 0 && thePrs->owner().IsNull()) {
238 myPresentedObject = ObjectPtr();
241 // Clear attribute if it still has selection
242 AttributePtr anAttr = attribute();
243 std::string aType = anAttr->attributeType();
244 if (aType == ModelAPI_AttributeSelection::typeId()) {
245 AttributeSelectionPtr aSelectAttr =
246 std::dynamic_pointer_cast<ModelAPI_AttributeSelection>(anAttr);
247 aSelectAttr->removeTemporaryValues();
253 //********************************************************************
254 AttributePtr ModuleBase_WidgetValidated::attribute() const
256 return myFeature->attribute(attributeID());
259 //********************************************************************
260 bool ModuleBase_WidgetValidated::isValidSelection(const ModuleBase_ViewerPrsPtr& theValue)
263 if (getValidState(theValue, aValid)) {
266 aValid = isValidSelectionCustom(theValue);
268 aValid = isValidSelectionForAttribute(theValue, attribute());
270 storeValidState(theValue, aValid);
274 //********************************************************************
275 bool ModuleBase_WidgetValidated::isValidSelectionForAttribute(
276 const ModuleBase_ViewerPrsPtr& theValue,
277 const AttributePtr& theAttribute)
281 // stores the current values of the widget attribute
282 bool isFlushesActived, isAttributeSetInitializedBlocked, isAttributeSendUpdatedBlocked;
284 blockAttribute(theAttribute, true, isFlushesActived, isAttributeSetInitializedBlocked,
285 isAttributeSendUpdatedBlocked);
287 storeAttributeValue(theAttribute);
289 // saves the owner value to the widget attribute
290 aValid = setSelectionCustom(theValue);
292 // checks the attribute validity
293 aValid = isValidAttribute(theAttribute);
295 // restores the current values of the widget attribute
296 restoreAttributeValue(theAttribute, aValid);
298 blockAttribute(theAttribute, false, isFlushesActived, isAttributeSetInitializedBlocked,
299 isAttributeSendUpdatedBlocked);
300 /// NDS: The following rows are commented for issue #1452 (to be removed after debug)
301 /// This is not correct to perform it here because it might cause update selection and
302 /// the selection mechanizm will be circled: use the scenario of the bug with preselected point.
303 // In particular case the results are deleted and called as redisplayed inside of this
304 // highlight-selection, to they must be flushed as soon as possible.
305 // Example: selection of group-vertices subshapes with shift pressend on body. Without
306 // these 4 lines below the application crashes because of left presentations on
307 // removed results still in the viewer.
308 /*static Events_ID aDeletedEvent = Events_Loop::eventByName(EVENT_OBJECT_DELETED);
309 static Events_ID aRedispEvent = Events_Loop::eventByName(EVENT_OBJECT_TO_REDISPLAY);
310 Events_Loop::loop()->flush(aDeletedEvent);
311 Events_Loop::loop()->flush(aRedispEvent);
316 //********************************************************************
317 bool ModuleBase_WidgetValidated::isValidSelectionCustom(const ModuleBase_ViewerPrsPtr& thePrs)
322 //********************************************************************
323 bool ModuleBase_WidgetValidated::isValidAttribute(const AttributePtr& theAttribute)
325 SessionPtr aMgr = ModelAPI_Session::get();
326 ModelAPI_ValidatorsFactory* aFactory = aMgr->validators();
327 std::string aValidatorID;
328 Events_InfoMessage anError;
329 return aFactory->validate(theAttribute, aValidatorID, anError);
332 //********************************************************************
333 bool ModuleBase_WidgetValidated::isFilterActivated() const
335 Handle(SelectMgr_Filter) aSelFilter = myWorkshop->validatorFilter();
336 ModuleBase_IViewer* aViewer = myWorkshop->viewer();
338 return aViewer->hasSelectionFilter(aSelFilter);
341 //********************************************************************
342 void ModuleBase_WidgetValidated::selectionFilters(QIntList& theModuleSelectionFilters,
343 SelectMgr_ListOfFilter& theSelectionFilters)
345 theSelectionFilters.Append(myWorkshop->validatorFilter());
348 //********************************************************************
349 void ModuleBase_WidgetValidated::blockAttribute(const AttributePtr& theAttribute,
350 const bool& theToBlock,
351 bool& isFlushesActived,
352 bool& isAttributeSetInitializedBlocked,
353 bool& isAttributeSendUpdatedBlocked)
355 blockFeatureAttribute(theAttribute, myFeature, theToBlock, isFlushesActived,
356 isAttributeSetInitializedBlocked, isAttributeSendUpdatedBlocked);
359 //********************************************************************
360 void ModuleBase_WidgetValidated::blockFeatureAttribute(const AttributePtr& theAttribute,
361 const FeaturePtr& theFeature,
362 const bool& theToBlock,
363 bool& isFlushesActived,
364 bool& isAttributeSetInitializedBlocked,
365 bool& isAttributeSendUpdatedBlocked)
367 Events_Loop* aLoop = Events_Loop::loop();
368 DataPtr aData = theFeature->data();
370 // blocks the flush signals to avoid the temporary objects visualization in the viewer
371 // they should not be shown in order to do not lose highlight by erasing them
372 isFlushesActived = aLoop->activateFlushes(false);
374 isAttributeSendUpdatedBlocked = aData->blockSendAttributeUpdated(true);
375 isAttributeSetInitializedBlocked = theAttribute->blockSetInitialized(true);
378 aData->blockSendAttributeUpdated(isAttributeSendUpdatedBlocked, false);
379 theAttribute->blockSetInitialized(isAttributeSetInitializedBlocked);
380 aLoop->activateFlushes(isFlushesActived);
384 //********************************************************************
385 void ModuleBase_WidgetValidated::storeValidState(const ModuleBase_ViewerPrsPtr& theValue,
388 GeomShapePtr aShape = theValue.get() ? theValue->shape() : GeomShapePtr();
391 const TopoDS_Shape& aTDShape = aShape->impl<TopoDS_Shape>();
392 bool aValidPrsContains = myValidPrs.IsBound(aTDShape) &&
393 theValue.get()->isEqual(myValidPrs.Find(aTDShape).get());
394 if (!aValidPrsContains) {
395 #ifdef LIST_OF_VALID_PRS
396 myValidPrs.append(theValue);
398 myValidPrs.Bind(aTDShape, theValue);
400 // the commented code will be useful when the valid state of the presentation
401 // will be changable between activate/deactivate. Currently it does not happen.
403 // myInvalidPrs.removeOne(theValue);
408 const TopoDS_Shape& aTDShape = aShape->impl<TopoDS_Shape>();
409 bool anIValidPrsContains = myInvalidPrs.IsBound(aTDShape) &&
410 theValue.get()->isEqual(myInvalidPrs.Find(aTDShape).get());
411 if (!anIValidPrsContains) {
412 #ifdef LIST_OF_VALID_PRS
413 myInvalidPrs.append(theValue);
415 myInvalidPrs.Bind(aTDShape, theValue);
418 // myValidPrs.removeOne(theValue);
423 #ifdef DEBUG_VALID_STATE
424 qDebug(QString("storeValidState: myValidPrs.size() = %1, myInvalidPrs.size() = %2")
425 .arg(myValidPrs.count())
426 .arg(myInvalidPrs.count()).toStdString().c_str());
430 //********************************************************************
431 bool ModuleBase_WidgetValidated::getValidState(const ModuleBase_ViewerPrsPtr& theValue,
437 #ifdef LIST_OF_VALID_PRS
438 bool aValidPrsContains = myValidPrs.contains(theValue);
439 bool anInvalidPrsContains = myInvalidPrs.contains(theValue);
441 GeomShapePtr aShape = theValue.get() ? theValue->shape() : GeomShapePtr();
445 const TopoDS_Shape& aTDShape = aShape->impl<TopoDS_Shape>();
446 bool aValidPrsContains = myValidPrs.IsBound(aTDShape) &&
447 theValue.get()->isEqual(myValidPrs.Find(aTDShape).get());
449 bool anInvalidPrsContains = myInvalidPrs.IsBound(aTDShape) &&
450 theValue.get()->isEqual(myInvalidPrs.Find(aTDShape).get());
452 bool aValidPrsContains = false, anInvalidPrsContains = false;
453 GeomShapePtr aShape = theValue.get() ? theValue->shape() : GeomShapePtr();
455 aValidPrsContains = myValidPrs.contains(aShape);
456 anInvalidPrsContains = myInvalidPrs.contains(aShape);
458 if (aValidPrsContains)
459 aValidPrsContains = theValue == myValidPrs[aShape];
461 anInvalidPrsContains = theValue == myInvalidPrs[aShape];*/
464 if (aValidPrsContains)
466 else if (anInvalidPrsContains)
469 return aValidPrsContains || anInvalidPrsContains;
472 //********************************************************************
473 QList<ModuleBase_ViewerPrsPtr> ModuleBase_WidgetValidated::getFilteredSelected()
475 QList<ModuleBase_ViewerPrsPtr> aSelected = myWorkshop->selection()->getSelected(
476 ModuleBase_ISelection::Viewer);
478 QList<ModuleBase_ViewerPrsPtr> anOBSelected = myWorkshop->selection()->getSelected(
479 ModuleBase_ISelection::Browser);
480 // filter the OB presentations
481 filterPresentations(anOBSelected);
482 if (!anOBSelected.isEmpty())
483 ModuleBase_ISelection::appendSelected(anOBSelected, aSelected);
485 filterCompSolids(aSelected);
487 if (myFeatureId == BuildPlugin_Face::ID() || myFeatureId == BuildPlugin_Wire::ID() ||
488 myFeatureId == BuildPlugin_Solid::ID())
490 /// remove objects of sub-type if ojects of correct type is in List, in some cases :
491 /// - Face builder: edges, faces and wires selected
492 /// --> remove edges and wires
493 /// Wire builder: wires and edges selected
495 /// Solid builder: faces and shapes shells or solids seleted
498 std::set<GeomAPI_Shape::ShapeType> shapeTypes;
499 for (auto aSelection: aSelected){
500 auto aShape = aSelection->shape();
502 shapeTypes.insert(aShape->shapeType());
505 std::vector<ModuleBase_ViewerPrsPtr> aRemove;
507 if (myFeatureId == BuildPlugin_Face::ID() && shapeTypes.find(GeomAPI_Shape::FACE)
510 for(auto aSelection: aSelected){
511 auto aType = aSelection->shape()->shapeType();
512 if (aType == GeomAPI_Shape::WIRE || aType == GeomAPI_Shape::EDGE)
513 aRemove.push_back(aSelection);
516 else if (myFeatureId == BuildPlugin_Wire::ID() && shapeTypes.find(GeomAPI_Shape::WIRE)
519 for(auto aSelection: aSelected){
520 auto aType = aSelection->shape()->shapeType();
521 if (aType == GeomAPI_Shape::EDGE)
522 aRemove.push_back(aSelection);
525 else if (myFeatureId == BuildPlugin_Solid::ID() &&
526 (shapeTypes.find(GeomAPI_Shape::SHAPE) != shapeTypes.end() ||
527 shapeTypes.find(GeomAPI_Shape::SOLID) != shapeTypes.end() ||
528 shapeTypes.find(GeomAPI_Shape::SHELL) != shapeTypes.end()) )
530 for(auto aSelection: aSelected){
531 auto aType = aSelection->shape()->shapeType();
532 if( aType == GeomAPI_Shape::FACE)
533 aRemove.push_back(aSelection);
537 for(auto aSelection: aRemove){
538 aSelected.removeOne(aSelection);
545 //********************************************************************
546 void ModuleBase_WidgetValidated::filterPresentations(QList<ModuleBase_ViewerPrsPtr>& theValues)
548 QList<ModuleBase_ViewerPrsPtr> aValidatedValues;
549 foreach(ModuleBase_ViewerPrsPtr aPrs, theValues) {
550 if (isValidInFilters(aPrs))
551 aValidatedValues.append(aPrs);
553 if (aValidatedValues.size() != theValues.size()) {
555 theValues = aValidatedValues;
559 //********************************************************************
560 void ModuleBase_WidgetValidated::filterCompSolids(QList<ModuleBase_ViewerPrsPtr>& theValues)
562 std::set<ResultPtr> aFilterOut; // all objects that must be filtered out with their children
563 QList<ModuleBase_ViewerPrsPtr> aValidatedValues;
565 // Collect compsolids.
566 QList<ModuleBase_ViewerPrsPtr>::const_iterator anIt = theValues.begin(), aLast = theValues.end();
567 for(; anIt != aLast; anIt++) {
568 const ModuleBase_ViewerPrsPtr& aViewerPrs = *anIt;
569 ObjectPtr anObject = aViewerPrs->object();
570 ResultBodyPtr aResultCompSolid =
571 std::dynamic_pointer_cast<ModelAPI_ResultBody>(anObject);
572 if (aResultCompSolid.get()) {
573 for(int aSubIndex = 0; aSubIndex < aResultCompSolid->numberOfSubs(); aSubIndex++)
574 aFilterOut.insert(aResultCompSolid->subResult(aSubIndex));
575 } else { // it could be a whole feature selected, so, add all results of this feature
576 FeaturePtr aFeature = std::dynamic_pointer_cast<ModelAPI_Feature>(anObject);
577 if (aFeature.get()) {
578 std::list<ResultPtr>::const_iterator aRes = aFeature->results().cbegin();
579 for(; aRes != aFeature->results().cend(); aRes++)
580 aFilterOut.insert(*aRes);
585 // Filter sub-solids of compsolids.
586 anIt = theValues.begin();
587 for(; anIt != aLast; anIt++) {
588 const ModuleBase_ViewerPrsPtr& aViewerPrs = *anIt;
589 ObjectPtr anObject = aViewerPrs->object();
590 ResultPtr aResult = std::dynamic_pointer_cast<ModelAPI_Result>(anObject);
591 while(aResult.get()) {
592 if (aFilterOut.find(aResult) != aFilterOut.end()) // skip if parent is filtered out
594 aResult = ModelAPI_Tools::bodyOwner(aResult); // iterate all parents
599 aValidatedValues.append(*anIt);
603 if (aValidatedValues.size() != theValues.size()) {
605 theValues = aValidatedValues;