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