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