Salome HOME
Shape plane filter should process shapes of objects selected in object browser.
[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
16 #include <Events_Loop.h>
17
18 #include <QWidget>
19
20 //#define DEBUG_VALID_STATE
21
22 ModuleBase_WidgetValidated::ModuleBase_WidgetValidated(QWidget* theParent,
23                                                        const Config_WidgetAPI* theData,
24                                                        const std::string& theParentId)
25  : ModuleBase_ModelWidget(theParent, theData, theParentId), isValidateBlocked(false)
26 {
27 }
28
29 ModuleBase_WidgetValidated::~ModuleBase_WidgetValidated()
30 {
31 }
32
33 //********************************************************************
34 bool ModuleBase_WidgetValidated::setSelection(QList<ModuleBase_ViewerPrs>& theValues)
35 {
36   if (theValues.empty())
37     return false;
38   // it removes the processed value from the parameters list
39   ModuleBase_ViewerPrs aValue = theValues.takeFirst();
40   bool isDone = false;
41
42   if (isValidSelection(aValue)) {
43     isDone = setSelectionCustom(aValue);
44     // updateObject - to update/redisplay feature
45     // it is commented in order to perfom it outside the method
46     //updateObject(myFeature);
47     // to storeValue()
48     //emit valuesChanged();
49   }
50   return isDone;
51 }
52
53 //********************************************************************
54 bool ModuleBase_WidgetValidated::isValidSelection(const ModuleBase_ViewerPrs& theValue)
55 {
56   bool aValid = false;
57   if (getValidState(theValue, aValid)) {
58     return aValid;
59   }
60
61   aValid = isValidSelectionCustom(theValue);
62   if (!aValid) {
63     storeValidState(theValue, aValid);
64     return aValid;
65   }
66
67   if (isValidateBlocked)
68     return true;
69   isValidateBlocked = true;
70
71   DataPtr aData = myFeature->data();
72   AttributePtr anAttribute = myFeature->attribute(attributeID());
73
74   // stores the current values of the widget attribute
75   Events_Loop* aLoop = Events_Loop::loop();
76   // blocks the flush signals to avoid the temporary objects visualization in the viewer
77   // they should not be shown in order to do not lose highlight by erasing them
78   bool isActive = aLoop->activateFlushes(false);
79
80   aData->blockSendAttributeUpdated(true);
81   bool isAttributeBlocked = anAttribute->blockSetInitialized(true);
82   storeAttributeValue();
83
84   // saves the owner value to the widget attribute
85   aValid = setSelectionCustom(theValue);
86   if (aValid)
87     // checks the attribute validity
88     aValid = isValidAttribute();
89
90   // restores the current values of the widget attribute
91   restoreAttributeValue(aValid);
92   aData->blockSendAttributeUpdated(false);
93   anAttribute->blockSetInitialized(isAttributeBlocked);
94   aLoop->activateFlushes(isActive);
95
96   // In particular case the results are deleted and called as redisplayed inside of this
97   // highlight-selection, to they must be flushed as soon as possible.
98   // Example: selection of group-vertices subshapes with shift pressend on body. Without
99   //  these 4 lines below the application crashes because of left presentations on
100   //  removed results still in the viewer.
101   static Events_ID aDeletedEvent = Events_Loop::eventByName(EVENT_OBJECT_DELETED);
102   static Events_ID aRedispEvent = Events_Loop::eventByName(EVENT_OBJECT_TO_REDISPLAY);
103   aLoop->flush(aDeletedEvent);
104   aLoop->flush(aRedispEvent);
105
106   storeValidState(theValue, aValid);
107   isValidateBlocked = false;
108   return aValid;
109 }
110
111 //********************************************************************
112 bool ModuleBase_WidgetValidated::isValidSelectionCustom(const ModuleBase_ViewerPrs& thePrs)
113 {
114   return true;
115 }
116
117 //********************************************************************
118 bool ModuleBase_WidgetValidated::isValidAttribute() const
119 {
120   SessionPtr aMgr = ModelAPI_Session::get();
121   ModelAPI_ValidatorsFactory* aFactory = aMgr->validators();
122   std::list<ModelAPI_Validator*> aValidators;
123   std::list<std::list<std::string> > anArguments;
124   aFactory->validators(myFeature->getKind(), attributeID(), aValidators, anArguments);
125
126   DataPtr aData = myFeature->data();
127   AttributePtr anAttribute = myFeature->attribute(attributeID());
128
129   std::list<ModelAPI_Validator*>::iterator aValidator = aValidators.begin();
130   std::list<std::list<std::string> >::iterator aArgs = anArguments.begin();
131   bool aValid = true;
132   for (; aValidator != aValidators.end() && aValid; aValidator++, aArgs++) {
133     const ModelAPI_AttributeValidator* aAttrValidator =
134         dynamic_cast<const ModelAPI_AttributeValidator*>(*aValidator);
135     if (aAttrValidator) {
136       aValid = aAttrValidator->isValid(anAttribute, *aArgs);
137     }
138   }
139   return aValid;
140 }
141
142 void ModuleBase_WidgetValidated::activateFilters(ModuleBase_IWorkshop* theWorkshop,
143                                                  const bool toActivate)
144 {
145   ModuleBase_IViewer* aViewer = theWorkshop->viewer();
146
147   Handle(SelectMgr_Filter) aSelFilter = theWorkshop->validatorFilter();
148   if (toActivate)
149     aViewer->addSelectionFilter(aSelFilter);
150   else {
151     aViewer->removeSelectionFilter(aSelFilter);
152     clearValidState();
153   }
154 }
155
156 //********************************************************************
157 void ModuleBase_WidgetValidated::storeValidState(const ModuleBase_ViewerPrs& theValue, const bool theValid)
158 {
159   bool aValidPrs = myInvalidPrs.contains(theValue);
160   bool anInvalidPrs = myInvalidPrs.contains(theValue);
161
162   if (theValid) {
163     if (!aValidPrs)
164       myValidPrs.append(theValue);
165     // the commented code will be useful when the valid state of the presentation
166     // will be changable between activate/deactivate. Currently it does not happen.
167     //if (anInvalidPrs)
168     //  myInvalidPrs.removeOne(theValue);
169   }
170   else { // !theValid
171     if (!anInvalidPrs)
172       myInvalidPrs.append(theValue);
173     //if (!aValidPrs)
174     //  myValidPrs.removeOne(theValue);
175   }
176 #ifdef DEBUG_VALID_STATE
177   qDebug(QString("storeValidState: myValidPrs.size() = %1, myInvalidPrs.size() = %2").arg(myValidPrs.count())
178                  .arg(myInvalidPrs.count()).toStdString().c_str());
179 #endif
180 }
181
182 //********************************************************************
183 bool ModuleBase_WidgetValidated::getValidState(const ModuleBase_ViewerPrs& theValue, bool& theValid)
184 {
185   bool aValidPrs = myValidPrs.contains(theValue);
186   bool anInvalidPrs = myInvalidPrs.contains(theValue);
187
188   if (aValidPrs)
189     theValid = true;
190   else if (anInvalidPrs)
191     theValid = false;
192
193   return aValidPrs || anInvalidPrs;
194 }
195
196 //********************************************************************
197 void ModuleBase_WidgetValidated::clearValidState()
198 {
199 #ifdef DEBUG_VALID_STATE
200   qDebug("clearValidState");
201 #endif
202   myValidPrs.clear();
203   myInvalidPrs.clear();
204 }
205