Salome HOME
7f7e1fa1487630f337e77b48450936a964600a3b
[modules/shaper.git] / src / ModuleBase / ModuleBase_ModelWidget.cpp
1 // Copyright (C) 2014-20xx CEA/DEN, EDF R&D
2
3 // File:        ModuleBase_ModelWidget.cpp
4 // Created:     25 Apr 2014
5 // Author:      Natalia ERMOLAEVA
6
7 #include "ModuleBase_ModelWidget.h"
8
9 #include <ModelAPI_Data.h>
10 #include <ModelAPI_Attribute.h>
11 #include <ModelAPI_Events.h>
12 #include <ModelAPI_Session.h>
13
14 #include <Config_Keywords.h>
15 #include <Config_WidgetAPI.h>
16
17 #include <Events_Loop.h>
18
19 #include <QEvent>
20 #include <QGraphicsDropShadowEffect>
21 #include <QColor>
22 #include <QLabel>
23 #include <QFocusEvent>
24
25 ModuleBase_ModelWidget::ModuleBase_ModelWidget(QWidget* theParent,
26                                                const Config_WidgetAPI* theData,
27                                                const std::string& theParentId)
28     : QWidget(theParent),
29       myParentId(theParentId),
30       myIsEditing(false),
31       myState(Stored),
32       myIsValueStateBlocked(false)
33 {
34   myDefaultValue = theData->getProperty(ATTR_DEFAULT);
35   myUseReset = theData->getBooleanAttribute(ATTR_USE_RESET, true);
36   myIsComputedDefault = theData->getProperty(ATTR_DEFAULT) == DOUBLE_WDG_DEFAULT_COMPUTED;
37   myAttributeID = theData ? theData->widgetId() : "";
38   myIsObligatory = theData->getBooleanAttribute(ATTR_OBLIGATORY, true);
39
40   connect(this, SIGNAL(valuesChanged()), this, SLOT(onWidgetValuesChanged()));
41   connect(this, SIGNAL(valuesModified()), this, SLOT(onWidgetValuesModified()));
42 }
43
44 bool ModuleBase_ModelWidget::reset()
45 {
46   bool aResult = resetCustom();
47   if (aResult)
48     setValueState(Reset);
49
50   return aResult;
51 }
52
53 bool ModuleBase_ModelWidget::isInitialized(ObjectPtr theObject) const
54 {
55   return theObject->data()->attribute(attributeID())->isInitialized();
56 }
57
58 void ModuleBase_ModelWidget::enableFocusProcessing()
59 {
60   QList<QWidget*> aMyControls = getControls();
61   foreach(QWidget*  eachControl, aMyControls) {
62     if (myIsObligatory) {
63       eachControl->setFocusPolicy(Qt::StrongFocus);
64       eachControl->installEventFilter(this);
65     }
66     else {
67       eachControl->setFocusPolicy(Qt::NoFocus);
68     }
69   }
70 }
71
72 void ModuleBase_ModelWidget::setHighlighted(bool isHighlighted)
73 {
74   QList<QWidget*> aWidgetList = getControls();
75   foreach(QWidget* aWidget, aWidgetList) {
76     QLabel* aLabel = qobject_cast<QLabel*>(aWidget);
77     // We won't set the effect to QLabels - it looks ugly
78     if(aLabel) continue;
79     if(isHighlighted) {
80       // If effect is the installed on a different widget, setGraphicsEffect() will
81       // remove the effect from the widget and install it on this widget.
82       // That's why we create a new effect for each widget
83       QGraphicsDropShadowEffect* aGlowEffect = new QGraphicsDropShadowEffect();
84       aGlowEffect->setOffset(.0);
85       aGlowEffect->setBlurRadius(10.0);
86       aGlowEffect->setColor(QColor(0, 170, 255)); // Light-blue color, #00AAFF
87       aWidget->setGraphicsEffect(aGlowEffect);
88     } else {
89       QGraphicsEffect* anEffect = aWidget->graphicsEffect();
90       if(anEffect)
91         anEffect->deleteLater();
92       aWidget->setGraphicsEffect(NULL);
93     }
94   }
95 }
96
97 void ModuleBase_ModelWidget::setFeature(const FeaturePtr& theFeature, const bool theToStoreValue)
98 {
99   myFeature = theFeature;
100   if (theToStoreValue)
101     storeValue();
102 }
103
104 bool ModuleBase_ModelWidget::focusTo()
105 {
106   QList<QWidget*> aControls = getControls();
107   QList<QWidget*>::const_iterator anIt = aControls.begin(), aLast = aControls.end();
108   bool isFocusAccepted = false;
109   for (; anIt != aLast && !isFocusAccepted; anIt++) {
110     QWidget* aWidget = *anIt;
111     if (aWidget && aWidget->focusPolicy() != Qt::NoFocus) {
112       aWidget->setFocus();
113       isFocusAccepted = true;
114     }
115   }
116   return isFocusAccepted;
117 }
118
119 void ModuleBase_ModelWidget::activate()
120 {
121   // the control value is stored to the mode by the focus in on the widget
122   // we need the value is initialized in order to enable the apply button in the property panel.
123   // It should happens in the creation mode only because all fields are filled in the edition mode
124   if (!isEditingMode()) {
125     AttributePtr anAttribute = myFeature->data()->attribute(myAttributeID);
126     if (anAttribute.get() != NULL && !anAttribute->isInitialized())
127       initializeValueByActivate();
128   }
129   activateCustom();
130 }
131
132 void ModuleBase_ModelWidget::deactivate()
133 {
134   myIsValueStateBlocked = false;
135   myState = Stored;
136 }
137
138 void ModuleBase_ModelWidget::initializeValueByActivate()
139 {
140   if (isComputedDefault()) {
141     if (myFeature->compute(myAttributeID)) {
142       restoreValue();
143     }
144   }
145   else {
146     storeValue();
147   }
148 }
149
150 QWidget* ModuleBase_ModelWidget::getControlAcceptingFocus(const bool isFirst)
151 {
152   QWidget* aControl = 0;
153
154   QList<QWidget*> aControls = getControls();
155   int aSize = aControls.size();
156
157   if (isFirst) {
158     for (int i = 0; i < aSize && !aControl; i++)  {
159       if (aControls[i]->focusPolicy() != Qt::NoFocus)
160         aControl = aControls[i];
161     }
162   }
163   else {
164     for (int i = aSize - 1; i >= 0 && !aControl; i--)  {
165       if (aControls[i]->focusPolicy() != Qt::NoFocus)
166         aControl = aControls[i];
167     }
168   }
169   return aControl;
170 }
171
172 void ModuleBase_ModelWidget::setDefaultValue(const std::string& theValue)
173 {
174   myDefaultValue = theValue;
175 }
176
177 bool ModuleBase_ModelWidget::storeValue()
178 {
179   setValueState(Stored);
180
181   emit beforeValuesChanged();
182   bool isDone = storeValueCustom();
183   emit afterValuesChanged();
184
185   return isDone;
186 }
187
188 ModuleBase_ModelWidget::ValueState ModuleBase_ModelWidget::setValueState(const ValueState& theState)
189 {
190   ValueState aState = myState;
191   if (myState != theState && !myIsValueStateBlocked) {
192     myState = theState;
193     emit valueStateChanged();
194   }
195   return aState;
196 }
197
198 bool ModuleBase_ModelWidget::blockValueState(const bool theBlocked)
199 {
200   bool isBlocked = myIsValueStateBlocked;
201   myIsValueStateBlocked = theBlocked;
202   return isBlocked;
203 }
204
205 bool ModuleBase_ModelWidget::restoreValue()
206 {
207   emit beforeValuesRestored();
208   bool isDone = restoreValueCustom();
209   emit afterValuesRestored();
210
211   return isDone;
212 }
213
214 void ModuleBase_ModelWidget::storeValueByApply()
215 {
216   // do not emit signal about update the currenty feature object
217   // in order to do not perform additional redisplay in the viewer.
218   // It should happens by finish operation of the apply action
219   storeValueCustom();
220 }
221
222 void ModuleBase_ModelWidget::updateObject(ObjectPtr theObj)
223 {
224   blockUpdateViewer(true);
225
226   Events_Loop::loop()->flush(Events_Loop::eventByName(EVENT_OBJECT_UPDATED));
227
228   blockUpdateViewer(false);
229 }
230
231 void ModuleBase_ModelWidget::moveObject(ObjectPtr theObj)
232 {
233   //blockUpdateViewer(true);
234
235   static Events_ID anEvent = Events_Loop::eventByName(EVENT_OBJECT_MOVED);
236   ModelAPI_EventCreator::get()->sendUpdated(theObj, anEvent);
237   Events_Loop::loop()->flush(anEvent);
238
239   //blockUpdateViewer(false);
240 }
241
242 bool ModuleBase_ModelWidget::processEnter()
243 {
244   return false;
245 }
246
247 bool ModuleBase_ModelWidget::eventFilter(QObject* theObject, QEvent *theEvent)
248 {
249   QWidget* aWidget = qobject_cast<QWidget*>(theObject);
250   if (theEvent->type() == QEvent::FocusIn) {
251     #ifdef _DEBUG
252     // The following two lines are for debugging purpose only
253     QFocusEvent* aFocusEvent = dynamic_cast<QFocusEvent*>(theEvent);
254     bool isWinFocus = aFocusEvent->reason() == Qt::ActiveWindowFocusReason;
255     #endif
256     if (getControls().contains(aWidget)) {
257       emit focusInWidget(this);
258     }
259   }
260   // pass the event on to the parent class
261
262   return QObject::eventFilter(theObject, theEvent);
263 }
264
265 //**************************************************************
266 void ModuleBase_ModelWidget::onWidgetValuesChanged()
267 {
268   storeValue();
269 }
270
271 //**************************************************************
272 void ModuleBase_ModelWidget::onWidgetValuesModified()
273 {
274   setValueState(ModifiedInPP);
275 }
276
277 //**************************************************************
278 void ModuleBase_ModelWidget::blockUpdateViewer(const bool theValue)
279 {
280   // the viewer update should be blocked in order to avoid the temporary feature content
281   // when the solver processes the feature, the redisplay message can be flushed
282   // what caused the display in the viewer preliminary states of object
283   // e.g. fillet feature, angle value change
284   std::shared_ptr<Events_Message> aMsg;
285   if (theValue) {
286     aMsg = std::shared_ptr<Events_Message>(
287         new Events_Message(Events_Loop::eventByName(EVENT_UPDATE_VIEWER_BLOCKED)));
288   }
289   else {
290     // the viewer update should be unblocked
291     aMsg = std::shared_ptr<Events_Message>(
292         new Events_Message(Events_Loop::eventByName(EVENT_UPDATE_VIEWER_UNBLOCKED)));
293   }
294   Events_Loop::loop()->send(aMsg);
295 }