Salome HOME
94f837e924f515748ea8ca98923f6acdf26af75d
[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 bool ModuleBase_WidgetValidated::isValidInFilters(const ModuleBase_ViewerPrs& thePrs)
59 {
60   bool aValid = true;
61   Handle(SelectMgr_EntityOwner) anOwner = thePrs.owner();
62
63   // if an owern is null, the selection happens in the Object browser.
64   // creates a selection owner on the base of object shape and the object AIS object
65   if (anOwner.IsNull() && thePrs.owner().IsNull() && thePrs.object().get()) {
66     ResultPtr aResult = myWorkshop->selection()->getResult(thePrs);
67     if (aResult.get()) {
68       GeomShapePtr aShape = aResult->shape();
69
70       const TopoDS_Shape aTDShape = aShape->impl<TopoDS_Shape>();
71       Handle(AIS_InteractiveObject) anIO = myWorkshop->selection()->getIO(thePrs);
72       anOwner = new StdSelect_BRepOwner(aTDShape, anIO);
73     }
74   }
75   // finds 
76   if (!anOwner.IsNull()) {
77     const SelectMgr_ListOfFilter& aFilters = myWorkshop->viewer()->AISContext()->Filters();
78     SelectMgr_ListIteratorOfListOfFilter anIt(aFilters);
79     for (; anIt.More() && aValid; anIt.Next()) {
80       Handle(SelectMgr_Filter) aFilter = anIt.Value();
81       //if (aFilter == myWorkshop->validatorFilter())
82       //  continue;
83       aValid = aFilter->IsOk(anOwner);
84     }
85   }
86   // removes created owner
87   if (!anOwner.IsNull() && anOwner != thePrs.owner())
88     anOwner.Nullify();
89   return aValid;
90 }
91
92 //********************************************************************
93 bool ModuleBase_WidgetValidated::isValidSelection(const ModuleBase_ViewerPrs& theValue)
94 {
95   bool aValid = false;
96   if (getValidState(theValue, aValid)) {
97     return aValid;
98   }
99
100   aValid = isValidSelectionCustom(theValue);
101   if (!aValid) {
102     storeValidState(theValue, aValid);
103     return aValid;
104   }
105
106   if (isValidateBlocked)
107     return true;
108   isValidateBlocked = true;
109
110   DataPtr aData = myFeature->data();
111   AttributePtr anAttribute = myFeature->attribute(attributeID());
112
113   // stores the current values of the widget attribute
114   Events_Loop* aLoop = Events_Loop::loop();
115   // blocks the flush signals to avoid the temporary objects visualization in the viewer
116   // they should not be shown in order to do not lose highlight by erasing them
117   bool isActive = aLoop->activateFlushes(false);
118
119   aData->blockSendAttributeUpdated(true);
120   bool isAttributeBlocked = anAttribute->blockSetInitialized(true);
121   storeAttributeValue();
122
123   // saves the owner value to the widget attribute
124   aValid = setSelectionCustom(theValue);
125   if (aValid)
126     // checks the attribute validity
127     aValid = isValidAttribute();
128
129   // restores the current values of the widget attribute
130   restoreAttributeValue(aValid);
131   aData->blockSendAttributeUpdated(false);
132   anAttribute->blockSetInitialized(isAttributeBlocked);
133   aLoop->activateFlushes(isActive);
134
135   // In particular case the results are deleted and called as redisplayed inside of this
136   // highlight-selection, to they must be flushed as soon as possible.
137   // Example: selection of group-vertices subshapes with shift pressend on body. Without
138   //  these 4 lines below the application crashes because of left presentations on
139   //  removed results still in the viewer.
140   static Events_ID aDeletedEvent = Events_Loop::eventByName(EVENT_OBJECT_DELETED);
141   static Events_ID aRedispEvent = Events_Loop::eventByName(EVENT_OBJECT_TO_REDISPLAY);
142   aLoop->flush(aDeletedEvent);
143   aLoop->flush(aRedispEvent);
144
145   storeValidState(theValue, aValid);
146   isValidateBlocked = false;
147   return aValid;
148 }
149
150 //********************************************************************
151 bool ModuleBase_WidgetValidated::isValidSelectionCustom(const ModuleBase_ViewerPrs& thePrs)
152 {
153   return true;
154 }
155
156 //********************************************************************
157 bool ModuleBase_WidgetValidated::isValidAttribute() const
158 {
159   SessionPtr aMgr = ModelAPI_Session::get();
160   ModelAPI_ValidatorsFactory* aFactory = aMgr->validators();
161   std::list<ModelAPI_Validator*> aValidators;
162   std::list<std::list<std::string> > anArguments;
163   aFactory->validators(myFeature->getKind(), attributeID(), aValidators, anArguments);
164
165   DataPtr aData = myFeature->data();
166   AttributePtr anAttribute = myFeature->attribute(attributeID());
167
168   std::list<ModelAPI_Validator*>::iterator aValidator = aValidators.begin();
169   std::list<std::list<std::string> >::iterator aArgs = anArguments.begin();
170   bool aValid = true;
171   for (; aValidator != aValidators.end() && aValid; aValidator++, aArgs++) {
172     const ModelAPI_AttributeValidator* aAttrValidator =
173         dynamic_cast<const ModelAPI_AttributeValidator*>(*aValidator);
174     if (aAttrValidator) {
175       aValid = aAttrValidator->isValid(anAttribute, *aArgs);
176     }
177   }
178   return aValid;
179 }
180
181 void ModuleBase_WidgetValidated::activateFilters(const bool toActivate)
182 {
183   ModuleBase_IViewer* aViewer = myWorkshop->viewer();
184
185   Handle(SelectMgr_Filter) aSelFilter = myWorkshop->validatorFilter();
186   if (toActivate)
187     aViewer->addSelectionFilter(aSelFilter);
188   else {
189     aViewer->removeSelectionFilter(aSelFilter);
190     clearValidState();
191   }
192 }
193
194 //********************************************************************
195 void ModuleBase_WidgetValidated::storeValidState(const ModuleBase_ViewerPrs& theValue, const bool theValid)
196 {
197   bool aValidPrs = myInvalidPrs.contains(theValue);
198   bool anInvalidPrs = myInvalidPrs.contains(theValue);
199
200   if (theValid) {
201     if (!aValidPrs)
202       myValidPrs.append(theValue);
203     // the commented code will be useful when the valid state of the presentation
204     // will be changable between activate/deactivate. Currently it does not happen.
205     //if (anInvalidPrs)
206     //  myInvalidPrs.removeOne(theValue);
207   }
208   else { // !theValid
209     if (!anInvalidPrs)
210       myInvalidPrs.append(theValue);
211     //if (!aValidPrs)
212     //  myValidPrs.removeOne(theValue);
213   }
214 #ifdef DEBUG_VALID_STATE
215   qDebug(QString("storeValidState: myValidPrs.size() = %1, myInvalidPrs.size() = %2").arg(myValidPrs.count())
216                  .arg(myInvalidPrs.count()).toStdString().c_str());
217 #endif
218 }
219
220 //********************************************************************
221 bool ModuleBase_WidgetValidated::getValidState(const ModuleBase_ViewerPrs& theValue, bool& theValid)
222 {
223   bool aValidPrs = myValidPrs.contains(theValue);
224   bool anInvalidPrs = myInvalidPrs.contains(theValue);
225
226   if (aValidPrs)
227     theValid = true;
228   else if (anInvalidPrs)
229     theValid = false;
230
231   return aValidPrs || anInvalidPrs;
232 }
233
234 //********************************************************************
235 void ModuleBase_WidgetValidated::clearValidState()
236 {
237 #ifdef DEBUG_VALID_STATE
238   qDebug("clearValidState");
239 #endif
240   myValidPrs.clear();
241   myInvalidPrs.clear();
242 }
243