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) const
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 Events_Loop* aLoop = Events_Loop::loop();
238 DataPtr aData = myFeature->data();
240 // blocks the flush signals to avoid the temporary objects visualization in the viewer
241 // they should not be shown in order to do not lose highlight by erasing them
242 isFlushesActived = aLoop->activateFlushes(false);
244 isAttributeSendUpdatedBlocked = aData->blockSendAttributeUpdated(true);
245 isAttributeSetInitializedBlocked = theAttribute->blockSetInitialized(true);
248 aData->blockSendAttributeUpdated(isAttributeSendUpdatedBlocked, false);
249 theAttribute->blockSetInitialized(isAttributeSetInitializedBlocked);
250 aLoop->activateFlushes(isFlushesActived);
254 //********************************************************************
255 void ModuleBase_WidgetValidated::storeValidState(const ModuleBase_ViewerPrsPtr& theValue,
258 GeomShapePtr aShape = theValue.get() ? theValue->shape() : GeomShapePtr();
261 const TopoDS_Shape& aTDShape = aShape->impl<TopoDS_Shape>();
262 bool aValidPrsContains = myValidPrs.IsBound(aTDShape) &&
263 theValue.get()->isEqual(myValidPrs.Find(aTDShape).get());
264 if (!aValidPrsContains) {
265 #ifdef LIST_OF_VALID_PRS
266 myValidPrs.append(theValue);
268 myValidPrs.Bind(aTDShape, theValue);
270 // the commented code will be useful when the valid state of the presentation
271 // will be changable between activate/deactivate. Currently it does not happen.
273 // myInvalidPrs.removeOne(theValue);
278 const TopoDS_Shape& aTDShape = aShape->impl<TopoDS_Shape>();
279 bool anIValidPrsContains = myInvalidPrs.IsBound(aTDShape) &&
280 theValue.get()->isEqual(myInvalidPrs.Find(aTDShape).get());
281 if (!anIValidPrsContains) {
282 #ifdef LIST_OF_VALID_PRS
283 myInvalidPrs.append(theValue);
285 myInvalidPrs.Bind(aTDShape, theValue);
288 // myValidPrs.removeOne(theValue);
293 #ifdef DEBUG_VALID_STATE
294 qDebug(QString("storeValidState: myValidPrs.size() = %1, myInvalidPrs.size() = %2")
295 .arg(myValidPrs.count())
296 .arg(myInvalidPrs.count()).toStdString().c_str());
300 //********************************************************************
301 bool ModuleBase_WidgetValidated::getValidState(const ModuleBase_ViewerPrsPtr& theValue,
307 #ifdef LIST_OF_VALID_PRS
308 bool aValidPrsContains = myValidPrs.contains(theValue);
309 bool anInvalidPrsContains = myInvalidPrs.contains(theValue);
311 GeomShapePtr aShape = theValue.get() ? theValue->shape() : GeomShapePtr();
315 const TopoDS_Shape& aTDShape = aShape->impl<TopoDS_Shape>();
316 bool aValidPrsContains = myValidPrs.IsBound(aTDShape) &&
317 theValue.get()->isEqual(myValidPrs.Find(aTDShape).get());
319 bool anInvalidPrsContains = myInvalidPrs.IsBound(aTDShape) &&
320 theValue.get()->isEqual(myInvalidPrs.Find(aTDShape).get());
322 bool aValidPrsContains = false, anInvalidPrsContains = false;
323 GeomShapePtr aShape = theValue.get() ? theValue->shape() : GeomShapePtr();
325 aValidPrsContains = myValidPrs.contains(aShape);
326 anInvalidPrsContains = myInvalidPrs.contains(aShape);
328 if (aValidPrsContains)
329 aValidPrsContains = theValue == myValidPrs[aShape];
331 anInvalidPrsContains = theValue == myInvalidPrs[aShape];*/
334 if (aValidPrsContains)
336 else if (anInvalidPrsContains)
339 return aValidPrsContains || anInvalidPrsContains;
342 //********************************************************************
343 QList<ModuleBase_ViewerPrsPtr> ModuleBase_WidgetValidated::getFilteredSelected()
345 QList<ModuleBase_ViewerPrsPtr> aSelected = myWorkshop->selection()->getSelected(
346 ModuleBase_ISelection::Viewer);
348 QList<ModuleBase_ViewerPrsPtr> anOBSelected = myWorkshop->selection()->getSelected(
349 ModuleBase_ISelection::Browser);
350 // filter the OB presentations
351 filterPresentations(anOBSelected);
352 if (!anOBSelected.isEmpty())
353 ModuleBase_ISelection::appendSelected(anOBSelected, aSelected);
355 filterCompSolids(aSelected);
360 //********************************************************************
361 void ModuleBase_WidgetValidated::filterPresentations(QList<ModuleBase_ViewerPrsPtr>& theValues)
363 QList<ModuleBase_ViewerPrsPtr> aValidatedValues;
365 QList<ModuleBase_ViewerPrsPtr>::const_iterator anIt = theValues.begin(), aLast = theValues.end();
367 for (; anIt != aLast; anIt++) {
368 if (isValidInFilters(*anIt))
369 aValidatedValues.append(*anIt);
371 if (aValidatedValues.size() != theValues.size()) {
373 theValues = aValidatedValues;
377 //********************************************************************
378 void ModuleBase_WidgetValidated::filterCompSolids(QList<ModuleBase_ViewerPrsPtr>& theValues)
380 std::set<ResultCompSolidPtr> aCompSolids;
381 QList<ModuleBase_ViewerPrsPtr> aValidatedValues;
383 // Collect compsolids.
384 QList<ModuleBase_ViewerPrsPtr>::const_iterator anIt = theValues.begin(), aLast = theValues.end();
385 for (; anIt != aLast; anIt++) {
386 const ModuleBase_ViewerPrsPtr& aViewerPrs = *anIt;
387 ObjectPtr anObject = aViewerPrs->object();
388 ResultCompSolidPtr aResultCompSolid =
389 std::dynamic_pointer_cast<ModelAPI_ResultCompSolid>(anObject);
390 if(aResultCompSolid.get()) {
391 aCompSolids.insert(aResultCompSolid);
395 // Filter sub-solids of compsolids.
396 anIt = theValues.begin();
397 for (; anIt != aLast; anIt++) {
398 const ModuleBase_ViewerPrsPtr& aViewerPrs = *anIt;
399 ObjectPtr anObject = aViewerPrs->object();
400 ResultPtr aResult = std::dynamic_pointer_cast<ModelAPI_Result>(anObject);
401 ResultCompSolidPtr aResCompSolidPtr = ModelAPI_Tools::compSolidOwner(aResult);
402 if(aResCompSolidPtr.get() && (aCompSolids.find(aResCompSolidPtr) != aCompSolids.end())) {
403 // Skip sub-solid of compsolid.
406 aValidatedValues.append(*anIt);
410 if (aValidatedValues.size() != theValues.size()) {
412 theValues = aValidatedValues;