Salome HOME
#1352 Selection mode is invalid on edit Partition
[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
8 #include <ModelAPI_Session.h>
9 #include <ModelAPI_Validator.h>
10 #include <ModelAPI_AttributeValidator.h>
11 #include <ModelAPI_Events.h>
12
13 #include <SelectMgr_ListIteratorOfListOfFilter.hxx>
14 #include <SelectMgr_EntityOwner.hxx>
15 #include <StdSelect_BRepOwner.hxx>
16
17 #include <Events_Loop.h>
18
19 #include <QWidget>
20
21 //#define DEBUG_VALID_STATE
22
23 ModuleBase_WidgetValidated::ModuleBase_WidgetValidated(QWidget* theParent,
24                                                        ModuleBase_IWorkshop* theWorkshop,
25                                                        const Config_WidgetAPI* theData,
26                                                        const std::string& theParentId)
27 : ModuleBase_ModelWidget(theParent, theData, theParentId),
28   myWorkshop(theWorkshop), myIsInValidate(false)
29 {
30 }
31
32 ModuleBase_WidgetValidated::~ModuleBase_WidgetValidated()
33 {
34 }
35
36 //********************************************************************
37 bool ModuleBase_WidgetValidated::setSelection(QList<ModuleBase_ViewerPrs>& theValues,
38                                               const bool theToValidate)
39 {
40   if (theValues.empty()) {
41     // In order to make reselection possible, set empty object and shape should be done
42     setSelectionCustom(ModuleBase_ViewerPrs());
43     return false;
44   }
45   // it removes the processed value from the parameters list
46   ModuleBase_ViewerPrs aValue = theValues.takeFirst();
47   bool isDone = false;
48
49   if (!theToValidate || isValidInFilters(aValue)) {
50     isDone = setSelectionCustom(aValue);
51     // updateObject - to update/redisplay feature
52     // it is commented in order to perfom it outside the method
53     //updateObject(myFeature);
54     // to storeValue()
55     //emit valuesChanged();
56   }
57   return isDone;
58 }
59
60 //********************************************************************
61 ObjectPtr ModuleBase_WidgetValidated::findPresentedObject(const AISObjectPtr& theAIS) const
62 {
63   return myPresentedObject;
64 }
65
66 //********************************************************************
67 void ModuleBase_WidgetValidated::clearValidatedCash()
68 {
69   myValidPrs.clear();
70   myInvalidPrs.clear();
71 }
72
73 //********************************************************************
74 void ModuleBase_WidgetValidated::storeAttributeValue()
75 {
76   myIsInValidate = true;
77 }
78
79 //********************************************************************
80 void ModuleBase_WidgetValidated::restoreAttributeValue(const bool theValid)
81 {
82   myIsInValidate = false;
83 }
84
85 //********************************************************************
86 bool ModuleBase_WidgetValidated::isValidInFilters(const ModuleBase_ViewerPrs& thePrs)
87 {
88   bool aValid = true;
89   Handle(SelectMgr_EntityOwner) anOwner = thePrs.owner();
90
91   // if an owner is null, the selection happens in the Object browser.
92   // creates a selection owner on the base of object shape and the object AIS object
93   if (anOwner.IsNull() && thePrs.owner().IsNull() && thePrs.object().get()) {
94     ResultPtr aResult = myWorkshop->selection()->getResult(thePrs);
95     if (aResult.get() && aResult->shape().get()) {
96       // some results have no shape, e.g. the parameter one. So, they should not be validated
97       GeomShapePtr aShape = aResult->shape();
98       const TopoDS_Shape aTDShape = aShape->impl<TopoDS_Shape>();
99       Handle(AIS_InteractiveObject) anIO = myWorkshop->selection()->getIO(thePrs);
100       anOwner = new StdSelect_BRepOwner(aTDShape, anIO);
101       myPresentedObject = aResult;
102     }
103     else
104       aValid = false; // only results with a shape can be filtered
105   }
106   // checks the owner by the AIS context activated filters
107   if (!anOwner.IsNull()) {
108     // the widget validator filter should be active, but during check by preselection
109     // it is not yet activated, so we need to activate/deactivate it manually
110     bool isActivated = isFilterActivated();
111     if (!isActivated)
112       activateFilters(true);
113
114     Handle(AIS_InteractiveContext) aContext = myWorkshop->viewer()->AISContext();
115     if (!aContext.IsNull()) {
116       const SelectMgr_ListOfFilter& aFilters = aContext->Filters();
117       SelectMgr_ListIteratorOfListOfFilter anIt(aFilters);
118       for (; anIt.More() && aValid; anIt.Next()) {
119         Handle(SelectMgr_Filter) aFilter = anIt.Value();
120         aValid = aFilter->IsOk(anOwner);
121       }
122     }
123     if (!isActivated)
124       activateFilters(false);
125   }
126
127   // removes created owner
128   if (!anOwner.IsNull() && anOwner != thePrs.owner()) {
129     anOwner.Nullify();
130     myPresentedObject = ObjectPtr();
131   }
132   return aValid;
133 }
134
135 //********************************************************************
136 bool ModuleBase_WidgetValidated::isValidSelection(const ModuleBase_ViewerPrs& theValue)
137 {
138   bool aValid = false;
139   if (getValidState(theValue, aValid)) {
140     return aValid;
141   }
142
143   aValid = isValidSelectionCustom(theValue);
144   if (!aValid) {
145     storeValidState(theValue, aValid);
146     return aValid;
147   }
148
149   // stores the current values of the widget attribute
150   bool isFlushesActived, isAttributeSetInitializedBlocked;
151   blockAttribute(true, isFlushesActived, isAttributeSetInitializedBlocked);
152
153   storeAttributeValue();
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();
160
161   // restores the current values of the widget attribute
162   restoreAttributeValue(aValid);
163
164   blockAttribute(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   storeValidState(theValue, aValid);
176   return aValid;
177 }
178
179 //********************************************************************
180 bool ModuleBase_WidgetValidated::isValidSelectionCustom(const ModuleBase_ViewerPrs& thePrs)
181 {
182   return true;
183 }
184
185 //********************************************************************
186 bool ModuleBase_WidgetValidated::isValidAttribute() const
187 {
188   SessionPtr aMgr = ModelAPI_Session::get();
189   ModelAPI_ValidatorsFactory* aFactory = aMgr->validators();
190   AttributePtr anAttribute = myFeature->attribute(attributeID());
191   std::string aValidatorID, anError;
192   return aFactory->validate(anAttribute, aValidatorID, anError);
193 }
194
195 bool ModuleBase_WidgetValidated::isFilterActivated() const
196 {
197   bool isActivated = false;
198
199   Handle(SelectMgr_Filter) aSelFilter = myWorkshop->validatorFilter();
200   ModuleBase_IViewer* aViewer = myWorkshop->viewer();
201
202   return aViewer->hasSelectionFilter(aSelFilter);
203 }
204
205 bool ModuleBase_WidgetValidated::activateFilters(const bool toActivate)
206 {
207   ModuleBase_IViewer* aViewer = myWorkshop->viewer();
208
209   Handle(SelectMgr_Filter) aSelFilter = myWorkshop->validatorFilter();
210   bool aHasSelectionFilter = aViewer->hasSelectionFilter(aSelFilter);
211
212   if (toActivate)
213     aViewer->addSelectionFilter(aSelFilter);
214   else {
215     aViewer->removeSelectionFilter(aSelFilter);
216     clearValidState();
217   }
218
219   return aHasSelectionFilter;
220 }
221
222 //********************************************************************
223 void ModuleBase_WidgetValidated::blockAttribute(const bool& theToBlock, bool& isFlushesActived,
224                                                 bool& isAttributeSetInitializedBlocked)
225 {
226   Events_Loop* aLoop = Events_Loop::loop();
227   DataPtr aData = myFeature->data();
228   AttributePtr anAttribute = myFeature->attribute(attributeID());
229   if (theToBlock) {
230     // blocks the flush signals to avoid the temporary objects visualization in the viewer
231     // they should not be shown in order to do not lose highlight by erasing them
232     isFlushesActived = aLoop->activateFlushes(false);
233
234     aData->blockSendAttributeUpdated(true);
235     isAttributeSetInitializedBlocked = anAttribute->blockSetInitialized(true);
236   }
237   else {
238     aData->blockSendAttributeUpdated(false);
239     anAttribute->blockSetInitialized(isAttributeSetInitializedBlocked);
240     aLoop->activateFlushes(isFlushesActived);
241   }
242 }
243
244 //********************************************************************
245 void ModuleBase_WidgetValidated::storeValidState(const ModuleBase_ViewerPrs& theValue, const bool theValid)
246 {
247   bool aValidPrs = myInvalidPrs.contains(theValue);
248   bool anInvalidPrs = myInvalidPrs.contains(theValue);
249
250   if (theValid) {
251     if (!aValidPrs)
252       myValidPrs.append(theValue);
253     // the commented code will be useful when the valid state of the presentation
254     // will be changable between activate/deactivate. Currently it does not happen.
255     //if (anInvalidPrs)
256     //  myInvalidPrs.removeOne(theValue);
257   }
258   else { // !theValid
259     if (!anInvalidPrs)
260       myInvalidPrs.append(theValue);
261     //if (!aValidPrs)
262     //  myValidPrs.removeOne(theValue);
263   }
264 #ifdef DEBUG_VALID_STATE
265   qDebug(QString("storeValidState: myValidPrs.size() = %1, myInvalidPrs.size() = %2").arg(myValidPrs.count())
266                  .arg(myInvalidPrs.count()).toStdString().c_str());
267 #endif
268 }
269
270 //********************************************************************
271 bool ModuleBase_WidgetValidated::getValidState(const ModuleBase_ViewerPrs& theValue, bool& theValid)
272 {
273   bool aValidPrs = myValidPrs.contains(theValue);
274   bool anInvalidPrs = myInvalidPrs.contains(theValue);
275
276   if (aValidPrs)
277     theValid = true;
278   else if (anInvalidPrs)
279     theValid = false;
280
281   return aValidPrs || anInvalidPrs;
282 }
283
284 //********************************************************************
285 void ModuleBase_WidgetValidated::clearValidState()
286 {
287 #ifdef DEBUG_VALID_STATE
288   qDebug("clearValidState");
289 #endif
290   myValidPrs.clear();
291   myInvalidPrs.clear();
292 }
293
294 //********************************************************************
295 QList<ModuleBase_ViewerPrs> ModuleBase_WidgetValidated::getFilteredSelected()
296 {
297   QList<ModuleBase_ViewerPrs> aSelected = myWorkshop->selection()->getSelected(
298                                                        ModuleBase_ISelection::Viewer);
299
300   QList<ModuleBase_ViewerPrs> anOBSelected = myWorkshop->selection()->getSelected(
301                                                        ModuleBase_ISelection::Browser);
302   // filter the OB presentations
303   filterPresentations(anOBSelected);
304   if (!anOBSelected.isEmpty())
305     ModuleBase_ISelection::appendSelected(anOBSelected, aSelected);
306
307   return aSelected;
308 }
309
310 //********************************************************************
311 void ModuleBase_WidgetValidated::filterPresentations(QList<ModuleBase_ViewerPrs>& theValues)
312 {
313   QList<ModuleBase_ViewerPrs> aValidatedValues;
314
315   QList<ModuleBase_ViewerPrs>::const_iterator anIt = theValues.begin(), aLast = theValues.end();
316   bool isDone = false;
317   for (; anIt != aLast; anIt++) {
318     if (isValidInFilters(*anIt))
319       aValidatedValues.append(*anIt);
320   }
321   if (aValidatedValues.size() != theValues.size()) {
322     theValues.clear();
323     theValues = aValidatedValues;
324   }
325 }