Salome HOME
ModuleBase_ViewerPrs is wrapped into shared_ptr.
[modules/shaper.git] / src / ModuleBase / ModuleBase_WidgetValidated.cpp
1 // Copyright (C) 2014-20xx CEA/DEN, EDF R&D
2
3 #include <ModuleBase_WidgetValidated.h>
4 #include <ModuleBase_FilterFactory.h>
5 #include <ModuleBase_IViewer.h>
6 #include <ModuleBase_ISelection.h>
7 #include <ModuleBase_WidgetSelectorStore.h>
8
9 #include <ModelAPI_Session.h>
10 #include <ModelAPI_Validator.h>
11 #include <ModelAPI_AttributeValidator.h>
12 #include <ModelAPI_Events.h>
13 #include <ModelAPI_ResultCompSolid.h>
14 #include <ModelAPI_Tools.h>
15
16 #include <SelectMgr_ListIteratorOfListOfFilter.hxx>
17 #include <SelectMgr_EntityOwner.hxx>
18 #include <StdSelect_BRepOwner.hxx>
19
20 #include <Events_Loop.h>
21
22 #include <QWidget>
23
24 //#define DEBUG_VALID_STATE
25
26 ModuleBase_WidgetValidated::ModuleBase_WidgetValidated(QWidget* theParent,
27                                                        ModuleBase_IWorkshop* theWorkshop,
28                                                        const Config_WidgetAPI* theData)
29 : ModuleBase_ModelWidget(theParent, theData),
30   myWorkshop(theWorkshop)
31 {
32   myAttributeStore = new ModuleBase_WidgetSelectorStore();
33 }
34
35 ModuleBase_WidgetValidated::~ModuleBase_WidgetValidated()
36 {
37   delete myAttributeStore;
38 }
39
40 //********************************************************************
41 ObjectPtr ModuleBase_WidgetValidated::findPresentedObject(const AISObjectPtr& theAIS) const
42 {
43   return myPresentedObject;
44 }
45
46 //********************************************************************
47 void ModuleBase_WidgetValidated::clearValidatedCash()
48 {
49 #ifdef DEBUG_VALID_STATE
50   qDebug("clearValidatedState");
51 #endif
52   myValidPrs.clear();
53   myInvalidPrs.clear();
54 }
55
56 //********************************************************************
57 void ModuleBase_WidgetValidated::storeAttributeValue(const AttributePtr& theAttribute)
58 {
59   myIsInValidate = true;
60   myAttributeStore->storeAttributeValue(theAttribute, myWorkshop);
61 }
62
63 //********************************************************************
64 void ModuleBase_WidgetValidated::restoreAttributeValue(const AttributePtr& theAttribute,
65                                                        const bool theValid)
66 {
67   myIsInValidate = false;
68   myAttributeStore->restoreAttributeValue(theAttribute, myWorkshop);
69 }
70
71 //********************************************************************
72 bool ModuleBase_WidgetValidated::isValidInFilters(const ModuleBase_ViewerPrsPtr& thePrs)
73 {
74   bool aValid = true;
75   Handle(SelectMgr_EntityOwner) anOwner = thePrs->owner();
76
77   // if an owner is null, the selection happens in the Object browser.
78   // creates a selection owner on the base of object shape and the object AIS object
79   if (anOwner.IsNull() && thePrs->owner().IsNull() && thePrs->object().get()) {
80     ResultPtr aResult = myWorkshop->selection()->getResult(thePrs);
81     if (aResult.get() && aResult->shape().get()) {
82       // some results have no shape, e.g. the parameter one. So, they should not be validated
83       GeomShapePtr aShape = aResult->shape();
84       const TopoDS_Shape aTDShape = aShape->impl<TopoDS_Shape>();
85       Handle(AIS_InteractiveObject) anIO = myWorkshop->selection()->getIO(thePrs);
86       anOwner = new StdSelect_BRepOwner(aTDShape, anIO);
87       myPresentedObject = aResult;
88     }
89     else
90       aValid = false; // only results with a shape can be filtered
91   }
92   // checks the owner by the AIS context activated filters
93   if (!anOwner.IsNull()) {
94     // the widget validator filter should be active, but during check by preselection
95     // it is not yet activated, so we need to activate/deactivate it manually
96     bool isActivated = isFilterActivated();
97     if (!isActivated)
98       activateFilters(true);
99
100     Handle(AIS_InteractiveContext) aContext = myWorkshop->viewer()->AISContext();
101     if (!aContext.IsNull()) {
102       const SelectMgr_ListOfFilter& aFilters = aContext->Filters();
103       SelectMgr_ListIteratorOfListOfFilter anIt(aFilters);
104       for (; anIt.More() && aValid; anIt.Next()) {
105         Handle(SelectMgr_Filter) aFilter = anIt.Value();
106         aValid = aFilter->IsOk(anOwner);
107       }
108     }
109     if (!isActivated)
110       activateFilters(false);
111   }
112
113   // removes created owner
114   if (!anOwner.IsNull() && anOwner != thePrs->owner()) {
115     anOwner.Nullify();
116     myPresentedObject = ObjectPtr();
117   }
118   return aValid;
119 }
120
121 //********************************************************************
122 AttributePtr ModuleBase_WidgetValidated::attribute() const
123 {
124   return myFeature->attribute(attributeID());
125 }
126
127 //********************************************************************
128 bool ModuleBase_WidgetValidated::isValidSelection(const ModuleBase_ViewerPrsPtr& theValue)
129 {
130   bool aValid = false;
131   if (getValidState(theValue, aValid)) {
132     return aValid;
133   }
134   aValid = isValidSelectionCustom(theValue);
135   if (aValid)
136     aValid = isValidSelectionForAttribute(theValue, attribute());
137
138   storeValidState(theValue, aValid);
139   return aValid;
140 }
141
142 //********************************************************************
143 bool ModuleBase_WidgetValidated::isValidSelectionForAttribute(const ModuleBase_ViewerPrsPtr& theValue,
144                                                               const AttributePtr& theAttribute)
145 {
146   bool aValid = false;
147
148   // stores the current values of the widget attribute
149   bool isFlushesActived, isAttributeSetInitializedBlocked;
150
151   blockAttribute(theAttribute, true, isFlushesActived, isAttributeSetInitializedBlocked);
152
153   storeAttributeValue(theAttribute);
154
155   // saves the owner value to the widget attribute
156   aValid = setSelectionCustom(theValue);
157   if (aValid)
158     // checks the attribute validity
159     aValid = isValidAttribute(theAttribute);
160
161   // restores the current values of the widget attribute
162   restoreAttributeValue(theAttribute, aValid);
163
164   blockAttribute(theAttribute, false, isFlushesActived, isAttributeSetInitializedBlocked);
165   // In particular case the results are deleted and called as redisplayed inside of this
166   // highlight-selection, to they must be flushed as soon as possible.
167   // Example: selection of group-vertices subshapes with shift pressend on body. Without
168   //  these 4 lines below the application crashes because of left presentations on
169   //  removed results still in the viewer.
170   static Events_ID aDeletedEvent = Events_Loop::eventByName(EVENT_OBJECT_DELETED);
171   static Events_ID aRedispEvent = Events_Loop::eventByName(EVENT_OBJECT_TO_REDISPLAY);
172   Events_Loop::loop()->flush(aDeletedEvent);
173   Events_Loop::loop()->flush(aRedispEvent);
174
175   return aValid;
176 }
177
178 //********************************************************************
179 bool ModuleBase_WidgetValidated::isValidSelectionCustom(const ModuleBase_ViewerPrsPtr& thePrs)
180 {
181   return true;
182 }
183
184 //********************************************************************
185 bool ModuleBase_WidgetValidated::isValidAttribute(const AttributePtr& theAttribute) const
186 {
187   SessionPtr aMgr = ModelAPI_Session::get();
188   ModelAPI_ValidatorsFactory* aFactory = aMgr->validators();
189   std::string aValidatorID, anError;
190   return aFactory->validate(theAttribute, aValidatorID, anError);
191 }
192
193 bool ModuleBase_WidgetValidated::isFilterActivated() const
194 {
195   bool isActivated = false;
196
197   Handle(SelectMgr_Filter) aSelFilter = myWorkshop->validatorFilter();
198   ModuleBase_IViewer* aViewer = myWorkshop->viewer();
199
200   return aViewer->hasSelectionFilter(aSelFilter);
201 }
202
203 bool ModuleBase_WidgetValidated::activateFilters(const bool toActivate)
204 {
205   ModuleBase_IViewer* aViewer = myWorkshop->viewer();
206
207   Handle(SelectMgr_Filter) aSelFilter = myWorkshop->validatorFilter();
208   bool aHasSelectionFilter = aViewer->hasSelectionFilter(aSelFilter);
209
210   if (toActivate)
211     aViewer->addSelectionFilter(aSelFilter);
212   else {
213     aViewer->removeSelectionFilter(aSelFilter);
214     clearValidatedCash();
215   }
216
217   return aHasSelectionFilter;
218 }
219
220 //********************************************************************
221 void ModuleBase_WidgetValidated::blockAttribute(const AttributePtr& theAttribute,
222                                                 const bool& theToBlock,
223                                                 bool& isFlushesActived,
224                                                 bool& isAttributeSetInitializedBlocked)
225 {
226   Events_Loop* aLoop = Events_Loop::loop();
227   DataPtr aData = myFeature->data();
228   if (theToBlock) {
229     // blocks the flush signals to avoid the temporary objects visualization in the viewer
230     // they should not be shown in order to do not lose highlight by erasing them
231     isFlushesActived = aLoop->activateFlushes(false);
232
233     aData->blockSendAttributeUpdated(true);
234     isAttributeSetInitializedBlocked = theAttribute->blockSetInitialized(true);
235   }
236   else {
237     aData->blockSendAttributeUpdated(false);
238     theAttribute->blockSetInitialized(isAttributeSetInitializedBlocked);
239     aLoop->activateFlushes(isFlushesActived);
240   }
241 }
242
243 //********************************************************************
244 void ModuleBase_WidgetValidated::storeValidState(const ModuleBase_ViewerPrsPtr& theValue, const bool theValid)
245 {
246   bool aValidPrs = myInvalidPrs.contains(theValue);
247   bool anInvalidPrs = myInvalidPrs.contains(theValue);
248
249   if (theValid) {
250     if (!aValidPrs)
251       myValidPrs.append(theValue);
252     // the commented code will be useful when the valid state of the presentation
253     // will be changable between activate/deactivate. Currently it does not happen.
254     //if (anInvalidPrs)
255     //  myInvalidPrs.removeOne(theValue);
256   }
257   else { // !theValid
258     if (!anInvalidPrs)
259       myInvalidPrs.append(theValue);
260     //if (!aValidPrs)
261     //  myValidPrs.removeOne(theValue);
262   }
263 #ifdef DEBUG_VALID_STATE
264   qDebug(QString("storeValidState: myValidPrs.size() = %1, myInvalidPrs.size() = %2").arg(myValidPrs.count())
265                  .arg(myInvalidPrs.count()).toStdString().c_str());
266 #endif
267 }
268
269 //********************************************************************
270 bool ModuleBase_WidgetValidated::getValidState(const ModuleBase_ViewerPrsPtr& theValue, bool& theValid)
271 {
272   bool aValidPrs = myValidPrs.contains(theValue);
273   bool anInvalidPrs = myInvalidPrs.contains(theValue);
274
275   if (aValidPrs)
276     theValid = true;
277   else if (anInvalidPrs)
278     theValid = false;
279
280   return aValidPrs || anInvalidPrs;
281 }
282
283 //********************************************************************
284 QList<ModuleBase_ViewerPrsPtr> ModuleBase_WidgetValidated::getFilteredSelected()
285 {
286   QList<ModuleBase_ViewerPrsPtr> aSelected = myWorkshop->selection()->getSelected(
287                                                        ModuleBase_ISelection::Viewer);
288
289   QList<ModuleBase_ViewerPrsPtr> anOBSelected = myWorkshop->selection()->getSelected(
290                                                        ModuleBase_ISelection::Browser);
291   // filter the OB presentations
292   filterPresentations(anOBSelected);
293   if (!anOBSelected.isEmpty())
294     ModuleBase_ISelection::appendSelected(anOBSelected, aSelected);
295
296   filterCompSolids(aSelected);
297
298   return aSelected;
299 }
300
301 //********************************************************************
302 void ModuleBase_WidgetValidated::filterPresentations(QList<ModuleBase_ViewerPrsPtr>& theValues)
303 {
304   QList<ModuleBase_ViewerPrsPtr> aValidatedValues;
305
306   QList<ModuleBase_ViewerPrsPtr>::const_iterator anIt = theValues.begin(), aLast = theValues.end();
307   bool isDone = false;
308   for (; anIt != aLast; anIt++) {
309     if (isValidInFilters(*anIt))
310       aValidatedValues.append(*anIt);
311   }
312   if (aValidatedValues.size() != theValues.size()) {
313     theValues.clear();
314     theValues = aValidatedValues;
315   }
316 }
317
318 //********************************************************************
319 void ModuleBase_WidgetValidated::filterCompSolids(QList<ModuleBase_ViewerPrsPtr>& theValues)
320 {
321   std::set<ResultCompSolidPtr> aCompSolids;
322   QList<ModuleBase_ViewerPrsPtr> aValidatedValues;
323
324   // Collect compsolids.
325   QList<ModuleBase_ViewerPrsPtr>::const_iterator anIt = theValues.begin(), aLast = theValues.end();
326   for (; anIt != aLast; anIt++) {
327     const ModuleBase_ViewerPrsPtr& aViewerPrs = *anIt;
328     ObjectPtr anObject = aViewerPrs->object();
329     ResultCompSolidPtr aResultCompSolid = std::dynamic_pointer_cast<ModelAPI_ResultCompSolid>(anObject);
330     if(aResultCompSolid.get()) {
331       aCompSolids.insert(aResultCompSolid);
332     }
333   }
334
335   // Filter sub-solids of compsolids.
336   anIt = theValues.begin();
337   for (; anIt != aLast; anIt++) {
338     const ModuleBase_ViewerPrsPtr& aViewerPrs = *anIt;
339     ObjectPtr anObject = aViewerPrs->object();
340     ResultPtr aResult = std::dynamic_pointer_cast<ModelAPI_Result>(anObject);
341     ResultCompSolidPtr aResCompSolidPtr = ModelAPI_Tools::compSolidOwner(aResult);
342     if(aResCompSolidPtr.get() && (aCompSolids.find(aResCompSolidPtr) != aCompSolids.end())) {
343       // Skip sub-solid of compsolid.
344       continue;
345     } else {
346       aValidatedValues.append(*anIt);
347     }
348   }
349
350   if (aValidatedValues.size() != theValues.size()) {
351     theValues.clear();
352     theValues = aValidatedValues;
353   }
354 }