Salome HOME
Start of the edit should not change the current feature.
[modules/shaper.git] / src / ModuleBase / ModuleBase_ModelWidget.cpp
index 721c6c7b0470afd3ce84db7a0ee2517d58a6f7d2..f0d33055320a9a9d21ef2fd36029e063aa75a5e1 100644 (file)
@@ -28,7 +28,8 @@ ModuleBase_ModelWidget::ModuleBase_ModelWidget(QWidget* theParent,
     : QWidget(theParent),
       myParentId(theParentId),
       myIsEditing(false),
-      myState(Stored)
+      myState(Stored),
+      myIsValueStateBlocked(false)
 {
   myDefaultValue = theData->getProperty(ATTR_DEFAULT);
   myUseReset = theData->getBooleanAttribute(ATTR_USE_RESET, true);
@@ -122,20 +123,32 @@ void ModuleBase_ModelWidget::activate()
   // It should happens in the creation mode only because all fields are filled in the edition mode
   if (!isEditingMode()) {
     AttributePtr anAttribute = myFeature->data()->attribute(myAttributeID);
-    if (anAttribute.get() != NULL && !anAttribute->isInitialized()) {
-      if (isComputedDefault()) {
-        if (myFeature->compute(myAttributeID)) {
-          restoreValue();
-        }
-      }
-      else {
-        storeValue();
-      }
-    }
+    if (anAttribute.get() != NULL && !anAttribute->isInitialized())
+      initializeValueByActivate();
   }
   activateCustom();
 }
 
+void ModuleBase_ModelWidget::deactivate()
+{
+  myIsValueStateBlocked = false;
+  if (myState == ModifiedInPP)
+    storeValue();
+  myState = Stored;
+}
+
+void ModuleBase_ModelWidget::initializeValueByActivate()
+{
+  if (isComputedDefault()) {
+    if (myFeature->compute(myAttributeID)) {
+      restoreValue();
+    }
+  }
+  else {
+    storeValue();
+  }
+}
+
 QWidget* ModuleBase_ModelWidget::getControlAcceptingFocus(const bool isFirst)
 {
   QWidget* aControl = 0;
@@ -174,13 +187,21 @@ bool ModuleBase_ModelWidget::storeValue()
   return isDone;
 }
 
-void ModuleBase_ModelWidget::setValueState(const ValueState& theState)
+ModuleBase_ModelWidget::ValueState ModuleBase_ModelWidget::setValueState(const ModuleBase_ModelWidget::ValueState& theState)
 {
-  if (myState == theState)
-    return;
+  ValueState aState = myState;
+  if (myState != theState && !myIsValueStateBlocked) {
+    myState = theState;
+    emit valueStateChanged(aState);
+  }
+  return aState;
+}
 
-  myState = theState;
-  emit valueStateChanged();
+bool ModuleBase_ModelWidget::blockValueState(const bool theBlocked)
+{
+  bool isBlocked = myIsValueStateBlocked;
+  myIsValueStateBlocked = theBlocked;
+  return isBlocked;
 }
 
 bool ModuleBase_ModelWidget::restoreValue()
@@ -192,14 +213,6 @@ bool ModuleBase_ModelWidget::restoreValue()
   return isDone;
 }
 
-void ModuleBase_ModelWidget::storeValueByApply()
-{
-  // do not emit signal about update the currenty feature object
-  // in order to do not perform additional redisplay in the viewer.
-  // It should happens by finish operation of the apply action
-  storeValueCustom();
-}
-
 void ModuleBase_ModelWidget::updateObject(ObjectPtr theObj)
 {
   blockUpdateViewer(true);
@@ -238,6 +251,17 @@ bool ModuleBase_ModelWidget::eventFilter(QObject* theObject, QEvent *theEvent)
       emit focusInWidget(this);
     }
   }
+  else if (theEvent->type() == QEvent::FocusOut) {
+    QFocusEvent* aFocusEvent = dynamic_cast<QFocusEvent*>(theEvent);
+
+    Qt::FocusReason aReason = aFocusEvent->reason();
+    bool aMouseOrKey = aReason == Qt::MouseFocusReason ||
+                        aReason == Qt::TabFocusReason ||
+                        aReason == Qt::BacktabFocusReason ||
+                        aReason == Qt::OtherFocusReason; // to process widget->setFocus()
+    if (aMouseOrKey && getControls().contains(aWidget) && getValueState() == ModifiedInPP)
+      storeValue();
+  }
   // pass the event on to the parent class
 
   return QObject::eventFilter(theObject, theEvent);
@@ -252,7 +276,7 @@ void ModuleBase_ModelWidget::onWidgetValuesChanged()
 //**************************************************************
 void ModuleBase_ModelWidget::onWidgetValuesModified()
 {
-  setValueState(Modified);
+  setValueState(ModifiedInPP);
 }
 
 //**************************************************************