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