]> SALOME platform Git repositories - modules/shaper.git/blob - src/ModuleBase/ModuleBase_WidgetValidated.cpp
Salome HOME
Merge remote-tracking branch 'remotes/origin/HigherLevelObjectsHistory'
[modules/shaper.git] / src / ModuleBase / ModuleBase_WidgetValidated.cpp
1 // Copyright (C) 2014-2019  CEA/DEN, EDF R&D
2 //
3 // This library is free software; you can redistribute it and/or
4 // modify it under the terms of the GNU Lesser General Public
5 // License as published by the Free Software Foundation; either
6 // version 2.1 of the License, or (at your option) any later version.
7 //
8 // This library is distributed in the hope that it will be useful,
9 // but WITHOUT ANY WARRANTY; without even the implied warranty of
10 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
11 // Lesser General Public License for more details.
12 //
13 // You should have received a copy of the GNU Lesser General Public
14 // License along with this library; if not, write to the Free Software
15 // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307 USA
16 //
17 // See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
18 //
19
20 #include <ModuleBase_WidgetValidated.h>
21 #include <ModuleBase_IModule.h>
22 #include <ModuleBase_IViewer.h>
23 #include <ModuleBase_IWorkshop.h>
24 #include <ModuleBase_ISelection.h>
25 #include <ModuleBase_ISelectionActivate.h>
26 #include <ModuleBase_WidgetSelectorStore.h>
27 #include <ModuleBase_ViewerPrs.h>
28
29 #include <Events_InfoMessage.h>
30
31 #include <ModelAPI_Session.h>
32 #include <ModelAPI_Validator.h>
33 #include <ModelAPI_AttributeValidator.h>
34 #include <ModelAPI_Events.h>
35 #include <ModelAPI_ResultBody.h>
36 #include <ModelAPI_Tools.h>
37 #include <ModelAPI_AttributeSelection.h>
38
39 #include <SelectMgr_ListIteratorOfListOfFilter.hxx>
40 #include <SelectMgr_EntityOwner.hxx>
41 #include <StdSelect_BRepOwner.hxx>
42
43 #include <Events_Loop.h>
44
45 #include <QWidget>
46
47 //#define DEBUG_VALID_STATE
48
49 ModuleBase_WidgetValidated::ModuleBase_WidgetValidated(QWidget* theParent,
50                                                        ModuleBase_IWorkshop* theWorkshop,
51                                                        const Config_WidgetAPI* theData)
52 : ModuleBase_ModelWidget(theParent, theData),
53   myWorkshop(theWorkshop), myIsInValidate(false)
54 {
55   myAttributeStore = new ModuleBase_WidgetSelectorStore();
56 }
57
58 ModuleBase_WidgetValidated::~ModuleBase_WidgetValidated()
59 {
60   delete myAttributeStore;
61 }
62
63 //********************************************************************
64 ObjectPtr ModuleBase_WidgetValidated::findPresentedObject(const AISObjectPtr& theAIS) const
65 {
66   return myPresentedObject;
67 }
68
69 //********************************************************************
70 void ModuleBase_WidgetValidated::deactivate()
71 {
72   clearValidatedCash();
73 }
74
75 //********************************************************************
76 void ModuleBase_WidgetValidated::clearValidatedCash()
77 {
78 #ifdef DEBUG_VALID_STATE
79   qDebug("clearValidatedCash");
80 #endif
81   myValidPrs.Clear();
82   myInvalidPrs.Clear();
83 }
84
85 //********************************************************************
86 void ModuleBase_WidgetValidated::storeAttributeValue(const AttributePtr& theAttribute)
87 {
88   myIsInValidate = true;
89   myAttributeStore->storeAttributeValue(theAttribute, myWorkshop);
90 }
91
92 //********************************************************************
93 void ModuleBase_WidgetValidated::restoreAttributeValue(const AttributePtr& theAttribute,
94                                                        const bool theValid)
95 {
96   myIsInValidate = false;
97   myAttributeStore->restoreAttributeValue(theAttribute, myWorkshop);
98 }
99
100 //********************************************************************
101 bool ModuleBase_WidgetValidated::isValidInFilters(const ModuleBase_ViewerPrsPtr& thePrs)
102 {
103   bool aValid = true;
104   Handle(SelectMgr_EntityOwner) anOwner = thePrs->owner();
105
106   // if an owner is null, the selection happens in the Object browser.
107   // creates a selection owner on the base of object shape and the object AIS object
108   if (anOwner.IsNull() && thePrs->owner().IsNull() && thePrs->object().get()) {
109     ResultPtr aResult = myWorkshop->selection()->getResult(thePrs);
110     GeomShapePtr aShape = aResult.get() ? aResult->shape() : GeomShapePtr();
111     // some results have no shape, e.g. the parameter one. So, they should not be validated
112     if (aShape.get()) {
113       const TopoDS_Shape aTDShape = aShape->impl<TopoDS_Shape>();
114       Handle(AIS_InteractiveObject) anIO = myWorkshop->selection()->getIO(thePrs);
115       anOwner = new StdSelect_BRepOwner(aTDShape, anIO);
116       myPresentedObject = aResult;
117     }
118     else {
119       FeaturePtr aFeature = ModelAPI_Feature::feature(thePrs->object());
120       if (aFeature.get()) {
121         // Use feature as a reference to all its results
122         myPresentedObject = aFeature;
123         AttributePtr anAttr = attribute();
124         std::string aType = anAttr->attributeType();
125
126         // Check that results of Feature is acceptable by filters for selection attribute
127         if (aType == ModelAPI_AttributeSelection::typeId()) {
128           AttributeSelectionPtr aSelectAttr =
129             std::dynamic_pointer_cast<ModelAPI_AttributeSelection>(anAttr);
130           aSelectAttr->setValue(myPresentedObject, GeomShapePtr(), true);
131           GeomShapePtr aShape = aSelectAttr->value();
132           if (!aShape.get() && aSelectAttr->contextFeature().get() &&
133             aSelectAttr->contextFeature()->firstResult().get()) {
134             aShape = aSelectAttr->contextFeature()->firstResult()->shape();
135           }
136           if (aShape.get()) {
137             const TopoDS_Shape aTDShape = aShape->impl<TopoDS_Shape>();
138             Handle(AIS_InteractiveObject) anIO = myWorkshop->selection()->getIO(thePrs);
139             anOwner = new StdSelect_BRepOwner(aTDShape, anIO);
140           }
141           else
142             aValid = false;
143           aSelectAttr->setValue(ObjectPtr(), GeomShapePtr(), true);
144         }
145         else {
146           ResultPtr aResult = aFeature->firstResult();
147           if (aResult.get()) {
148             GeomShapePtr aShapePtr = ModelAPI_Tools::shape(aResult);
149             if (aShapePtr.get()) {
150               const TopoDS_Shape aTDShape = aShapePtr->impl<TopoDS_Shape>();
151               Handle(AIS_InteractiveObject) anIO = myWorkshop->selection()->getIO(thePrs);
152               anOwner = new StdSelect_BRepOwner(aTDShape, anIO);
153             }
154           }
155           aValid = !anOwner.IsNull(); // only results with a shape can be filtered
156         }
157       } else
158         aValid = false; // only results with a shape can be filtered
159     }
160   }
161   // checks the owner by the AIS context activated filters
162   if (!anOwner.IsNull()) {
163     // the widget validator filter should be active, but during check by preselection
164     // it is not yet activated, so we need to activate/deactivate it manually
165     bool isActivated = isFilterActivated();
166     if (!isActivated) {
167       QIntList aModuleSelectionFilters = myWorkshop->module()->selectionFilters();
168       SelectMgr_ListOfFilter aSelectionFilters;
169       selectionFilters(aModuleSelectionFilters, aSelectionFilters);
170       /// after validation, the selection filters should be restored
171       myWorkshop->selectionActivate()->activateSelectionFilters(aSelectionFilters);
172     }
173
174     Handle(AIS_InteractiveContext) aContext = myWorkshop->viewer()->AISContext();
175     if (!aContext.IsNull()) {
176       const SelectMgr_ListOfFilter& aFilters = aContext->Filters();
177       SelectMgr_ListIteratorOfListOfFilter anIt(aFilters);
178       for (; anIt.More() && aValid; anIt.Next()) {
179         Handle(SelectMgr_Filter) aFilter = anIt.Value();
180         aValid = aFilter->IsOk(anOwner);
181       }
182     }
183     if (!isActivated)
184     {
185       // reset filters set in activateSelectionFilters above
186       myWorkshop->selectionActivate()->updateSelectionFilters();
187       clearValidatedCash();
188     }
189   }
190
191   // removes created owner
192   if (!anOwner.IsNull() && anOwner != thePrs->owner()) {
193     anOwner.Nullify();
194     myPresentedObject = ObjectPtr();
195   }
196   return aValid;
197 }
198
199 //********************************************************************
200 AttributePtr ModuleBase_WidgetValidated::attribute() const
201 {
202   return myFeature->attribute(attributeID());
203 }
204
205 //********************************************************************
206 bool ModuleBase_WidgetValidated::isValidSelection(const ModuleBase_ViewerPrsPtr& theValue)
207 {
208   bool aValid = false;
209   if (getValidState(theValue, aValid)) {
210     return aValid;
211   }
212   aValid = isValidSelectionCustom(theValue);
213   if (aValid)
214     aValid = isValidSelectionForAttribute(theValue, attribute());
215
216   storeValidState(theValue, aValid);
217   return aValid;
218 }
219
220 //********************************************************************
221 bool ModuleBase_WidgetValidated::isValidSelectionForAttribute(
222                                             const ModuleBase_ViewerPrsPtr& theValue,
223                                             const AttributePtr& theAttribute)
224 {
225   bool aValid = false;
226
227   // stores the current values of the widget attribute
228   bool isFlushesActived, isAttributeSetInitializedBlocked, isAttributeSendUpdatedBlocked;
229
230   blockAttribute(theAttribute, true, isFlushesActived, isAttributeSetInitializedBlocked,
231                  isAttributeSendUpdatedBlocked);
232
233   storeAttributeValue(theAttribute);
234
235   // saves the owner value to the widget attribute
236   aValid = setSelectionCustom(theValue);
237   if (aValid)
238     // checks the attribute validity
239     aValid = isValidAttribute(theAttribute);
240
241   // restores the current values of the widget attribute
242   restoreAttributeValue(theAttribute, aValid);
243
244   blockAttribute(theAttribute, false, isFlushesActived, isAttributeSetInitializedBlocked,
245                  isAttributeSendUpdatedBlocked);
246   /// NDS: The following rows are commented for issue #1452 (to be removed after debug)
247   /// This is not correct to perform it here because it might cause update selection and
248   /// the selection mechanizm will be circled: use the scenario of the bug with preselected point.
249   // In particular case the results are deleted and called as redisplayed inside of this
250   // highlight-selection, to they must be flushed as soon as possible.
251   // Example: selection of group-vertices subshapes with shift pressend on body. Without
252   //  these 4 lines below the application crashes because of left presentations on
253   //  removed results still in the viewer.
254   /*static Events_ID aDeletedEvent = Events_Loop::eventByName(EVENT_OBJECT_DELETED);
255   static Events_ID aRedispEvent = Events_Loop::eventByName(EVENT_OBJECT_TO_REDISPLAY);
256   Events_Loop::loop()->flush(aDeletedEvent);
257   Events_Loop::loop()->flush(aRedispEvent);
258   */
259   return aValid;
260 }
261
262 //********************************************************************
263 bool ModuleBase_WidgetValidated::isValidSelectionCustom(const ModuleBase_ViewerPrsPtr& thePrs)
264 {
265   return true;
266 }
267
268 //********************************************************************
269 bool ModuleBase_WidgetValidated::isValidAttribute(const AttributePtr& theAttribute)
270 {
271   SessionPtr aMgr = ModelAPI_Session::get();
272   ModelAPI_ValidatorsFactory* aFactory = aMgr->validators();
273   std::string aValidatorID;
274   Events_InfoMessage anError;
275   return aFactory->validate(theAttribute, aValidatorID, anError);
276 }
277
278 //********************************************************************
279 bool ModuleBase_WidgetValidated::isFilterActivated() const
280 {
281   bool isActivated = false;
282
283   Handle(SelectMgr_Filter) aSelFilter = myWorkshop->validatorFilter();
284   ModuleBase_IViewer* aViewer = myWorkshop->viewer();
285
286   return aViewer->hasSelectionFilter(aSelFilter);
287 }
288
289 //********************************************************************
290 void ModuleBase_WidgetValidated::selectionFilters(QIntList& theModuleSelectionFilters,
291                                                   SelectMgr_ListOfFilter& theSelectionFilters)
292 {
293   theSelectionFilters.Append(myWorkshop->validatorFilter());
294 }
295
296 //********************************************************************
297 void ModuleBase_WidgetValidated::blockAttribute(const AttributePtr& theAttribute,
298                                                 const bool& theToBlock,
299                                                 bool& isFlushesActived,
300                                                 bool& isAttributeSetInitializedBlocked,
301                                                 bool& isAttributeSendUpdatedBlocked)
302 {
303   blockFeatureAttribute(theAttribute, myFeature, theToBlock, isFlushesActived,
304                         isAttributeSetInitializedBlocked, isAttributeSendUpdatedBlocked);
305 }
306
307 //********************************************************************
308 void ModuleBase_WidgetValidated::blockFeatureAttribute(const AttributePtr& theAttribute,
309                                                 const FeaturePtr& theFeature,
310                                                 const bool& theToBlock,
311                                                 bool& isFlushesActived,
312                                                 bool& isAttributeSetInitializedBlocked,
313                                                 bool& isAttributeSendUpdatedBlocked)
314 {
315   Events_Loop* aLoop = Events_Loop::loop();
316   DataPtr aData = theFeature->data();
317   if (theToBlock) {
318     // blocks the flush signals to avoid the temporary objects visualization in the viewer
319     // they should not be shown in order to do not lose highlight by erasing them
320     isFlushesActived = aLoop->activateFlushes(false);
321
322     isAttributeSendUpdatedBlocked = aData->blockSendAttributeUpdated(true);
323     isAttributeSetInitializedBlocked = theAttribute->blockSetInitialized(true);
324   }
325   else {
326     aData->blockSendAttributeUpdated(isAttributeSendUpdatedBlocked, false);
327     theAttribute->blockSetInitialized(isAttributeSetInitializedBlocked);
328     aLoop->activateFlushes(isFlushesActived);
329   }
330 }
331
332 //********************************************************************
333 void ModuleBase_WidgetValidated::storeValidState(const ModuleBase_ViewerPrsPtr& theValue,
334                                                  const bool theValid)
335 {
336   GeomShapePtr aShape = theValue.get() ? theValue->shape() : GeomShapePtr();
337   if (aShape.get()) {
338     if (theValid) {
339       const TopoDS_Shape& aTDShape = aShape->impl<TopoDS_Shape>();
340       bool aValidPrsContains = myValidPrs.IsBound(aTDShape) &&
341                                theValue.get()->isEqual(myValidPrs.Find(aTDShape).get());
342       if (!aValidPrsContains) {
343   #ifdef LIST_OF_VALID_PRS
344         myValidPrs.append(theValue);
345   #else
346         myValidPrs.Bind(aTDShape, theValue);
347   #endif
348       // the commented code will be useful when the valid state of the presentation
349       // will be changable between activate/deactivate. Currently it does not happen.
350       //if (anInvalidPrs)
351       //  myInvalidPrs.removeOne(theValue);
352       }
353     }
354     else { // !theValid
355       if (aShape.get()) {
356         const TopoDS_Shape& aTDShape = aShape->impl<TopoDS_Shape>();
357         bool anIValidPrsContains = myInvalidPrs.IsBound(aTDShape) &&
358                                    theValue.get()->isEqual(myInvalidPrs.Find(aTDShape).get());
359         if (!anIValidPrsContains) {
360     #ifdef LIST_OF_VALID_PRS
361           myInvalidPrs.append(theValue);
362     #else
363           myInvalidPrs.Bind(aTDShape, theValue);
364     #endif
365         //if (!aValidPrs)
366         //  myValidPrs.removeOne(theValue);
367         }
368       }
369     }
370   }
371   #ifdef DEBUG_VALID_STATE
372     qDebug(QString("storeValidState: myValidPrs.size() = %1, myInvalidPrs.size() = %2")
373                    .arg(myValidPrs.count())
374                    .arg(myInvalidPrs.count()).toStdString().c_str());
375   #endif
376 }
377
378 //********************************************************************
379 bool ModuleBase_WidgetValidated::getValidState(const ModuleBase_ViewerPrsPtr& theValue,
380                                                bool& theValid)
381 {
382   if (!theValue.get())
383     return false;
384
385 #ifdef LIST_OF_VALID_PRS
386   bool aValidPrsContains = myValidPrs.contains(theValue);
387   bool anInvalidPrsContains = myInvalidPrs.contains(theValue);
388 #else
389   GeomShapePtr aShape = theValue.get() ? theValue->shape() : GeomShapePtr();
390   if (!aShape.get())
391     return false;
392
393   const TopoDS_Shape& aTDShape = aShape->impl<TopoDS_Shape>();
394   bool aValidPrsContains = myValidPrs.IsBound(aTDShape) &&
395                            theValue.get()->isEqual(myValidPrs.Find(aTDShape).get());
396
397   bool anInvalidPrsContains = myInvalidPrs.IsBound(aTDShape) &&
398                               theValue.get()->isEqual(myInvalidPrs.Find(aTDShape).get());
399   /*
400   bool aValidPrsContains = false, anInvalidPrsContains = false;
401   GeomShapePtr aShape = theValue.get() ? theValue->shape() : GeomShapePtr();
402   if (aShape.get()) {
403     aValidPrsContains = myValidPrs.contains(aShape);
404     anInvalidPrsContains = myInvalidPrs.contains(aShape);
405
406     if (aValidPrsContains)
407       aValidPrsContains = theValue == myValidPrs[aShape];
408     else
409       anInvalidPrsContains = theValue == myInvalidPrs[aShape];*/
410 #endif
411
412   if (aValidPrsContains)
413     theValid = true;
414   else if (anInvalidPrsContains)
415     theValid = false;
416
417   return aValidPrsContains || anInvalidPrsContains;
418 }
419
420 //********************************************************************
421 QList<ModuleBase_ViewerPrsPtr> ModuleBase_WidgetValidated::getFilteredSelected()
422 {
423   QList<ModuleBase_ViewerPrsPtr> aSelected = myWorkshop->selection()->getSelected(
424                                                        ModuleBase_ISelection::Viewer);
425
426   QList<ModuleBase_ViewerPrsPtr> anOBSelected = myWorkshop->selection()->getSelected(
427                                                        ModuleBase_ISelection::Browser);
428   // filter the OB presentations
429   filterPresentations(anOBSelected);
430   if (!anOBSelected.isEmpty())
431     ModuleBase_ISelection::appendSelected(anOBSelected, aSelected);
432
433   filterCompSolids(aSelected);
434
435   return aSelected;
436 }
437
438 //********************************************************************
439 void ModuleBase_WidgetValidated::filterPresentations(QList<ModuleBase_ViewerPrsPtr>& theValues)
440 {
441   QList<ModuleBase_ViewerPrsPtr> aValidatedValues;
442
443   QList<ModuleBase_ViewerPrsPtr>::const_iterator anIt = theValues.begin(), aLast = theValues.end();
444   bool isDone = false;
445   for (; anIt != aLast; anIt++) {
446     if (isValidInFilters(*anIt))
447       aValidatedValues.append(*anIt);
448   }
449   if (aValidatedValues.size() != theValues.size()) {
450     theValues.clear();
451     theValues = aValidatedValues;
452   }
453 }
454
455 //********************************************************************
456 void ModuleBase_WidgetValidated::filterCompSolids(QList<ModuleBase_ViewerPrsPtr>& theValues)
457 {
458   std::set<ResultPtr> aFilterOut; // all objects that must be filtered out with their children
459   QList<ModuleBase_ViewerPrsPtr> aValidatedValues;
460
461   // Collect compsolids.
462   QList<ModuleBase_ViewerPrsPtr>::const_iterator anIt = theValues.begin(), aLast = theValues.end();
463   for(; anIt != aLast; anIt++) {
464     const ModuleBase_ViewerPrsPtr& aViewerPrs = *anIt;
465     ObjectPtr anObject = aViewerPrs->object();
466     ResultBodyPtr aResultCompSolid =
467       std::dynamic_pointer_cast<ModelAPI_ResultBody>(anObject);
468     if (aResultCompSolid.get()) {
469       for(int aSubIndex = 0; aSubIndex < aResultCompSolid->numberOfSubs(); aSubIndex++)
470         aFilterOut.insert(aResultCompSolid->subResult(aSubIndex));
471     } else { // it could be a whole feature selected, so, add all results of this feature
472       FeaturePtr aFeature = std::dynamic_pointer_cast<ModelAPI_Feature>(anObject);
473       if (aFeature.get()) {
474         std::list<ResultPtr>::const_iterator aRes = aFeature->results().cbegin();
475         for(; aRes != aFeature->results().cend(); aRes++)
476           aFilterOut.insert(*aRes);
477       }
478     }
479   }
480
481   // Filter sub-solids of compsolids.
482   anIt = theValues.begin();
483   for(; anIt != aLast; anIt++) {
484     const ModuleBase_ViewerPrsPtr& aViewerPrs = *anIt;
485     ObjectPtr anObject = aViewerPrs->object();
486     ResultPtr aResult = std::dynamic_pointer_cast<ModelAPI_Result>(anObject);
487     while(aResult.get()) {
488       if (aFilterOut.find(aResult) != aFilterOut.end()) // skip if parent is filtered out
489         break;
490       aResult = ModelAPI_Tools::bodyOwner(aResult); // iterate all parents
491     }
492     if (aResult.get()) {
493       continue; // skip
494     } else {
495       aValidatedValues.append(*anIt);
496     }
497   }
498
499   if (aValidatedValues.size() != theValues.size()) {
500     theValues.clear();
501     theValues = aValidatedValues;
502   }
503 }