1 // Copyright (C) 2014-2019 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_ResultBody.h>
36 #include <ModelAPI_Tools.h>
37 #include <ModelAPI_AttributeSelection.h>
39 #include <SelectMgr_ListIteratorOfListOfFilter.hxx>
40 #include <SelectMgr_EntityOwner.hxx>
41 #include <StdSelect_BRepOwner.hxx>
43 #include <Events_Loop.h>
47 //#define DEBUG_VALID_STATE
49 ModuleBase_WidgetValidated::ModuleBase_WidgetValidated(QWidget* theParent,
50 ModuleBase_IWorkshop* theWorkshop,
51 const Config_WidgetAPI* theData)
52 : ModuleBase_ModelWidget(theParent, theData),
53 myWorkshop(theWorkshop), myIsInValidate(false)
55 myAttributeStore = new ModuleBase_WidgetSelectorStore();
58 ModuleBase_WidgetValidated::~ModuleBase_WidgetValidated()
60 delete myAttributeStore;
63 //********************************************************************
64 ObjectPtr ModuleBase_WidgetValidated::findPresentedObject(const AISObjectPtr& theAIS) const
66 return myPresentedObject;
69 //********************************************************************
70 void ModuleBase_WidgetValidated::deactivate()
75 //********************************************************************
76 void ModuleBase_WidgetValidated::clearValidatedCash()
78 #ifdef DEBUG_VALID_STATE
79 qDebug("clearValidatedCash");
85 //********************************************************************
86 void ModuleBase_WidgetValidated::storeAttributeValue(const AttributePtr& theAttribute)
88 myIsInValidate = true;
89 myAttributeStore->storeAttributeValue(theAttribute, myWorkshop);
92 //********************************************************************
93 void ModuleBase_WidgetValidated::restoreAttributeValue(const AttributePtr& theAttribute,
96 myIsInValidate = false;
97 myAttributeStore->restoreAttributeValue(theAttribute, myWorkshop);
100 //********************************************************************
101 bool ModuleBase_WidgetValidated::isValidInFilters(const ModuleBase_ViewerPrsPtr& thePrs)
104 Handle(SelectMgr_EntityOwner) anOwner = thePrs->owner();
106 // if an owner is null, the selection happens in the Object browser.
107 // creates a selection owner on the base of object shape and the object AIS object
108 if (anOwner.IsNull() && thePrs->owner().IsNull() && thePrs->object().get()) {
109 ResultPtr aResult = myWorkshop->selection()->getResult(thePrs);
110 GeomShapePtr aShape = aResult.get() ? aResult->shape() : GeomShapePtr();
111 // some results have no shape, e.g. the parameter one. So, they should not be validated
113 const TopoDS_Shape aTDShape = aShape->impl<TopoDS_Shape>();
114 Handle(AIS_InteractiveObject) anIO = myWorkshop->selection()->getIO(thePrs);
115 anOwner = new StdSelect_BRepOwner(aTDShape, anIO);
116 myPresentedObject = aResult;
119 //FeaturePtr aFeature = ModelAPI_Feature::feature(thePrs->object());
120 FeaturePtr aFeature = std::dynamic_pointer_cast<ModelAPI_Feature>(thePrs->object());
121 if (aFeature.get()) {
122 // Use feature as a reference to all its results
123 myPresentedObject = aFeature;
124 AttributePtr anAttr = attribute();
125 std::string aType = anAttr->attributeType();
127 // Check that results of Feature is acceptable by filters for selection attribute
128 if (aType == ModelAPI_AttributeSelection::typeId()) {
129 AttributeSelectionPtr aSelectAttr =
130 std::dynamic_pointer_cast<ModelAPI_AttributeSelection>(anAttr);
131 aSelectAttr->setValue(myPresentedObject, GeomShapePtr(), true);
132 GeomShapePtr aShape = aSelectAttr->value();
133 if (!aShape.get() && aSelectAttr->contextFeature().get() &&
134 aSelectAttr->contextFeature()->firstResult().get()) {
135 aShape = aSelectAttr->contextFeature()->firstResult()->shape();
138 const TopoDS_Shape aTDShape = aShape->impl<TopoDS_Shape>();
139 Handle(AIS_InteractiveObject) anIO = myWorkshop->selection()->getIO(thePrs);
140 anOwner = new StdSelect_BRepOwner(aTDShape, anIO);
144 //aSelectAttr->setValue(ObjectPtr(), GeomShapePtr(), true);
147 ResultPtr aResult = aFeature->firstResult();
149 GeomShapePtr aShapePtr = ModelAPI_Tools::shape(aResult);
150 if (aShapePtr.get()) {
151 const TopoDS_Shape aTDShape = aShapePtr->impl<TopoDS_Shape>();
152 AISObjectPtr aIOPtr = myWorkshop->findPresentation(aResult);
154 Handle(AIS_InteractiveObject) anIO = aIOPtr->impl<Handle(AIS_InteractiveObject)>();
155 anOwner = new StdSelect_BRepOwner(aTDShape, anIO);
159 aValid = !anOwner.IsNull(); // only results with a shape can be filtered
162 aValid = false; // only results with a shape can be filtered
165 // checks the owner by the AIS context activated filters
166 if (!anOwner.IsNull()) {
167 // the widget validator filter should be active, but during check by preselection
168 // it is not yet activated, so we need to activate/deactivate it manually
169 bool isActivated = isFilterActivated();
171 QIntList aModuleSelectionFilters = myWorkshop->module()->selectionFilters();
172 SelectMgr_ListOfFilter aSelectionFilters;
173 selectionFilters(aModuleSelectionFilters, aSelectionFilters);
174 /// after validation, the selection filters should be restored
175 myWorkshop->selectionActivate()->activateSelectionFilters(aSelectionFilters);
178 Handle(AIS_InteractiveContext) aContext = myWorkshop->viewer()->AISContext();
179 if (!aContext.IsNull()) {
180 const SelectMgr_ListOfFilter& aFilters = aContext->Filters();
181 SelectMgr_ListIteratorOfListOfFilter anIt(aFilters);
182 for (; anIt.More() && aValid; anIt.Next()) {
183 Handle(SelectMgr_Filter) aFilter = anIt.Value();
184 aValid = aFilter->IsOk(anOwner);
189 // reset filters set in activateSelectionFilters above
190 myWorkshop->selectionActivate()->updateSelectionFilters();
191 clearValidatedCash();
195 // removes created owner
196 if (!anOwner.IsNull() && anOwner != thePrs->owner()) {
198 myPresentedObject = ObjectPtr();
201 // Clear attribute if it still has selection
202 AttributePtr anAttr = attribute();
203 std::string aType = anAttr->attributeType();
204 if (aType == ModelAPI_AttributeSelection::typeId()) {
205 AttributeSelectionPtr aSelectAttr =
206 std::dynamic_pointer_cast<ModelAPI_AttributeSelection>(anAttr);
207 aSelectAttr->removeTemporaryValues();
213 //********************************************************************
214 AttributePtr ModuleBase_WidgetValidated::attribute() const
216 return myFeature->attribute(attributeID());
219 //********************************************************************
220 bool ModuleBase_WidgetValidated::isValidSelection(const ModuleBase_ViewerPrsPtr& theValue)
223 if (getValidState(theValue, aValid)) {
226 aValid = isValidSelectionCustom(theValue);
228 aValid = isValidSelectionForAttribute(theValue, attribute());
230 storeValidState(theValue, aValid);
234 //********************************************************************
235 bool ModuleBase_WidgetValidated::isValidSelectionForAttribute(
236 const ModuleBase_ViewerPrsPtr& theValue,
237 const AttributePtr& theAttribute)
241 // stores the current values of the widget attribute
242 bool isFlushesActived, isAttributeSetInitializedBlocked, isAttributeSendUpdatedBlocked;
244 blockAttribute(theAttribute, true, isFlushesActived, isAttributeSetInitializedBlocked,
245 isAttributeSendUpdatedBlocked);
247 storeAttributeValue(theAttribute);
249 // saves the owner value to the widget attribute
250 aValid = setSelectionCustom(theValue);
252 // checks the attribute validity
253 aValid = isValidAttribute(theAttribute);
255 // restores the current values of the widget attribute
256 restoreAttributeValue(theAttribute, aValid);
258 blockAttribute(theAttribute, false, isFlushesActived, isAttributeSetInitializedBlocked,
259 isAttributeSendUpdatedBlocked);
260 /// NDS: The following rows are commented for issue #1452 (to be removed after debug)
261 /// This is not correct to perform it here because it might cause update selection and
262 /// the selection mechanizm will be circled: use the scenario of the bug with preselected point.
263 // In particular case the results are deleted and called as redisplayed inside of this
264 // highlight-selection, to they must be flushed as soon as possible.
265 // Example: selection of group-vertices subshapes with shift pressend on body. Without
266 // these 4 lines below the application crashes because of left presentations on
267 // removed results still in the viewer.
268 /*static Events_ID aDeletedEvent = Events_Loop::eventByName(EVENT_OBJECT_DELETED);
269 static Events_ID aRedispEvent = Events_Loop::eventByName(EVENT_OBJECT_TO_REDISPLAY);
270 Events_Loop::loop()->flush(aDeletedEvent);
271 Events_Loop::loop()->flush(aRedispEvent);
276 //********************************************************************
277 bool ModuleBase_WidgetValidated::isValidSelectionCustom(const ModuleBase_ViewerPrsPtr& thePrs)
282 //********************************************************************
283 bool ModuleBase_WidgetValidated::isValidAttribute(const AttributePtr& theAttribute)
285 SessionPtr aMgr = ModelAPI_Session::get();
286 ModelAPI_ValidatorsFactory* aFactory = aMgr->validators();
287 std::string aValidatorID;
288 Events_InfoMessage anError;
289 return aFactory->validate(theAttribute, aValidatorID, anError);
292 //********************************************************************
293 bool ModuleBase_WidgetValidated::isFilterActivated() const
295 bool isActivated = false;
297 Handle(SelectMgr_Filter) aSelFilter = myWorkshop->validatorFilter();
298 ModuleBase_IViewer* aViewer = myWorkshop->viewer();
300 return aViewer->hasSelectionFilter(aSelFilter);
303 //********************************************************************
304 void ModuleBase_WidgetValidated::selectionFilters(QIntList& theModuleSelectionFilters,
305 SelectMgr_ListOfFilter& theSelectionFilters)
307 theSelectionFilters.Append(myWorkshop->validatorFilter());
310 //********************************************************************
311 void ModuleBase_WidgetValidated::blockAttribute(const AttributePtr& theAttribute,
312 const bool& theToBlock,
313 bool& isFlushesActived,
314 bool& isAttributeSetInitializedBlocked,
315 bool& isAttributeSendUpdatedBlocked)
317 blockFeatureAttribute(theAttribute, myFeature, theToBlock, isFlushesActived,
318 isAttributeSetInitializedBlocked, isAttributeSendUpdatedBlocked);
321 //********************************************************************
322 void ModuleBase_WidgetValidated::blockFeatureAttribute(const AttributePtr& theAttribute,
323 const FeaturePtr& theFeature,
324 const bool& theToBlock,
325 bool& isFlushesActived,
326 bool& isAttributeSetInitializedBlocked,
327 bool& isAttributeSendUpdatedBlocked)
329 Events_Loop* aLoop = Events_Loop::loop();
330 DataPtr aData = theFeature->data();
332 // blocks the flush signals to avoid the temporary objects visualization in the viewer
333 // they should not be shown in order to do not lose highlight by erasing them
334 isFlushesActived = aLoop->activateFlushes(false);
336 isAttributeSendUpdatedBlocked = aData->blockSendAttributeUpdated(true);
337 isAttributeSetInitializedBlocked = theAttribute->blockSetInitialized(true);
340 aData->blockSendAttributeUpdated(isAttributeSendUpdatedBlocked, false);
341 theAttribute->blockSetInitialized(isAttributeSetInitializedBlocked);
342 aLoop->activateFlushes(isFlushesActived);
346 //********************************************************************
347 void ModuleBase_WidgetValidated::storeValidState(const ModuleBase_ViewerPrsPtr& theValue,
350 GeomShapePtr aShape = theValue.get() ? theValue->shape() : GeomShapePtr();
353 const TopoDS_Shape& aTDShape = aShape->impl<TopoDS_Shape>();
354 bool aValidPrsContains = myValidPrs.IsBound(aTDShape) &&
355 theValue.get()->isEqual(myValidPrs.Find(aTDShape).get());
356 if (!aValidPrsContains) {
357 #ifdef LIST_OF_VALID_PRS
358 myValidPrs.append(theValue);
360 myValidPrs.Bind(aTDShape, theValue);
362 // the commented code will be useful when the valid state of the presentation
363 // will be changable between activate/deactivate. Currently it does not happen.
365 // myInvalidPrs.removeOne(theValue);
370 const TopoDS_Shape& aTDShape = aShape->impl<TopoDS_Shape>();
371 bool anIValidPrsContains = myInvalidPrs.IsBound(aTDShape) &&
372 theValue.get()->isEqual(myInvalidPrs.Find(aTDShape).get());
373 if (!anIValidPrsContains) {
374 #ifdef LIST_OF_VALID_PRS
375 myInvalidPrs.append(theValue);
377 myInvalidPrs.Bind(aTDShape, theValue);
380 // myValidPrs.removeOne(theValue);
385 #ifdef DEBUG_VALID_STATE
386 qDebug(QString("storeValidState: myValidPrs.size() = %1, myInvalidPrs.size() = %2")
387 .arg(myValidPrs.count())
388 .arg(myInvalidPrs.count()).toStdString().c_str());
392 //********************************************************************
393 bool ModuleBase_WidgetValidated::getValidState(const ModuleBase_ViewerPrsPtr& theValue,
399 #ifdef LIST_OF_VALID_PRS
400 bool aValidPrsContains = myValidPrs.contains(theValue);
401 bool anInvalidPrsContains = myInvalidPrs.contains(theValue);
403 GeomShapePtr aShape = theValue.get() ? theValue->shape() : GeomShapePtr();
407 const TopoDS_Shape& aTDShape = aShape->impl<TopoDS_Shape>();
408 bool aValidPrsContains = myValidPrs.IsBound(aTDShape) &&
409 theValue.get()->isEqual(myValidPrs.Find(aTDShape).get());
411 bool anInvalidPrsContains = myInvalidPrs.IsBound(aTDShape) &&
412 theValue.get()->isEqual(myInvalidPrs.Find(aTDShape).get());
414 bool aValidPrsContains = false, anInvalidPrsContains = false;
415 GeomShapePtr aShape = theValue.get() ? theValue->shape() : GeomShapePtr();
417 aValidPrsContains = myValidPrs.contains(aShape);
418 anInvalidPrsContains = myInvalidPrs.contains(aShape);
420 if (aValidPrsContains)
421 aValidPrsContains = theValue == myValidPrs[aShape];
423 anInvalidPrsContains = theValue == myInvalidPrs[aShape];*/
426 if (aValidPrsContains)
428 else if (anInvalidPrsContains)
431 return aValidPrsContains || anInvalidPrsContains;
434 //********************************************************************
435 QList<ModuleBase_ViewerPrsPtr> ModuleBase_WidgetValidated::getFilteredSelected()
437 QList<ModuleBase_ViewerPrsPtr> aSelected = myWorkshop->selection()->getSelected(
438 ModuleBase_ISelection::Viewer);
440 QList<ModuleBase_ViewerPrsPtr> anOBSelected = myWorkshop->selection()->getSelected(
441 ModuleBase_ISelection::Browser);
442 // filter the OB presentations
443 filterPresentations(anOBSelected);
444 if (!anOBSelected.isEmpty())
445 ModuleBase_ISelection::appendSelected(anOBSelected, aSelected);
447 filterCompSolids(aSelected);
452 //********************************************************************
453 void ModuleBase_WidgetValidated::filterPresentations(QList<ModuleBase_ViewerPrsPtr>& theValues)
455 QList<ModuleBase_ViewerPrsPtr> aValidatedValues;
456 foreach(ModuleBase_ViewerPrsPtr aPrs, theValues) {
457 if (isValidInFilters(aPrs))
458 aValidatedValues.append(aPrs);
460 if (aValidatedValues.size() != theValues.size()) {
462 theValues = aValidatedValues;
466 //********************************************************************
467 void ModuleBase_WidgetValidated::filterCompSolids(QList<ModuleBase_ViewerPrsPtr>& theValues)
469 std::set<ResultPtr> aFilterOut; // all objects that must be filtered out with their children
470 QList<ModuleBase_ViewerPrsPtr> aValidatedValues;
472 // Collect compsolids.
473 QList<ModuleBase_ViewerPrsPtr>::const_iterator anIt = theValues.begin(), aLast = theValues.end();
474 for(; anIt != aLast; anIt++) {
475 const ModuleBase_ViewerPrsPtr& aViewerPrs = *anIt;
476 ObjectPtr anObject = aViewerPrs->object();
477 ResultBodyPtr aResultCompSolid =
478 std::dynamic_pointer_cast<ModelAPI_ResultBody>(anObject);
479 if (aResultCompSolid.get()) {
480 for(int aSubIndex = 0; aSubIndex < aResultCompSolid->numberOfSubs(); aSubIndex++)
481 aFilterOut.insert(aResultCompSolid->subResult(aSubIndex));
482 } else { // it could be a whole feature selected, so, add all results of this feature
483 FeaturePtr aFeature = std::dynamic_pointer_cast<ModelAPI_Feature>(anObject);
484 if (aFeature.get()) {
485 std::list<ResultPtr>::const_iterator aRes = aFeature->results().cbegin();
486 for(; aRes != aFeature->results().cend(); aRes++)
487 aFilterOut.insert(*aRes);
492 // Filter sub-solids of compsolids.
493 anIt = theValues.begin();
494 for(; anIt != aLast; anIt++) {
495 const ModuleBase_ViewerPrsPtr& aViewerPrs = *anIt;
496 ObjectPtr anObject = aViewerPrs->object();
497 ResultPtr aResult = std::dynamic_pointer_cast<ModelAPI_Result>(anObject);
498 while(aResult.get()) {
499 if (aFilterOut.find(aResult) != aFilterOut.end()) // skip if parent is filtered out
501 aResult = ModelAPI_Tools::bodyOwner(aResult); // iterate all parents
506 aValidatedValues.append(*anIt);
510 if (aValidatedValues.size() != theValues.size()) {
512 theValues = aValidatedValues;