Salome HOME
Issue #1701 Preselecting before calling constraints does not put objects in the input...
[modules/shaper.git] / src / ModuleBase / ModuleBase_ModelWidget.cpp
index 01bb7f33e64e7fd4b50177aa4ea130e6c8c23ea8..713adea890d132a12a0b4b231bf6a3190eb14d7e 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
+
 ModuleBase_ModelWidget::ModuleBase_ModelWidget(QWidget* theParent,
                                                const Config_WidgetAPI* theData)
     : QWidget(theParent),
@@ -35,6 +42,10 @@ ModuleBase_ModelWidget::ModuleBase_ModelWidget(QWidget* theParent,
       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);
@@ -43,10 +54,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();
@@ -61,6 +86,19 @@ bool ModuleBase_ModelWidget::isInitialized(ObjectPtr theObject) const
   return theObject->data()->attribute(attributeID())->isInitialized();
 }
 
+bool ModuleBase_ModelWidget::isValueEnabled() const
+{
+  bool anEnabled = true;
+  if (myIsValueEnabled == DefinedInPreferences) {
+    bool aCanDisable = Config_PropManager::boolean(SKETCH_TAB_NAME, "disable_input_fields", "true");
+    if (aCanDisable)
+      anEnabled = false;
+  }
+  else if (myIsValueEnabled == Off)
+    anEnabled = false;
+  return anEnabled;
+}
+
 void ModuleBase_ModelWidget::processValueState()
 {
   if (myState == ModifiedInPP || myState == ModifiedInViewer)
@@ -106,22 +144,41 @@ QString ModuleBase_ModelWidget::getError(const bool theValueStateChecked) const
     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 = 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());
   }
 
-  anError = QString::fromStdString(anErrorMsg);
   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();
@@ -152,13 +209,21 @@ void ModuleBase_ModelWidget::setFeature(const FeaturePtr& theFeature, const bool
   /// 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;
@@ -174,6 +239,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
@@ -191,6 +259,9 @@ void ModuleBase_ModelWidget::activate()
 
 void ModuleBase_ModelWidget::deactivate()
 {
+#ifdef DEBUG_WIDGET_INSTANCE
+  qDebug("ModuleBase_ModelWidget::deactivate");
+#endif
   myIsValueStateBlocked = false;
   myState = Stored;
   if (myWidgetValidator)
@@ -297,6 +368,9 @@ bool ModuleBase_ModelWidget::restoreValue()
 void ModuleBase_ModelWidget::updateObject(ObjectPtr theObject)
 {
   if (!myFlushUpdateBlocked) {
+#ifdef DEBUG_WIDGET_INSTANCE
+    qDebug("ModuleBase_ModelWidget::updateObject");
+#endif
     ModuleBase_Tools::flushUpdated(theObject);
     emit objectUpdated();
   }
@@ -305,6 +379,9 @@ void ModuleBase_ModelWidget::updateObject(ObjectPtr theObject)
 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);