]> SALOME platform Git repositories - modules/shaper.git/blob - src/ModuleBase/ModuleBase_ModelWidget.cpp
Salome HOME
db4a41f66e0d912ac4d8098d3af523f1cbb380b5
[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 ValueState& theState)
191 {
192   ValueState aState = myState;
193   if (myState != theState && !myIsValueStateBlocked) {
194     myState = theState;
195     emit valueStateChanged();
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::storeValueByApply()
217 {
218   // do not emit signal about update the currenty feature object
219   // in order to do not perform additional redisplay in the viewer.
220   // It should happens by finish operation of the apply action
221   storeValueCustom();
222 }
223
224 void ModuleBase_ModelWidget::updateObject(ObjectPtr theObj)
225 {
226   blockUpdateViewer(true);
227
228   Events_Loop::loop()->flush(Events_Loop::eventByName(EVENT_OBJECT_UPDATED));
229
230   blockUpdateViewer(false);
231 }
232
233 void ModuleBase_ModelWidget::moveObject(ObjectPtr theObj)
234 {
235   //blockUpdateViewer(true);
236
237   static Events_ID anEvent = Events_Loop::eventByName(EVENT_OBJECT_MOVED);
238   ModelAPI_EventCreator::get()->sendUpdated(theObj, anEvent);
239   Events_Loop::loop()->flush(anEvent);
240
241   //blockUpdateViewer(false);
242 }
243
244 bool ModuleBase_ModelWidget::processEnter()
245 {
246   return false;
247 }
248
249 bool ModuleBase_ModelWidget::eventFilter(QObject* theObject, QEvent *theEvent)
250 {
251   QWidget* aWidget = qobject_cast<QWidget*>(theObject);
252   if (theEvent->type() == QEvent::FocusIn) {
253     #ifdef _DEBUG
254     // The following two lines are for debugging purpose only
255     QFocusEvent* aFocusEvent = dynamic_cast<QFocusEvent*>(theEvent);
256     bool isWinFocus = aFocusEvent->reason() == Qt::ActiveWindowFocusReason;
257     #endif
258     if (getControls().contains(aWidget)) {
259       emit focusInWidget(this);
260     }
261   }
262   // pass the event on to the parent class
263
264   return QObject::eventFilter(theObject, theEvent);
265 }
266
267 //**************************************************************
268 void ModuleBase_ModelWidget::onWidgetValuesChanged()
269 {
270   storeValue();
271 }
272
273 //**************************************************************
274 void ModuleBase_ModelWidget::onWidgetValuesModified()
275 {
276   setValueState(ModifiedInPP);
277 }
278
279 //**************************************************************
280 void ModuleBase_ModelWidget::blockUpdateViewer(const bool theValue)
281 {
282   // the viewer update should be blocked in order to avoid the temporary feature content
283   // when the solver processes the feature, the redisplay message can be flushed
284   // what caused the display in the viewer preliminary states of object
285   // e.g. fillet feature, angle value change
286   std::shared_ptr<Events_Message> aMsg;
287   if (theValue) {
288     aMsg = std::shared_ptr<Events_Message>(
289         new Events_Message(Events_Loop::eventByName(EVENT_UPDATE_VIEWER_BLOCKED)));
290   }
291   else {
292     // the viewer update should be unblocked
293     aMsg = std::shared_ptr<Events_Message>(
294         new Events_Message(Events_Loop::eventByName(EVENT_UPDATE_VIEWER_UNBLOCKED)));
295   }
296   Events_Loop::loop()->send(aMsg);
297 }