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