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