]> SALOME platform Git repositories - modules/shaper.git/blob - src/ModuleBase/ModuleBase_WidgetValidated.cpp
Salome HOME
e00fd80941614a75dfb4002a1c2bb32f7e5cd11d
[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), isValidateBlocked(false),
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   if (isValidateBlocked)
124     return true;
125   isValidateBlocked = true;
126
127   DataPtr aData = myFeature->data();
128   AttributePtr anAttribute = myFeature->attribute(attributeID());
129
130   // stores the current values of the widget attribute
131   Events_Loop* aLoop = Events_Loop::loop();
132   // blocks the flush signals to avoid the temporary objects visualization in the viewer
133   // they should not be shown in order to do not lose highlight by erasing them
134   bool isActive = aLoop->activateFlushes(false);
135
136   aData->blockSendAttributeUpdated(true);
137   bool isAttributeBlocked = anAttribute->blockSetInitialized(true);
138   storeAttributeValue();
139
140   // saves the owner value to the widget attribute
141   aValid = setSelectionCustom(theValue);
142   if (aValid)
143     // checks the attribute validity
144     aValid = isValidAttribute();
145
146   // restores the current values of the widget attribute
147   restoreAttributeValue(aValid);
148   aData->blockSendAttributeUpdated(false);
149   anAttribute->blockSetInitialized(isAttributeBlocked);
150   aLoop->activateFlushes(isActive);
151
152   // In particular case the results are deleted and called as redisplayed inside of this
153   // highlight-selection, to they must be flushed as soon as possible.
154   // Example: selection of group-vertices subshapes with shift pressend on body. Without
155   //  these 4 lines below the application crashes because of left presentations on
156   //  removed results still in the viewer.
157   static Events_ID aDeletedEvent = Events_Loop::eventByName(EVENT_OBJECT_DELETED);
158   static Events_ID aRedispEvent = Events_Loop::eventByName(EVENT_OBJECT_TO_REDISPLAY);
159   aLoop->flush(aDeletedEvent);
160   aLoop->flush(aRedispEvent);
161
162   storeValidState(theValue, aValid);
163   isValidateBlocked = false;
164   return aValid;
165 }
166
167 //********************************************************************
168 bool ModuleBase_WidgetValidated::isValidSelectionCustom(const ModuleBase_ViewerPrs& thePrs)
169 {
170   return true;
171 }
172
173 //********************************************************************
174 bool ModuleBase_WidgetValidated::isValidAttribute() const
175 {
176   SessionPtr aMgr = ModelAPI_Session::get();
177   ModelAPI_ValidatorsFactory* aFactory = aMgr->validators();
178   std::list<ModelAPI_Validator*> aValidators;
179   std::list<std::list<std::string> > anArguments;
180   aFactory->validators(myFeature->getKind(), attributeID(), aValidators, anArguments);
181
182   DataPtr aData = myFeature->data();
183   AttributePtr anAttribute = myFeature->attribute(attributeID());
184
185   std::list<ModelAPI_Validator*>::iterator aValidator = aValidators.begin();
186   std::list<std::list<std::string> >::iterator aArgs = anArguments.begin();
187   bool aValid = true;
188   for (; aValidator != aValidators.end() && aValid; aValidator++, aArgs++) {
189     const ModelAPI_AttributeValidator* aAttrValidator =
190         dynamic_cast<const ModelAPI_AttributeValidator*>(*aValidator);
191     if (aAttrValidator) {
192       aValid = aAttrValidator->isValid(anAttribute, *aArgs);
193     }
194   }
195   return aValid;
196 }
197
198 bool ModuleBase_WidgetValidated::isFilterActivated() const
199 {
200   bool isActivated = false;
201
202   Handle(SelectMgr_Filter) aSelFilter = myWorkshop->validatorFilter();
203
204   const SelectMgr_ListOfFilter& aFilters = myWorkshop->viewer()->AISContext()->Filters();
205   SelectMgr_ListIteratorOfListOfFilter aIt(aFilters);
206   for (; aIt.More(); aIt.Next()) {
207     if (aSelFilter.Access() == aIt.Value().Access())
208       isActivated = true;
209   }
210   return isActivated;
211 }
212
213
214 void ModuleBase_WidgetValidated::activateFilters(const bool toActivate)
215 {
216   ModuleBase_IViewer* aViewer = myWorkshop->viewer();
217
218   Handle(SelectMgr_Filter) aSelFilter = myWorkshop->validatorFilter();
219   if (toActivate)
220     aViewer->addSelectionFilter(aSelFilter);
221   else {
222     aViewer->removeSelectionFilter(aSelFilter);
223     clearValidState();
224   }
225 }
226
227 //********************************************************************
228 void ModuleBase_WidgetValidated::storeValidState(const ModuleBase_ViewerPrs& theValue, const bool theValid)
229 {
230   bool aValidPrs = myInvalidPrs.contains(theValue);
231   bool anInvalidPrs = myInvalidPrs.contains(theValue);
232
233   if (theValid) {
234     if (!aValidPrs)
235       myValidPrs.append(theValue);
236     // the commented code will be useful when the valid state of the presentation
237     // will be changable between activate/deactivate. Currently it does not happen.
238     //if (anInvalidPrs)
239     //  myInvalidPrs.removeOne(theValue);
240   }
241   else { // !theValid
242     if (!anInvalidPrs)
243       myInvalidPrs.append(theValue);
244     //if (!aValidPrs)
245     //  myValidPrs.removeOne(theValue);
246   }
247 #ifdef DEBUG_VALID_STATE
248   qDebug(QString("storeValidState: myValidPrs.size() = %1, myInvalidPrs.size() = %2").arg(myValidPrs.count())
249                  .arg(myInvalidPrs.count()).toStdString().c_str());
250 #endif
251 }
252
253 //********************************************************************
254 bool ModuleBase_WidgetValidated::getValidState(const ModuleBase_ViewerPrs& theValue, bool& theValid)
255 {
256   bool aValidPrs = myValidPrs.contains(theValue);
257   bool anInvalidPrs = myInvalidPrs.contains(theValue);
258
259   if (aValidPrs)
260     theValid = true;
261   else if (anInvalidPrs)
262     theValid = false;
263
264   return aValidPrs || anInvalidPrs;
265 }
266
267 //********************************************************************
268 void ModuleBase_WidgetValidated::clearValidState()
269 {
270 #ifdef DEBUG_VALID_STATE
271   qDebug("clearValidState");
272 #endif
273   myValidPrs.clear();
274   myInvalidPrs.clear();
275 }
276
277 //********************************************************************
278 QList<ModuleBase_ViewerPrs> ModuleBase_WidgetValidated::getFilteredSelected()
279 {
280   QList<ModuleBase_ViewerPrs> aSelected = myWorkshop->selection()->getSelected(
281                                                        ModuleBase_ISelection::Viewer);
282
283   QList<ModuleBase_ViewerPrs> anOBSelected = myWorkshop->selection()->getSelected(
284                                                        ModuleBase_ISelection::Browser);
285   // filter the OB presentations
286   filterPresentations(anOBSelected);
287   if (!anOBSelected.isEmpty())
288     ModuleBase_ISelection::appendSelected(anOBSelected, aSelected);
289
290   return aSelected;
291 }
292
293 //********************************************************************
294 void ModuleBase_WidgetValidated::filterPresentations(QList<ModuleBase_ViewerPrs>& theValues)
295 {
296   QList<ModuleBase_ViewerPrs> aValidatedValues;
297
298   QList<ModuleBase_ViewerPrs>::const_iterator anIt = theValues.begin(), aLast = theValues.end();
299   bool isDone = false;
300   for (; anIt != aLast; anIt++) {
301     if (isValidInFilters(*anIt))
302       aValidatedValues.append(*anIt);
303   }
304   if (aValidatedValues.size() != theValues.size()) {
305     theValues.clear();
306     theValues = aValidatedValues;
307   }
308 }