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