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