Salome HOME
Working with pre-selection in sketch operations
[modules/shaper.git] / src / ModuleBase / ModuleBase_WidgetPoint2D.cpp
index 2d8c814798d423fb16cbbe8a1f1dee2bc1865ae9..baa7285b054873ef2bf7f8c59defbac16ebd0f05 100644 (file)
@@ -3,8 +3,10 @@
 // Author:      Natalia ERMOLAEVA
 
 #include <ModuleBase_WidgetPoint2D.h>
+#include <ModuleBase_WidgetValueFeature.h>
 
 #include <Config_Keywords.h>
+#include <Config_WidgetAPI.h>
 
 #include <Events_Loop.h>
 #include <Model_Events.h>
@@ -13,6 +15,7 @@
 #include <ModelAPI_Data.h>
 #include <ModelAPI_Object.h>
 #include <GeomDataAPI_Point2D.h>
+#include <GeomAPI_Pnt2d.h>
 
 #include <QGroupBox>
 #include <QGridLayout>
 #include <cfloat>
 #include <climits>
 
-ModuleBase_WidgetPoint2D::ModuleBase_WidgetPoint2D(QWidget* theParent, QString theTitle,
-                                                   const std::string& theFeatureAttributeID)
-: ModuleBase_ModelWidget(theParent), myFeatureAttributeID(theFeatureAttributeID)
+ModuleBase_WidgetPoint2D::ModuleBase_WidgetPoint2D(QWidget* theParent,
+                                                   const Config_WidgetAPI* theData)
+: ModuleBase_ModelWidget(theParent, theData)
 {
-  myGroupBox = new QGroupBox(theTitle, theParent);
+  myOptionParam = theData->getProperty(PREVIOUS_FEATURE_PARAM);
+  myGroupBox = new QGroupBox(QString::fromStdString(theData->getProperty(CONTAINER_PAGE_NAME)),
+                             theParent);
   QGridLayout* aGroupLay = new QGridLayout(myGroupBox);
   aGroupLay->setContentsMargins(0, 0, 0, 0);
   aGroupLay->setColumnStretch(1, 1);
@@ -68,11 +73,39 @@ ModuleBase_WidgetPoint2D::~ModuleBase_WidgetPoint2D()
 {
 }
 
+bool ModuleBase_WidgetPoint2D::setValue(ModuleBase_WidgetValue* theValue)
+{
+  bool isDone = false;
+  if (theValue) {
+    ModuleBase_WidgetValueFeature* aFeatureValue = 
+                         dynamic_cast<ModuleBase_WidgetValueFeature*>(theValue);
+    if (aFeatureValue) {
+      boost::shared_ptr<GeomAPI_Pnt2d> aPoint = aFeatureValue->point();
+      if (aPoint) {
+        setPoint(aPoint);
+        isDone = true;
+      }
+    }
+  }
+  return isDone;
+}
+
+void ModuleBase_WidgetPoint2D::setPoint(const boost::shared_ptr<GeomAPI_Pnt2d>& thePoint)
+{
+
+  bool isBlocked = this->blockSignals(true);
+  myXSpin->setValue(thePoint->x());
+  myYSpin->setValue(thePoint->y());
+  this->blockSignals(isBlocked);
+
+  emit valuesChanged();
+}
+
 bool ModuleBase_WidgetPoint2D::storeValue(FeaturePtr theFeature) const
 {
   boost::shared_ptr<ModelAPI_Data> aData = theFeature->data();
   boost::shared_ptr<GeomDataAPI_Point2D> aPoint =
-    boost::dynamic_pointer_cast<GeomDataAPI_Point2D>(aData->attribute(myFeatureAttributeID));
+    boost::dynamic_pointer_cast<GeomDataAPI_Point2D>(aData->attribute(attributeID()));
 
   ModuleBase_WidgetPoint2D* that = (ModuleBase_WidgetPoint2D*) this;
   bool isBlocked = that->blockSignals(true);
@@ -87,7 +120,7 @@ bool ModuleBase_WidgetPoint2D::restoreValue(FeaturePtr theFeature)
 {
   boost::shared_ptr<ModelAPI_Data> aData = theFeature->data();
   boost::shared_ptr<GeomDataAPI_Point2D> aPoint =
-    boost::dynamic_pointer_cast<GeomDataAPI_Point2D>(aData->attribute(myFeatureAttributeID));
+    boost::dynamic_pointer_cast<GeomDataAPI_Point2D>(aData->attribute(attributeID()));
 
   bool isBlocked = this->blockSignals(true);
   myXSpin->setValue(aPoint->x());
@@ -96,17 +129,6 @@ bool ModuleBase_WidgetPoint2D::restoreValue(FeaturePtr theFeature)
   return true;
 }
 
-bool ModuleBase_WidgetPoint2D::canFocusTo(const std::string& theAttributeName)
-{
-  return theAttributeName == myFeatureAttributeID;
-}
-
-void ModuleBase_WidgetPoint2D::focusTo()
-{
-  if (!myXSpin->hasFocus() && !myYSpin->hasFocus())
-    myXSpin->setFocus();
-}
-
 QWidget* ModuleBase_WidgetPoint2D::getControl() const
 {
   return myGroupBox;
@@ -125,9 +147,33 @@ bool ModuleBase_WidgetPoint2D::eventFilter(QObject *theObject, QEvent *theEvent)
 {
   if (theObject == myXSpin || theObject == myYSpin) {
     if (theEvent->type() == QEvent::KeyRelease) {
-      emit keyReleased(myFeatureAttributeID, (QKeyEvent*) theEvent);
+      QKeyEvent* aKeyEvent = (QKeyEvent*)theEvent;
+      if (aKeyEvent && aKeyEvent->key() == Qt::Key_Return) {
+        emit focusOutWidget(this);
+      }
+      emit keyReleased(attributeID(), (QKeyEvent*) theEvent);
       return true;
     }
   }
   return ModuleBase_ModelWidget::eventFilter(theObject, theEvent);
 }
+
+bool ModuleBase_WidgetPoint2D::initFromPrevious(FeaturePtr theFeature)
+{
+  if (myOptionParam.length() == 0)
+    return false;
+  boost::shared_ptr<ModelAPI_Data> aData = theFeature->data();
+  boost::shared_ptr<GeomDataAPI_Point2D> aPoint =
+    boost::dynamic_pointer_cast<GeomDataAPI_Point2D>(aData->attribute(myOptionParam));
+  if (aPoint) {
+    bool isBlocked = this->blockSignals(true);
+    myXSpin->setValue(aPoint->x());
+    myYSpin->setValue(aPoint->y());
+    this->blockSignals(isBlocked);
+
+    emit valuesChanged();
+    emit storedPoint2D(theFeature, myOptionParam);
+    return true;
+  }
+  return false;
+}