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