Salome HOME
7d88e7afcccc67b685a99b6114f17379200be2c4
[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       SelectMgr_ListOfFilter aSelectionFilters;
127       selectionFilters(aSelectionFilters);
128       /// after validation, the selection filters should be restored
129       myWorkshop->selectionActivate()->activateSelectionFilters(aSelectionFilters);
130     }
131
132     Handle(AIS_InteractiveContext) aContext = myWorkshop->viewer()->AISContext();
133     if (!aContext.IsNull()) {
134       const SelectMgr_ListOfFilter& aFilters = aContext->Filters();
135       SelectMgr_ListIteratorOfListOfFilter anIt(aFilters);
136       for (; anIt.More() && aValid; anIt.Next()) {
137         Handle(SelectMgr_Filter) aFilter = anIt.Value();
138         aValid = aFilter->IsOk(anOwner);
139       }
140     }
141     if (!isActivated)
142     {
143       myWorkshop->selectionActivate()->updateSelectionFilters();
144       clearValidatedCash();
145     }
146   }
147
148   // removes created owner
149   if (!anOwner.IsNull() && anOwner != thePrs->owner()) {
150     anOwner.Nullify();
151     myPresentedObject = ObjectPtr();
152   }
153   return aValid;
154 }
155
156 //********************************************************************
157 AttributePtr ModuleBase_WidgetValidated::attribute() const
158 {
159   return myFeature->attribute(attributeID());
160 }
161
162 //********************************************************************
163 bool ModuleBase_WidgetValidated::isValidSelection(const ModuleBase_ViewerPrsPtr& theValue)
164 {
165   bool aValid = false;
166   if (getValidState(theValue, aValid)) {
167     return aValid;
168   }
169   aValid = isValidSelectionCustom(theValue);
170   if (aValid)
171     aValid = isValidSelectionForAttribute(theValue, attribute());
172
173   storeValidState(theValue, aValid);
174   return aValid;
175 }
176
177 //********************************************************************
178 bool ModuleBase_WidgetValidated::isValidSelectionForAttribute(
179                                             const ModuleBase_ViewerPrsPtr& theValue,
180                                             const AttributePtr& theAttribute)
181 {
182   bool aValid = false;
183
184   // stores the current values of the widget attribute
185   bool isFlushesActived, isAttributeSetInitializedBlocked, isAttributeSendUpdatedBlocked;
186
187   blockAttribute(theAttribute, true, isFlushesActived, isAttributeSetInitializedBlocked,
188                  isAttributeSendUpdatedBlocked);
189
190   storeAttributeValue(theAttribute);
191
192   // saves the owner value to the widget attribute
193   aValid = setSelectionCustom(theValue);
194   if (aValid)
195     // checks the attribute validity
196     aValid = isValidAttribute(theAttribute);
197
198   // restores the current values of the widget attribute
199   restoreAttributeValue(theAttribute, aValid);
200
201   blockAttribute(theAttribute, false, isFlushesActived, isAttributeSetInitializedBlocked,
202                  isAttributeSendUpdatedBlocked);
203   /// NDS: The following rows are commented for issue #1452 (to be removed after debug)
204   /// This is not correct to perform it here because it might cause update selection and
205   /// the selection mechanizm will be circled: use the scenario of the bug with preselected point.
206   // In particular case the results are deleted and called as redisplayed inside of this
207   // highlight-selection, to they must be flushed as soon as possible.
208   // Example: selection of group-vertices subshapes with shift pressend on body. Without
209   //  these 4 lines below the application crashes because of left presentations on
210   //  removed results still in the viewer.
211   /*static Events_ID aDeletedEvent = Events_Loop::eventByName(EVENT_OBJECT_DELETED);
212   static Events_ID aRedispEvent = Events_Loop::eventByName(EVENT_OBJECT_TO_REDISPLAY);
213   Events_Loop::loop()->flush(aDeletedEvent);
214   Events_Loop::loop()->flush(aRedispEvent);
215   */
216   return aValid;
217 }
218
219 //********************************************************************
220 bool ModuleBase_WidgetValidated::isValidSelectionCustom(const ModuleBase_ViewerPrsPtr& thePrs)
221 {
222   return true;
223 }
224
225 //********************************************************************
226 bool ModuleBase_WidgetValidated::isValidAttribute(const AttributePtr& theAttribute)
227 {
228   SessionPtr aMgr = ModelAPI_Session::get();
229   ModelAPI_ValidatorsFactory* aFactory = aMgr->validators();
230   std::string aValidatorID;
231   Events_InfoMessage anError;
232   return aFactory->validate(theAttribute, aValidatorID, anError);
233 }
234
235 //********************************************************************
236 bool ModuleBase_WidgetValidated::isFilterActivated() const
237 {
238   bool isActivated = false;
239
240   Handle(SelectMgr_Filter) aSelFilter = myWorkshop->validatorFilter();
241   ModuleBase_IViewer* aViewer = myWorkshop->viewer();
242
243   return aViewer->hasSelectionFilter(aSelFilter);
244 }
245
246 //********************************************************************
247 void ModuleBase_WidgetValidated::selectionFilters(SelectMgr_ListOfFilter& theSelectionFilters)
248 {
249   theSelectionFilters.Append(myWorkshop->validatorFilter());
250 }
251
252 //********************************************************************
253 void ModuleBase_WidgetValidated::blockAttribute(const AttributePtr& theAttribute,
254                                                 const bool& theToBlock,
255                                                 bool& isFlushesActived,
256                                                 bool& isAttributeSetInitializedBlocked,
257                                                 bool& isAttributeSendUpdatedBlocked)
258 {
259   blockFeatureAttribute(theAttribute, myFeature, theToBlock, isFlushesActived,
260                         isAttributeSetInitializedBlocked, isAttributeSendUpdatedBlocked);
261 }
262
263 //********************************************************************
264 void ModuleBase_WidgetValidated::blockFeatureAttribute(const AttributePtr& theAttribute,
265                                                 const FeaturePtr& theFeature,
266                                                 const bool& theToBlock,
267                                                 bool& isFlushesActived,
268                                                 bool& isAttributeSetInitializedBlocked,
269                                                 bool& isAttributeSendUpdatedBlocked)
270 {
271   Events_Loop* aLoop = Events_Loop::loop();
272   DataPtr aData = theFeature->data();
273   if (theToBlock) {
274     // blocks the flush signals to avoid the temporary objects visualization in the viewer
275     // they should not be shown in order to do not lose highlight by erasing them
276     isFlushesActived = aLoop->activateFlushes(false);
277
278     isAttributeSendUpdatedBlocked = aData->blockSendAttributeUpdated(true);
279     isAttributeSetInitializedBlocked = theAttribute->blockSetInitialized(true);
280   }
281   else {
282     aData->blockSendAttributeUpdated(isAttributeSendUpdatedBlocked, false);
283     theAttribute->blockSetInitialized(isAttributeSetInitializedBlocked);
284     aLoop->activateFlushes(isFlushesActived);
285   }
286 }
287
288 //********************************************************************
289 void ModuleBase_WidgetValidated::storeValidState(const ModuleBase_ViewerPrsPtr& theValue,
290                                                  const bool theValid)
291 {
292   GeomShapePtr aShape = theValue.get() ? theValue->shape() : GeomShapePtr();
293   if (aShape.get()) {
294     if (theValid) {
295       const TopoDS_Shape& aTDShape = aShape->impl<TopoDS_Shape>();
296       bool aValidPrsContains = myValidPrs.IsBound(aTDShape) &&
297                                theValue.get()->isEqual(myValidPrs.Find(aTDShape).get());
298       if (!aValidPrsContains) {
299   #ifdef LIST_OF_VALID_PRS
300         myValidPrs.append(theValue);
301   #else
302         myValidPrs.Bind(aTDShape, theValue);
303   #endif
304       // the commented code will be useful when the valid state of the presentation
305       // will be changable between activate/deactivate. Currently it does not happen.
306       //if (anInvalidPrs)
307       //  myInvalidPrs.removeOne(theValue);
308       }
309     }
310     else { // !theValid
311       if (aShape.get()) {
312         const TopoDS_Shape& aTDShape = aShape->impl<TopoDS_Shape>();
313         bool anIValidPrsContains = myInvalidPrs.IsBound(aTDShape) &&
314                                    theValue.get()->isEqual(myInvalidPrs.Find(aTDShape).get());
315         if (!anIValidPrsContains) {
316     #ifdef LIST_OF_VALID_PRS
317           myInvalidPrs.append(theValue);
318     #else
319           myInvalidPrs.Bind(aTDShape, theValue);
320     #endif
321         //if (!aValidPrs)
322         //  myValidPrs.removeOne(theValue);
323         }
324       }
325     }
326   }
327   #ifdef DEBUG_VALID_STATE
328     qDebug(QString("storeValidState: myValidPrs.size() = %1, myInvalidPrs.size() = %2")
329                    .arg(myValidPrs.count())
330                    .arg(myInvalidPrs.count()).toStdString().c_str());
331   #endif
332 }
333
334 //********************************************************************
335 bool ModuleBase_WidgetValidated::getValidState(const ModuleBase_ViewerPrsPtr& theValue,
336                                                bool& theValid)
337 {
338   if (!theValue.get())
339     return false;
340
341 #ifdef LIST_OF_VALID_PRS
342   bool aValidPrsContains = myValidPrs.contains(theValue);
343   bool anInvalidPrsContains = myInvalidPrs.contains(theValue);
344 #else
345   GeomShapePtr aShape = theValue.get() ? theValue->shape() : GeomShapePtr();
346   if (!aShape.get())
347     return false;
348
349   const TopoDS_Shape& aTDShape = aShape->impl<TopoDS_Shape>();
350   bool aValidPrsContains = myValidPrs.IsBound(aTDShape) &&
351                            theValue.get()->isEqual(myValidPrs.Find(aTDShape).get());
352
353   bool anInvalidPrsContains = myInvalidPrs.IsBound(aTDShape) &&
354                               theValue.get()->isEqual(myInvalidPrs.Find(aTDShape).get());
355   /*
356   bool aValidPrsContains = false, anInvalidPrsContains = false;
357   GeomShapePtr aShape = theValue.get() ? theValue->shape() : GeomShapePtr();
358   if (aShape.get()) {
359     aValidPrsContains = myValidPrs.contains(aShape);
360     anInvalidPrsContains = myInvalidPrs.contains(aShape);
361
362     if (aValidPrsContains)
363       aValidPrsContains = theValue == myValidPrs[aShape];
364     else
365       anInvalidPrsContains = theValue == myInvalidPrs[aShape];*/
366 #endif
367
368   if (aValidPrsContains)
369     theValid = true;
370   else if (anInvalidPrsContains)
371     theValid = false;
372
373   return aValidPrsContains || anInvalidPrsContains;
374 }
375
376 //********************************************************************
377 QList<ModuleBase_ViewerPrsPtr> ModuleBase_WidgetValidated::getFilteredSelected()
378 {
379   QList<ModuleBase_ViewerPrsPtr> aSelected = myWorkshop->selection()->getSelected(
380                                                        ModuleBase_ISelection::Viewer);
381
382   QList<ModuleBase_ViewerPrsPtr> anOBSelected = myWorkshop->selection()->getSelected(
383                                                        ModuleBase_ISelection::Browser);
384   // filter the OB presentations
385   filterPresentations(anOBSelected);
386   if (!anOBSelected.isEmpty())
387     ModuleBase_ISelection::appendSelected(anOBSelected, aSelected);
388
389   filterCompSolids(aSelected);
390
391   return aSelected;
392 }
393
394 //********************************************************************
395 void ModuleBase_WidgetValidated::filterPresentations(QList<ModuleBase_ViewerPrsPtr>& theValues)
396 {
397   QList<ModuleBase_ViewerPrsPtr> aValidatedValues;
398
399   QList<ModuleBase_ViewerPrsPtr>::const_iterator anIt = theValues.begin(), aLast = theValues.end();
400   bool isDone = false;
401   for (; anIt != aLast; anIt++) {
402     if (isValidInFilters(*anIt))
403       aValidatedValues.append(*anIt);
404   }
405   if (aValidatedValues.size() != theValues.size()) {
406     theValues.clear();
407     theValues = aValidatedValues;
408   }
409 }
410
411 //********************************************************************
412 void ModuleBase_WidgetValidated::filterCompSolids(QList<ModuleBase_ViewerPrsPtr>& theValues)
413 {
414   std::set<ResultCompSolidPtr> aCompSolids;
415   QList<ModuleBase_ViewerPrsPtr> aValidatedValues;
416
417   // Collect compsolids.
418   QList<ModuleBase_ViewerPrsPtr>::const_iterator anIt = theValues.begin(), aLast = theValues.end();
419   for (; anIt != aLast; anIt++) {
420     const ModuleBase_ViewerPrsPtr& aViewerPrs = *anIt;
421     ObjectPtr anObject = aViewerPrs->object();
422     ResultCompSolidPtr aResultCompSolid =
423       std::dynamic_pointer_cast<ModelAPI_ResultCompSolid>(anObject);
424     if(aResultCompSolid.get()) {
425       aCompSolids.insert(aResultCompSolid);
426     }
427   }
428
429   // Filter sub-solids of compsolids.
430   anIt = theValues.begin();
431   for (; anIt != aLast; anIt++) {
432     const ModuleBase_ViewerPrsPtr& aViewerPrs = *anIt;
433     ObjectPtr anObject = aViewerPrs->object();
434     ResultPtr aResult = std::dynamic_pointer_cast<ModelAPI_Result>(anObject);
435     ResultCompSolidPtr aResCompSolidPtr = ModelAPI_Tools::compSolidOwner(aResult);
436     if(aResCompSolidPtr.get() && (aCompSolids.find(aResCompSolidPtr) != aCompSolids.end())) {
437       // Skip sub-solid of compsolid.
438       continue;
439     } else {
440       aValidatedValues.append(*anIt);
441     }
442   }
443
444   if (aValidatedValues.size() != theValues.size()) {
445     theValues.clear();
446     theValues = aValidatedValues;
447   }
448 }