Salome HOME
Issue #1834: Fix length of lines
[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 <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;
154
155   blockAttribute(theAttribute, true, isFlushesActived, isAttributeSetInitializedBlocked);
156
157   storeAttributeValue(theAttribute);
158
159   // saves the owner value to the widget attribute
160   aValid = setSelectionCustom(theValue);
161   if (aValid)
162     // checks the attribute validity
163     aValid = isValidAttribute(theAttribute);
164
165   // restores the current values of the widget attribute
166   restoreAttributeValue(theAttribute, aValid);
167
168   blockAttribute(theAttribute, false, isFlushesActived, isAttributeSetInitializedBlocked);
169   /// NDS: The following rows are commented for issue #1452 (to be removed after debug)
170   /// This is not correct to perform it here because it might cause update selection and
171   /// the selection mechanizm will be circled: use the scenario of the bug with preselected point.
172   // In particular case the results are deleted and called as redisplayed inside of this
173   // highlight-selection, to they must be flushed as soon as possible.
174   // Example: selection of group-vertices subshapes with shift pressend on body. Without
175   //  these 4 lines below the application crashes because of left presentations on
176   //  removed results still in the viewer.
177   /*static Events_ID aDeletedEvent = Events_Loop::eventByName(EVENT_OBJECT_DELETED);
178   static Events_ID aRedispEvent = Events_Loop::eventByName(EVENT_OBJECT_TO_REDISPLAY);
179   Events_Loop::loop()->flush(aDeletedEvent);
180   Events_Loop::loop()->flush(aRedispEvent);
181   */
182   return aValid;
183 }
184
185 //********************************************************************
186 bool ModuleBase_WidgetValidated::isValidSelectionCustom(const ModuleBase_ViewerPrsPtr& thePrs)
187 {
188   return true;
189 }
190
191 //********************************************************************
192 bool ModuleBase_WidgetValidated::isValidAttribute(const AttributePtr& theAttribute) const
193 {
194   SessionPtr aMgr = ModelAPI_Session::get();
195   ModelAPI_ValidatorsFactory* aFactory = aMgr->validators();
196   std::string aValidatorID;
197   Events_InfoMessage anError;
198   return aFactory->validate(theAttribute, aValidatorID, anError);
199 }
200
201 bool ModuleBase_WidgetValidated::isFilterActivated() const
202 {
203   bool isActivated = false;
204
205   Handle(SelectMgr_Filter) aSelFilter = myWorkshop->validatorFilter();
206   ModuleBase_IViewer* aViewer = myWorkshop->viewer();
207
208   return aViewer->hasSelectionFilter(aSelFilter);
209 }
210
211 bool ModuleBase_WidgetValidated::activateFilters(const bool toActivate)
212 {
213   ModuleBase_IViewer* aViewer = myWorkshop->viewer();
214
215   Handle(SelectMgr_Filter) aSelFilter = myWorkshop->validatorFilter();
216   bool aHasSelectionFilter = aViewer->hasSelectionFilter(aSelFilter);
217
218   if (toActivate)
219     aViewer->addSelectionFilter(aSelFilter);
220   else {
221     aViewer->removeSelectionFilter(aSelFilter);
222     clearValidatedCash();
223   }
224
225   return aHasSelectionFilter;
226 }
227
228 //********************************************************************
229 void ModuleBase_WidgetValidated::blockAttribute(const AttributePtr& theAttribute,
230                                                 const bool& theToBlock,
231                                                 bool& isFlushesActived,
232                                                 bool& isAttributeSetInitializedBlocked)
233 {
234   Events_Loop* aLoop = Events_Loop::loop();
235   DataPtr aData = myFeature->data();
236   if (theToBlock) {
237     // blocks the flush signals to avoid the temporary objects visualization in the viewer
238     // they should not be shown in order to do not lose highlight by erasing them
239     isFlushesActived = aLoop->activateFlushes(false);
240
241     aData->blockSendAttributeUpdated(true);
242     isAttributeSetInitializedBlocked = theAttribute->blockSetInitialized(true);
243   }
244   else {
245     aData->blockSendAttributeUpdated(false, false);
246     theAttribute->blockSetInitialized(isAttributeSetInitializedBlocked);
247     aLoop->activateFlushes(isFlushesActived);
248   }
249 }
250
251 //********************************************************************
252 void ModuleBase_WidgetValidated::storeValidState(const ModuleBase_ViewerPrsPtr& theValue, 
253                                                  const bool theValid)
254 {
255   GeomShapePtr aShape = theValue.get() ? theValue->shape() : GeomShapePtr();
256   if (aShape.get()) {
257     if (theValid) {
258       const TopoDS_Shape& aTDShape = aShape->impl<TopoDS_Shape>();
259       bool aValidPrsContains = myValidPrs.IsBound(aTDShape) &&
260                                theValue.get()->isEqual(myValidPrs.Find(aTDShape).get());
261       if (!aValidPrsContains) {
262   #ifdef LIST_OF_VALID_PRS
263         myValidPrs.append(theValue);
264   #else
265         myValidPrs.Bind(aTDShape, theValue);
266   #endif
267       // the commented code will be useful when the valid state of the presentation
268       // will be changable between activate/deactivate. Currently it does not happen.
269       //if (anInvalidPrs)
270       //  myInvalidPrs.removeOne(theValue);
271       }
272     }
273     else { // !theValid
274       if (aShape.get()) {
275         const TopoDS_Shape& aTDShape = aShape->impl<TopoDS_Shape>();
276         bool anIValidPrsContains = myInvalidPrs.IsBound(aTDShape) &&
277                                    theValue.get()->isEqual(myInvalidPrs.Find(aTDShape).get());
278         if (!anIValidPrsContains) {
279     #ifdef LIST_OF_VALID_PRS
280           myInvalidPrs.append(theValue);
281     #else
282           myInvalidPrs.Bind(aTDShape, theValue);
283     #endif
284         //if (!aValidPrs)
285         //  myValidPrs.removeOne(theValue);
286         }
287       }
288     }
289   }
290   #ifdef DEBUG_VALID_STATE
291     qDebug(QString("storeValidState: myValidPrs.size() = %1, myInvalidPrs.size() = %2")
292                    .arg(myValidPrs.count())
293                    .arg(myInvalidPrs.count()).toStdString().c_str());
294   #endif
295 }
296
297 //********************************************************************
298 bool ModuleBase_WidgetValidated::getValidState(const ModuleBase_ViewerPrsPtr& theValue, 
299                                                bool& theValid)
300 {
301   if (!theValue.get())
302     return false;
303
304 #ifdef LIST_OF_VALID_PRS
305   bool aValidPrsContains = myValidPrs.contains(theValue);
306   bool anInvalidPrsContains = myInvalidPrs.contains(theValue);
307 #else
308   GeomShapePtr aShape = theValue.get() ? theValue->shape() : GeomShapePtr();
309   if (!aShape.get())
310     return false;
311
312   const TopoDS_Shape& aTDShape = aShape->impl<TopoDS_Shape>();
313   bool aValidPrsContains = myValidPrs.IsBound(aTDShape) &&
314                            theValue.get()->isEqual(myValidPrs.Find(aTDShape).get());
315
316   bool anInvalidPrsContains = myInvalidPrs.IsBound(aTDShape) &&
317                               theValue.get()->isEqual(myInvalidPrs.Find(aTDShape).get());
318   /*
319   bool aValidPrsContains = false, anInvalidPrsContains = false;
320   GeomShapePtr aShape = theValue.get() ? theValue->shape() : GeomShapePtr();
321   if (aShape.get()) {
322     aValidPrsContains = myValidPrs.contains(aShape);
323     anInvalidPrsContains = myInvalidPrs.contains(aShape);
324
325     if (aValidPrsContains)
326       aValidPrsContains = theValue == myValidPrs[aShape];
327     else
328       anInvalidPrsContains = theValue == myInvalidPrs[aShape];*/
329 #endif
330
331   if (aValidPrsContains)
332     theValid = true;
333   else if (anInvalidPrsContains)
334     theValid = false;
335
336   return aValidPrsContains || anInvalidPrsContains;
337 }
338
339 //********************************************************************
340 QList<ModuleBase_ViewerPrsPtr> ModuleBase_WidgetValidated::getFilteredSelected()
341 {
342   QList<ModuleBase_ViewerPrsPtr> aSelected = myWorkshop->selection()->getSelected(
343                                                        ModuleBase_ISelection::Viewer);
344
345   QList<ModuleBase_ViewerPrsPtr> anOBSelected = myWorkshop->selection()->getSelected(
346                                                        ModuleBase_ISelection::Browser);
347   // filter the OB presentations
348   filterPresentations(anOBSelected);
349   if (!anOBSelected.isEmpty())
350     ModuleBase_ISelection::appendSelected(anOBSelected, aSelected);
351
352   filterCompSolids(aSelected);
353
354   return aSelected;
355 }
356
357 //********************************************************************
358 void ModuleBase_WidgetValidated::filterPresentations(QList<ModuleBase_ViewerPrsPtr>& theValues)
359 {
360   QList<ModuleBase_ViewerPrsPtr> aValidatedValues;
361
362   QList<ModuleBase_ViewerPrsPtr>::const_iterator anIt = theValues.begin(), aLast = theValues.end();
363   bool isDone = false;
364   for (; anIt != aLast; anIt++) {
365     if (isValidInFilters(*anIt))
366       aValidatedValues.append(*anIt);
367   }
368   if (aValidatedValues.size() != theValues.size()) {
369     theValues.clear();
370     theValues = aValidatedValues;
371   }
372 }
373
374 //********************************************************************
375 void ModuleBase_WidgetValidated::filterCompSolids(QList<ModuleBase_ViewerPrsPtr>& theValues)
376 {
377   std::set<ResultCompSolidPtr> aCompSolids;
378   QList<ModuleBase_ViewerPrsPtr> aValidatedValues;
379
380   // Collect compsolids.
381   QList<ModuleBase_ViewerPrsPtr>::const_iterator anIt = theValues.begin(), aLast = theValues.end();
382   for (; anIt != aLast; anIt++) {
383     const ModuleBase_ViewerPrsPtr& aViewerPrs = *anIt;
384     ObjectPtr anObject = aViewerPrs->object();
385     ResultCompSolidPtr aResultCompSolid = 
386       std::dynamic_pointer_cast<ModelAPI_ResultCompSolid>(anObject);
387     if(aResultCompSolid.get()) {
388       aCompSolids.insert(aResultCompSolid);
389     }
390   }
391
392   // Filter sub-solids of compsolids.
393   anIt = theValues.begin();
394   for (; anIt != aLast; anIt++) {
395     const ModuleBase_ViewerPrsPtr& aViewerPrs = *anIt;
396     ObjectPtr anObject = aViewerPrs->object();
397     ResultPtr aResult = std::dynamic_pointer_cast<ModelAPI_Result>(anObject);
398     ResultCompSolidPtr aResCompSolidPtr = ModelAPI_Tools::compSolidOwner(aResult);
399     if(aResCompSolidPtr.get() && (aCompSolids.find(aResCompSolidPtr) != aCompSolids.end())) {
400       // Skip sub-solid of compsolid.
401       continue;
402     } else {
403       aValidatedValues.append(*anIt);
404     }
405   }
406
407   if (aValidatedValues.size() != theValues.size()) {
408     theValues.clear();
409     theValues = aValidatedValues;
410   }
411 }