Salome HOME
Merge branch 'Dev_GroupsRevision'
[modules/shaper.git] / src / ModuleBase / ModuleBase_ModelWidget.cpp
index fdfffa34389b7df51a34a367975047e4141ebec0..50c7734d280c69e7af1b38b516efb322c6be5353 100644 (file)
@@ -1,10 +1,25 @@
-// Copyright (C) 2014-20xx CEA/DEN, EDF R&D
-
-// File:        ModuleBase_ModelWidget.cpp
-// Created:     25 Apr 2014
-// Author:      Natalia ERMOLAEVA
+// Copyright (C) 2014-2017  CEA/DEN, EDF R&D
+//
+// This library is free software; you can redistribute it and/or
+// modify it under the terms of the GNU Lesser General Public
+// License as published by the Free Software Foundation; either
+// version 2.1 of the License, or (at your option) any later version.
+//
+// This library is distributed in the hope that it will be useful,
+// but WITHOUT ANY WARRANTY; without even the implied warranty of
+// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
+// Lesser General Public License for more details.
+//
+// You should have received a copy of the GNU Lesser General Public
+// License along with this library; if not, write to the Free Software
+// Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
+//
+// See http://www.salome-platform.org/ or
+// email : webmaster.salome@opencascade.com<mailto:webmaster.salome@opencascade.com>
+//
 
 #include "ModuleBase_ModelWidget.h"
+#include "ModuleBase_IPropertyPanel.h"
 #include "ModuleBase_ViewerPrs.h"
 #include "ModuleBase_Tools.h"
 #include "ModuleBase_WidgetValidator.h"
@@ -34,6 +49,7 @@
 //#define DEBUG_WIDGET_INSTANCE
 //#define DEBUG_ENABLE_SKETCH_INPUT_FIELDS
 
+//**************************************************************
 ModuleBase_ModelWidget::ModuleBase_ModelWidget(QWidget* theParent,
                                                const Config_WidgetAPI* theData)
     : QWidget(theParent),
@@ -47,8 +63,12 @@ ModuleBase_ModelWidget::ModuleBase_ModelWidget(QWidget* theParent,
   qDebug("ModuleBase_ModelWidget::ModuleBase_ModelWidget");
 #endif
 
+  myFeatureId = theData->featureId();
+
   myIsInternal = theData->getBooleanAttribute(ATTR_INTERNAL, false);
 
+  myIsModifiedInEdit = theData->getProperty(ATTR_MODIFIED_IN_EDIT);
+
   myDefaultValue = theData->getProperty(ATTR_DEFAULT);
   myUseReset = theData->getBooleanAttribute(ATTR_USE_RESET, true);
   myIsComputedDefault = theData->getProperty(ATTR_DEFAULT) == DOUBLE_WDG_DEFAULT_COMPUTED;
@@ -66,6 +86,7 @@ ModuleBase_ModelWidget::ModuleBase_ModelWidget(QWidget* theParent,
   connect(this, SIGNAL(valuesModified()), this, SLOT(onWidgetValuesModified()));
 }
 
+//**************************************************************
 ModuleBase_ModelWidget::~ModuleBase_ModelWidget()
 {
 #ifdef DEBUG_WIDGET_INSTANCE
@@ -73,6 +94,7 @@ ModuleBase_ModelWidget::~ModuleBase_ModelWidget()
 #endif
 }
 
+//**************************************************************
 bool ModuleBase_ModelWidget::reset()
 {
   bool aResult = resetCustom();
@@ -82,11 +104,29 @@ bool ModuleBase_ModelWidget::reset()
   return aResult;
 }
 
+//**************************************************************
 bool ModuleBase_ModelWidget::isInitialized(ObjectPtr theObject) const
 {
   return theObject->data()->attribute(attributeID())->isInitialized();
 }
 
+//**************************************************************
+void ModuleBase_ModelWidget::selectionModes(int& theModuleSelectionModes, QIntList& theModes)
+{
+  theModuleSelectionModes = -1;
+  if (myWidgetValidator)
+    myWidgetValidator->selectionModes(theModuleSelectionModes, theModes);
+}
+
+//**************************************************************
+void ModuleBase_ModelWidget::selectionFilters(QIntList& theModuleSelectionFilters,
+                                              SelectMgr_ListOfFilter& theSelectionFilters)
+{
+  if (myWidgetValidator)
+    myWidgetValidator->selectionFilters(theModuleSelectionFilters, theSelectionFilters);
+}
+
+//**************************************************************
 bool ModuleBase_ModelWidget::isValueEnabled() const
 {
   bool anEnabled = true;
@@ -105,28 +145,31 @@ bool ModuleBase_ModelWidget::isValueEnabled() const
   return anEnabled;
 }
 
+//**************************************************************
 void ModuleBase_ModelWidget::processValueState()
 {
   if (myState == ModifiedInPP || myState == ModifiedInViewer)
     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:
@@ -134,9 +177,10 @@ QString ModuleBase_ModelWidget::getValueStateError() const
       }
     }
   }
-  return anError;
+  return aMessage;
 }
 
+//**************************************************************
 QString ModuleBase_ModelWidget::getError(const bool theValueStateChecked) const
 {
   QString anError;
@@ -144,6 +188,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())
@@ -155,36 +200,25 @@ 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();
@@ -194,6 +228,7 @@ void ModuleBase_ModelWidget::enableFocusProcessing()
   }
 }
 
+//**************************************************************
 void ModuleBase_ModelWidget::setHighlighted(bool isHighlighted)
 {
   QList<QWidget*> aWidgetList = getControls();
@@ -208,6 +243,7 @@ void ModuleBase_ModelWidget::setHighlighted(bool isHighlighted)
   }
 }
 
+//**************************************************************
 void ModuleBase_ModelWidget::setFeature(const FeaturePtr& theFeature, const bool theToStoreValue,
                                         const bool isUpdateFlushed)
 {
@@ -225,6 +261,7 @@ void ModuleBase_ModelWidget::setFeature(const FeaturePtr& theFeature, const bool
   myFlushUpdateBlocked = false;
 }
 
+//**************************************************************
 bool ModuleBase_ModelWidget::focusTo()
 {
 #ifdef DEBUG_WIDGET_INSTANCE
@@ -243,6 +280,7 @@ bool ModuleBase_ModelWidget::focusTo()
   return isFocusAccepted;
 }
 
+//**************************************************************
 void ModuleBase_ModelWidget::activate()
 {
 #ifdef DEBUG_WIDGET_INSTANCE
@@ -256,13 +294,10 @@ void ModuleBase_ModelWidget::activate()
     if (anAttribute.get() != NULL && !anAttribute->isInitialized())
       initializeValueByActivate();
   }
-
-  if (myWidgetValidator)
-    myWidgetValidator->activateFilters(true);
-
   activateCustom();
 }
 
+//**************************************************************
 void ModuleBase_ModelWidget::deactivate()
 {
 #ifdef DEBUG_WIDGET_INSTANCE
@@ -271,9 +306,10 @@ void ModuleBase_ModelWidget::deactivate()
   myIsValueStateBlocked = false;
   myState = Stored;
   if (myWidgetValidator)
-    myWidgetValidator->activateFilters(false);
+    myWidgetValidator->clearValidatedCash();
 }
 
+//**************************************************************
 void ModuleBase_ModelWidget::initializeValueByActivate()
 {
   if (isComputedDefault()) {
@@ -286,6 +322,7 @@ void ModuleBase_ModelWidget::initializeValueByActivate()
   }
 }
 
+//**************************************************************
 QWidget* ModuleBase_ModelWidget::getControlAcceptingFocus(const bool isFirst)
 {
   QWidget* aControl = 0;
@@ -308,22 +345,41 @@ QWidget* ModuleBase_ModelWidget::getControlAcceptingFocus(const bool isFirst)
   return aControl;
 }
 
+//**************************************************************
 void ModuleBase_ModelWidget::setDefaultValue(const std::string& theValue)
 {
   myDefaultValue = theValue;
 }
 
+//**************************************************************
 bool ModuleBase_ModelWidget::storeValue()
 {
   setValueState(Stored);
 
   emit beforeValuesChanged();
-  bool isDone = storeValueCustom();
+  bool isDone = false;
+  // value is stored only in creation mode and in edition if there is not
+  // XML flag prohibited modification in edit mode(macro feature circle/arc)
+  if (!isEditingMode() || isModifiedInEdit().empty())
+    isDone = storeValueCustom();
+  else {
+    /// store value in an alternative attribute if possible(attribute has the same type)
+    std::string aWidgetAttribute = attributeID();
+    myAttributeID = isModifiedInEdit();
+    storeValueCustom();
+    myAttributeID = aWidgetAttribute;
+    // operation will be restarted but if isDone == true, PagedContainer will try to set focus
+    // to the current widget, but will be already deleted
+    isDone = false;
+  }
+
   emit afterValuesChanged();
 
   return isDone;
 }
 #ifdef DEBUG_VALUE_STATE
+
+//**************************************************************
 std::string getDebugInfo(const ModuleBase_ModelWidget::ValueState& theState)
 {
   std::string anInfo;
@@ -336,8 +392,9 @@ std::string getDebugInfo(const ModuleBase_ModelWidget::ValueState& theState)
   }
   return anInfo;
 }
-
 #endif
+
+//**************************************************************
 ModuleBase_ModelWidget::ValueState ModuleBase_ModelWidget::setValueState
                                          (const ModuleBase_ModelWidget::ValueState& theState)
 {
@@ -355,6 +412,7 @@ ModuleBase_ModelWidget::ValueState ModuleBase_ModelWidget::setValueState
   return aState;
 }
 
+//**************************************************************
 bool ModuleBase_ModelWidget::blockValueState(const bool theBlocked)
 {
   bool isBlocked = myIsValueStateBlocked;
@@ -362,6 +420,7 @@ bool ModuleBase_ModelWidget::blockValueState(const bool theBlocked)
   return isBlocked;
 }
 
+//**************************************************************
 bool ModuleBase_ModelWidget::restoreValue()
 {
   emit beforeValuesRestored();
@@ -371,6 +430,7 @@ bool ModuleBase_ModelWidget::restoreValue()
   return isDone;
 }
 
+//**************************************************************
 void ModuleBase_ModelWidget::updateObject(ObjectPtr theObject)
 {
   if (!myFlushUpdateBlocked) {
@@ -382,25 +442,56 @@ void ModuleBase_ModelWidget::updateObject(ObjectPtr theObject)
   }
 }
 
-void ModuleBase_ModelWidget::moveObject(ObjectPtr theObj)
+//**************************************************************
+bool ModuleBase_ModelWidget::canProcessAction(ModuleBase_ActionType theActionType,
+                                              bool& isActionEnabled)
 {
-  //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);
-  Events_Loop::loop()->flush(anEvent);
+  isActionEnabled = false;
+  switch (theActionType) {
+    case ActionEnter: return false;
+    case ActionEscape: return false;
+    case ActionDelete: return true;
+    case ActionSelection: return true;
+    case ActionUndo:
+    case ActionRedo:
+    default:
+      return false;
+  }
+}
 
-  //blockUpdateViewer(false);
+//**************************************************************
+bool ModuleBase_ModelWidget::processAction(ModuleBase_ActionType theActionType,
+                                           const ActionParamPtr& theParam)
+{
+  switch (theActionType) {
+    case ActionEnter:
+      return processEnter();
+    case ActionEscape:
+      return processEscape();
+    case ActionDelete:
+      return processDelete();
+    case ActionSelection:
+      processSelection();
+    case ActionUndo:
+    case ActionRedo:
+    default:
+      return false;
+  }
 }
 
+//**************************************************************
 bool ModuleBase_ModelWidget::processEnter()
 {
   return false;
 }
 
+//**************************************************************
+bool ModuleBase_ModelWidget::processEscape()
+{
+  return false;
+}
+
+//**************************************************************
 bool ModuleBase_ModelWidget::processDelete()
 {
   // we consider that model objects eats delete key in order to
@@ -408,17 +499,26 @@ bool ModuleBase_ModelWidget::processDelete()
   return true;
 }
 
+//**************************************************************
+bool ModuleBase_ModelWidget::processSelection()
+{
+  return false;
+}
+
+//**************************************************************
 bool ModuleBase_ModelWidget::eventFilter(QObject* theObject, QEvent *theEvent)
 {
   QWidget* aWidget = qobject_cast<QWidget*>(theObject);
   if (theEvent->type() == QEvent::FocusIn) {
-    #ifdef _DEBUG
-    // The following two lines are for debugging purpose only
     QFocusEvent* aFocusEvent = dynamic_cast<QFocusEvent*>(theEvent);
-    bool isWinFocus = aFocusEvent->reason() == Qt::ActiveWindowFocusReason;
-    #endif
-    if (getControls().contains(aWidget)) {
-      emit focusInWidget(this);
+    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)) {
+    //if (getControls().contains(aWidget)) {
+      emitFocusInWidget();
     }
   }
   else if (theEvent->type() == QEvent::FocusOut) {
@@ -451,3 +551,27 @@ void ModuleBase_ModelWidget::onWidgetValuesModified()
 {
   setValueState(ModifiedInPP);
 }
+
+//**************************************************************
+QString ModuleBase_ModelWidget::translate(const std::string& theStr) const
+{
+  return ModuleBase_Tools::translate(context(), theStr);
+}
+
+//**************************************************************
+ModuleBase_ModelWidget* ModuleBase_ModelWidget::findModelWidget(ModuleBase_IPropertyPanel* theProp,
+                                                                QWidget* theWidget)
+{
+  ModuleBase_ModelWidget* aModelWidget = 0;
+  if (!theWidget)
+    return aModelWidget;
+
+  QObject* aParent = theWidget->parent();
+  while (aParent) {
+    aModelWidget = qobject_cast<ModuleBase_ModelWidget*>(aParent);
+    if (aModelWidget)
+      break;
+    aParent = aParent->parent();
+  }
+  return aModelWidget;
+}