Salome HOME
Merge branch 'Dev_1.3.0' of newgeom:newgeom into Dev_1.3.0
[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)
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 bool ModuleBase_WidgetValidated::isValidInFilters(const ModuleBase_ViewerPrs& thePrs)
65 {
66   bool aValid = true;
67   Handle(SelectMgr_EntityOwner) anOwner = thePrs.owner();
68
69   // if an owner is null, the selection happens in the Object browser.
70   // creates a selection owner on the base of object shape and the object AIS object
71   if (anOwner.IsNull() && thePrs.owner().IsNull() && thePrs.object().get()) {
72     ResultPtr aResult = myWorkshop->selection()->getResult(thePrs);
73     if (aResult.get()) {
74       GeomShapePtr aShape = aResult->shape();
75       const TopoDS_Shape aTDShape = aShape->impl<TopoDS_Shape>();
76       Handle(AIS_InteractiveObject) anIO = myWorkshop->selection()->getIO(thePrs);
77       anOwner = new StdSelect_BRepOwner(aTDShape, anIO);
78       myPresentedObject = aResult;
79     }
80     else
81       aValid = false; // only results can be filtered
82   }
83   // checks the owner by the AIS context activated filters
84   if (!anOwner.IsNull()) {
85     // the widget validator filter should be active, but during check by preselection
86     // it is not yet activated, so we need to activate/deactivate it manually
87     bool isActivated = isFilterActivated();
88     if (!isActivated)
89       activateFilters(true);
90
91     const SelectMgr_ListOfFilter& aFilters = myWorkshop->viewer()->AISContext()->Filters();
92     SelectMgr_ListIteratorOfListOfFilter anIt(aFilters);
93     for (; anIt.More() && aValid; anIt.Next()) {
94       Handle(SelectMgr_Filter) aFilter = anIt.Value();
95       aValid = aFilter->IsOk(anOwner);
96     }
97     if (!isActivated)
98       activateFilters(false);
99   }
100
101   // removes created owner
102   if (!anOwner.IsNull() && anOwner != thePrs.owner()) {
103     anOwner.Nullify();
104     myPresentedObject = ObjectPtr();
105   }
106   return aValid;
107 }
108
109 //********************************************************************
110 bool ModuleBase_WidgetValidated::isValidSelection(const ModuleBase_ViewerPrs& theValue)
111 {
112   bool aValid = false;
113   if (getValidState(theValue, aValid)) {
114     return aValid;
115   }
116
117   aValid = isValidSelectionCustom(theValue);
118   if (!aValid) {
119     storeValidState(theValue, aValid);
120     return aValid;
121   }
122
123   DataPtr aData = myFeature->data();
124   AttributePtr anAttribute = myFeature->attribute(attributeID());
125
126   // stores the current values of the widget attribute
127   Events_Loop* aLoop = Events_Loop::loop();
128   // blocks the flush signals to avoid the temporary objects visualization in the viewer
129   // they should not be shown in order to do not lose highlight by erasing them
130   bool isActive = aLoop->activateFlushes(false);
131
132   aData->blockSendAttributeUpdated(true);
133   bool isAttributeBlocked = anAttribute->blockSetInitialized(true);
134   storeAttributeValue();
135
136   // saves the owner value to the widget attribute
137   aValid = setSelectionCustom(theValue);
138   if (aValid)
139     // checks the attribute validity
140     aValid = isValidAttribute();
141
142   // restores the current values of the widget attribute
143   restoreAttributeValue(aValid);
144   aData->blockSendAttributeUpdated(false);
145   anAttribute->blockSetInitialized(isAttributeBlocked);
146   aLoop->activateFlushes(isActive);
147
148   // In particular case the results are deleted and called as redisplayed inside of this
149   // highlight-selection, to they must be flushed as soon as possible.
150   // Example: selection of group-vertices subshapes with shift pressend on body. Without
151   //  these 4 lines below the application crashes because of left presentations on
152   //  removed results still in the viewer.
153   static Events_ID aDeletedEvent = Events_Loop::eventByName(EVENT_OBJECT_DELETED);
154   static Events_ID aRedispEvent = Events_Loop::eventByName(EVENT_OBJECT_TO_REDISPLAY);
155   aLoop->flush(aDeletedEvent);
156   aLoop->flush(aRedispEvent);
157
158   storeValidState(theValue, aValid);
159   return aValid;
160 }
161
162 //********************************************************************
163 bool ModuleBase_WidgetValidated::isValidSelectionCustom(const ModuleBase_ViewerPrs& thePrs)
164 {
165   return true;
166 }
167
168 //********************************************************************
169 bool ModuleBase_WidgetValidated::isValidAttribute() const
170 {
171   SessionPtr aMgr = ModelAPI_Session::get();
172   ModelAPI_ValidatorsFactory* aFactory = aMgr->validators();
173   std::list<ModelAPI_Validator*> aValidators;
174   std::list<std::list<std::string> > anArguments;
175   aFactory->validators(myFeature->getKind(), attributeID(), aValidators, anArguments);
176
177   DataPtr aData = myFeature->data();
178   AttributePtr anAttribute = myFeature->attribute(attributeID());
179
180   std::list<ModelAPI_Validator*>::iterator aValidator = aValidators.begin();
181   std::list<std::list<std::string> >::iterator aArgs = anArguments.begin();
182   bool aValid = true;
183   for (; aValidator != aValidators.end() && aValid; aValidator++, aArgs++) {
184     const ModelAPI_AttributeValidator* aAttrValidator =
185         dynamic_cast<const ModelAPI_AttributeValidator*>(*aValidator);
186     if (aAttrValidator) {
187       aValid = aAttrValidator->isValid(anAttribute, *aArgs);
188     }
189   }
190   return aValid;
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 void ModuleBase_WidgetValidated::activateFilters(const bool toActivate)
204 {
205   ModuleBase_IViewer* aViewer = myWorkshop->viewer();
206
207   Handle(SelectMgr_Filter) aSelFilter = myWorkshop->validatorFilter();
208   if (toActivate)
209     aViewer->addSelectionFilter(aSelFilter);
210   else {
211     aViewer->removeSelectionFilter(aSelFilter);
212     clearValidState();
213   }
214 }
215
216 //********************************************************************
217 void ModuleBase_WidgetValidated::storeValidState(const ModuleBase_ViewerPrs& theValue, const bool theValid)
218 {
219   bool aValidPrs = myInvalidPrs.contains(theValue);
220   bool anInvalidPrs = myInvalidPrs.contains(theValue);
221
222   if (theValid) {
223     if (!aValidPrs)
224       myValidPrs.append(theValue);
225     // the commented code will be useful when the valid state of the presentation
226     // will be changable between activate/deactivate. Currently it does not happen.
227     //if (anInvalidPrs)
228     //  myInvalidPrs.removeOne(theValue);
229   }
230   else { // !theValid
231     if (!anInvalidPrs)
232       myInvalidPrs.append(theValue);
233     //if (!aValidPrs)
234     //  myValidPrs.removeOne(theValue);
235   }
236 #ifdef DEBUG_VALID_STATE
237   qDebug(QString("storeValidState: myValidPrs.size() = %1, myInvalidPrs.size() = %2").arg(myValidPrs.count())
238                  .arg(myInvalidPrs.count()).toStdString().c_str());
239 #endif
240 }
241
242 //********************************************************************
243 bool ModuleBase_WidgetValidated::getValidState(const ModuleBase_ViewerPrs& theValue, bool& theValid)
244 {
245   bool aValidPrs = myValidPrs.contains(theValue);
246   bool anInvalidPrs = myInvalidPrs.contains(theValue);
247
248   if (aValidPrs)
249     theValid = true;
250   else if (anInvalidPrs)
251     theValid = false;
252
253   return aValidPrs || anInvalidPrs;
254 }
255
256 //********************************************************************
257 void ModuleBase_WidgetValidated::clearValidState()
258 {
259 #ifdef DEBUG_VALID_STATE
260   qDebug("clearValidState");
261 #endif
262   myValidPrs.clear();
263   myInvalidPrs.clear();
264 }
265
266 //********************************************************************
267 QList<ModuleBase_ViewerPrs> ModuleBase_WidgetValidated::getFilteredSelected()
268 {
269   QList<ModuleBase_ViewerPrs> aSelected = myWorkshop->selection()->getSelected(
270                                                        ModuleBase_ISelection::Viewer);
271
272   QList<ModuleBase_ViewerPrs> anOBSelected = myWorkshop->selection()->getSelected(
273                                                        ModuleBase_ISelection::Browser);
274   // filter the OB presentations
275   filterPresentations(anOBSelected);
276   if (!anOBSelected.isEmpty())
277     ModuleBase_ISelection::appendSelected(anOBSelected, aSelected);
278
279   return aSelected;
280 }
281
282 //********************************************************************
283 void ModuleBase_WidgetValidated::filterPresentations(QList<ModuleBase_ViewerPrs>& theValues)
284 {
285   QList<ModuleBase_ViewerPrs> aValidatedValues;
286
287   QList<ModuleBase_ViewerPrs>::const_iterator anIt = theValues.begin(), aLast = theValues.end();
288   bool isDone = false;
289   for (; anIt != aLast; anIt++) {
290     if (isValidInFilters(*anIt))
291       aValidatedValues.append(*anIt);
292   }
293   if (aValidatedValues.size() != theValues.size()) {
294     theValues.clear();
295     theValues = aValidatedValues;
296   }
297 }