Salome HOME
f0d33055320a9a9d21ef2fd36029e063aa75a5e1
[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   if (myState == ModifiedInPP)
136     storeValue();
137   myState = Stored;
138 }
139
140 void ModuleBase_ModelWidget::initializeValueByActivate()
141 {
142   if (isComputedDefault()) {
143     if (myFeature->compute(myAttributeID)) {
144       restoreValue();
145     }
146   }
147   else {
148     storeValue();
149   }
150 }
151
152 QWidget* ModuleBase_ModelWidget::getControlAcceptingFocus(const bool isFirst)
153 {
154   QWidget* aControl = 0;
155
156   QList<QWidget*> aControls = getControls();
157   int aSize = aControls.size();
158
159   if (isFirst) {
160     for (int i = 0; i < aSize && !aControl; i++)  {
161       if (aControls[i]->focusPolicy() != Qt::NoFocus)
162         aControl = aControls[i];
163     }
164   }
165   else {
166     for (int i = aSize - 1; i >= 0 && !aControl; i--)  {
167       if (aControls[i]->focusPolicy() != Qt::NoFocus)
168         aControl = aControls[i];
169     }
170   }
171   return aControl;
172 }
173
174 void ModuleBase_ModelWidget::setDefaultValue(const std::string& theValue)
175 {
176   myDefaultValue = theValue;
177 }
178
179 bool ModuleBase_ModelWidget::storeValue()
180 {
181   setValueState(Stored);
182
183   emit beforeValuesChanged();
184   bool isDone = storeValueCustom();
185   emit afterValuesChanged();
186
187   return isDone;
188 }
189
190 ModuleBase_ModelWidget::ValueState ModuleBase_ModelWidget::setValueState(const ModuleBase_ModelWidget::ValueState& theState)
191 {
192   ValueState aState = myState;
193   if (myState != theState && !myIsValueStateBlocked) {
194     myState = theState;
195     emit valueStateChanged(aState);
196   }
197   return aState;
198 }
199
200 bool ModuleBase_ModelWidget::blockValueState(const bool theBlocked)
201 {
202   bool isBlocked = myIsValueStateBlocked;
203   myIsValueStateBlocked = theBlocked;
204   return isBlocked;
205 }
206
207 bool ModuleBase_ModelWidget::restoreValue()
208 {
209   emit beforeValuesRestored();
210   bool isDone = restoreValueCustom();
211   emit afterValuesRestored();
212
213   return isDone;
214 }
215
216 void ModuleBase_ModelWidget::updateObject(ObjectPtr theObj)
217 {
218   blockUpdateViewer(true);
219
220   Events_Loop::loop()->flush(Events_Loop::eventByName(EVENT_OBJECT_UPDATED));
221
222   blockUpdateViewer(false);
223 }
224
225 void ModuleBase_ModelWidget::moveObject(ObjectPtr theObj)
226 {
227   //blockUpdateViewer(true);
228
229   static Events_ID anEvent = Events_Loop::eventByName(EVENT_OBJECT_MOVED);
230   ModelAPI_EventCreator::get()->sendUpdated(theObj, anEvent);
231   Events_Loop::loop()->flush(anEvent);
232
233   //blockUpdateViewer(false);
234 }
235
236 bool ModuleBase_ModelWidget::processEnter()
237 {
238   return false;
239 }
240
241 bool ModuleBase_ModelWidget::eventFilter(QObject* theObject, QEvent *theEvent)
242 {
243   QWidget* aWidget = qobject_cast<QWidget*>(theObject);
244   if (theEvent->type() == QEvent::FocusIn) {
245     #ifdef _DEBUG
246     // The following two lines are for debugging purpose only
247     QFocusEvent* aFocusEvent = dynamic_cast<QFocusEvent*>(theEvent);
248     bool isWinFocus = aFocusEvent->reason() == Qt::ActiveWindowFocusReason;
249     #endif
250     if (getControls().contains(aWidget)) {
251       emit focusInWidget(this);
252     }
253   }
254   else if (theEvent->type() == QEvent::FocusOut) {
255     QFocusEvent* aFocusEvent = dynamic_cast<QFocusEvent*>(theEvent);
256
257     Qt::FocusReason aReason = aFocusEvent->reason();
258     bool aMouseOrKey = aReason == Qt::MouseFocusReason ||
259                         aReason == Qt::TabFocusReason ||
260                         aReason == Qt::BacktabFocusReason ||
261                         aReason == Qt::OtherFocusReason; // to process widget->setFocus()
262     if (aMouseOrKey && getControls().contains(aWidget) && getValueState() == ModifiedInPP)
263       storeValue();
264   }
265   // pass the event on to the parent class
266
267   return QObject::eventFilter(theObject, theEvent);
268 }
269
270 //**************************************************************
271 void ModuleBase_ModelWidget::onWidgetValuesChanged()
272 {
273   storeValue();
274 }
275
276 //**************************************************************
277 void ModuleBase_ModelWidget::onWidgetValuesModified()
278 {
279   setValueState(ModifiedInPP);
280 }
281
282 //**************************************************************
283 void ModuleBase_ModelWidget::blockUpdateViewer(const bool theValue)
284 {
285   // the viewer update should be blocked in order to avoid the temporary feature content
286   // when the solver processes the feature, the redisplay message can be flushed
287   // what caused the display in the viewer preliminary states of object
288   // e.g. fillet feature, angle value change
289   std::shared_ptr<Events_Message> aMsg;
290   if (theValue) {
291     aMsg = std::shared_ptr<Events_Message>(
292         new Events_Message(Events_Loop::eventByName(EVENT_UPDATE_VIEWER_BLOCKED)));
293   }
294   else {
295     // the viewer update should be unblocked
296     aMsg = std::shared_ptr<Events_Message>(
297         new Events_Message(Events_Loop::eventByName(EVENT_UPDATE_VIEWER_UNBLOCKED)));
298   }
299   Events_Loop::loop()->send(aMsg);
300 }