Salome HOME
Issue #1084: parameter cyclic dependence
[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 QString ModuleBase_ModelWidget::getValueStateError() const
59 {
60   QString anError = "";
61
62   ModuleBase_ModelWidget::ValueState aState = getValueState();
63   if (aState != ModuleBase_ModelWidget::Stored) {
64     AttributePtr anAttr = feature()->attribute(attributeID());
65     if (anAttr.get()) {
66       QString anAttributeName = anAttr->id().c_str();
67       switch (aState) {
68         case ModuleBase_ModelWidget::ModifiedInPP:
69           anError = "Attribute \"" + anAttributeName +
70                     "\" modification is not applyed. Please click \"Enter\" or \"Tab\".";
71           break;
72         case ModuleBase_ModelWidget::ModifiedInViewer:
73           anError = "Attribute \"" + anAttributeName +
74                     "\" is locked by modification value in the viewer.";
75           break;
76         case ModuleBase_ModelWidget::Reset:
77           anError = "Attribute \"" + anAttributeName + "\" is not initialized.";
78           break;
79       }
80     }
81   }
82   return anError;
83 }
84
85 void ModuleBase_ModelWidget::enableFocusProcessing()
86 {
87   QList<QWidget*> aMyControls = getControls();
88   foreach(QWidget*  eachControl, aMyControls) {
89     if (myIsObligatory) {
90       eachControl->setFocusPolicy(Qt::StrongFocus);
91       eachControl->installEventFilter(this);
92     }
93     else {
94       eachControl->setFocusPolicy(Qt::NoFocus);
95     }
96   }
97 }
98
99 void ModuleBase_ModelWidget::setHighlighted(bool isHighlighted)
100 {
101   QList<QWidget*> aWidgetList = getControls();
102   foreach(QWidget* aWidget, aWidgetList) {
103     QLabel* aLabel = qobject_cast<QLabel*>(aWidget);
104     // We won't set the effect to QLabels - it looks ugly
105     if(aLabel) continue;
106     if(isHighlighted) {
107       // If effect is the installed on a different widget, setGraphicsEffect() will
108       // remove the effect from the widget and install it on this widget.
109       // That's why we create a new effect for each widget
110       QGraphicsDropShadowEffect* aGlowEffect = new QGraphicsDropShadowEffect();
111       aGlowEffect->setOffset(.0);
112       aGlowEffect->setBlurRadius(10.0);
113       aGlowEffect->setColor(QColor(0, 170, 255)); // Light-blue color, #00AAFF
114       aWidget->setGraphicsEffect(aGlowEffect);
115     } else {
116       QGraphicsEffect* anEffect = aWidget->graphicsEffect();
117       if(anEffect)
118         anEffect->deleteLater();
119       aWidget->setGraphicsEffect(NULL);
120     }
121   }
122 }
123
124 void ModuleBase_ModelWidget::setFeature(const FeaturePtr& theFeature, const bool theToStoreValue)
125 {
126   myFeature = theFeature;
127   if (theToStoreValue)
128     storeValue();
129 }
130
131 bool ModuleBase_ModelWidget::focusTo()
132 {
133   QList<QWidget*> aControls = getControls();
134   QList<QWidget*>::const_iterator anIt = aControls.begin(), aLast = aControls.end();
135   bool isFocusAccepted = false;
136   for (; anIt != aLast && !isFocusAccepted; anIt++) {
137     QWidget* aWidget = *anIt;
138     if (aWidget && aWidget->focusPolicy() != Qt::NoFocus) {
139       aWidget->setFocus();
140       isFocusAccepted = true;
141     }
142   }
143   return isFocusAccepted;
144 }
145
146 void ModuleBase_ModelWidget::activate()
147 {
148   // the control value is stored to the mode by the focus in on the widget
149   // we need the value is initialized in order to enable the apply button in the property panel.
150   // It should happens in the creation mode only because all fields are filled in the edition mode
151   if (!isEditingMode()) {
152     AttributePtr anAttribute = myFeature->data()->attribute(myAttributeID);
153     if (anAttribute.get() != NULL && !anAttribute->isInitialized())
154       initializeValueByActivate();
155   }
156   activateCustom();
157 }
158
159 void ModuleBase_ModelWidget::deactivate()
160 {
161   myIsValueStateBlocked = false;
162   if (myState == ModifiedInPP)
163     storeValue();
164   myState = Stored;
165 }
166
167 void ModuleBase_ModelWidget::initializeValueByActivate()
168 {
169   if (isComputedDefault()) {
170     if (myFeature->compute(myAttributeID)) {
171       restoreValue();
172     }
173   }
174   else {
175     storeValue();
176   }
177 }
178
179 QWidget* ModuleBase_ModelWidget::getControlAcceptingFocus(const bool isFirst)
180 {
181   QWidget* aControl = 0;
182
183   QList<QWidget*> aControls = getControls();
184   int aSize = aControls.size();
185
186   if (isFirst) {
187     for (int i = 0; i < aSize && !aControl; i++)  {
188       if (aControls[i]->focusPolicy() != Qt::NoFocus)
189         aControl = aControls[i];
190     }
191   }
192   else {
193     for (int i = aSize - 1; i >= 0 && !aControl; i--)  {
194       if (aControls[i]->focusPolicy() != Qt::NoFocus)
195         aControl = aControls[i];
196     }
197   }
198   return aControl;
199 }
200
201 void ModuleBase_ModelWidget::setDefaultValue(const std::string& theValue)
202 {
203   myDefaultValue = theValue;
204 }
205
206 bool ModuleBase_ModelWidget::storeValue()
207 {
208   setValueState(Stored);
209
210   emit beforeValuesChanged();
211   bool isDone = storeValueCustom();
212   emit afterValuesChanged();
213
214   return isDone;
215 }
216
217 ModuleBase_ModelWidget::ValueState ModuleBase_ModelWidget::setValueState(const ModuleBase_ModelWidget::ValueState& theState)
218 {
219   ValueState aState = myState;
220   if (myState != theState && !myIsValueStateBlocked) {
221     myState = theState;
222     emit valueStateChanged(aState);
223   }
224   return aState;
225 }
226
227 bool ModuleBase_ModelWidget::blockValueState(const bool theBlocked)
228 {
229   bool isBlocked = myIsValueStateBlocked;
230   myIsValueStateBlocked = theBlocked;
231   return isBlocked;
232 }
233
234 bool ModuleBase_ModelWidget::restoreValue()
235 {
236   emit beforeValuesRestored();
237   bool isDone = restoreValueCustom();
238   emit afterValuesRestored();
239
240   return isDone;
241 }
242
243 void ModuleBase_ModelWidget::updateObject(ObjectPtr theObj)
244 {
245   blockUpdateViewer(true);
246
247   Events_Loop::loop()->flush(Events_Loop::eventByName(EVENT_OBJECT_UPDATED));
248
249   blockUpdateViewer(false);
250 }
251
252 void ModuleBase_ModelWidget::moveObject(ObjectPtr theObj)
253 {
254   //blockUpdateViewer(true);
255
256   static Events_ID anEvent = Events_Loop::eventByName(EVENT_OBJECT_MOVED);
257   ModelAPI_EventCreator::get()->sendUpdated(theObj, anEvent);
258   Events_Loop::loop()->flush(anEvent);
259
260   //blockUpdateViewer(false);
261 }
262
263 bool ModuleBase_ModelWidget::processEnter()
264 {
265   return false;
266 }
267
268 bool ModuleBase_ModelWidget::eventFilter(QObject* theObject, QEvent *theEvent)
269 {
270   QWidget* aWidget = qobject_cast<QWidget*>(theObject);
271   if (theEvent->type() == QEvent::FocusIn) {
272     #ifdef _DEBUG
273     // The following two lines are for debugging purpose only
274     QFocusEvent* aFocusEvent = dynamic_cast<QFocusEvent*>(theEvent);
275     bool isWinFocus = aFocusEvent->reason() == Qt::ActiveWindowFocusReason;
276     #endif
277     if (getControls().contains(aWidget)) {
278       emit focusInWidget(this);
279     }
280   }
281   else if (theEvent->type() == QEvent::FocusOut) {
282     QFocusEvent* aFocusEvent = dynamic_cast<QFocusEvent*>(theEvent);
283
284     Qt::FocusReason aReason = aFocusEvent->reason();
285     bool aMouseOrKey = aReason == Qt::MouseFocusReason ||
286                         aReason == Qt::TabFocusReason ||
287                         aReason == Qt::BacktabFocusReason ||
288                         aReason == Qt::OtherFocusReason; // to process widget->setFocus()
289     if (aMouseOrKey && getControls().contains(aWidget) && getValueState() == ModifiedInPP)
290       storeValue();
291   }
292   // pass the event on to the parent class
293
294   return QObject::eventFilter(theObject, theEvent);
295 }
296
297 //**************************************************************
298 void ModuleBase_ModelWidget::onWidgetValuesChanged()
299 {
300   storeValue();
301 }
302
303 //**************************************************************
304 void ModuleBase_ModelWidget::onWidgetValuesModified()
305 {
306   setValueState(ModifiedInPP);
307 }
308
309 //**************************************************************
310 void ModuleBase_ModelWidget::blockUpdateViewer(const bool theValue)
311 {
312   // the viewer update should be blocked in order to avoid the temporary feature content
313   // when the solver processes the feature, the redisplay message can be flushed
314   // what caused the display in the viewer preliminary states of object
315   // e.g. fillet feature, angle value change
316   std::shared_ptr<Events_Message> aMsg;
317   if (theValue) {
318     aMsg = std::shared_ptr<Events_Message>(
319         new Events_Message(Events_Loop::eventByName(EVENT_UPDATE_VIEWER_BLOCKED)));
320   }
321   else {
322     // the viewer update should be unblocked
323     aMsg = std::shared_ptr<Events_Message>(
324         new Events_Message(Events_Loop::eventByName(EVENT_UPDATE_VIEWER_UNBLOCKED)));
325   }
326   Events_Loop::loop()->send(aMsg);
327 }