Salome HOME
Improve multi-selector control to provide items multi-selection and "Delete" context...
[modules/shaper.git] / src / ModuleBase / ModuleBase_ModelWidget.cpp
index d9c678185b11bd4138c2783094a028e4cf0ae382..86fe765daafc4e328d73f14677ec27471f799be4 100644 (file)
@@ -11,6 +11,7 @@
 #include <ModelAPI_Attribute.h>
 #include <ModelAPI_Events.h>
 #include <ModelAPI_Session.h>
+#include <ModelAPI_Validator.h>
 
 #include <Config_Keywords.h>
 #include <Config_WidgetAPI.h>
@@ -30,6 +31,8 @@ ModuleBase_ModelWidget::ModuleBase_ModelWidget(QWidget* theParent,
       myState(Stored),
       myIsValueStateBlocked(false)
 {
+  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;
@@ -64,10 +67,6 @@ QString ModuleBase_ModelWidget::getValueStateError() const
     if (anAttr.get()) {
       QString anAttributeName = anAttr->id().c_str();
       switch (aState) {
-        case ModuleBase_ModelWidget::ModifiedInPP:
-          anError = "Attribute \"" + anAttributeName +
-                    "\" modification is not applyed. Please click \"Enter\" or \"Tab\".";
-          break;
         case ModuleBase_ModelWidget::ModifiedInViewer:
           anError = "Attribute \"" + anAttributeName +
                     "\" is locked by modification value in the viewer.";
@@ -75,23 +74,50 @@ QString ModuleBase_ModelWidget::getValueStateError() const
         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
+{
+  QString anError;
+
+  if (!feature().get())
+    return anError;
+
+  std::string anAttributeID = attributeID();
+  AttributePtr anAttribute = feature()->attribute(anAttributeID);
+  if (!anAttribute.get())
+    return anError;
+
+  std::string aValidatorID;
+  std::string 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;
+  }
+
+  anError = QString::fromStdString(anErrorMsg);
+  if (anError.isEmpty())
+    anError = getValueStateError();
+
+  return anError;
+}
+
 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);
-    }
   }
 }
 
@@ -147,7 +173,7 @@ void ModuleBase_ModelWidget::activate()
 void ModuleBase_ModelWidget::deactivate()
 {
   myIsValueStateBlocked = false;
-  if (myState == ModifiedInPP)
+  if (myState == ModifiedInPP || myState == ModifiedInViewer)
     storeValue();
   myState = Stored;
 }
@@ -274,8 +300,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