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_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_ResultCompSolid.h>
37 #include <ModelAPI_Tools.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       aValid = false; // only results with a shape can be filtered
120   }
121   // checks the owner by the AIS context activated filters
122   if (!anOwner.IsNull()) {
123     // the widget validator filter should be active, but during check by preselection
124     // it is not yet activated, so we need to activate/deactivate it manually
125     bool isActivated = isFilterActivated();
126     if (!isActivated) {
127       QIntList aModuleSelectionFilters = myWorkshop->module()->selectionFilters();
128       SelectMgr_ListOfFilter aSelectionFilters;
129       selectionFilters(aModuleSelectionFilters, aSelectionFilters);
130       /// after validation, the selection filters should be restored
131       myWorkshop->selectionActivate()->activateSelectionFilters(aSelectionFilters);
132     }
133
134     Handle(AIS_InteractiveContext) aContext = myWorkshop->viewer()->AISContext();
135     if (!aContext.IsNull()) {
136       const SelectMgr_ListOfFilter& aFilters = aContext->Filters();
137       SelectMgr_ListIteratorOfListOfFilter anIt(aFilters);
138       for (; anIt.More() && aValid; anIt.Next()) {
139         Handle(SelectMgr_Filter) aFilter = anIt.Value();
140         aValid = aFilter->IsOk(anOwner);
141       }
142     }
143     if (!isActivated)
144     {
145       // reset filters set in activateSelectionFilters above
146       myWorkshop->selectionActivate()->updateSelectionFilters();
147       clearValidatedCash();
148     }
149   }
150
151   // removes created owner
152   if (!anOwner.IsNull() && anOwner != thePrs->owner()) {
153     anOwner.Nullify();
154     myPresentedObject = ObjectPtr();
155   }
156   return aValid;
157 }
158
159 //********************************************************************
160 AttributePtr ModuleBase_WidgetValidated::attribute() const
161 {
162   return myFeature->attribute(attributeID());
163 }
164
165 //********************************************************************
166 bool ModuleBase_WidgetValidated::isValidSelection(const ModuleBase_ViewerPrsPtr& theValue)
167 {
168   bool aValid = false;
169   if (getValidState(theValue, aValid)) {
170     return aValid;
171   }
172   aValid = isValidSelectionCustom(theValue);
173   if (aValid)
174     aValid = isValidSelectionForAttribute(theValue, attribute());
175
176   storeValidState(theValue, aValid);
177   return aValid;
178 }
179
180 //********************************************************************
181 bool ModuleBase_WidgetValidated::isValidSelectionForAttribute(
182                                             const ModuleBase_ViewerPrsPtr& theValue,
183                                             const AttributePtr& theAttribute)
184 {
185   bool aValid = false;
186
187   // stores the current values of the widget attribute
188   bool isFlushesActived, isAttributeSetInitializedBlocked, isAttributeSendUpdatedBlocked;
189
190   blockAttribute(theAttribute, true, isFlushesActived, isAttributeSetInitializedBlocked,
191                  isAttributeSendUpdatedBlocked);
192
193   storeAttributeValue(theAttribute);
194
195   // saves the owner value to the widget attribute
196   aValid = setSelectionCustom(theValue);
197   if (aValid)
198     // checks the attribute validity
199     aValid = isValidAttribute(theAttribute);
200
201   // restores the current values of the widget attribute
202   restoreAttributeValue(theAttribute, aValid);
203
204   blockAttribute(theAttribute, false, isFlushesActived, isAttributeSetInitializedBlocked,
205                  isAttributeSendUpdatedBlocked);
206   /// NDS: The following rows are commented for issue #1452 (to be removed after debug)
207   /// This is not correct to perform it here because it might cause update selection and
208   /// the selection mechanizm will be circled: use the scenario of the bug with preselected point.
209   // In particular case the results are deleted and called as redisplayed inside of this
210   // highlight-selection, to they must be flushed as soon as possible.
211   // Example: selection of group-vertices subshapes with shift pressend on body. Without
212   //  these 4 lines below the application crashes because of left presentations on
213   //  removed results still in the viewer.
214   /*static Events_ID aDeletedEvent = Events_Loop::eventByName(EVENT_OBJECT_DELETED);
215   static Events_ID aRedispEvent = Events_Loop::eventByName(EVENT_OBJECT_TO_REDISPLAY);
216   Events_Loop::loop()->flush(aDeletedEvent);
217   Events_Loop::loop()->flush(aRedispEvent);
218   */
219   return aValid;
220 }
221
222 //********************************************************************
223 bool ModuleBase_WidgetValidated::isValidSelectionCustom(const ModuleBase_ViewerPrsPtr& thePrs)
224 {
225   return true;
226 }
227
228 //********************************************************************
229 bool ModuleBase_WidgetValidated::isValidAttribute(const AttributePtr& theAttribute)
230 {
231   SessionPtr aMgr = ModelAPI_Session::get();
232   ModelAPI_ValidatorsFactory* aFactory = aMgr->validators();
233   std::string aValidatorID;
234   Events_InfoMessage anError;
235   return aFactory->validate(theAttribute, aValidatorID, anError);
236 }
237
238 //********************************************************************
239 bool ModuleBase_WidgetValidated::isFilterActivated() const
240 {
241   bool isActivated = false;
242
243   Handle(SelectMgr_Filter) aSelFilter = myWorkshop->validatorFilter();
244   ModuleBase_IViewer* aViewer = myWorkshop->viewer();
245
246   return aViewer->hasSelectionFilter(aSelFilter);
247 }
248
249 //********************************************************************
250 void ModuleBase_WidgetValidated::selectionFilters(QIntList& theModuleSelectionFilters,
251                                                   SelectMgr_ListOfFilter& theSelectionFilters)
252 {
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 }