Salome HOME
Validators return InfoMessage instead of string as an error
[modules/shaper.git] / src / ModuleBase / ModuleBase_ModelWidget.cpp
index f0d33055320a9a9d21ef2fd36029e063aa75a5e1..69cd60c463ba29b321ad6d99cf508fe7c15fb82a 100644 (file)
@@ -5,32 +5,48 @@
 // Author:      Natalia ERMOLAEVA
 
 #include "ModuleBase_ModelWidget.h"
+#include "ModuleBase_ViewerPrs.h"
+#include "ModuleBase_Tools.h"
+#include "ModuleBase_WidgetValidator.h"
+
+#include <Events_InfoMessage.h>
 
 #include <ModelAPI_Data.h>
 #include <ModelAPI_Attribute.h>
 #include <ModelAPI_Events.h>
 #include <ModelAPI_Session.h>
+#include <ModelAPI_Validator.h>
 
 #include <Config_Keywords.h>
 #include <Config_WidgetAPI.h>
+#include <Config_Translator.h>
 
 #include <Events_Loop.h>
 
 #include <QEvent>
-#include <QGraphicsDropShadowEffect>
-#include <QColor>
 #include <QLabel>
 #include <QFocusEvent>
+#include <QTextCodec>
+
+//#define DEBUG_VALUE_STATE
+
+//#define DEBUG_WIDGET_INSTANCE
 
 ModuleBase_ModelWidget::ModuleBase_ModelWidget(QWidget* theParent,
-                                               const Config_WidgetAPI* theData,
-                                               const std::string& theParentId)
+                                               const Config_WidgetAPI* theData)
     : QWidget(theParent),
-      myParentId(theParentId),
       myIsEditing(false),
       myState(Stored),
-      myIsValueStateBlocked(false)
+      myIsValueStateBlocked(false),
+      myFlushUpdateBlocked(false),
+      myWidgetValidator(0)
 {
+#ifdef DEBUG_WIDGET_INSTANCE
+  qDebug("ModuleBase_ModelWidget::ModuleBase_ModelWidget");
+#endif
+
+  myIsInternal = theData->getBooleanAttribute(ATTR_INTERNAL, false);
+
   myDefaultValue = theData->getProperty(ATTR_DEFAULT);
   myUseReset = theData->getBooleanAttribute(ATTR_USE_RESET, true);
   myIsComputedDefault = theData->getProperty(ATTR_DEFAULT) == DOUBLE_WDG_DEFAULT_COMPUTED;
@@ -41,6 +57,13 @@ ModuleBase_ModelWidget::ModuleBase_ModelWidget(QWidget* theParent,
   connect(this, SIGNAL(valuesModified()), this, SLOT(onWidgetValuesModified()));
 }
 
+ModuleBase_ModelWidget::~ModuleBase_ModelWidget()
+{
+#ifdef DEBUG_WIDGET_INSTANCE
+  qDebug("ModuleBase_ModelWidget::~ModuleBase_ModelWidget");
+#endif
+}
+
 bool ModuleBase_ModelWidget::reset()
 {
   bool aResult = resetCustom();
@@ -55,17 +78,92 @@ bool ModuleBase_ModelWidget::isInitialized(ObjectPtr theObject) const
   return theObject->data()->attribute(attributeID())->isInitialized();
 }
 
+void ModuleBase_ModelWidget::processValueState()
+{
+  if (myState == ModifiedInPP || myState == ModifiedInViewer)
+    storeValue();
+}
+
+QString ModuleBase_ModelWidget::getValueStateError() const
+{
+  QString anError = "";
+
+  ModuleBase_ModelWidget::ValueState aState = getValueState();
+  if (aState != ModuleBase_ModelWidget::Stored) {
+    AttributePtr anAttr = feature()->attribute(attributeID());
+    if (anAttr.get()) {
+      QString anAttributeName = anAttr->id().c_str();
+      switch (aState) {
+        case ModuleBase_ModelWidget::ModifiedInViewer:
+          anError = "Attribute \"" + anAttributeName +
+                    "\" is locked by modification value in the viewer.";
+          break;
+        case ModuleBase_ModelWidget::Reset:
+          anError = "Attribute \"" + anAttributeName + "\" is not initialized.";
+          break;
+        case ModuleBase_ModelWidget::ModifiedInPP: // Apply should be enabled in this mode
+        default:
+          break;
+      }
+    }
+  }
+  return anError;
+}
+
+QString ModuleBase_ModelWidget::getError(const bool theValueStateChecked) const
+{
+  QString anError;
+
+  if (!feature().get())
+    return anError;
+
+  std::string anAttributeID = attributeID();
+  AttributePtr anAttribute = feature()->attribute(anAttributeID);
+  if (!anAttribute.get())
+    return anError;
+
+  std::string aValidatorID;
+  Events_InfoMessage anErrorMsg;
+
+  static ModelAPI_ValidatorsFactory* aValidators = ModelAPI_Session::get()->validators();
+  if (!aValidators->validate(anAttribute, aValidatorID, anErrorMsg)) {
+    if (anErrorMsg.empty())
+      anErrorMsg = "unknown error.";
+    anErrorMsg = anAttributeID + " - " + aValidatorID + ": " + anErrorMsg.messageString();
+  }
+
+  if (!anErrorMsg.empty()) {
+    std::string aStr = Config_Translator::translate(anErrorMsg);
+    std::string aCodec = Config_Translator::codec(anErrorMsg.context());
+    anError = QTextCodec::codecForName(aCodec.c_str())->toUnicode(aStr.c_str());
+  }
+
+  if (anError.isEmpty() && theValueStateChecked)
+    anError = getValueStateError();
+
+  anError = translateString(anError);
+  return anError;
+}
+
+
+QString ModuleBase_ModelWidget::translateString(const QString& theMsg) const
+{
+  if (!theMsg.isEmpty()) {
+    std::string aContext = feature()->getKind();
+    std::string aStr = Config_Translator::translate(aContext, theMsg.toStdString().c_str());
+    std::string aCodec = Config_Translator::codec(aContext);
+    return QTextCodec::codecForName(aCodec.c_str())->toUnicode(aStr.c_str());
+  }
+  return theMsg;
+}
+
+
 void ModuleBase_ModelWidget::enableFocusProcessing()
 {
   QList<QWidget*> aMyControls = getControls();
   foreach(QWidget*  eachControl, aMyControls) {
-    if (myIsObligatory) {
       eachControl->setFocusPolicy(Qt::StrongFocus);
       eachControl->installEventFilter(this);
-    }
-    else {
-      eachControl->setFocusPolicy(Qt::NoFocus);
-    }
   }
 }
 
@@ -76,40 +174,37 @@ void ModuleBase_ModelWidget::setHighlighted(bool isHighlighted)
     QLabel* aLabel = qobject_cast<QLabel*>(aWidget);
     // We won't set the effect to QLabels - it looks ugly
     if(aLabel) continue;
-    if(isHighlighted) {
-      // If effect is the installed on a different widget, setGraphicsEffect() will
-      // remove the effect from the widget and install it on this widget.
-      // That's why we create a new effect for each widget
-      QGraphicsDropShadowEffect* aGlowEffect = new QGraphicsDropShadowEffect();
-      aGlowEffect->setOffset(.0);
-      aGlowEffect->setBlurRadius(10.0);
-      aGlowEffect->setColor(QColor(0, 170, 255)); // Light-blue color, #00AAFF
-      aWidget->setGraphicsEffect(aGlowEffect);
-    } else {
-      QGraphicsEffect* anEffect = aWidget->graphicsEffect();
-      if(anEffect)
-        anEffect->deleteLater();
-      aWidget->setGraphicsEffect(NULL);
-    }
+    // If effect is the installed on a different widget, setGraphicsEffect() will
+    // remove the effect from the widget and install it on this widget.
+    // That's why we create a new effect for each widget
+    ModuleBase_Tools::setShadowEffect(aWidget, isHighlighted);
   }
 }
 
-void ModuleBase_ModelWidget::setFeature(const FeaturePtr& theFeature, const bool theToStoreValue)
+void ModuleBase_ModelWidget::setFeature(const FeaturePtr& theFeature, const bool theToStoreValue,
+                                        const bool isUpdateFlushed)
 {
+  /// it is possible to give this flag as parameter in storeValue/storeCustomValue
+  /// after debug, it may be corrected
+  myFlushUpdateBlocked = !isUpdateFlushed;
   myFeature = theFeature;
   if (theToStoreValue)
     storeValue();
+  myFlushUpdateBlocked = false;
 }
 
 bool ModuleBase_ModelWidget::focusTo()
 {
+#ifdef DEBUG_WIDGET_INSTANCE
+  qDebug("ModuleBase_ModelWidget::focusTo");
+#endif
   QList<QWidget*> aControls = getControls();
   QList<QWidget*>::const_iterator anIt = aControls.begin(), aLast = aControls.end();
   bool isFocusAccepted = false;
   for (; anIt != aLast && !isFocusAccepted; anIt++) {
     QWidget* aWidget = *anIt;
     if (aWidget && aWidget->focusPolicy() != Qt::NoFocus) {
-      aWidget->setFocus();
+      ModuleBase_Tools::setFocus(aWidget, "ModuleBase_ModelWidget::focusTo()");
       isFocusAccepted = true;
     }
   }
@@ -118,6 +213,9 @@ bool ModuleBase_ModelWidget::focusTo()
 
 void ModuleBase_ModelWidget::activate()
 {
+#ifdef DEBUG_WIDGET_INSTANCE
+  qDebug("ModuleBase_ModelWidget::activate");
+#endif
   // the control value is stored to the mode by the focus in on the widget
   // we need the value is initialized in order to enable the apply button in the property panel.
   // It should happens in the creation mode only because all fields are filled in the edition mode
@@ -126,15 +224,22 @@ void ModuleBase_ModelWidget::activate()
     if (anAttribute.get() != NULL && !anAttribute->isInitialized())
       initializeValueByActivate();
   }
+
+  if (myWidgetValidator)
+    myWidgetValidator->activateFilters(true);
+
   activateCustom();
 }
 
 void ModuleBase_ModelWidget::deactivate()
 {
+#ifdef DEBUG_WIDGET_INSTANCE
+  qDebug("ModuleBase_ModelWidget::deactivate");
+#endif
   myIsValueStateBlocked = false;
-  if (myState == ModifiedInPP)
-    storeValue();
   myState = Stored;
+  if (myWidgetValidator)
+    myWidgetValidator->activateFilters(false);
 }
 
 void ModuleBase_ModelWidget::initializeValueByActivate()
@@ -186,11 +291,32 @@ bool ModuleBase_ModelWidget::storeValue()
 
   return isDone;
 }
+#ifdef DEBUG_VALUE_STATE
+std::string getDebugInfo(const ModuleBase_ModelWidget::ValueState& theState)
+{
+  std::string anInfo;
+  switch (theState) {
+    case ModuleBase_ModelWidget::Stored:           anInfo = "Stored          "; break;
+    case ModuleBase_ModelWidget::ModifiedInPP:     anInfo = "ModifiedInPP    "; break;
+    case ModuleBase_ModelWidget::ModifiedInViewer: anInfo = "ModifiedInViewer"; break;
+    case ModuleBase_ModelWidget::Reset:            anInfo = "Reset           "; break;
+    default: break;
+  }
+  return anInfo;
+}
 
-ModuleBase_ModelWidget::ValueState ModuleBase_ModelWidget::setValueState(const ModuleBase_ModelWidget::ValueState& theState)
+#endif
+ModuleBase_ModelWidget::ValueState ModuleBase_ModelWidget::setValueState
+                                         (const ModuleBase_ModelWidget::ValueState& theState)
 {
   ValueState aState = myState;
+
   if (myState != theState && !myIsValueStateBlocked) {
+#ifdef DEBUG_VALUE_STATE
+    qDebug(QString("setValueState: previous state = %1,\t new state = %2")
+           .arg(getDebugInfo(myState).c_str())
+           .arg(getDebugInfo(theState).c_str()).toStdString().c_str());
+#endif
     myState = theState;
     emit valueStateChanged(aState);
   }
@@ -213,18 +339,23 @@ bool ModuleBase_ModelWidget::restoreValue()
   return isDone;
 }
 
-void ModuleBase_ModelWidget::updateObject(ObjectPtr theObj)
+void ModuleBase_ModelWidget::updateObject(ObjectPtr theObject)
 {
-  blockUpdateViewer(true);
-
-  Events_Loop::loop()->flush(Events_Loop::eventByName(EVENT_OBJECT_UPDATED));
-
-  blockUpdateViewer(false);
+  if (!myFlushUpdateBlocked) {
+#ifdef DEBUG_WIDGET_INSTANCE
+    qDebug("ModuleBase_ModelWidget::updateObject");
+#endif
+    ModuleBase_Tools::flushUpdated(theObject);
+    emit objectUpdated();
+  }
 }
 
 void ModuleBase_ModelWidget::moveObject(ObjectPtr theObj)
 {
   //blockUpdateViewer(true);
+#ifdef DEBUG_WIDGET_INSTANCE
+  qDebug("ModuleBase_ModelWidget::moveObject");
+#endif
 
   static Events_ID anEvent = Events_Loop::eventByName(EVENT_OBJECT_MOVED);
   ModelAPI_EventCreator::get()->sendUpdated(theObj, anEvent);
@@ -238,6 +369,13 @@ bool ModuleBase_ModelWidget::processEnter()
   return false;
 }
 
+bool ModuleBase_ModelWidget::processDelete()
+{
+  // we consider that model objects eats delete key in order to
+  // do nothing by for example symbol delete in line edit or spin box
+  return true;
+}
+
 bool ModuleBase_ModelWidget::eventFilter(QObject* theObject, QEvent *theEvent)
 {
   QWidget* aWidget = qobject_cast<QWidget*>(theObject);
@@ -259,8 +397,11 @@ bool ModuleBase_ModelWidget::eventFilter(QObject* theObject, QEvent *theEvent)
                         aReason == Qt::TabFocusReason ||
                         aReason == Qt::BacktabFocusReason ||
                         aReason == Qt::OtherFocusReason; // to process widget->setFocus()
-    if (aMouseOrKey && getControls().contains(aWidget) && getValueState() == ModifiedInPP)
-      storeValue();
+    if (aMouseOrKey && getControls().contains(aWidget)) {
+      if (getValueState() == ModifiedInPP) {
+        storeValue();
+      }
+    }
   }
   // pass the event on to the parent class
 
@@ -278,23 +419,3 @@ void ModuleBase_ModelWidget::onWidgetValuesModified()
 {
   setValueState(ModifiedInPP);
 }
-
-//**************************************************************
-void ModuleBase_ModelWidget::blockUpdateViewer(const bool theValue)
-{
-  // the viewer update should be blocked in order to avoid the temporary feature content
-  // when the solver processes the feature, the redisplay message can be flushed
-  // what caused the display in the viewer preliminary states of object
-  // e.g. fillet feature, angle value change
-  std::shared_ptr<Events_Message> aMsg;
-  if (theValue) {
-    aMsg = std::shared_ptr<Events_Message>(
-        new Events_Message(Events_Loop::eventByName(EVENT_UPDATE_VIEWER_BLOCKED)));
-  }
-  else {
-    // the viewer update should be unblocked
-    aMsg = std::shared_ptr<Events_Message>(
-        new Events_Message(Events_Loop::eventByName(EVENT_UPDATE_VIEWER_UNBLOCKED)));
-  }
-  Events_Loop::loop()->send(aMsg);
-}