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