Salome HOME
Issue #725 - Translation with parameters - wrong coordinates
[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
199   const SelectMgr_ListOfFilter& aFilters = myWorkshop->viewer()->AISContext()->Filters();
200   SelectMgr_ListIteratorOfListOfFilter aIt(aFilters);
201   for (; aIt.More(); aIt.Next()) {
202     if (aSelFilter.Access() == aIt.Value().Access())
203       isActivated = true;
204   }
205   return isActivated;
206 }
207
208
209 void ModuleBase_WidgetValidated::activateFilters(const bool toActivate)
210 {
211   ModuleBase_IViewer* aViewer = myWorkshop->viewer();
212
213   Handle(SelectMgr_Filter) aSelFilter = myWorkshop->validatorFilter();
214   if (toActivate)
215     aViewer->addSelectionFilter(aSelFilter);
216   else {
217     aViewer->removeSelectionFilter(aSelFilter);
218     clearValidState();
219   }
220 }
221
222 //********************************************************************
223 void ModuleBase_WidgetValidated::storeValidState(const ModuleBase_ViewerPrs& theValue, const bool theValid)
224 {
225   bool aValidPrs = myInvalidPrs.contains(theValue);
226   bool anInvalidPrs = myInvalidPrs.contains(theValue);
227
228   if (theValid) {
229     if (!aValidPrs)
230       myValidPrs.append(theValue);
231     // the commented code will be useful when the valid state of the presentation
232     // will be changable between activate/deactivate. Currently it does not happen.
233     //if (anInvalidPrs)
234     //  myInvalidPrs.removeOne(theValue);
235   }
236   else { // !theValid
237     if (!anInvalidPrs)
238       myInvalidPrs.append(theValue);
239     //if (!aValidPrs)
240     //  myValidPrs.removeOne(theValue);
241   }
242 #ifdef DEBUG_VALID_STATE
243   qDebug(QString("storeValidState: myValidPrs.size() = %1, myInvalidPrs.size() = %2").arg(myValidPrs.count())
244                  .arg(myInvalidPrs.count()).toStdString().c_str());
245 #endif
246 }
247
248 //********************************************************************
249 bool ModuleBase_WidgetValidated::getValidState(const ModuleBase_ViewerPrs& theValue, bool& theValid)
250 {
251   bool aValidPrs = myValidPrs.contains(theValue);
252   bool anInvalidPrs = myInvalidPrs.contains(theValue);
253
254   if (aValidPrs)
255     theValid = true;
256   else if (anInvalidPrs)
257     theValid = false;
258
259   return aValidPrs || anInvalidPrs;
260 }
261
262 //********************************************************************
263 void ModuleBase_WidgetValidated::clearValidState()
264 {
265 #ifdef DEBUG_VALID_STATE
266   qDebug("clearValidState");
267 #endif
268   myValidPrs.clear();
269   myInvalidPrs.clear();
270 }
271
272 //********************************************************************
273 QList<ModuleBase_ViewerPrs> ModuleBase_WidgetValidated::getFilteredSelected()
274 {
275   QList<ModuleBase_ViewerPrs> aSelected = myWorkshop->selection()->getSelected(
276                                                        ModuleBase_ISelection::Viewer);
277
278   QList<ModuleBase_ViewerPrs> anOBSelected = myWorkshop->selection()->getSelected(
279                                                        ModuleBase_ISelection::Browser);
280   // filter the OB presentations
281   filterPresentations(anOBSelected);
282   if (!anOBSelected.isEmpty())
283     ModuleBase_ISelection::appendSelected(anOBSelected, aSelected);
284
285   return aSelected;
286 }
287
288 //********************************************************************
289 void ModuleBase_WidgetValidated::filterPresentations(QList<ModuleBase_ViewerPrs>& theValues)
290 {
291   QList<ModuleBase_ViewerPrs> aValidatedValues;
292
293   QList<ModuleBase_ViewerPrs>::const_iterator anIt = theValues.begin(), aLast = theValues.end();
294   bool isDone = false;
295   for (; anIt != aLast; anIt++) {
296     if (isValidInFilters(*anIt))
297       aValidatedValues.append(*anIt);
298   }
299   if (aValidatedValues.size() != theValues.size()) {
300     theValues.clear();
301     theValues = aValidatedValues;
302   }
303 }