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