Salome HOME
Better management of errors.
[modules/shaper.git] / src / ModuleBase / ModuleBase_ModelWidget.cpp
index 66d1c8c96daca4ad89f36bc567d7ed64348dd5ef..db9908a41da042ddfb9ca90f48b5799b48ddf1e5 100644 (file)
@@ -47,6 +47,8 @@ ModuleBase_ModelWidget::ModuleBase_ModelWidget(QWidget* theParent,
   qDebug("ModuleBase_ModelWidget::ModuleBase_ModelWidget");
 #endif
 
+  myFeatureId = theData->featureId();
+
   myIsInternal = theData->getBooleanAttribute(ATTR_INTERNAL, false);
 
   myDefaultValue = theData->getProperty(ATTR_DEFAULT);
@@ -94,7 +96,8 @@ bool ModuleBase_ModelWidget::isValueEnabled() const
 #ifdef DEBUG_ENABLE_SKETCH_INPUT_FIELDS
     bool aCanDisable = false;
 #else
-    bool aCanDisable = true;//Config_PropManager::boolean(SKETCH_TAB_NAME, "disable_input_fields", "true");
+    //Config_PropManager::boolean(SKETCH_TAB_NAME, "disable_input_fields", "true");
+    bool aCanDisable = true;
 #endif
     if (aCanDisable)
       anEnabled = false;
@@ -110,22 +113,23 @@ void ModuleBase_ModelWidget::processValueState()
     storeValue();
 }
 
-QString ModuleBase_ModelWidget::getValueStateError() const
+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:
@@ -133,7 +137,7 @@ QString ModuleBase_ModelWidget::getValueStateError() const
       }
     }
   }
-  return anError;
+  return aMessage;
 }
 
 QString ModuleBase_ModelWidget::getError(const bool theValueStateChecked) const
@@ -143,6 +147,7 @@ QString ModuleBase_ModelWidget::getError(const bool theValueStateChecked) const
   if (!feature().get())
     return anError;
 
+  std::string aFeatureID = feature()->getKind();
   std::string anAttributeID = attributeID();
   AttributePtr anAttribute = feature()->attribute(anAttributeID);
   if (!anAttribute.get())
@@ -154,36 +159,24 @@ QString ModuleBase_ModelWidget::getError(const bool theValueStateChecked) const
   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();
+      anErrorMsg = "Unknown error.";
+
+    if (anErrorMsg.context().empty()) {
+      anErrorMsg.setContext(aFeatureID + ":" + anAttributeID + ":" + aValidatorID);
+    }
   }
 
-  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 (anErrorMsg.empty() && theValueStateChecked) {
+    anErrorMsg = getValueStateError();
   }
 
-  if (anError.isEmpty() && theValueStateChecked)
-    anError = getValueStateError();
+  if (!anErrorMsg.empty()) {
+    anError = ModuleBase_Tools::translate(anErrorMsg);
+  }
 
-  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();
@@ -450,3 +443,8 @@ void ModuleBase_ModelWidget::onWidgetValuesModified()
 {
   setValueState(ModifiedInPP);
 }
+
+QString ModuleBase_ModelWidget::translate(const std::string& theStr) const
+{
+  return ModuleBase_Tools::translate(context(), theStr);
+}