1 // Copyright (C) 2014-20xx CEA/DEN, EDF R&D
3 #include <ModuleBase_WidgetValidated.h>
4 #include <ModuleBase_IViewer.h>
5 #include <ModuleBase_IWorkshop.h>
6 #include <ModuleBase_ISelection.h>
7 #include <ModuleBase_WidgetSelectorStore.h>
8 #include <ModuleBase_ViewerPrs.h>
10 #include <Events_InfoMessage.h>
12 #include <ModelAPI_Session.h>
13 #include <ModelAPI_Validator.h>
14 #include <ModelAPI_AttributeValidator.h>
15 #include <ModelAPI_Events.h>
16 #include <ModelAPI_ResultCompSolid.h>
17 #include <ModelAPI_Tools.h>
19 #include <SelectMgr_ListIteratorOfListOfFilter.hxx>
20 #include <SelectMgr_EntityOwner.hxx>
21 #include <StdSelect_BRepOwner.hxx>
23 #include <Events_Loop.h>
27 //#define DEBUG_VALID_STATE
29 ModuleBase_WidgetValidated::ModuleBase_WidgetValidated(QWidget* theParent,
30 ModuleBase_IWorkshop* theWorkshop,
31 const Config_WidgetAPI* theData)
32 : ModuleBase_ModelWidget(theParent, theData),
33 myWorkshop(theWorkshop), myIsInValidate(false)
35 myAttributeStore = new ModuleBase_WidgetSelectorStore();
38 ModuleBase_WidgetValidated::~ModuleBase_WidgetValidated()
40 delete myAttributeStore;
43 //********************************************************************
44 ObjectPtr ModuleBase_WidgetValidated::findPresentedObject(const AISObjectPtr& theAIS) const
46 return myPresentedObject;
49 //********************************************************************
50 void ModuleBase_WidgetValidated::clearValidatedCash()
52 #ifdef DEBUG_VALID_STATE
53 qDebug("clearValidatedCash");
59 //********************************************************************
60 void ModuleBase_WidgetValidated::storeAttributeValue(const AttributePtr& theAttribute)
62 myIsInValidate = true;
63 myAttributeStore->storeAttributeValue(theAttribute, myWorkshop);
66 //********************************************************************
67 void ModuleBase_WidgetValidated::restoreAttributeValue(const AttributePtr& theAttribute,
70 myIsInValidate = false;
71 myAttributeStore->restoreAttributeValue(theAttribute, myWorkshop);
74 //********************************************************************
75 bool ModuleBase_WidgetValidated::isValidInFilters(const ModuleBase_ViewerPrsPtr& thePrs)
78 Handle(SelectMgr_EntityOwner) anOwner = thePrs->owner();
80 // if an owner is null, the selection happens in the Object browser.
81 // creates a selection owner on the base of object shape and the object AIS object
82 if (anOwner.IsNull() && thePrs->owner().IsNull() && thePrs->object().get()) {
83 ResultPtr aResult = myWorkshop->selection()->getResult(thePrs);
84 GeomShapePtr aShape = aResult.get() ? aResult->shape() : GeomShapePtr();
85 // some results have no shape, e.g. the parameter one. So, they should not be validated
87 const TopoDS_Shape aTDShape = aShape->impl<TopoDS_Shape>();
88 Handle(AIS_InteractiveObject) anIO = myWorkshop->selection()->getIO(thePrs);
89 anOwner = new StdSelect_BRepOwner(aTDShape, anIO);
90 myPresentedObject = aResult;
93 aValid = false; // only results with a shape can be filtered
95 // checks the owner by the AIS context activated filters
96 if (!anOwner.IsNull()) {
97 // the widget validator filter should be active, but during check by preselection
98 // it is not yet activated, so we need to activate/deactivate it manually
99 bool isActivated = isFilterActivated();
101 activateFilters(true);
103 Handle(AIS_InteractiveContext) aContext = myWorkshop->viewer()->AISContext();
104 if (!aContext.IsNull()) {
105 const SelectMgr_ListOfFilter& aFilters = aContext->Filters();
106 SelectMgr_ListIteratorOfListOfFilter anIt(aFilters);
107 for (; anIt.More() && aValid; anIt.Next()) {
108 Handle(SelectMgr_Filter) aFilter = anIt.Value();
109 aValid = aFilter->IsOk(anOwner);
113 activateFilters(false);
116 // removes created owner
117 if (!anOwner.IsNull() && anOwner != thePrs->owner()) {
119 myPresentedObject = ObjectPtr();
124 //********************************************************************
125 AttributePtr ModuleBase_WidgetValidated::attribute() const
127 return myFeature->attribute(attributeID());
130 //********************************************************************
131 bool ModuleBase_WidgetValidated::isValidSelection(const ModuleBase_ViewerPrsPtr& theValue)
134 if (getValidState(theValue, aValid)) {
137 aValid = isValidSelectionCustom(theValue);
139 aValid = isValidSelectionForAttribute(theValue, attribute());
141 storeValidState(theValue, aValid);
145 //********************************************************************
146 bool ModuleBase_WidgetValidated::isValidSelectionForAttribute(
147 const ModuleBase_ViewerPrsPtr& theValue,
148 const AttributePtr& theAttribute)
152 // stores the current values of the widget attribute
153 bool isFlushesActived, isAttributeSetInitializedBlocked, isAttributeSendUpdatedBlocked;
155 blockAttribute(theAttribute, true, isFlushesActived, isAttributeSetInitializedBlocked,
156 isAttributeSendUpdatedBlocked);
158 storeAttributeValue(theAttribute);
160 // saves the owner value to the widget attribute
161 aValid = setSelectionCustom(theValue);
163 // checks the attribute validity
164 aValid = isValidAttribute(theAttribute);
166 // restores the current values of the widget attribute
167 restoreAttributeValue(theAttribute, aValid);
169 blockAttribute(theAttribute, false, isFlushesActived, isAttributeSetInitializedBlocked,
170 isAttributeSendUpdatedBlocked);
171 /// NDS: The following rows are commented for issue #1452 (to be removed after debug)
172 /// This is not correct to perform it here because it might cause update selection and
173 /// the selection mechanizm will be circled: use the scenario of the bug with preselected point.
174 // In particular case the results are deleted and called as redisplayed inside of this
175 // highlight-selection, to they must be flushed as soon as possible.
176 // Example: selection of group-vertices subshapes with shift pressend on body. Without
177 // these 4 lines below the application crashes because of left presentations on
178 // removed results still in the viewer.
179 /*static Events_ID aDeletedEvent = Events_Loop::eventByName(EVENT_OBJECT_DELETED);
180 static Events_ID aRedispEvent = Events_Loop::eventByName(EVENT_OBJECT_TO_REDISPLAY);
181 Events_Loop::loop()->flush(aDeletedEvent);
182 Events_Loop::loop()->flush(aRedispEvent);
187 //********************************************************************
188 bool ModuleBase_WidgetValidated::isValidSelectionCustom(const ModuleBase_ViewerPrsPtr& thePrs)
193 //********************************************************************
194 bool ModuleBase_WidgetValidated::isValidAttribute(const AttributePtr& theAttribute)
196 SessionPtr aMgr = ModelAPI_Session::get();
197 ModelAPI_ValidatorsFactory* aFactory = aMgr->validators();
198 std::string aValidatorID;
199 Events_InfoMessage anError;
200 return aFactory->validate(theAttribute, aValidatorID, anError);
203 bool ModuleBase_WidgetValidated::isFilterActivated() const
205 bool isActivated = false;
207 Handle(SelectMgr_Filter) aSelFilter = myWorkshop->validatorFilter();
208 ModuleBase_IViewer* aViewer = myWorkshop->viewer();
210 return aViewer->hasSelectionFilter(aSelFilter);
213 bool ModuleBase_WidgetValidated::activateFilters(const bool toActivate)
215 ModuleBase_IViewer* aViewer = myWorkshop->viewer();
217 Handle(SelectMgr_Filter) aSelFilter = myWorkshop->validatorFilter();
218 bool aHasSelectionFilter = aViewer->hasSelectionFilter(aSelFilter);
221 aViewer->addSelectionFilter(aSelFilter);
223 aViewer->removeSelectionFilter(aSelFilter);
224 clearValidatedCash();
227 return aHasSelectionFilter;
230 //********************************************************************
231 void ModuleBase_WidgetValidated::blockAttribute(const AttributePtr& theAttribute,
232 const bool& theToBlock,
233 bool& isFlushesActived,
234 bool& isAttributeSetInitializedBlocked,
235 bool& isAttributeSendUpdatedBlocked)
237 blockFeatureAttribute(theAttribute, myFeature, theToBlock, isFlushesActived,
238 isAttributeSetInitializedBlocked, isAttributeSendUpdatedBlocked);
241 //********************************************************************
242 void ModuleBase_WidgetValidated::blockFeatureAttribute(const AttributePtr& theAttribute,
243 const FeaturePtr& theFeature,
244 const bool& theToBlock,
245 bool& isFlushesActived,
246 bool& isAttributeSetInitializedBlocked,
247 bool& isAttributeSendUpdatedBlocked)
249 Events_Loop* aLoop = Events_Loop::loop();
250 DataPtr aData = theFeature->data();
252 // blocks the flush signals to avoid the temporary objects visualization in the viewer
253 // they should not be shown in order to do not lose highlight by erasing them
254 isFlushesActived = aLoop->activateFlushes(false);
256 isAttributeSendUpdatedBlocked = aData->blockSendAttributeUpdated(true);
257 isAttributeSetInitializedBlocked = theAttribute->blockSetInitialized(true);
260 aData->blockSendAttributeUpdated(isAttributeSendUpdatedBlocked, false);
261 theAttribute->blockSetInitialized(isAttributeSetInitializedBlocked);
262 aLoop->activateFlushes(isFlushesActived);
266 //********************************************************************
267 void ModuleBase_WidgetValidated::storeValidState(const ModuleBase_ViewerPrsPtr& theValue,
270 GeomShapePtr aShape = theValue.get() ? theValue->shape() : GeomShapePtr();
273 const TopoDS_Shape& aTDShape = aShape->impl<TopoDS_Shape>();
274 bool aValidPrsContains = myValidPrs.IsBound(aTDShape) &&
275 theValue.get()->isEqual(myValidPrs.Find(aTDShape).get());
276 if (!aValidPrsContains) {
277 #ifdef LIST_OF_VALID_PRS
278 myValidPrs.append(theValue);
280 myValidPrs.Bind(aTDShape, theValue);
282 // the commented code will be useful when the valid state of the presentation
283 // will be changable between activate/deactivate. Currently it does not happen.
285 // myInvalidPrs.removeOne(theValue);
290 const TopoDS_Shape& aTDShape = aShape->impl<TopoDS_Shape>();
291 bool anIValidPrsContains = myInvalidPrs.IsBound(aTDShape) &&
292 theValue.get()->isEqual(myInvalidPrs.Find(aTDShape).get());
293 if (!anIValidPrsContains) {
294 #ifdef LIST_OF_VALID_PRS
295 myInvalidPrs.append(theValue);
297 myInvalidPrs.Bind(aTDShape, theValue);
300 // myValidPrs.removeOne(theValue);
305 #ifdef DEBUG_VALID_STATE
306 qDebug(QString("storeValidState: myValidPrs.size() = %1, myInvalidPrs.size() = %2")
307 .arg(myValidPrs.count())
308 .arg(myInvalidPrs.count()).toStdString().c_str());
312 //********************************************************************
313 bool ModuleBase_WidgetValidated::getValidState(const ModuleBase_ViewerPrsPtr& theValue,
319 #ifdef LIST_OF_VALID_PRS
320 bool aValidPrsContains = myValidPrs.contains(theValue);
321 bool anInvalidPrsContains = myInvalidPrs.contains(theValue);
323 GeomShapePtr aShape = theValue.get() ? theValue->shape() : GeomShapePtr();
327 const TopoDS_Shape& aTDShape = aShape->impl<TopoDS_Shape>();
328 bool aValidPrsContains = myValidPrs.IsBound(aTDShape) &&
329 theValue.get()->isEqual(myValidPrs.Find(aTDShape).get());
331 bool anInvalidPrsContains = myInvalidPrs.IsBound(aTDShape) &&
332 theValue.get()->isEqual(myInvalidPrs.Find(aTDShape).get());
334 bool aValidPrsContains = false, anInvalidPrsContains = false;
335 GeomShapePtr aShape = theValue.get() ? theValue->shape() : GeomShapePtr();
337 aValidPrsContains = myValidPrs.contains(aShape);
338 anInvalidPrsContains = myInvalidPrs.contains(aShape);
340 if (aValidPrsContains)
341 aValidPrsContains = theValue == myValidPrs[aShape];
343 anInvalidPrsContains = theValue == myInvalidPrs[aShape];*/
346 if (aValidPrsContains)
348 else if (anInvalidPrsContains)
351 return aValidPrsContains || anInvalidPrsContains;
354 //********************************************************************
355 QList<ModuleBase_ViewerPrsPtr> ModuleBase_WidgetValidated::getFilteredSelected()
357 QList<ModuleBase_ViewerPrsPtr> aSelected = myWorkshop->selection()->getSelected(
358 ModuleBase_ISelection::Viewer);
360 QList<ModuleBase_ViewerPrsPtr> anOBSelected = myWorkshop->selection()->getSelected(
361 ModuleBase_ISelection::Browser);
362 // filter the OB presentations
363 filterPresentations(anOBSelected);
364 if (!anOBSelected.isEmpty())
365 ModuleBase_ISelection::appendSelected(anOBSelected, aSelected);
367 filterCompSolids(aSelected);
372 //********************************************************************
373 void ModuleBase_WidgetValidated::filterPresentations(QList<ModuleBase_ViewerPrsPtr>& theValues)
375 QList<ModuleBase_ViewerPrsPtr> aValidatedValues;
377 QList<ModuleBase_ViewerPrsPtr>::const_iterator anIt = theValues.begin(), aLast = theValues.end();
379 for (; anIt != aLast; anIt++) {
380 if (isValidInFilters(*anIt))
381 aValidatedValues.append(*anIt);
383 if (aValidatedValues.size() != theValues.size()) {
385 theValues = aValidatedValues;
389 //********************************************************************
390 void ModuleBase_WidgetValidated::filterCompSolids(QList<ModuleBase_ViewerPrsPtr>& theValues)
392 std::set<ResultCompSolidPtr> aCompSolids;
393 QList<ModuleBase_ViewerPrsPtr> aValidatedValues;
395 // Collect compsolids.
396 QList<ModuleBase_ViewerPrsPtr>::const_iterator anIt = theValues.begin(), aLast = theValues.end();
397 for (; anIt != aLast; anIt++) {
398 const ModuleBase_ViewerPrsPtr& aViewerPrs = *anIt;
399 ObjectPtr anObject = aViewerPrs->object();
400 ResultCompSolidPtr aResultCompSolid =
401 std::dynamic_pointer_cast<ModelAPI_ResultCompSolid>(anObject);
402 if(aResultCompSolid.get()) {
403 aCompSolids.insert(aResultCompSolid);
407 // Filter sub-solids of compsolids.
408 anIt = theValues.begin();
409 for (; anIt != aLast; anIt++) {
410 const ModuleBase_ViewerPrsPtr& aViewerPrs = *anIt;
411 ObjectPtr anObject = aViewerPrs->object();
412 ResultPtr aResult = std::dynamic_pointer_cast<ModelAPI_Result>(anObject);
413 ResultCompSolidPtr aResCompSolidPtr = ModelAPI_Tools::compSolidOwner(aResult);
414 if(aResCompSolidPtr.get() && (aCompSolids.find(aResCompSolidPtr) != aCompSolids.end())) {
415 // Skip sub-solid of compsolid.
418 aValidatedValues.append(*anIt);
422 if (aValidatedValues.size() != theValues.size()) {
424 theValues = aValidatedValues;