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