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