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