1 // Copyright (C) 2014-20xx CEA/DEN, EDF R&D
\r
3 #include <ModuleBase_WidgetValidated.h>
\r
4 #include <ModuleBase_FilterFactory.h>
\r
5 #include <ModuleBase_IViewer.h>
\r
6 #include <ModuleBase_ISelection.h>
\r
7 #include <ModuleBase_WidgetSelectorStore.h>
\r
8 #include <ModuleBase_ViewerPrs.h>
\r
10 #include <Events_InfoMessage.h>
\r
12 #include <ModelAPI_Session.h>
\r
13 #include <ModelAPI_Validator.h>
\r
14 #include <ModelAPI_AttributeValidator.h>
\r
15 #include <ModelAPI_Events.h>
\r
16 #include <ModelAPI_ResultCompSolid.h>
\r
17 #include <ModelAPI_Tools.h>
\r
19 #include <SelectMgr_ListIteratorOfListOfFilter.hxx>
\r
20 #include <SelectMgr_EntityOwner.hxx>
\r
21 #include <StdSelect_BRepOwner.hxx>
\r
23 #include <Events_Loop.h>
\r
27 //#define DEBUG_VALID_STATE
\r
29 ModuleBase_WidgetValidated::ModuleBase_WidgetValidated(QWidget* theParent,
\r
30 ModuleBase_IWorkshop* theWorkshop,
\r
31 const Config_WidgetAPI* theData)
\r
32 : ModuleBase_ModelWidget(theParent, theData),
\r
33 myWorkshop(theWorkshop), myIsInValidate(false)
\r
35 myAttributeStore = new ModuleBase_WidgetSelectorStore();
\r
38 ModuleBase_WidgetValidated::~ModuleBase_WidgetValidated()
\r
40 delete myAttributeStore;
\r
43 //********************************************************************
\r
44 ObjectPtr ModuleBase_WidgetValidated::findPresentedObject(const AISObjectPtr& theAIS) const
\r
46 return myPresentedObject;
\r
49 //********************************************************************
\r
50 void ModuleBase_WidgetValidated::clearValidatedCash()
\r
52 #ifdef DEBUG_VALID_STATE
\r
53 qDebug("clearValidatedCash");
\r
56 myInvalidPrs.Clear();
\r
59 //********************************************************************
\r
60 void ModuleBase_WidgetValidated::storeAttributeValue(const AttributePtr& theAttribute)
\r
62 myIsInValidate = true;
\r
63 myAttributeStore->storeAttributeValue(theAttribute, myWorkshop);
\r
66 //********************************************************************
\r
67 void ModuleBase_WidgetValidated::restoreAttributeValue(const AttributePtr& theAttribute,
\r
68 const bool theValid)
\r
70 myIsInValidate = false;
\r
71 myAttributeStore->restoreAttributeValue(theAttribute, myWorkshop);
\r
74 //********************************************************************
\r
75 bool ModuleBase_WidgetValidated::isValidInFilters(const ModuleBase_ViewerPrsPtr& thePrs)
\r
78 Handle(SelectMgr_EntityOwner) anOwner = thePrs->owner();
\r
80 // if an owner is null, the selection happens in the Object browser.
\r
81 // creates a selection owner on the base of object shape and the object AIS object
\r
82 if (anOwner.IsNull() && thePrs->owner().IsNull() && thePrs->object().get()) {
\r
83 ResultPtr aResult = myWorkshop->selection()->getResult(thePrs);
\r
84 GeomShapePtr aShape = aResult.get() ? aResult->shape() : GeomShapePtr();
\r
85 // some results have no shape, e.g. the parameter one. So, they should not be validated
\r
87 const TopoDS_Shape aTDShape = aShape->impl<TopoDS_Shape>();
\r
88 Handle(AIS_InteractiveObject) anIO = myWorkshop->selection()->getIO(thePrs);
\r
89 anOwner = new StdSelect_BRepOwner(aTDShape, anIO);
\r
90 myPresentedObject = aResult;
\r
93 aValid = false; // only results with a shape can be filtered
\r
95 // checks the owner by the AIS context activated filters
\r
96 if (!anOwner.IsNull()) {
\r
97 // the widget validator filter should be active, but during check by preselection
\r
98 // it is not yet activated, so we need to activate/deactivate it manually
\r
99 bool isActivated = isFilterActivated();
\r
101 activateFilters(true);
\r
103 Handle(AIS_InteractiveContext) aContext = myWorkshop->viewer()->AISContext();
\r
104 if (!aContext.IsNull()) {
\r
105 const SelectMgr_ListOfFilter& aFilters = aContext->Filters();
\r
106 SelectMgr_ListIteratorOfListOfFilter anIt(aFilters);
\r
107 for (; anIt.More() && aValid; anIt.Next()) {
\r
108 Handle(SelectMgr_Filter) aFilter = anIt.Value();
\r
109 aValid = aFilter->IsOk(anOwner);
\r
113 activateFilters(false);
\r
116 // removes created owner
\r
117 if (!anOwner.IsNull() && anOwner != thePrs->owner()) {
\r
119 myPresentedObject = ObjectPtr();
\r
124 //********************************************************************
\r
125 AttributePtr ModuleBase_WidgetValidated::attribute() const
\r
127 return myFeature->attribute(attributeID());
\r
130 //********************************************************************
\r
131 bool ModuleBase_WidgetValidated::isValidSelection(const ModuleBase_ViewerPrsPtr& theValue)
\r
133 bool aValid = false;
\r
134 if (getValidState(theValue, aValid)) {
\r
137 aValid = isValidSelectionCustom(theValue);
\r
139 aValid = isValidSelectionForAttribute(theValue, attribute());
\r
141 storeValidState(theValue, aValid);
\r
145 //********************************************************************
\r
146 bool ModuleBase_WidgetValidated::isValidSelectionForAttribute(const ModuleBase_ViewerPrsPtr& theValue,
\r
147 const AttributePtr& theAttribute)
\r
149 bool aValid = false;
\r
151 // stores the current values of the widget attribute
\r
152 bool isFlushesActived, isAttributeSetInitializedBlocked;
\r
154 blockAttribute(theAttribute, true, isFlushesActived, isAttributeSetInitializedBlocked);
\r
156 storeAttributeValue(theAttribute);
\r
158 // saves the owner value to the widget attribute
\r
159 aValid = setSelectionCustom(theValue);
\r
161 // checks the attribute validity
\r
162 aValid = isValidAttribute(theAttribute);
\r
164 // restores the current values of the widget attribute
\r
165 restoreAttributeValue(theAttribute, aValid);
\r
167 blockAttribute(theAttribute, false, isFlushesActived, isAttributeSetInitializedBlocked);
\r
168 /// NDS: The following rows are commented for issue #1452 (to be removed after debug)
\r
169 /// This is not correct to perform it here because it might cause update selection and
\r
170 /// the selection mechanizm will be circled: use the scenario of the bug with preselected point.
\r
171 // In particular case the results are deleted and called as redisplayed inside of this
\r
172 // highlight-selection, to they must be flushed as soon as possible.
\r
173 // Example: selection of group-vertices subshapes with shift pressend on body. Without
\r
174 // these 4 lines below the application crashes because of left presentations on
\r
175 // removed results still in the viewer.
\r
176 /*static Events_ID aDeletedEvent = Events_Loop::eventByName(EVENT_OBJECT_DELETED);
\r
177 static Events_ID aRedispEvent = Events_Loop::eventByName(EVENT_OBJECT_TO_REDISPLAY);
\r
178 Events_Loop::loop()->flush(aDeletedEvent);
\r
179 Events_Loop::loop()->flush(aRedispEvent);
\r
184 //********************************************************************
\r
185 bool ModuleBase_WidgetValidated::isValidSelectionCustom(const ModuleBase_ViewerPrsPtr& thePrs)
\r
190 //********************************************************************
\r
191 bool ModuleBase_WidgetValidated::isValidAttribute(const AttributePtr& theAttribute) const
\r
193 SessionPtr aMgr = ModelAPI_Session::get();
\r
194 ModelAPI_ValidatorsFactory* aFactory = aMgr->validators();
\r
195 std::string aValidatorID;
\r
196 Events_InfoMessage anError;
\r
197 return aFactory->validate(theAttribute, aValidatorID, anError);
\r
200 bool ModuleBase_WidgetValidated::isFilterActivated() const
\r
202 bool isActivated = false;
\r
204 Handle(SelectMgr_Filter) aSelFilter = myWorkshop->validatorFilter();
\r
205 ModuleBase_IViewer* aViewer = myWorkshop->viewer();
\r
207 return aViewer->hasSelectionFilter(aSelFilter);
\r
210 bool ModuleBase_WidgetValidated::activateFilters(const bool toActivate)
\r
212 ModuleBase_IViewer* aViewer = myWorkshop->viewer();
\r
214 Handle(SelectMgr_Filter) aSelFilter = myWorkshop->validatorFilter();
\r
215 bool aHasSelectionFilter = aViewer->hasSelectionFilter(aSelFilter);
\r
218 aViewer->addSelectionFilter(aSelFilter);
\r
220 aViewer->removeSelectionFilter(aSelFilter);
\r
221 clearValidatedCash();
\r
224 return aHasSelectionFilter;
\r
227 //********************************************************************
\r
228 void ModuleBase_WidgetValidated::blockAttribute(const AttributePtr& theAttribute,
\r
229 const bool& theToBlock,
\r
230 bool& isFlushesActived,
\r
231 bool& isAttributeSetInitializedBlocked)
\r
233 Events_Loop* aLoop = Events_Loop::loop();
\r
234 DataPtr aData = myFeature->data();
\r
236 // blocks the flush signals to avoid the temporary objects visualization in the viewer
\r
237 // they should not be shown in order to do not lose highlight by erasing them
\r
238 isFlushesActived = aLoop->activateFlushes(false);
\r
240 aData->blockSendAttributeUpdated(true);
\r
241 isAttributeSetInitializedBlocked = theAttribute->blockSetInitialized(true);
\r
244 aData->blockSendAttributeUpdated(false, false);
\r
245 theAttribute->blockSetInitialized(isAttributeSetInitializedBlocked);
\r
246 aLoop->activateFlushes(isFlushesActived);
\r
250 //********************************************************************
\r
251 void ModuleBase_WidgetValidated::storeValidState(const ModuleBase_ViewerPrsPtr& theValue, const bool theValid)
\r
253 GeomShapePtr aShape = theValue.get() ? theValue->shape() : GeomShapePtr();
\r
254 if (aShape.get()) {
\r
256 const TopoDS_Shape& aTDShape = aShape->impl<TopoDS_Shape>();
\r
257 bool aValidPrsContains = myValidPrs.IsBound(aTDShape) &&
\r
258 theValue.get()->isEqual(myValidPrs.Find(aTDShape).get());
\r
259 if (!aValidPrsContains) {
\r
260 #ifdef LIST_OF_VALID_PRS
\r
261 myValidPrs.append(theValue);
\r
263 myValidPrs.Bind(aTDShape, theValue);
\r
265 // the commented code will be useful when the valid state of the presentation
\r
266 // will be changable between activate/deactivate. Currently it does not happen.
\r
267 //if (anInvalidPrs)
\r
268 // myInvalidPrs.removeOne(theValue);
\r
271 else { // !theValid
\r
272 if (aShape.get()) {
\r
273 const TopoDS_Shape& aTDShape = aShape->impl<TopoDS_Shape>();
\r
274 bool anIValidPrsContains = myInvalidPrs.IsBound(aTDShape) &&
\r
275 theValue.get()->isEqual(myInvalidPrs.Find(aTDShape).get());
\r
276 if (!anIValidPrsContains) {
\r
277 #ifdef LIST_OF_VALID_PRS
\r
278 myInvalidPrs.append(theValue);
\r
280 myInvalidPrs.Bind(aTDShape, theValue);
\r
283 // myValidPrs.removeOne(theValue);
\r
288 #ifdef DEBUG_VALID_STATE
\r
289 qDebug(QString("storeValidState: myValidPrs.size() = %1, myInvalidPrs.size() = %2").arg(myValidPrs.count())
\r
290 .arg(myInvalidPrs.count()).toStdString().c_str());
\r
294 //********************************************************************
\r
295 bool ModuleBase_WidgetValidated::getValidState(const ModuleBase_ViewerPrsPtr& theValue, bool& theValid)
\r
297 if (!theValue.get())
\r
300 #ifdef LIST_OF_VALID_PRS
\r
301 bool aValidPrsContains = myValidPrs.contains(theValue);
\r
302 bool anInvalidPrsContains = myInvalidPrs.contains(theValue);
\r
304 GeomShapePtr aShape = theValue.get() ? theValue->shape() : GeomShapePtr();
\r
308 const TopoDS_Shape& aTDShape = aShape->impl<TopoDS_Shape>();
\r
309 bool aValidPrsContains = myValidPrs.IsBound(aTDShape) &&
\r
310 theValue.get()->isEqual(myValidPrs.Find(aTDShape).get());
\r
312 bool anInvalidPrsContains = myInvalidPrs.IsBound(aTDShape) &&
\r
313 theValue.get()->isEqual(myInvalidPrs.Find(aTDShape).get());
\r
315 bool aValidPrsContains = false, anInvalidPrsContains = false;
\r
316 GeomShapePtr aShape = theValue.get() ? theValue->shape() : GeomShapePtr();
\r
317 if (aShape.get()) {
\r
318 aValidPrsContains = myValidPrs.contains(aShape);
\r
319 anInvalidPrsContains = myInvalidPrs.contains(aShape);
\r
321 if (aValidPrsContains)
\r
322 aValidPrsContains = theValue == myValidPrs[aShape];
\r
324 anInvalidPrsContains = theValue == myInvalidPrs[aShape];*/
\r
327 if (aValidPrsContains)
\r
329 else if (anInvalidPrsContains)
\r
332 return aValidPrsContains || anInvalidPrsContains;
\r
335 //********************************************************************
\r
336 QList<ModuleBase_ViewerPrsPtr> ModuleBase_WidgetValidated::getFilteredSelected()
\r
338 QList<ModuleBase_ViewerPrsPtr> aSelected = myWorkshop->selection()->getSelected(
\r
339 ModuleBase_ISelection::Viewer);
\r
341 QList<ModuleBase_ViewerPrsPtr> anOBSelected = myWorkshop->selection()->getSelected(
\r
342 ModuleBase_ISelection::Browser);
\r
343 // filter the OB presentations
\r
344 filterPresentations(anOBSelected);
\r
345 if (!anOBSelected.isEmpty())
\r
346 ModuleBase_ISelection::appendSelected(anOBSelected, aSelected);
\r
348 filterCompSolids(aSelected);
\r
353 //********************************************************************
\r
354 void ModuleBase_WidgetValidated::filterPresentations(QList<ModuleBase_ViewerPrsPtr>& theValues)
\r
356 QList<ModuleBase_ViewerPrsPtr> aValidatedValues;
\r
358 QList<ModuleBase_ViewerPrsPtr>::const_iterator anIt = theValues.begin(), aLast = theValues.end();
\r
359 bool isDone = false;
\r
360 for (; anIt != aLast; anIt++) {
\r
361 if (isValidInFilters(*anIt))
\r
362 aValidatedValues.append(*anIt);
\r
364 if (aValidatedValues.size() != theValues.size()) {
\r
366 theValues = aValidatedValues;
\r
370 //********************************************************************
\r
371 void ModuleBase_WidgetValidated::filterCompSolids(QList<ModuleBase_ViewerPrsPtr>& theValues)
\r
373 std::set<ResultCompSolidPtr> aCompSolids;
\r
374 QList<ModuleBase_ViewerPrsPtr> aValidatedValues;
\r
376 // Collect compsolids.
\r
377 QList<ModuleBase_ViewerPrsPtr>::const_iterator anIt = theValues.begin(), aLast = theValues.end();
\r
378 for (; anIt != aLast; anIt++) {
\r
379 const ModuleBase_ViewerPrsPtr& aViewerPrs = *anIt;
\r
380 ObjectPtr anObject = aViewerPrs->object();
\r
381 ResultCompSolidPtr aResultCompSolid = std::dynamic_pointer_cast<ModelAPI_ResultCompSolid>(anObject);
\r
382 if(aResultCompSolid.get()) {
\r
383 aCompSolids.insert(aResultCompSolid);
\r
387 // Filter sub-solids of compsolids.
\r
388 anIt = theValues.begin();
\r
389 for (; anIt != aLast; anIt++) {
\r
390 const ModuleBase_ViewerPrsPtr& aViewerPrs = *anIt;
\r
391 ObjectPtr anObject = aViewerPrs->object();
\r
392 ResultPtr aResult = std::dynamic_pointer_cast<ModelAPI_Result>(anObject);
\r
393 ResultCompSolidPtr aResCompSolidPtr = ModelAPI_Tools::compSolidOwner(aResult);
\r
394 if(aResCompSolidPtr.get() && (aCompSolids.find(aResCompSolidPtr) != aCompSolids.end())) {
\r
395 // Skip sub-solid of compsolid.
\r
398 aValidatedValues.append(*anIt);
\r
402 if (aValidatedValues.size() != theValues.size()) {
\r
404 theValues = aValidatedValues;
\r