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 <ModelAPI_Session.h>
11 #include <ModelAPI_Validator.h>
12 #include <ModelAPI_AttributeValidator.h>
13 #include <ModelAPI_Events.h>
14 #include <ModelAPI_ResultCompSolid.h>
15 #include <ModelAPI_Tools.h>
17 #include <SelectMgr_ListIteratorOfListOfFilter.hxx>
18 #include <SelectMgr_EntityOwner.hxx>
19 #include <StdSelect_BRepOwner.hxx>
21 #include <Events_Loop.h>
25 //#define DEBUG_VALID_STATE
27 ModuleBase_WidgetValidated::ModuleBase_WidgetValidated(QWidget* theParent,
28 ModuleBase_IWorkshop* theWorkshop,
29 const Config_WidgetAPI* theData)
30 : ModuleBase_ModelWidget(theParent, theData),
31 myWorkshop(theWorkshop), myIsInValidate(false)
33 myAttributeStore = new ModuleBase_WidgetSelectorStore();
36 ModuleBase_WidgetValidated::~ModuleBase_WidgetValidated()
38 delete myAttributeStore;
41 //********************************************************************
42 ObjectPtr ModuleBase_WidgetValidated::findPresentedObject(const AISObjectPtr& theAIS) const
44 return myPresentedObject;
47 //********************************************************************
48 void ModuleBase_WidgetValidated::clearValidatedCash()
50 #ifdef DEBUG_VALID_STATE
51 qDebug("clearValidatedCash");
57 //********************************************************************
58 void ModuleBase_WidgetValidated::storeAttributeValue(const AttributePtr& theAttribute)
60 myIsInValidate = true;
61 myAttributeStore->storeAttributeValue(theAttribute, myWorkshop);
64 //********************************************************************
65 void ModuleBase_WidgetValidated::restoreAttributeValue(const AttributePtr& theAttribute,
68 myIsInValidate = false;
69 myAttributeStore->restoreAttributeValue(theAttribute, myWorkshop);
72 //********************************************************************
73 bool ModuleBase_WidgetValidated::isValidInFilters(const ModuleBase_ViewerPrsPtr& thePrs)
76 Handle(SelectMgr_EntityOwner) anOwner = thePrs->owner();
78 // if an owner is null, the selection happens in the Object browser.
79 // creates a selection owner on the base of object shape and the object AIS object
80 if (anOwner.IsNull() && thePrs->owner().IsNull() && thePrs->object().get()) {
81 ResultPtr aResult = myWorkshop->selection()->getResult(thePrs);
82 GeomShapePtr aShape = aResult.get() ? aResult->shape() : GeomShapePtr();
83 // some results have no shape, e.g. the parameter one. So, they should not be validated
85 const TopoDS_Shape aTDShape = aShape->impl<TopoDS_Shape>();
86 Handle(AIS_InteractiveObject) anIO = myWorkshop->selection()->getIO(thePrs);
87 anOwner = new StdSelect_BRepOwner(aTDShape, anIO);
88 myPresentedObject = aResult;
91 aValid = false; // only results with a shape can be filtered
93 // checks the owner by the AIS context activated filters
94 if (!anOwner.IsNull()) {
95 // the widget validator filter should be active, but during check by preselection
96 // it is not yet activated, so we need to activate/deactivate it manually
97 bool isActivated = isFilterActivated();
99 activateFilters(true);
101 Handle(AIS_InteractiveContext) aContext = myWorkshop->viewer()->AISContext();
102 if (!aContext.IsNull()) {
103 const SelectMgr_ListOfFilter& aFilters = aContext->Filters();
104 SelectMgr_ListIteratorOfListOfFilter anIt(aFilters);
105 for (; anIt.More() && aValid; anIt.Next()) {
106 Handle(SelectMgr_Filter) aFilter = anIt.Value();
107 aValid = aFilter->IsOk(anOwner);
111 activateFilters(false);
114 // removes created owner
115 if (!anOwner.IsNull() && anOwner != thePrs->owner()) {
117 myPresentedObject = ObjectPtr();
122 //********************************************************************
123 AttributePtr ModuleBase_WidgetValidated::attribute() const
125 return myFeature->attribute(attributeID());
128 //********************************************************************
129 bool ModuleBase_WidgetValidated::isValidSelection(const ModuleBase_ViewerPrsPtr& theValue)
132 if (getValidState(theValue, aValid)) {
135 aValid = isValidSelectionCustom(theValue);
137 aValid = isValidSelectionForAttribute(theValue, attribute());
139 storeValidState(theValue, aValid);
143 //********************************************************************
144 bool ModuleBase_WidgetValidated::isValidSelectionForAttribute(const ModuleBase_ViewerPrsPtr& theValue,
145 const AttributePtr& theAttribute)
149 // stores the current values of the widget attribute
150 bool isFlushesActived, isAttributeSetInitializedBlocked;
152 blockAttribute(theAttribute, true, isFlushesActived, isAttributeSetInitializedBlocked);
154 storeAttributeValue(theAttribute);
156 // saves the owner value to the widget attribute
157 aValid = setSelectionCustom(theValue);
159 // checks the attribute validity
160 aValid = isValidAttribute(theAttribute);
162 // restores the current values of the widget attribute
163 restoreAttributeValue(theAttribute, aValid);
165 blockAttribute(theAttribute, false, isFlushesActived, isAttributeSetInitializedBlocked);
166 /// NDS: The following rows are commented for issue #1452 (to be removed after debug)
167 /// This is not correct to perform it here because it might cause update selection and
168 /// the selection mechanizm will be circled: use the scenario of the bug with preselected point.
169 // In particular case the results are deleted and called as redisplayed inside of this
170 // highlight-selection, to they must be flushed as soon as possible.
171 // Example: selection of group-vertices subshapes with shift pressend on body. Without
172 // these 4 lines below the application crashes because of left presentations on
173 // removed results still in the viewer.
174 /*static Events_ID aDeletedEvent = Events_Loop::eventByName(EVENT_OBJECT_DELETED);
175 static Events_ID aRedispEvent = Events_Loop::eventByName(EVENT_OBJECT_TO_REDISPLAY);
176 Events_Loop::loop()->flush(aDeletedEvent);
177 Events_Loop::loop()->flush(aRedispEvent);
182 //********************************************************************
183 bool ModuleBase_WidgetValidated::isValidSelectionCustom(const ModuleBase_ViewerPrsPtr& thePrs)
188 //********************************************************************
189 bool ModuleBase_WidgetValidated::isValidAttribute(const AttributePtr& theAttribute) const
191 SessionPtr aMgr = ModelAPI_Session::get();
192 ModelAPI_ValidatorsFactory* aFactory = aMgr->validators();
193 std::string aValidatorID, anError;
194 return aFactory->validate(theAttribute, aValidatorID, anError);
197 bool ModuleBase_WidgetValidated::isFilterActivated() const
199 bool isActivated = false;
201 Handle(SelectMgr_Filter) aSelFilter = myWorkshop->validatorFilter();
202 ModuleBase_IViewer* aViewer = myWorkshop->viewer();
204 return aViewer->hasSelectionFilter(aSelFilter);
207 bool ModuleBase_WidgetValidated::activateFilters(const bool toActivate)
209 ModuleBase_IViewer* aViewer = myWorkshop->viewer();
211 Handle(SelectMgr_Filter) aSelFilter = myWorkshop->validatorFilter();
212 bool aHasSelectionFilter = aViewer->hasSelectionFilter(aSelFilter);
215 aViewer->addSelectionFilter(aSelFilter);
217 aViewer->removeSelectionFilter(aSelFilter);
218 clearValidatedCash();
221 return aHasSelectionFilter;
224 //********************************************************************
225 void ModuleBase_WidgetValidated::blockAttribute(const AttributePtr& theAttribute,
226 const bool& theToBlock,
227 bool& isFlushesActived,
228 bool& isAttributeSetInitializedBlocked)
230 Events_Loop* aLoop = Events_Loop::loop();
231 DataPtr aData = myFeature->data();
233 // blocks the flush signals to avoid the temporary objects visualization in the viewer
234 // they should not be shown in order to do not lose highlight by erasing them
235 isFlushesActived = aLoop->activateFlushes(false);
237 aData->blockSendAttributeUpdated(true);
238 isAttributeSetInitializedBlocked = theAttribute->blockSetInitialized(true);
241 aData->blockSendAttributeUpdated(false, false);
242 theAttribute->blockSetInitialized(isAttributeSetInitializedBlocked);
243 aLoop->activateFlushes(isFlushesActived);
247 //********************************************************************
248 void ModuleBase_WidgetValidated::storeValidState(const ModuleBase_ViewerPrsPtr& theValue, const bool theValid)
250 GeomShapePtr aShape = theValue.get() ? theValue->shape() : GeomShapePtr();
253 const TopoDS_Shape& aTDShape = aShape->impl<TopoDS_Shape>();
254 bool aValidPrsContains = myValidPrs.IsBound(aTDShape) &&
255 theValue.get()->isEqual(myValidPrs.Find(aTDShape).get());
256 if (!aValidPrsContains) {
257 #ifdef LIST_OF_VALID_PRS
258 myValidPrs.append(theValue);
260 myValidPrs.Bind(aTDShape, theValue);
262 // the commented code will be useful when the valid state of the presentation
263 // will be changable between activate/deactivate. Currently it does not happen.
265 // myInvalidPrs.removeOne(theValue);
270 const TopoDS_Shape& aTDShape = aShape->impl<TopoDS_Shape>();
271 bool anIValidPrsContains = myInvalidPrs.IsBound(aTDShape) &&
272 theValue.get()->isEqual(myInvalidPrs.Find(aTDShape).get());
273 if (!anIValidPrsContains) {
274 #ifdef LIST_OF_VALID_PRS
275 myInvalidPrs.append(theValue);
277 myInvalidPrs.Bind(aTDShape, theValue);
280 // myValidPrs.removeOne(theValue);
285 #ifdef DEBUG_VALID_STATE
286 qDebug(QString("storeValidState: myValidPrs.size() = %1, myInvalidPrs.size() = %2").arg(myValidPrs.count())
287 .arg(myInvalidPrs.count()).toStdString().c_str());
291 //********************************************************************
292 bool ModuleBase_WidgetValidated::getValidState(const ModuleBase_ViewerPrsPtr& theValue, bool& theValid)
297 #ifdef LIST_OF_VALID_PRS
298 bool aValidPrsContains = myValidPrs.contains(theValue);
299 bool anInvalidPrsContains = myInvalidPrs.contains(theValue);
301 GeomShapePtr aShape = theValue.get() ? theValue->shape() : GeomShapePtr();
305 const TopoDS_Shape& aTDShape = aShape->impl<TopoDS_Shape>();
306 bool aValidPrsContains = myValidPrs.IsBound(aTDShape) &&
307 theValue.get()->isEqual(myValidPrs.Find(aTDShape).get());
309 bool anInvalidPrsContains = myInvalidPrs.IsBound(aTDShape) &&
310 theValue.get()->isEqual(myInvalidPrs.Find(aTDShape).get());
312 bool aValidPrsContains = false, anInvalidPrsContains = false;
313 GeomShapePtr aShape = theValue.get() ? theValue->shape() : GeomShapePtr();
315 aValidPrsContains = myValidPrs.contains(aShape);
316 anInvalidPrsContains = myInvalidPrs.contains(aShape);
318 if (aValidPrsContains)
319 aValidPrsContains = theValue == myValidPrs[aShape];
321 anInvalidPrsContains = theValue == myInvalidPrs[aShape];*/
324 if (aValidPrsContains)
326 else if (anInvalidPrsContains)
329 return aValidPrsContains || anInvalidPrsContains;
332 //********************************************************************
333 QList<ModuleBase_ViewerPrsPtr> ModuleBase_WidgetValidated::getFilteredSelected()
335 QList<ModuleBase_ViewerPrsPtr> aSelected = myWorkshop->selection()->getSelected(
336 ModuleBase_ISelection::Viewer);
338 QList<ModuleBase_ViewerPrsPtr> anOBSelected = myWorkshop->selection()->getSelected(
339 ModuleBase_ISelection::Browser);
340 // filter the OB presentations
341 filterPresentations(anOBSelected);
342 if (!anOBSelected.isEmpty())
343 ModuleBase_ISelection::appendSelected(anOBSelected, aSelected);
345 filterCompSolids(aSelected);
350 //********************************************************************
351 void ModuleBase_WidgetValidated::filterPresentations(QList<ModuleBase_ViewerPrsPtr>& theValues)
353 QList<ModuleBase_ViewerPrsPtr> aValidatedValues;
355 QList<ModuleBase_ViewerPrsPtr>::const_iterator anIt = theValues.begin(), aLast = theValues.end();
357 for (; anIt != aLast; anIt++) {
358 if (isValidInFilters(*anIt))
359 aValidatedValues.append(*anIt);
361 if (aValidatedValues.size() != theValues.size()) {
363 theValues = aValidatedValues;
367 //********************************************************************
368 void ModuleBase_WidgetValidated::filterCompSolids(QList<ModuleBase_ViewerPrsPtr>& theValues)
370 std::set<ResultCompSolidPtr> aCompSolids;
371 QList<ModuleBase_ViewerPrsPtr> aValidatedValues;
373 // Collect compsolids.
374 QList<ModuleBase_ViewerPrsPtr>::const_iterator anIt = theValues.begin(), aLast = theValues.end();
375 for (; anIt != aLast; anIt++) {
376 const ModuleBase_ViewerPrsPtr& aViewerPrs = *anIt;
377 ObjectPtr anObject = aViewerPrs->object();
378 ResultCompSolidPtr aResultCompSolid = std::dynamic_pointer_cast<ModelAPI_ResultCompSolid>(anObject);
379 if(aResultCompSolid.get()) {
380 aCompSolids.insert(aResultCompSolid);
384 // Filter sub-solids of compsolids.
385 anIt = theValues.begin();
386 for (; anIt != aLast; anIt++) {
387 const ModuleBase_ViewerPrsPtr& aViewerPrs = *anIt;
388 ObjectPtr anObject = aViewerPrs->object();
389 ResultPtr aResult = std::dynamic_pointer_cast<ModelAPI_Result>(anObject);
390 ResultCompSolidPtr aResCompSolidPtr = ModelAPI_Tools::compSolidOwner(aResult);
391 if(aResCompSolidPtr.get() && (aCompSolids.find(aResCompSolidPtr) != aCompSolids.end())) {
392 // Skip sub-solid of compsolid.
395 aValidatedValues.append(*anIt);
399 if (aValidatedValues.size() != theValues.size()) {
401 theValues = aValidatedValues;