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