Salome HOME
Better management of errors.
[modules/shaper.git] / src / ModuleBase / ModuleBase_ModelWidget.cpp
index b1d5b5a461865730ccdedd057c0edcee06c1a9bd..db9908a41da042ddfb9ca90f48b5799b48ddf1e5 100644 (file)
@@ -9,6 +9,8 @@
 #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 <Config_Keywords.h>
 #include <Config_WidgetAPI.h>
+#include <Config_Translator.h>
+#include <Config_PropManager.h>
 
 #include <Events_Loop.h>
 
 #include <QEvent>
 #include <QLabel>
 #include <QFocusEvent>
+#include <QTextCodec>
 
 //#define DEBUG_VALUE_STATE
 
+//#define DEBUG_WIDGET_INSTANCE
+//#define DEBUG_ENABLE_SKETCH_INPUT_FIELDS
+
 ModuleBase_ModelWidget::ModuleBase_ModelWidget(QWidget* theParent,
                                                const Config_WidgetAPI* theData)
     : QWidget(theParent),
       myIsEditing(false),
       myState(Stored),
       myIsValueStateBlocked(false),
+      myFlushUpdateBlocked(false),
       myWidgetValidator(0)
 {
+#ifdef DEBUG_WIDGET_INSTANCE
+  qDebug("ModuleBase_ModelWidget::ModuleBase_ModelWidget");
+#endif
+
+  myFeatureId = theData->featureId();
+
   myIsInternal = theData->getBooleanAttribute(ATTR_INTERNAL, false);
 
   myDefaultValue = theData->getProperty(ATTR_DEFAULT);
@@ -42,10 +57,24 @@ ModuleBase_ModelWidget::ModuleBase_ModelWidget(QWidget* theParent,
   myAttributeID = theData ? theData->widgetId() : "";
   myIsObligatory = theData->getBooleanAttribute(ATTR_OBLIGATORY, true);
 
+  myIsValueEnabled = On; // not defined or "true"
+  std::string anEnableValue = theData->getProperty(DOUBLE_WDG_ENABLE_VALUE);
+  if (anEnableValue == "false")
+    myIsValueEnabled = Off;
+  if (anEnableValue == DOUBLE_WDG_ENABLE_VALUE_BY_PREFERENCES)
+    myIsValueEnabled = DefinedInPreferences;
+
   connect(this, SIGNAL(valuesChanged()), this, SLOT(onWidgetValuesChanged()));
   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();
@@ -60,22 +89,47 @@ bool ModuleBase_ModelWidget::isInitialized(ObjectPtr theObject) const
   return theObject->data()->attribute(attributeID())->isInitialized();
 }
 
-QString ModuleBase_ModelWidget::getValueStateError() const
+bool ModuleBase_ModelWidget::isValueEnabled() const
+{
+  bool anEnabled = true;
+  if (myIsValueEnabled == DefinedInPreferences) {
+#ifdef DEBUG_ENABLE_SKETCH_INPUT_FIELDS
+    bool aCanDisable = false;
+#else
+    //Config_PropManager::boolean(SKETCH_TAB_NAME, "disable_input_fields", "true");
+    bool aCanDisable = true;
+#endif
+    if (aCanDisable)
+      anEnabled = false;
+  }
+  else if (myIsValueEnabled == Off)
+    anEnabled = false;
+  return anEnabled;
+}
+
+void ModuleBase_ModelWidget::processValueState()
+{
+  if (myState == ModifiedInPP || myState == ModifiedInViewer)
+    storeValue();
+}
+
+Events_InfoMessage ModuleBase_ModelWidget::getValueStateError() const
 {
-  QString anError = "";
+  Events_InfoMessage aMessage;
 
   ModuleBase_ModelWidget::ValueState aState = getValueState();
   if (aState != ModuleBase_ModelWidget::Stored) {
     AttributePtr anAttr = feature()->attribute(attributeID());
     if (anAttr.get()) {
-      QString anAttributeName = anAttr->id().c_str();
+      const std::string& anAttributeName = anAttr->id();
       switch (aState) {
         case ModuleBase_ModelWidget::ModifiedInViewer:
-          anError = "Attribute \"" + anAttributeName +
-                    "\" is locked by modification value in the viewer.";
+          aMessage = "Attribute \"%1\" is locked by modification value in the viewer.";
+          aMessage.addParameter(anAttributeName);
           break;
         case ModuleBase_ModelWidget::Reset:
-          anError = "Attribute \"" + anAttributeName + "\" is not initialized.";
+          aMessage = "Attribute \"%1\" is not initialized.";
+          aMessage.addParameter(anAttributeName);
           break;
         case ModuleBase_ModelWidget::ModifiedInPP: // Apply should be enabled in this mode
         default:
@@ -83,34 +137,42 @@ QString ModuleBase_ModelWidget::getValueStateError() const
       }
     }
   }
-  return anError;
+  return aMessage;
 }
 
-QString ModuleBase_ModelWidget::getError() const
+QString ModuleBase_ModelWidget::getError(const bool theValueStateChecked) const
 {
   QString anError;
 
   if (!feature().get())
     return anError;
 
+  std::string aFeatureID = feature()->getKind();
   std::string anAttributeID = attributeID();
   AttributePtr anAttribute = feature()->attribute(anAttributeID);
   if (!anAttribute.get())
     return anError;
 
   std::string aValidatorID;
-  std::string anErrorMsg;
+  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;
+      anErrorMsg = "Unknown error.";
+
+    if (anErrorMsg.context().empty()) {
+      anErrorMsg.setContext(aFeatureID + ":" + anAttributeID + ":" + aValidatorID);
+    }
+  }
+
+  if (anErrorMsg.empty() && theValueStateChecked) {
+    anErrorMsg = getValueStateError();
   }
 
-  anError = QString::fromStdString(anErrorMsg);
-  if (anError.isEmpty())
-    anError = getValueStateError();
+  if (!anErrorMsg.empty()) {
+    anError = ModuleBase_Tools::translate(anErrorMsg);
+  }
 
   return anError;
 }
@@ -138,15 +200,28 @@ void ModuleBase_ModelWidget::setHighlighted(bool 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();
+  if (theToStoreValue) {
+    /// it is possible that the attribute is filled before the operation is started,
+    /// e.g. by reentrant operation case some attributes are filled by values of
+    /// feature of previous operation, we should not lost them here
+    if (!theFeature->data()->attribute(attributeID())->isInitialized())
+      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;
@@ -162,6 +237,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
@@ -179,11 +257,11 @@ void ModuleBase_ModelWidget::activate()
 
 void ModuleBase_ModelWidget::deactivate()
 {
+#ifdef DEBUG_WIDGET_INSTANCE
+  qDebug("ModuleBase_ModelWidget::deactivate");
+#endif
   myIsValueStateBlocked = false;
-  if (myState == ModifiedInPP || myState == ModifiedInViewer)
-    storeValue();
   myState = Stored;
-
   if (myWidgetValidator)
     myWidgetValidator->activateFilters(false);
 }
@@ -287,13 +365,21 @@ bool ModuleBase_ModelWidget::restoreValue()
 
 void ModuleBase_ModelWidget::updateObject(ObjectPtr theObject)
 {
-  ModuleBase_Tools::flushUpdated(theObject);
-  emit objectUpdated();
+  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);
@@ -357,3 +443,8 @@ void ModuleBase_ModelWidget::onWidgetValuesModified()
 {
   setValueState(ModifiedInPP);
 }
+
+QString ModuleBase_ModelWidget::translate(const std::string& theStr) const
+{
+  return ModuleBase_Tools::translate(context(), theStr);
+}