Salome HOME
Warnings correction for moc files generation
[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_IViewer.h>
5 #include <ModuleBase_IWorkshop.h>
6 #include <ModuleBase_ISelection.h>
7 #include <ModuleBase_WidgetSelectorStore.h>
8 #include <ModuleBase_ViewerPrs.h>
9
10 #include <Events_InfoMessage.h>
11
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>
18
19 #include <SelectMgr_ListIteratorOfListOfFilter.hxx>
20 #include <SelectMgr_EntityOwner.hxx>
21 #include <StdSelect_BRepOwner.hxx>
22
23 #include <Events_Loop.h>
24
25 #include <QWidget>
26
27 //#define DEBUG_VALID_STATE
28
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)
34 {
35   myAttributeStore = new ModuleBase_WidgetSelectorStore();
36 }
37
38 ModuleBase_WidgetValidated::~ModuleBase_WidgetValidated()
39 {
40   delete myAttributeStore;
41 }
42
43 //********************************************************************
44 ObjectPtr ModuleBase_WidgetValidated::findPresentedObject(const AISObjectPtr& theAIS) const
45 {
46   return myPresentedObject;
47 }
48
49 //********************************************************************
50 void ModuleBase_WidgetValidated::clearValidatedCash()
51 {
52 #ifdef DEBUG_VALID_STATE
53   qDebug("clearValidatedCash");
54 #endif
55   myValidPrs.Clear();
56   myInvalidPrs.Clear();
57 }
58
59 //********************************************************************
60 void ModuleBase_WidgetValidated::storeAttributeValue(const AttributePtr& theAttribute)
61 {
62   myIsInValidate = true;
63   myAttributeStore->storeAttributeValue(theAttribute, myWorkshop);
64 }
65
66 //********************************************************************
67 void ModuleBase_WidgetValidated::restoreAttributeValue(const AttributePtr& theAttribute,
68                                                        const bool theValid)
69 {
70   myIsInValidate = false;
71   myAttributeStore->restoreAttributeValue(theAttribute, myWorkshop);
72 }
73
74 //********************************************************************
75 bool ModuleBase_WidgetValidated::isValidInFilters(const ModuleBase_ViewerPrsPtr& thePrs)
76 {
77   bool aValid = true;
78   Handle(SelectMgr_EntityOwner) anOwner = thePrs->owner();
79
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
86     if (aShape.get()) {
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;
91     }
92     else
93       aValid = false; // only results with a shape can be filtered
94   }
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();
100     if (!isActivated)
101       activateFilters(true);
102
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);
110       }
111     }
112     if (!isActivated)
113       activateFilters(false);
114   }
115
116   // removes created owner
117   if (!anOwner.IsNull() && anOwner != thePrs->owner()) {
118     anOwner.Nullify();
119     myPresentedObject = ObjectPtr();
120   }
121   return aValid;
122 }
123
124 //********************************************************************
125 AttributePtr ModuleBase_WidgetValidated::attribute() const
126 {
127   return myFeature->attribute(attributeID());
128 }
129
130 //********************************************************************
131 bool ModuleBase_WidgetValidated::isValidSelection(const ModuleBase_ViewerPrsPtr& theValue)
132 {
133   bool aValid = false;
134   if (getValidState(theValue, aValid)) {
135     return aValid;
136   }
137   aValid = isValidSelectionCustom(theValue);
138   if (aValid)
139     aValid = isValidSelectionForAttribute(theValue, attribute());
140
141   storeValidState(theValue, aValid);
142   return aValid;
143 }
144
145 //********************************************************************
146 bool ModuleBase_WidgetValidated::isValidSelectionForAttribute(
147                                             const ModuleBase_ViewerPrsPtr& theValue,
148                                             const AttributePtr& theAttribute)
149 {
150   bool aValid = false;
151
152   // stores the current values of the widget attribute
153   bool isFlushesActived, isAttributeSetInitializedBlocked, isAttributeSendUpdatedBlocked;
154
155   blockAttribute(theAttribute, true, isFlushesActived, isAttributeSetInitializedBlocked,
156                  isAttributeSendUpdatedBlocked);
157
158   storeAttributeValue(theAttribute);
159
160   // saves the owner value to the widget attribute
161   aValid = setSelectionCustom(theValue);
162   if (aValid)
163     // checks the attribute validity
164     aValid = isValidAttribute(theAttribute);
165
166   // restores the current values of the widget attribute
167   restoreAttributeValue(theAttribute, aValid);
168
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);
183   */
184   return aValid;
185 }
186
187 //********************************************************************
188 bool ModuleBase_WidgetValidated::isValidSelectionCustom(const ModuleBase_ViewerPrsPtr& thePrs)
189 {
190   return true;
191 }
192
193 //********************************************************************
194 bool ModuleBase_WidgetValidated::isValidAttribute(const AttributePtr& theAttribute) const
195 {
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);
201 }
202
203 bool ModuleBase_WidgetValidated::isFilterActivated() const
204 {
205   bool isActivated = false;
206
207   Handle(SelectMgr_Filter) aSelFilter = myWorkshop->validatorFilter();
208   ModuleBase_IViewer* aViewer = myWorkshop->viewer();
209
210   return aViewer->hasSelectionFilter(aSelFilter);
211 }
212
213 bool ModuleBase_WidgetValidated::activateFilters(const bool toActivate)
214 {
215   ModuleBase_IViewer* aViewer = myWorkshop->viewer();
216
217   Handle(SelectMgr_Filter) aSelFilter = myWorkshop->validatorFilter();
218   bool aHasSelectionFilter = aViewer->hasSelectionFilter(aSelFilter);
219
220   if (toActivate)
221     aViewer->addSelectionFilter(aSelFilter);
222   else {
223     aViewer->removeSelectionFilter(aSelFilter);
224     clearValidatedCash();
225   }
226
227   return aHasSelectionFilter;
228 }
229
230 //********************************************************************
231 void ModuleBase_WidgetValidated::blockAttribute(const AttributePtr& theAttribute,
232                                                 const bool& theToBlock,
233                                                 bool& isFlushesActived,
234                                                 bool& isAttributeSetInitializedBlocked,
235                                                 bool& isAttributeSendUpdatedBlocked)
236 {
237   Events_Loop* aLoop = Events_Loop::loop();
238   DataPtr aData = myFeature->data();
239   if (theToBlock) {
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);
243
244     isAttributeSendUpdatedBlocked = aData->blockSendAttributeUpdated(true);
245     isAttributeSetInitializedBlocked = theAttribute->blockSetInitialized(true);
246   }
247   else {
248     aData->blockSendAttributeUpdated(isAttributeSendUpdatedBlocked, false);
249     theAttribute->blockSetInitialized(isAttributeSetInitializedBlocked);
250     aLoop->activateFlushes(isFlushesActived);
251   }
252 }
253
254 //********************************************************************
255 void ModuleBase_WidgetValidated::storeValidState(const ModuleBase_ViewerPrsPtr& theValue,
256                                                  const bool theValid)
257 {
258   GeomShapePtr aShape = theValue.get() ? theValue->shape() : GeomShapePtr();
259   if (aShape.get()) {
260     if (theValid) {
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);
267   #else
268         myValidPrs.Bind(aTDShape, theValue);
269   #endif
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.
272       //if (anInvalidPrs)
273       //  myInvalidPrs.removeOne(theValue);
274       }
275     }
276     else { // !theValid
277       if (aShape.get()) {
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);
284     #else
285           myInvalidPrs.Bind(aTDShape, theValue);
286     #endif
287         //if (!aValidPrs)
288         //  myValidPrs.removeOne(theValue);
289         }
290       }
291     }
292   }
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());
297   #endif
298 }
299
300 //********************************************************************
301 bool ModuleBase_WidgetValidated::getValidState(const ModuleBase_ViewerPrsPtr& theValue,
302                                                bool& theValid)
303 {
304   if (!theValue.get())
305     return false;
306
307 #ifdef LIST_OF_VALID_PRS
308   bool aValidPrsContains = myValidPrs.contains(theValue);
309   bool anInvalidPrsContains = myInvalidPrs.contains(theValue);
310 #else
311   GeomShapePtr aShape = theValue.get() ? theValue->shape() : GeomShapePtr();
312   if (!aShape.get())
313     return false;
314
315   const TopoDS_Shape& aTDShape = aShape->impl<TopoDS_Shape>();
316   bool aValidPrsContains = myValidPrs.IsBound(aTDShape) &&
317                            theValue.get()->isEqual(myValidPrs.Find(aTDShape).get());
318
319   bool anInvalidPrsContains = myInvalidPrs.IsBound(aTDShape) &&
320                               theValue.get()->isEqual(myInvalidPrs.Find(aTDShape).get());
321   /*
322   bool aValidPrsContains = false, anInvalidPrsContains = false;
323   GeomShapePtr aShape = theValue.get() ? theValue->shape() : GeomShapePtr();
324   if (aShape.get()) {
325     aValidPrsContains = myValidPrs.contains(aShape);
326     anInvalidPrsContains = myInvalidPrs.contains(aShape);
327
328     if (aValidPrsContains)
329       aValidPrsContains = theValue == myValidPrs[aShape];
330     else
331       anInvalidPrsContains = theValue == myInvalidPrs[aShape];*/
332 #endif
333
334   if (aValidPrsContains)
335     theValid = true;
336   else if (anInvalidPrsContains)
337     theValid = false;
338
339   return aValidPrsContains || anInvalidPrsContains;
340 }
341
342 //********************************************************************
343 QList<ModuleBase_ViewerPrsPtr> ModuleBase_WidgetValidated::getFilteredSelected()
344 {
345   QList<ModuleBase_ViewerPrsPtr> aSelected = myWorkshop->selection()->getSelected(
346                                                        ModuleBase_ISelection::Viewer);
347
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);
354
355   filterCompSolids(aSelected);
356
357   return aSelected;
358 }
359
360 //********************************************************************
361 void ModuleBase_WidgetValidated::filterPresentations(QList<ModuleBase_ViewerPrsPtr>& theValues)
362 {
363   QList<ModuleBase_ViewerPrsPtr> aValidatedValues;
364
365   QList<ModuleBase_ViewerPrsPtr>::const_iterator anIt = theValues.begin(), aLast = theValues.end();
366   bool isDone = false;
367   for (; anIt != aLast; anIt++) {
368     if (isValidInFilters(*anIt))
369       aValidatedValues.append(*anIt);
370   }
371   if (aValidatedValues.size() != theValues.size()) {
372     theValues.clear();
373     theValues = aValidatedValues;
374   }
375 }
376
377 //********************************************************************
378 void ModuleBase_WidgetValidated::filterCompSolids(QList<ModuleBase_ViewerPrsPtr>& theValues)
379 {
380   std::set<ResultCompSolidPtr> aCompSolids;
381   QList<ModuleBase_ViewerPrsPtr> aValidatedValues;
382
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);
392     }
393   }
394
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.
404       continue;
405     } else {
406       aValidatedValues.append(*anIt);
407     }
408   }
409
410   if (aValidatedValues.size() != theValues.size()) {
411     theValues.clear();
412     theValues = aValidatedValues;
413   }
414 }