Salome HOME
c8e76a6c60512ff6ed1dfdfcf66572b072482eba
[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)
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   DataPtr aData = myFeature->data();
68   AttributePtr anAttribute = myFeature->attribute(attributeID());
69
70   // stores the current values of the widget attribute
71   Events_Loop* aLoop = Events_Loop::loop();
72   // blocks the flush signals to avoid the temporary objects visualization in the viewer
73   // they should not be shown in order to do not lose highlight by erasing them
74   aLoop->activateFlushes(false);
75
76   aData->blockSendAttributeUpdated(true);
77   bool isAttributeBlocked = anAttribute->blockSetInitialized(true);
78   storeAttributeValue();
79
80   // saves the owner value to the widget attribute
81   aValid = setSelectionCustom(theValue);
82   if (aValid)
83     // checks the attribute validity
84     aValid = isValidAttribute();
85
86   // restores the current values of the widget attribute
87   restoreAttributeValue(aValid);
88   aData->blockSendAttributeUpdated(false);
89   anAttribute->blockSetInitialized(isAttributeBlocked);
90   aLoop->activateFlushes(true);
91
92   // In particular case the results are deleted and called as redisplayed inside of this
93   // highlight-selection, to they must be flushed as soon as possible.
94   // Example: selection of group-vertices subshapes with shift pressend on body. Without
95   //  these 4 lines below the application crashes because of left presentations on
96   //  removed results still in the viewer.
97   static Events_ID aDeletedEvent = Events_Loop::eventByName(EVENT_OBJECT_DELETED);
98   static Events_ID aRedispEvent = Events_Loop::eventByName(EVENT_OBJECT_TO_REDISPLAY);
99   aLoop->flush(aDeletedEvent);
100   aLoop->flush(aRedispEvent);
101
102   storeValidState(theValue, aValid);
103   return aValid;
104 }
105
106 //********************************************************************
107 bool ModuleBase_WidgetValidated::isValidSelectionCustom(const ModuleBase_ViewerPrs& thePrs)
108 {
109   return true;
110 }
111
112 //********************************************************************
113 bool ModuleBase_WidgetValidated::isValidAttribute() const
114 {
115   SessionPtr aMgr = ModelAPI_Session::get();
116   ModelAPI_ValidatorsFactory* aFactory = aMgr->validators();
117   std::list<ModelAPI_Validator*> aValidators;
118   std::list<std::list<std::string> > anArguments;
119   aFactory->validators(myFeature->getKind(), attributeID(), aValidators, anArguments);
120
121   DataPtr aData = myFeature->data();
122   AttributePtr anAttribute = myFeature->attribute(attributeID());
123
124   std::list<ModelAPI_Validator*>::iterator aValidator = aValidators.begin();
125   std::list<std::list<std::string> >::iterator aArgs = anArguments.begin();
126   bool aValid = true;
127   for (; aValidator != aValidators.end() && aValid; aValidator++, aArgs++) {
128     const ModelAPI_AttributeValidator* aAttrValidator =
129         dynamic_cast<const ModelAPI_AttributeValidator*>(*aValidator);
130     if (aAttrValidator) {
131       aValid = aAttrValidator->isValid(anAttribute, *aArgs);
132     }
133   }
134   return aValid;
135 }
136
137 void ModuleBase_WidgetValidated::activateFilters(ModuleBase_IWorkshop* theWorkshop,
138                                                  const bool toActivate)
139 {
140   ModuleBase_IViewer* aViewer = theWorkshop->viewer();
141
142   Handle(SelectMgr_Filter) aSelFilter = theWorkshop->validatorFilter();
143   if (toActivate)
144     aViewer->addSelectionFilter(aSelFilter);
145   else {
146     aViewer->removeSelectionFilter(aSelFilter);
147     clearValidState();
148   }
149 }
150
151 //********************************************************************
152 void ModuleBase_WidgetValidated::storeValidState(const ModuleBase_ViewerPrs& theValue, const bool theValid)
153 {
154   bool aValidPrs = myInvalidPrs.contains(theValue);
155   bool anInvalidPrs = myInvalidPrs.contains(theValue);
156
157   if (theValid) {
158     if (!aValidPrs)
159       myValidPrs.append(theValue);
160     // the commented code will be useful when the valid state of the presentation
161     // will be changable between activate/deactivate. Currently it does not happen.
162     //if (anInvalidPrs)
163     //  myInvalidPrs.removeOne(theValue);
164   }
165   else { // !theValid
166     if (!anInvalidPrs)
167       myInvalidPrs.append(theValue);
168     //if (!aValidPrs)
169     //  myValidPrs.removeOne(theValue);
170   }
171 #ifdef DEBUG_VALID_STATE
172   qDebug(QString("storeValidState: myValidPrs.size() = %1, myInvalidPrs.size() = %2").arg(myValidPrs.count())
173                  .arg(myInvalidPrs.count()).toStdString().c_str());
174 #endif
175 }
176
177 //********************************************************************
178 bool ModuleBase_WidgetValidated::getValidState(const ModuleBase_ViewerPrs& theValue, bool& theValid)
179 {
180   bool aValidPrs = myValidPrs.contains(theValue);
181   bool anInvalidPrs = myInvalidPrs.contains(theValue);
182
183   if (aValidPrs)
184     theValid = true;
185   else if (anInvalidPrs)
186     theValid = false;
187
188   return aValidPrs || anInvalidPrs;
189 }
190
191 //********************************************************************
192 void ModuleBase_WidgetValidated::clearValidState()
193 {
194 #ifdef DEBUG_VALID_STATE
195   qDebug("clearValidState");
196 #endif
197   myValidPrs.clear();
198   myInvalidPrs.clear();
199 }
200