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