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