Salome HOME
Issue 1299 Angle constraint: support of additional and complementary angles. Test...
[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 //#define DEBUG_VALUE_STATE
26
27 ModuleBase_ModelWidget::ModuleBase_ModelWidget(QWidget* theParent,
28                                                const Config_WidgetAPI* theData)
29     : QWidget(theParent),
30       myIsEditing(false),
31       myState(Stored),
32       myIsValueStateBlocked(false)
33 {
34   myIsInternal = theData->getBooleanAttribute(ATTR_INTERNAL, false);
35
36   myDefaultValue = theData->getProperty(ATTR_DEFAULT);
37   myUseReset = theData->getBooleanAttribute(ATTR_USE_RESET, true);
38   myIsComputedDefault = theData->getProperty(ATTR_DEFAULT) == DOUBLE_WDG_DEFAULT_COMPUTED;
39   myAttributeID = theData ? theData->widgetId() : "";
40   myIsObligatory = theData->getBooleanAttribute(ATTR_OBLIGATORY, true);
41
42   connect(this, SIGNAL(valuesChanged()), this, SLOT(onWidgetValuesChanged()));
43   connect(this, SIGNAL(valuesModified()), this, SLOT(onWidgetValuesModified()));
44 }
45
46 bool ModuleBase_ModelWidget::reset()
47 {
48   bool aResult = resetCustom();
49   if (aResult)
50     setValueState(Reset);
51
52   return aResult;
53 }
54
55 bool ModuleBase_ModelWidget::isInitialized(ObjectPtr theObject) const
56 {
57   return theObject->data()->attribute(attributeID())->isInitialized();
58 }
59
60 QString ModuleBase_ModelWidget::getValueStateError() const
61 {
62   QString anError = "";
63
64   ModuleBase_ModelWidget::ValueState aState = getValueState();
65   if (aState != ModuleBase_ModelWidget::Stored) {
66     AttributePtr anAttr = feature()->attribute(attributeID());
67     if (anAttr.get()) {
68       QString anAttributeName = anAttr->id().c_str();
69       switch (aState) {
70         case ModuleBase_ModelWidget::ModifiedInViewer:
71           anError = "Attribute \"" + anAttributeName +
72                     "\" is locked by modification value in the viewer.";
73           break;
74         case ModuleBase_ModelWidget::Reset:
75           anError = "Attribute \"" + anAttributeName + "\" is not initialized.";
76           break;
77         case ModuleBase_ModelWidget::ModifiedInPP: // Apply should be enabled in this mode
78         default:
79           break;
80       }
81     }
82   }
83   return anError;
84 }
85
86 QString ModuleBase_ModelWidget::getError() const
87 {
88   QString anError;
89
90   if (!feature().get())
91     return anError;
92
93   std::string anAttributeID = attributeID();
94   AttributePtr anAttribute = feature()->attribute(anAttributeID);
95   if (!anAttribute.get())
96     return anError;
97
98   std::string aValidatorID;
99   std::string anErrorMsg;
100
101   static ModelAPI_ValidatorsFactory* aValidators = ModelAPI_Session::get()->validators();
102   if (!aValidators->validate(anAttribute, aValidatorID, anErrorMsg)) {
103     if (anErrorMsg.empty())
104       anErrorMsg = "unknown error.";
105     anErrorMsg = anAttributeID + " - " + aValidatorID + ": " + anErrorMsg;
106   }
107
108   anError = QString::fromStdString(anErrorMsg);
109   if (anError.isEmpty())
110     anError = getValueStateError();
111
112   return anError;
113 }
114
115 void ModuleBase_ModelWidget::enableFocusProcessing()
116 {
117   QList<QWidget*> aMyControls = getControls();
118   foreach(QWidget*  eachControl, aMyControls) {
119       eachControl->setFocusPolicy(Qt::StrongFocus);
120       eachControl->installEventFilter(this);
121   }
122 }
123
124 void ModuleBase_ModelWidget::setHighlighted(bool isHighlighted)
125 {
126   QList<QWidget*> aWidgetList = getControls();
127   foreach(QWidget* aWidget, aWidgetList) {
128     QLabel* aLabel = qobject_cast<QLabel*>(aWidget);
129     // We won't set the effect to QLabels - it looks ugly
130     if(aLabel) continue;
131     // If effect is the installed on a different widget, setGraphicsEffect() will
132     // remove the effect from the widget and install it on this widget.
133     // That's why we create a new effect for each widget
134     ModuleBase_Tools::setShadowEffect(aWidget, isHighlighted);
135   }
136 }
137
138 void ModuleBase_ModelWidget::setFeature(const FeaturePtr& theFeature, const bool theToStoreValue)
139 {
140   myFeature = theFeature;
141   if (theToStoreValue)
142     storeValue();
143 }
144
145 bool ModuleBase_ModelWidget::focusTo()
146 {
147   QList<QWidget*> aControls = getControls();
148   QList<QWidget*>::const_iterator anIt = aControls.begin(), aLast = aControls.end();
149   bool isFocusAccepted = false;
150   for (; anIt != aLast && !isFocusAccepted; anIt++) {
151     QWidget* aWidget = *anIt;
152     if (aWidget && aWidget->focusPolicy() != Qt::NoFocus) {
153       ModuleBase_Tools::setFocus(aWidget, "ModuleBase_ModelWidget::focusTo()");
154       isFocusAccepted = true;
155     }
156   }
157   return isFocusAccepted;
158 }
159
160 void ModuleBase_ModelWidget::activate()
161 {
162   // the control value is stored to the mode by the focus in on the widget
163   // we need the value is initialized in order to enable the apply button in the property panel.
164   // It should happens in the creation mode only because all fields are filled in the edition mode
165   if (!isEditingMode()) {
166     AttributePtr anAttribute = myFeature->data()->attribute(myAttributeID);
167     if (anAttribute.get() != NULL && !anAttribute->isInitialized())
168       initializeValueByActivate();
169   }
170   activateCustom();
171 }
172
173 void ModuleBase_ModelWidget::deactivate()
174 {
175   myIsValueStateBlocked = false;
176   if (myState == ModifiedInPP || myState == ModifiedInViewer)
177     storeValue();
178   myState = Stored;
179 }
180
181 void ModuleBase_ModelWidget::initializeValueByActivate()
182 {
183   if (isComputedDefault()) {
184     if (myFeature->compute(myAttributeID)) {
185       restoreValue();
186     }
187   }
188   else {
189     storeValue();
190   }
191 }
192
193 QWidget* ModuleBase_ModelWidget::getControlAcceptingFocus(const bool isFirst)
194 {
195   QWidget* aControl = 0;
196
197   QList<QWidget*> aControls = getControls();
198   int aSize = aControls.size();
199
200   if (isFirst) {
201     for (int i = 0; i < aSize && !aControl; i++)  {
202       if (aControls[i]->focusPolicy() != Qt::NoFocus)
203         aControl = aControls[i];
204     }
205   }
206   else {
207     for (int i = aSize - 1; i >= 0 && !aControl; i--)  {
208       if (aControls[i]->focusPolicy() != Qt::NoFocus)
209         aControl = aControls[i];
210     }
211   }
212   return aControl;
213 }
214
215 void ModuleBase_ModelWidget::setDefaultValue(const std::string& theValue)
216 {
217   myDefaultValue = theValue;
218 }
219
220 bool ModuleBase_ModelWidget::storeValue()
221 {
222   setValueState(Stored);
223
224   emit beforeValuesChanged();
225   bool isDone = storeValueCustom();
226   emit afterValuesChanged();
227
228   return isDone;
229 }
230 #ifdef DEBUG_VALUE_STATE
231 std::string getDebugInfo(const ModuleBase_ModelWidget::ValueState& theState)
232 {
233   std::string anInfo;
234   switch (theState) {
235     case ModuleBase_ModelWidget::Stored:           anInfo = "Stored          "; break;
236     case ModuleBase_ModelWidget::ModifiedInPP:     anInfo = "ModifiedInPP    "; break;
237     case ModuleBase_ModelWidget::ModifiedInViewer: anInfo = "ModifiedInViewer"; break;
238     case ModuleBase_ModelWidget::Reset:            anInfo = "Reset           "; break;
239     default: break;
240   }
241   return anInfo;
242 }
243
244 #endif
245 ModuleBase_ModelWidget::ValueState ModuleBase_ModelWidget::setValueState
246                                          (const ModuleBase_ModelWidget::ValueState& theState)
247 {
248   ValueState aState = myState;
249
250   if (myState != theState && !myIsValueStateBlocked) {
251 #ifdef DEBUG_VALUE_STATE
252     qDebug(QString("setValueState: previous state = %1,\t new state = %2")
253            .arg(getDebugInfo(myState).c_str())
254            .arg(getDebugInfo(theState).c_str()).toStdString().c_str());
255 #endif
256     myState = theState;
257     emit valueStateChanged(aState);
258   }
259   return aState;
260 }
261
262 bool ModuleBase_ModelWidget::blockValueState(const bool theBlocked)
263 {
264   bool isBlocked = myIsValueStateBlocked;
265   myIsValueStateBlocked = theBlocked;
266   return isBlocked;
267 }
268
269 bool ModuleBase_ModelWidget::restoreValue()
270 {
271   emit beforeValuesRestored();
272   bool isDone = restoreValueCustom();
273   emit afterValuesRestored();
274
275   return isDone;
276 }
277
278 void ModuleBase_ModelWidget::updateObject(ObjectPtr theObj)
279 {
280   blockUpdateViewer(true);
281
282   Events_Loop::loop()->flush(Events_Loop::eventByName(EVENT_OBJECT_UPDATED));
283
284   blockUpdateViewer(false);
285 }
286
287 void ModuleBase_ModelWidget::moveObject(ObjectPtr theObj)
288 {
289   //blockUpdateViewer(true);
290
291   static Events_ID anEvent = Events_Loop::eventByName(EVENT_OBJECT_MOVED);
292   ModelAPI_EventCreator::get()->sendUpdated(theObj, anEvent);
293   Events_Loop::loop()->flush(anEvent);
294
295   //blockUpdateViewer(false);
296 }
297
298 bool ModuleBase_ModelWidget::processEnter()
299 {
300   return false;
301 }
302
303 bool ModuleBase_ModelWidget::processDelete()
304 {
305   // we consider that model objects eats delete key in order to
306   // do nothing by for example symbol delete in line edit or spin box
307   return true;
308 }
309
310 bool ModuleBase_ModelWidget::eventFilter(QObject* theObject, QEvent *theEvent)
311 {
312   QWidget* aWidget = qobject_cast<QWidget*>(theObject);
313   if (theEvent->type() == QEvent::FocusIn) {
314     #ifdef _DEBUG
315     // The following two lines are for debugging purpose only
316     QFocusEvent* aFocusEvent = dynamic_cast<QFocusEvent*>(theEvent);
317     bool isWinFocus = aFocusEvent->reason() == Qt::ActiveWindowFocusReason;
318     #endif
319     if (getControls().contains(aWidget)) {
320       emit focusInWidget(this);
321     }
322   }
323   else if (theEvent->type() == QEvent::FocusOut) {
324     QFocusEvent* aFocusEvent = dynamic_cast<QFocusEvent*>(theEvent);
325
326     Qt::FocusReason aReason = aFocusEvent->reason();
327     bool aMouseOrKey = aReason == Qt::MouseFocusReason ||
328                         aReason == Qt::TabFocusReason ||
329                         aReason == Qt::BacktabFocusReason ||
330                         aReason == Qt::OtherFocusReason; // to process widget->setFocus()
331     if (aMouseOrKey && getControls().contains(aWidget)) {
332       if (getValueState() == ModifiedInPP) {
333         storeValue();
334       }
335     }
336   }
337   // pass the event on to the parent class
338
339   return QObject::eventFilter(theObject, theEvent);
340 }
341
342 //**************************************************************
343 void ModuleBase_ModelWidget::onWidgetValuesChanged()
344 {
345   storeValue();
346 }
347
348 //**************************************************************
349 void ModuleBase_ModelWidget::onWidgetValuesModified()
350 {
351   setValueState(ModifiedInPP);
352 }
353
354 //**************************************************************
355 void ModuleBase_ModelWidget::blockUpdateViewer(const bool theValue)
356 {
357   // the viewer update should be blocked in order to avoid the temporary feature content
358   // when the solver processes the feature, the redisplay message can be flushed
359   // what caused the display in the viewer preliminary states of object
360   // e.g. fillet feature, angle value change
361   std::shared_ptr<Events_Message> aMsg;
362   if (theValue) {
363     aMsg = std::shared_ptr<Events_Message>(
364         new Events_Message(Events_Loop::eventByName(EVENT_UPDATE_VIEWER_BLOCKED)));
365   }
366   else {
367     // the viewer update should be unblocked
368     aMsg = std::shared_ptr<Events_Message>(
369         new Events_Message(Events_Loop::eventByName(EVENT_UPDATE_VIEWER_UNBLOCKED)));
370   }
371   Events_Loop::loop()->send(aMsg);
372 }