Salome HOME
Issue #1037 Time performance on a big model: cash in widget validated is corrected
[modules/shaper.git] / src / ModuleBase / ModuleBase_WidgetValidated.cpp
1 // Copyright (C) 2014-20xx CEA/DEN, EDF R&D
2
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>
9
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>
16
17 #include <SelectMgr_ListIteratorOfListOfFilter.hxx>
18 #include <SelectMgr_EntityOwner.hxx>
19 #include <StdSelect_BRepOwner.hxx>
20
21 #include <Events_Loop.h>
22
23 #include <QWidget>
24
25 //#define DEBUG_VALID_STATE
26
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)
32 {
33   myAttributeStore = new ModuleBase_WidgetSelectorStore();
34 }
35
36 ModuleBase_WidgetValidated::~ModuleBase_WidgetValidated()
37 {
38   delete myAttributeStore;
39 }
40
41 //********************************************************************
42 ObjectPtr ModuleBase_WidgetValidated::findPresentedObject(const AISObjectPtr& theAIS) const
43 {
44   return myPresentedObject;
45 }
46
47 //********************************************************************
48 void ModuleBase_WidgetValidated::clearValidatedCash()
49 {
50 #ifdef DEBUG_VALID_STATE
51   qDebug("clearValidatedCash");
52 #endif
53   myValidPrs.Clear();
54   myInvalidPrs.Clear();
55 }
56
57 //********************************************************************
58 void ModuleBase_WidgetValidated::storeAttributeValue(const AttributePtr& theAttribute)
59 {
60   myIsInValidate = true;
61   myAttributeStore->storeAttributeValue(theAttribute, myWorkshop);
62 }
63
64 //********************************************************************
65 void ModuleBase_WidgetValidated::restoreAttributeValue(const AttributePtr& theAttribute,
66                                                        const bool theValid)
67 {
68   myIsInValidate = false;
69   myAttributeStore->restoreAttributeValue(theAttribute, myWorkshop);
70 }
71
72 //********************************************************************
73 bool ModuleBase_WidgetValidated::isValidInFilters(const ModuleBase_ViewerPrsPtr& thePrs)
74 {
75   bool aValid = true;
76   Handle(SelectMgr_EntityOwner) anOwner = thePrs->owner();
77
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     if (aResult.get() && aResult->shape().get()) {
83       // some results have no shape, e.g. the parameter one. So, they should not be validated
84       GeomShapePtr aShape = aResult->shape();
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;
89     }
90     else
91       aValid = false; // only results with a shape can be filtered
92   }
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();
98     if (!isActivated)
99       activateFilters(true);
100
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);
108       }
109     }
110     if (!isActivated)
111       activateFilters(false);
112   }
113
114   // removes created owner
115   if (!anOwner.IsNull() && anOwner != thePrs->owner()) {
116     anOwner.Nullify();
117     myPresentedObject = ObjectPtr();
118   }
119   return aValid;
120 }
121
122 //********************************************************************
123 AttributePtr ModuleBase_WidgetValidated::attribute() const
124 {
125   return myFeature->attribute(attributeID());
126 }
127
128 //********************************************************************
129 bool ModuleBase_WidgetValidated::isValidSelection(const ModuleBase_ViewerPrsPtr& theValue)
130 {
131   bool aValid = false;
132   if (getValidState(theValue, aValid)) {
133     return aValid;
134   }
135   aValid = isValidSelectionCustom(theValue);
136   if (aValid)
137     aValid = isValidSelectionForAttribute(theValue, attribute());
138
139   storeValidState(theValue, aValid);
140   return aValid;
141 }
142
143 //********************************************************************
144 bool ModuleBase_WidgetValidated::isValidSelectionForAttribute(const ModuleBase_ViewerPrsPtr& theValue,
145                                                               const AttributePtr& theAttribute)
146 {
147   bool aValid = false;
148
149   // stores the current values of the widget attribute
150   bool isFlushesActived, isAttributeSetInitializedBlocked;
151
152   blockAttribute(theAttribute, true, isFlushesActived, isAttributeSetInitializedBlocked);
153
154   storeAttributeValue(theAttribute);
155
156   // saves the owner value to the widget attribute
157   aValid = setSelectionCustom(theValue);
158   if (aValid)
159     // checks the attribute validity
160     aValid = isValidAttribute(theAttribute);
161
162   // restores the current values of the widget attribute
163   restoreAttributeValue(theAttribute, aValid);
164
165   blockAttribute(theAttribute, false, isFlushesActived, isAttributeSetInitializedBlocked);
166   // In particular case the results are deleted and called as redisplayed inside of this
167   // highlight-selection, to they must be flushed as soon as possible.
168   // Example: selection of group-vertices subshapes with shift pressend on body. Without
169   //  these 4 lines below the application crashes because of left presentations on
170   //  removed results still in the viewer.
171   static Events_ID aDeletedEvent = Events_Loop::eventByName(EVENT_OBJECT_DELETED);
172   static Events_ID aRedispEvent = Events_Loop::eventByName(EVENT_OBJECT_TO_REDISPLAY);
173   Events_Loop::loop()->flush(aDeletedEvent);
174   Events_Loop::loop()->flush(aRedispEvent);
175
176   return aValid;
177 }
178
179 //********************************************************************
180 bool ModuleBase_WidgetValidated::isValidSelectionCustom(const ModuleBase_ViewerPrsPtr& thePrs)
181 {
182   return true;
183 }
184
185 //********************************************************************
186 bool ModuleBase_WidgetValidated::isValidAttribute(const AttributePtr& theAttribute) const
187 {
188   SessionPtr aMgr = ModelAPI_Session::get();
189   ModelAPI_ValidatorsFactory* aFactory = aMgr->validators();
190   std::string aValidatorID, anError;
191   return aFactory->validate(theAttribute, aValidatorID, anError);
192 }
193
194 bool ModuleBase_WidgetValidated::isFilterActivated() const
195 {
196   bool isActivated = false;
197
198   Handle(SelectMgr_Filter) aSelFilter = myWorkshop->validatorFilter();
199   ModuleBase_IViewer* aViewer = myWorkshop->viewer();
200
201   return aViewer->hasSelectionFilter(aSelFilter);
202 }
203
204 bool ModuleBase_WidgetValidated::activateFilters(const bool toActivate)
205 {
206   ModuleBase_IViewer* aViewer = myWorkshop->viewer();
207
208   Handle(SelectMgr_Filter) aSelFilter = myWorkshop->validatorFilter();
209   bool aHasSelectionFilter = aViewer->hasSelectionFilter(aSelFilter);
210
211   if (toActivate)
212     aViewer->addSelectionFilter(aSelFilter);
213   else {
214     aViewer->removeSelectionFilter(aSelFilter);
215     clearValidatedCash();
216   }
217
218   return aHasSelectionFilter;
219 }
220
221 //********************************************************************
222 void ModuleBase_WidgetValidated::blockAttribute(const AttributePtr& theAttribute,
223                                                 const bool& theToBlock,
224                                                 bool& isFlushesActived,
225                                                 bool& isAttributeSetInitializedBlocked)
226 {
227   Events_Loop* aLoop = Events_Loop::loop();
228   DataPtr aData = myFeature->data();
229   if (theToBlock) {
230     // blocks the flush signals to avoid the temporary objects visualization in the viewer
231     // they should not be shown in order to do not lose highlight by erasing them
232     isFlushesActived = aLoop->activateFlushes(false);
233
234     aData->blockSendAttributeUpdated(true);
235     isAttributeSetInitializedBlocked = theAttribute->blockSetInitialized(true);
236   }
237   else {
238     aData->blockSendAttributeUpdated(false);
239     theAttribute->blockSetInitialized(isAttributeSetInitializedBlocked);
240     aLoop->activateFlushes(isFlushesActived);
241   }
242 }
243
244 //********************************************************************
245 void ModuleBase_WidgetValidated::storeValidState(const ModuleBase_ViewerPrsPtr& theValue, const bool theValid)
246 {
247   if (theValid) {
248     GeomShapePtr aShape = theValue.get() ? theValue->shape() : GeomShapePtr();
249     const TopoDS_Shape& aTDShape = aShape->impl<TopoDS_Shape>();
250     bool aValidPrsContains = aShape.get() && myValidPrs.IsBound(aTDShape) &&
251                              theValue.get()->isEqual(myValidPrs.Find(aTDShape).get());
252     if (!aValidPrsContains) {
253 #ifdef LIST_OF_VALID_PRS
254
255       myValidPrs.append(theValue);
256 #else
257       GeomShapePtr aShape = theValue->shape();
258       if (aShape.get()) {
259         const TopoDS_Shape& aTDShape = aShape->impl<TopoDS_Shape>();
260         myValidPrs.Bind(aTDShape, theValue);
261       }
262 #endif
263     // the commented code will be useful when the valid state of the presentation
264     // will be changable between activate/deactivate. Currently it does not happen.
265     //if (anInvalidPrs)
266     //  myInvalidPrs.removeOne(theValue);
267     }
268   }
269   else { // !theValid
270     GeomShapePtr aShape = theValue.get() ? theValue->shape() : GeomShapePtr();
271     const TopoDS_Shape& aTDShape = aShape->impl<TopoDS_Shape>();
272     bool anIValidPrsContains = aShape.get() && myInvalidPrs.IsBound(aTDShape) &&
273                                theValue.get()->isEqual(myInvalidPrs.Find(aTDShape).get());
274
275     if (!anIValidPrsContains) {
276 #ifdef LIST_OF_VALID_PRS
277       myInvalidPrs.append(theValue);
278 #else
279       GeomShapePtr aShape = theValue->shape();
280       if (aShape.get()) {
281         const TopoDS_Shape& aTDShape = aShape->impl<TopoDS_Shape>();
282         myInvalidPrs.Bind(aTDShape, theValue);
283       }
284 #endif
285     //if (!aValidPrs)
286     //  myValidPrs.removeOne(theValue);
287     }
288   }
289 #ifdef DEBUG_VALID_STATE
290   qDebug(QString("storeValidState: myValidPrs.size() = %1, myInvalidPrs.size() = %2").arg(myValidPrs.count())
291                  .arg(myInvalidPrs.count()).toStdString().c_str());
292 #endif
293 }
294
295 //********************************************************************
296 bool ModuleBase_WidgetValidated::getValidState(const ModuleBase_ViewerPrsPtr& theValue, bool& theValid)
297 {
298   if (!theValue.get())
299     return false;
300
301 #ifdef LIST_OF_VALID_PRS
302   bool aValidPrsContains = myValidPrs.contains(theValue);
303   bool anInvalidPrsContains = myInvalidPrs.contains(theValue);
304 #else
305   GeomShapePtr aShape = theValue.get() ? theValue->shape() : GeomShapePtr();
306   const TopoDS_Shape& aTDShape = aShape->impl<TopoDS_Shape>();
307   bool aValidPrsContains = aShape.get() && myValidPrs.IsBound(aTDShape) &&
308                            theValue.get()->isEqual(myValidPrs.Find(aTDShape).get());
309   /*if (aShape.get() && myValidPrs.IsBound(aTDShape)) {
310     ModuleBase_ViewerPrs* aPrs1 = theValue.get();
311     ModuleBase_ViewerPrs* aPrs2 = myValidPrs.Find(aTDShape).get();
312
313     bool isEqual2 = aPrs1->isEqual(aPrs2);
314
315     bool aValue = 9;
316   }*/
317
318   bool anInvalidPrsContains = aShape.get() && myInvalidPrs.IsBound(aTDShape) &&
319                               theValue.get()->isEqual(myInvalidPrs.Find(aTDShape).get());
320   /*
321   bool aValidPrsContains = false, anInvalidPrsContains = false;
322   GeomShapePtr aShape = theValue.get() ? theValue->shape() : GeomShapePtr();
323   if (aShape.get()) {
324     aValidPrsContains = myValidPrs.contains(aShape);
325     anInvalidPrsContains = myInvalidPrs.contains(aShape);
326
327     if (aValidPrsContains)
328       aValidPrsContains = theValue == myValidPrs[aShape];
329     else
330       anInvalidPrsContains = theValue == myInvalidPrs[aShape];*/
331 #endif
332
333   if (aValidPrsContains)
334     theValid = true;
335   else if (anInvalidPrsContains)
336     theValid = false;
337
338   return aValidPrsContains || anInvalidPrsContains;
339 }
340
341 //********************************************************************
342 QList<ModuleBase_ViewerPrsPtr> ModuleBase_WidgetValidated::getFilteredSelected()
343 {
344   QList<ModuleBase_ViewerPrsPtr> aSelected = myWorkshop->selection()->getSelected(
345                                                        ModuleBase_ISelection::Viewer);
346
347   QList<ModuleBase_ViewerPrsPtr> anOBSelected = myWorkshop->selection()->getSelected(
348                                                        ModuleBase_ISelection::Browser);
349   // filter the OB presentations
350   filterPresentations(anOBSelected);
351   if (!anOBSelected.isEmpty())
352     ModuleBase_ISelection::appendSelected(anOBSelected, aSelected);
353
354   filterCompSolids(aSelected);
355
356   return aSelected;
357 }
358
359 //********************************************************************
360 void ModuleBase_WidgetValidated::filterPresentations(QList<ModuleBase_ViewerPrsPtr>& theValues)
361 {
362   QList<ModuleBase_ViewerPrsPtr> aValidatedValues;
363
364   QList<ModuleBase_ViewerPrsPtr>::const_iterator anIt = theValues.begin(), aLast = theValues.end();
365   bool isDone = false;
366   for (; anIt != aLast; anIt++) {
367     if (isValidInFilters(*anIt))
368       aValidatedValues.append(*anIt);
369   }
370   if (aValidatedValues.size() != theValues.size()) {
371     theValues.clear();
372     theValues = aValidatedValues;
373   }
374 }
375
376 //********************************************************************
377 void ModuleBase_WidgetValidated::filterCompSolids(QList<ModuleBase_ViewerPrsPtr>& theValues)
378 {
379   std::set<ResultCompSolidPtr> aCompSolids;
380   QList<ModuleBase_ViewerPrsPtr> aValidatedValues;
381
382   // Collect compsolids.
383   QList<ModuleBase_ViewerPrsPtr>::const_iterator anIt = theValues.begin(), aLast = theValues.end();
384   for (; anIt != aLast; anIt++) {
385     const ModuleBase_ViewerPrsPtr& aViewerPrs = *anIt;
386     ObjectPtr anObject = aViewerPrs->object();
387     ResultCompSolidPtr aResultCompSolid = std::dynamic_pointer_cast<ModelAPI_ResultCompSolid>(anObject);
388     if(aResultCompSolid.get()) {
389       aCompSolids.insert(aResultCompSolid);
390     }
391   }
392
393   // Filter sub-solids of compsolids.
394   anIt = theValues.begin();
395   for (; anIt != aLast; anIt++) {
396     const ModuleBase_ViewerPrsPtr& aViewerPrs = *anIt;
397     ObjectPtr anObject = aViewerPrs->object();
398     ResultPtr aResult = std::dynamic_pointer_cast<ModelAPI_Result>(anObject);
399     ResultCompSolidPtr aResCompSolidPtr = ModelAPI_Tools::compSolidOwner(aResult);
400     if(aResCompSolidPtr.get() && (aCompSolids.find(aResCompSolidPtr) != aCompSolids.end())) {
401       // Skip sub-solid of compsolid.
402       continue;
403     } else {
404       aValidatedValues.append(*anIt);
405     }
406   }
407
408   if (aValidatedValues.size() != theValues.size()) {
409     theValues.clear();
410     theValues = aValidatedValues;
411   }
412 }