1 // Copyright (C) 2014-20xx CEA/DEN, EDF R&D
3 #include <ModuleBase_WidgetValidated.h>
4 #include <ModuleBase_FilterFactory.h>
5 #include <ModuleBase_IViewer.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(const ModuleBase_ViewerPrsPtr& theValue,
147 const AttributePtr& theAttribute)
151 // stores the current values of the widget attribute
152 bool isFlushesActived, isAttributeSetInitializedBlocked;
154 blockAttribute(theAttribute, true, isFlushesActived, isAttributeSetInitializedBlocked);
156 storeAttributeValue(theAttribute);
158 // saves the owner value to the widget attribute
159 aValid = setSelectionCustom(theValue);
161 // checks the attribute validity
162 aValid = isValidAttribute(theAttribute);
164 // restores the current values of the widget attribute
165 restoreAttributeValue(theAttribute, aValid);
167 blockAttribute(theAttribute, false, isFlushesActived, isAttributeSetInitializedBlocked);
168 /// NDS: The following rows are commented for issue #1452 (to be removed after debug)
169 /// This is not correct to perform it here because it might cause update selection and
170 /// the selection mechanizm will be circled: use the scenario of the bug with preselected point.
171 // In particular case the results are deleted and called as redisplayed inside of this
172 // highlight-selection, to they must be flushed as soon as possible.
173 // Example: selection of group-vertices subshapes with shift pressend on body. Without
174 // these 4 lines below the application crashes because of left presentations on
175 // removed results still in the viewer.
176 /*static Events_ID aDeletedEvent = Events_Loop::eventByName(EVENT_OBJECT_DELETED);
177 static Events_ID aRedispEvent = Events_Loop::eventByName(EVENT_OBJECT_TO_REDISPLAY);
178 Events_Loop::loop()->flush(aDeletedEvent);
179 Events_Loop::loop()->flush(aRedispEvent);
184 //********************************************************************
185 bool ModuleBase_WidgetValidated::isValidSelectionCustom(const ModuleBase_ViewerPrsPtr& thePrs)
190 //********************************************************************
191 bool ModuleBase_WidgetValidated::isValidAttribute(const AttributePtr& theAttribute) const
193 SessionPtr aMgr = ModelAPI_Session::get();
194 ModelAPI_ValidatorsFactory* aFactory = aMgr->validators();
195 std::string aValidatorID;
196 Events_InfoMessage anError;
197 return aFactory->validate(theAttribute, aValidatorID, anError);
200 bool ModuleBase_WidgetValidated::isFilterActivated() const
202 bool isActivated = false;
204 Handle(SelectMgr_Filter) aSelFilter = myWorkshop->validatorFilter();
205 ModuleBase_IViewer* aViewer = myWorkshop->viewer();
207 return aViewer->hasSelectionFilter(aSelFilter);
210 bool ModuleBase_WidgetValidated::activateFilters(const bool toActivate)
212 ModuleBase_IViewer* aViewer = myWorkshop->viewer();
214 Handle(SelectMgr_Filter) aSelFilter = myWorkshop->validatorFilter();
215 bool aHasSelectionFilter = aViewer->hasSelectionFilter(aSelFilter);
218 aViewer->addSelectionFilter(aSelFilter);
220 aViewer->removeSelectionFilter(aSelFilter);
221 clearValidatedCash();
224 return aHasSelectionFilter;
227 //********************************************************************
228 void ModuleBase_WidgetValidated::blockAttribute(const AttributePtr& theAttribute,
229 const bool& theToBlock,
230 bool& isFlushesActived,
231 bool& isAttributeSetInitializedBlocked)
233 Events_Loop* aLoop = Events_Loop::loop();
234 DataPtr aData = myFeature->data();
236 // blocks the flush signals to avoid the temporary objects visualization in the viewer
237 // they should not be shown in order to do not lose highlight by erasing them
238 isFlushesActived = aLoop->activateFlushes(false);
240 aData->blockSendAttributeUpdated(true);
241 isAttributeSetInitializedBlocked = theAttribute->blockSetInitialized(true);
244 aData->blockSendAttributeUpdated(false, false);
245 theAttribute->blockSetInitialized(isAttributeSetInitializedBlocked);
246 aLoop->activateFlushes(isFlushesActived);
250 //********************************************************************
251 void ModuleBase_WidgetValidated::storeValidState(const ModuleBase_ViewerPrsPtr& theValue, const bool theValid)
253 GeomShapePtr aShape = theValue.get() ? theValue->shape() : GeomShapePtr();
256 const TopoDS_Shape& aTDShape = aShape->impl<TopoDS_Shape>();
257 bool aValidPrsContains = myValidPrs.IsBound(aTDShape) &&
258 theValue.get()->isEqual(myValidPrs.Find(aTDShape).get());
259 if (!aValidPrsContains) {
260 #ifdef LIST_OF_VALID_PRS
261 myValidPrs.append(theValue);
263 myValidPrs.Bind(aTDShape, theValue);
265 // the commented code will be useful when the valid state of the presentation
266 // will be changable between activate/deactivate. Currently it does not happen.
268 // myInvalidPrs.removeOne(theValue);
273 const TopoDS_Shape& aTDShape = aShape->impl<TopoDS_Shape>();
274 bool anIValidPrsContains = myInvalidPrs.IsBound(aTDShape) &&
275 theValue.get()->isEqual(myInvalidPrs.Find(aTDShape).get());
276 if (!anIValidPrsContains) {
277 #ifdef LIST_OF_VALID_PRS
278 myInvalidPrs.append(theValue);
280 myInvalidPrs.Bind(aTDShape, theValue);
283 // myValidPrs.removeOne(theValue);
288 #ifdef DEBUG_VALID_STATE
289 qDebug(QString("storeValidState: myValidPrs.size() = %1, myInvalidPrs.size() = %2").arg(myValidPrs.count())
290 .arg(myInvalidPrs.count()).toStdString().c_str());
294 //********************************************************************
295 bool ModuleBase_WidgetValidated::getValidState(const ModuleBase_ViewerPrsPtr& theValue, bool& theValid)
300 #ifdef LIST_OF_VALID_PRS
301 bool aValidPrsContains = myValidPrs.contains(theValue);
302 bool anInvalidPrsContains = myInvalidPrs.contains(theValue);
304 GeomShapePtr aShape = theValue.get() ? theValue->shape() : GeomShapePtr();
308 const TopoDS_Shape& aTDShape = aShape->impl<TopoDS_Shape>();
309 bool aValidPrsContains = myValidPrs.IsBound(aTDShape) &&
310 theValue.get()->isEqual(myValidPrs.Find(aTDShape).get());
312 bool anInvalidPrsContains = myInvalidPrs.IsBound(aTDShape) &&
313 theValue.get()->isEqual(myInvalidPrs.Find(aTDShape).get());
315 bool aValidPrsContains = false, anInvalidPrsContains = false;
316 GeomShapePtr aShape = theValue.get() ? theValue->shape() : GeomShapePtr();
318 aValidPrsContains = myValidPrs.contains(aShape);
319 anInvalidPrsContains = myInvalidPrs.contains(aShape);
321 if (aValidPrsContains)
322 aValidPrsContains = theValue == myValidPrs[aShape];
324 anInvalidPrsContains = theValue == myInvalidPrs[aShape];*/
327 if (aValidPrsContains)
329 else if (anInvalidPrsContains)
332 return aValidPrsContains || anInvalidPrsContains;
335 //********************************************************************
336 QList<ModuleBase_ViewerPrsPtr> ModuleBase_WidgetValidated::getFilteredSelected()
338 QList<ModuleBase_ViewerPrsPtr> aSelected = myWorkshop->selection()->getSelected(
339 ModuleBase_ISelection::Viewer);
341 QList<ModuleBase_ViewerPrsPtr> anOBSelected = myWorkshop->selection()->getSelected(
342 ModuleBase_ISelection::Browser);
343 // filter the OB presentations
344 filterPresentations(anOBSelected);
345 if (!anOBSelected.isEmpty())
346 ModuleBase_ISelection::appendSelected(anOBSelected, aSelected);
348 filterCompSolids(aSelected);
353 //********************************************************************
354 void ModuleBase_WidgetValidated::filterPresentations(QList<ModuleBase_ViewerPrsPtr>& theValues)
356 QList<ModuleBase_ViewerPrsPtr> aValidatedValues;
358 QList<ModuleBase_ViewerPrsPtr>::const_iterator anIt = theValues.begin(), aLast = theValues.end();
360 for (; anIt != aLast; anIt++) {
361 if (isValidInFilters(*anIt))
362 aValidatedValues.append(*anIt);
364 if (aValidatedValues.size() != theValues.size()) {
366 theValues = aValidatedValues;
370 //********************************************************************
371 void ModuleBase_WidgetValidated::filterCompSolids(QList<ModuleBase_ViewerPrsPtr>& theValues)
373 std::set<ResultCompSolidPtr> aCompSolids;
374 QList<ModuleBase_ViewerPrsPtr> aValidatedValues;
376 // Collect compsolids.
377 QList<ModuleBase_ViewerPrsPtr>::const_iterator anIt = theValues.begin(), aLast = theValues.end();
378 for (; anIt != aLast; anIt++) {
379 const ModuleBase_ViewerPrsPtr& aViewerPrs = *anIt;
380 ObjectPtr anObject = aViewerPrs->object();
381 ResultCompSolidPtr aResultCompSolid = std::dynamic_pointer_cast<ModelAPI_ResultCompSolid>(anObject);
382 if(aResultCompSolid.get()) {
383 aCompSolids.insert(aResultCompSolid);
387 // Filter sub-solids of compsolids.
388 anIt = theValues.begin();
389 for (; anIt != aLast; anIt++) {
390 const ModuleBase_ViewerPrsPtr& aViewerPrs = *anIt;
391 ObjectPtr anObject = aViewerPrs->object();
392 ResultPtr aResult = std::dynamic_pointer_cast<ModelAPI_Result>(anObject);
393 ResultCompSolidPtr aResCompSolidPtr = ModelAPI_Tools::compSolidOwner(aResult);
394 if(aResCompSolidPtr.get() && (aCompSolids.find(aResCompSolidPtr) != aCompSolids.end())) {
395 // Skip sub-solid of compsolid.
398 aValidatedValues.append(*anIt);
402 if (aValidatedValues.size() != theValues.size()) {
404 theValues = aValidatedValues;