Salome HOME
Issue #2024: Redesign of circle and arc of circle (validate preselected object)
[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)
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   blockFeatureAttribute(theAttribute, myFeature, theToBlock, isFlushesActived,
238                         isAttributeSetInitializedBlocked, isAttributeSendUpdatedBlocked);
239 }
240
241 //********************************************************************
242 void ModuleBase_WidgetValidated::blockFeatureAttribute(const AttributePtr& theAttribute,
243                                                 const FeaturePtr& theFeature,
244                                                 const bool& theToBlock,
245                                                 bool& isFlushesActived,
246                                                 bool& isAttributeSetInitializedBlocked,
247                                                 bool& isAttributeSendUpdatedBlocked)
248 {
249   Events_Loop* aLoop = Events_Loop::loop();
250   DataPtr aData = theFeature->data();
251   if (theToBlock) {
252     // blocks the flush signals to avoid the temporary objects visualization in the viewer
253     // they should not be shown in order to do not lose highlight by erasing them
254     isFlushesActived = aLoop->activateFlushes(false);
255
256     isAttributeSendUpdatedBlocked = aData->blockSendAttributeUpdated(true);
257     isAttributeSetInitializedBlocked = theAttribute->blockSetInitialized(true);
258   }
259   else {
260     aData->blockSendAttributeUpdated(isAttributeSendUpdatedBlocked, false);
261     theAttribute->blockSetInitialized(isAttributeSetInitializedBlocked);
262     aLoop->activateFlushes(isFlushesActived);
263   }
264 }
265
266 //********************************************************************
267 void ModuleBase_WidgetValidated::storeValidState(const ModuleBase_ViewerPrsPtr& theValue,
268                                                  const bool theValid)
269 {
270   GeomShapePtr aShape = theValue.get() ? theValue->shape() : GeomShapePtr();
271   if (aShape.get()) {
272     if (theValid) {
273       const TopoDS_Shape& aTDShape = aShape->impl<TopoDS_Shape>();
274       bool aValidPrsContains = myValidPrs.IsBound(aTDShape) &&
275                                theValue.get()->isEqual(myValidPrs.Find(aTDShape).get());
276       if (!aValidPrsContains) {
277   #ifdef LIST_OF_VALID_PRS
278         myValidPrs.append(theValue);
279   #else
280         myValidPrs.Bind(aTDShape, theValue);
281   #endif
282       // the commented code will be useful when the valid state of the presentation
283       // will be changable between activate/deactivate. Currently it does not happen.
284       //if (anInvalidPrs)
285       //  myInvalidPrs.removeOne(theValue);
286       }
287     }
288     else { // !theValid
289       if (aShape.get()) {
290         const TopoDS_Shape& aTDShape = aShape->impl<TopoDS_Shape>();
291         bool anIValidPrsContains = myInvalidPrs.IsBound(aTDShape) &&
292                                    theValue.get()->isEqual(myInvalidPrs.Find(aTDShape).get());
293         if (!anIValidPrsContains) {
294     #ifdef LIST_OF_VALID_PRS
295           myInvalidPrs.append(theValue);
296     #else
297           myInvalidPrs.Bind(aTDShape, theValue);
298     #endif
299         //if (!aValidPrs)
300         //  myValidPrs.removeOne(theValue);
301         }
302       }
303     }
304   }
305   #ifdef DEBUG_VALID_STATE
306     qDebug(QString("storeValidState: myValidPrs.size() = %1, myInvalidPrs.size() = %2")
307                    .arg(myValidPrs.count())
308                    .arg(myInvalidPrs.count()).toStdString().c_str());
309   #endif
310 }
311
312 //********************************************************************
313 bool ModuleBase_WidgetValidated::getValidState(const ModuleBase_ViewerPrsPtr& theValue,
314                                                bool& theValid)
315 {
316   if (!theValue.get())
317     return false;
318
319 #ifdef LIST_OF_VALID_PRS
320   bool aValidPrsContains = myValidPrs.contains(theValue);
321   bool anInvalidPrsContains = myInvalidPrs.contains(theValue);
322 #else
323   GeomShapePtr aShape = theValue.get() ? theValue->shape() : GeomShapePtr();
324   if (!aShape.get())
325     return false;
326
327   const TopoDS_Shape& aTDShape = aShape->impl<TopoDS_Shape>();
328   bool aValidPrsContains = myValidPrs.IsBound(aTDShape) &&
329                            theValue.get()->isEqual(myValidPrs.Find(aTDShape).get());
330
331   bool anInvalidPrsContains = myInvalidPrs.IsBound(aTDShape) &&
332                               theValue.get()->isEqual(myInvalidPrs.Find(aTDShape).get());
333   /*
334   bool aValidPrsContains = false, anInvalidPrsContains = false;
335   GeomShapePtr aShape = theValue.get() ? theValue->shape() : GeomShapePtr();
336   if (aShape.get()) {
337     aValidPrsContains = myValidPrs.contains(aShape);
338     anInvalidPrsContains = myInvalidPrs.contains(aShape);
339
340     if (aValidPrsContains)
341       aValidPrsContains = theValue == myValidPrs[aShape];
342     else
343       anInvalidPrsContains = theValue == myInvalidPrs[aShape];*/
344 #endif
345
346   if (aValidPrsContains)
347     theValid = true;
348   else if (anInvalidPrsContains)
349     theValid = false;
350
351   return aValidPrsContains || anInvalidPrsContains;
352 }
353
354 //********************************************************************
355 QList<ModuleBase_ViewerPrsPtr> ModuleBase_WidgetValidated::getFilteredSelected()
356 {
357   QList<ModuleBase_ViewerPrsPtr> aSelected = myWorkshop->selection()->getSelected(
358                                                        ModuleBase_ISelection::Viewer);
359
360   QList<ModuleBase_ViewerPrsPtr> anOBSelected = myWorkshop->selection()->getSelected(
361                                                        ModuleBase_ISelection::Browser);
362   // filter the OB presentations
363   filterPresentations(anOBSelected);
364   if (!anOBSelected.isEmpty())
365     ModuleBase_ISelection::appendSelected(anOBSelected, aSelected);
366
367   filterCompSolids(aSelected);
368
369   return aSelected;
370 }
371
372 //********************************************************************
373 void ModuleBase_WidgetValidated::filterPresentations(QList<ModuleBase_ViewerPrsPtr>& theValues)
374 {
375   QList<ModuleBase_ViewerPrsPtr> aValidatedValues;
376
377   QList<ModuleBase_ViewerPrsPtr>::const_iterator anIt = theValues.begin(), aLast = theValues.end();
378   bool isDone = false;
379   for (; anIt != aLast; anIt++) {
380     if (isValidInFilters(*anIt))
381       aValidatedValues.append(*anIt);
382   }
383   if (aValidatedValues.size() != theValues.size()) {
384     theValues.clear();
385     theValues = aValidatedValues;
386   }
387 }
388
389 //********************************************************************
390 void ModuleBase_WidgetValidated::filterCompSolids(QList<ModuleBase_ViewerPrsPtr>& theValues)
391 {
392   std::set<ResultCompSolidPtr> aCompSolids;
393   QList<ModuleBase_ViewerPrsPtr> aValidatedValues;
394
395   // Collect compsolids.
396   QList<ModuleBase_ViewerPrsPtr>::const_iterator anIt = theValues.begin(), aLast = theValues.end();
397   for (; anIt != aLast; anIt++) {
398     const ModuleBase_ViewerPrsPtr& aViewerPrs = *anIt;
399     ObjectPtr anObject = aViewerPrs->object();
400     ResultCompSolidPtr aResultCompSolid =
401       std::dynamic_pointer_cast<ModelAPI_ResultCompSolid>(anObject);
402     if(aResultCompSolid.get()) {
403       aCompSolids.insert(aResultCompSolid);
404     }
405   }
406
407   // Filter sub-solids of compsolids.
408   anIt = theValues.begin();
409   for (; anIt != aLast; anIt++) {
410     const ModuleBase_ViewerPrsPtr& aViewerPrs = *anIt;
411     ObjectPtr anObject = aViewerPrs->object();
412     ResultPtr aResult = std::dynamic_pointer_cast<ModelAPI_Result>(anObject);
413     ResultCompSolidPtr aResCompSolidPtr = ModelAPI_Tools::compSolidOwner(aResult);
414     if(aResCompSolidPtr.get() && (aCompSolids.find(aResCompSolidPtr) != aCompSolids.end())) {
415       // Skip sub-solid of compsolid.
416       continue;
417     } else {
418       aValidatedValues.append(*anIt);
419     }
420   }
421
422   if (aValidatedValues.size() != theValues.size()) {
423     theValues.clear();
424     theValues = aValidatedValues;
425   }
426 }