Salome HOME
refs #30 - Sketch base GUI: create, draw lines
[modules/shaper.git] / src / PartSet / PartSet_OperationEditLine.cpp
index 72020fb9ac11a479596aa97f8a33e97127f988db..f0049b007d46a956a908173655af8fda6266e2f6 100644 (file)
@@ -3,21 +3,25 @@
 // Author:      Natalia ERMOLAEVA
 
 #include <PartSet_OperationEditLine.h>
+#include <PartSet_Tools.h>
+
+#include <XGUI_ViewerPrs.h>
 
 #include <SketchPlugin_Feature.h>
 #include <GeomDataAPI_Point2D.h>
-#include <GeomDataAPI_Point.h>
-#include <GeomDataAPI_Dir.h>
 #include <ModelAPI_Data.h>
 #include <ModelAPI_Document.h>
 
-#include <SketchPlugin_Sketch.h>
 #include <SketchPlugin_Line.h>
 
+#include <V3d_View.hxx>
+
 #ifdef _DEBUG
 #include <QDebug>
 #endif
 
+#include <QMouseEvent>
+
 using namespace std;
 
 PartSet_OperationEditLine::PartSet_OperationEditLine(const QString& theId,
@@ -39,8 +43,8 @@ bool PartSet_OperationEditLine::isGranted() const
 std::list<int> PartSet_OperationEditLine::getSelectionModes(boost::shared_ptr<ModelAPI_Feature> theFeature) const
 {
   std::list<int> aModes;
-  //if (theFeature != feature())
-  //  aModes.push_back(TopAbs_VERTEX);
+  aModes.push_back(TopAbs_VERTEX);
+  aModes.push_back(TopAbs_EDGE);
   return aModes;
 }
 
@@ -49,158 +53,89 @@ void PartSet_OperationEditLine::init(boost::shared_ptr<ModelAPI_Feature> theFeat
   setFeature(theFeature);
 }
 
-void PartSet_OperationEditLine::mouseReleased(const gp_Pnt& thePoint)
+void PartSet_OperationEditLine::mousePressed(QMouseEvent* theEvent, Handle(V3d_View) theView)
 {
-  /*switch (myPointSelectionMode)
-  {
-    case SM_FirstPoint: {
-      setLinePoint(thePoint, LINE_ATTR_START);
-      myPointSelectionMode = SM_SecondPoint;
-    }
-    break;
-    case SM_SecondPoint: {
-      setLinePoint(thePoint, LINE_ATTR_END);
-      myPointSelectionMode = SM_None;
-    }
-    break;
-    case SM_None: {
-
-    }
-    break;
-    default:
-      break;
-  }
-*/
+  if (!(theEvent->buttons() &  Qt::LeftButton))
+    return;
+  gp_Pnt aPoint = PartSet_Tools::ConvertClickToPoint(theEvent->pos(), theView);
+  myCurPoint.setPoint(aPoint);
 }
 
-void PartSet_OperationEditLine::mouseMoved(const gp_Pnt& thePoint)
+void PartSet_OperationEditLine::mouseMoved(QMouseEvent* theEvent, Handle(V3d_View) theView,
+                                           const std::list<XGUI_ViewerPrs>& theSelected)
 {
-/*  switch (myPointSelectionMode)
-  {
-    case SM_SecondPoint:
-      setLinePoint(thePoint, LINE_ATTR_END);
-      break;
-    case SM_None: {
-      boost::shared_ptr<ModelAPI_Feature> aPrevFeature = feature();
-      // stop the last operation
-      commitOperation();
-      document()->finishOperation();
-      //emit changeSelectionMode(aPrevFeature, TopAbs_VERTEX);
-      // start a new operation
-      document()->startOperation();
-      startOperation();
-      // use the last point of the previous feature as the first of the new one
-      setLinePoint(aPrevFeature, LINE_ATTR_END, LINE_ATTR_START);
-      myPointSelectionMode = SM_SecondPoint;
-
-      emit featureConstructed(aPrevFeature, FM_Deactivation);
-    }
-    break;
-    default:
-      break;
-  }
-*/
-}
+  if (!(theEvent->buttons() &  Qt::LeftButton))
+    return;
+  gp_Pnt aPoint = PartSet_Tools::ConvertClickToPoint(theEvent->pos(), theView);
 
-void PartSet_OperationEditLine::keyReleased(const int theKey)
-{
-/*  switch (theKey) {
-    case Qt::Key_Escape: {
-      if (myPointSelectionMode != SM_None)
-        emit featureConstructed(feature(), FM_Abort);
-      abort();
-    }
-    break;
-    case Qt::Key_Return: {
-      if (myPointSelectionMode != SM_None) {
-        emit featureConstructed(feature(), FM_Abort);
-        myPointSelectionMode = SM_FirstPoint;
-        document()->abortOperation();
-      }
-      else
-        myPointSelectionMode = SM_FirstPoint;
-    }
-    break;
-    default:
-    break;
+  if (myCurPoint.myIsInitialized) {
+    double aCurX, aCurY;
+    PartSet_Tools::ConvertTo2D(myCurPoint.myPoint, mySketch, theView, aCurX, aCurY);
+
+    double aX, anY;
+    PartSet_Tools::ConvertTo2D(aPoint, mySketch, theView, aX, anY);
+
+    double aDeltaX = aX - aCurX;
+    double aDeltaY = anY - aCurY;
+
+    moveLinePoint(feature(), aDeltaX, aDeltaY, LINE_ATTR_START);
+    moveLinePoint(feature(), aDeltaX, aDeltaY, LINE_ATTR_END);
+
+    /*std::list<XGUI_ViewerPrs>::const_iterator anIt = theSelected.begin(), aLast = theSelected.end();
+    for (; anIt != aLast; anIt++) {
+      boost::shared_ptr<ModelAPI_Feature> aFeature = (*anIt).feature();
+      if (!aFeature)
+        continue;
+      moveLinePoint(aFeature, aDeltaX, aDeltaY, LINE_ATTR_START);
+      moveLinePoint(aFeature, aDeltaX, aDeltaY, LINE_ATTR_END);
+    }*/
   }
-  */
+  myCurPoint.setPoint(aPoint);
 }
 
-void PartSet_OperationEditLine::setSelected(boost::shared_ptr<ModelAPI_Feature> theFeature,
-                                            const TopoDS_Shape& theShape)
+void PartSet_OperationEditLine::mouseReleased(QMouseEvent* theEvent, Handle(V3d_View) theView,
+                                              const std::list<XGUI_ViewerPrs>& theSelected)
 {
-  if (theFeature == feature())
+  boost::shared_ptr<ModelAPI_Feature> aFeature;
+  if (!theSelected.empty())
+    aFeature = theSelected.front().feature();
+  
+  if (aFeature == feature())
     return;
-
+  
   commit();
-
-  if (theFeature)
-    emit launchOperation("EditLine", theFeature);
+  if (aFeature)
+    emit launchOperation(PartSet_OperationEditLine::Type(), aFeature);
 }
 
 void PartSet_OperationEditLine::startOperation()
 {
-  //PartSet_OperationSketchBase::startOperation();
-  //myPointSelectionMode = SM_FirstPoint;
+  // do nothing in order to do not create a new feature
+  emit selectionEnabled(false);
+  myCurPoint.clear();
 }
 
 void PartSet_OperationEditLine::stopOperation()
 {
-  PartSet_OperationSketchBase::stopOperation();
-  //myPointSelectionMode = SM_None;
+  emit selectionEnabled(true);
 }
 
 boost::shared_ptr<ModelAPI_Feature> PartSet_OperationEditLine::createFeature()
 {
+  // do nothing in order to do not create a new feature
   return boost::shared_ptr<ModelAPI_Feature>();
 }
 
-void PartSet_OperationEditLine::setLinePoint(const gp_Pnt& thePoint,
+void  PartSet_OperationEditLine::moveLinePoint(boost::shared_ptr<ModelAPI_Feature> theFeature,
+                                               double theDeltaX, double theDeltaY,
                                                const std::string& theAttribute)
 {
-  boost::shared_ptr<ModelAPI_Data> aData = feature()->data();
-  boost::shared_ptr<GeomDataAPI_Point2D> aPoint =
-        boost::dynamic_pointer_cast<GeomDataAPI_Point2D>(aData->attribute(theAttribute));
-
-  double aX = 0;
-  double anY = 0;
-  convertTo2D(thePoint, aX, anY);
-  aPoint->setValue(aX, anY);
-}
-
-void PartSet_OperationEditLine::setLinePoint(boost::shared_ptr<ModelAPI_Feature> theSourceFeature,
-                                               const std::string& theSourceAttribute,
-                                               const std::string& theAttribute)
-{
-  boost::shared_ptr<ModelAPI_Data> aData = theSourceFeature->data();
-  boost::shared_ptr<GeomDataAPI_Point2D> aPoint =
-        boost::dynamic_pointer_cast<GeomDataAPI_Point2D>(aData->attribute(theSourceAttribute));
-  double aX = aPoint->x();
-  double anY = aPoint->y();
-
-  aData = feature()->data();
-  aPoint = boost::dynamic_pointer_cast<GeomDataAPI_Point2D>(aData->attribute(theAttribute));
-  aPoint->setValue(aX, anY);
-}
-
-void PartSet_OperationEditLine::convertTo2D(const gp_Pnt& thePoint, double& theX, double& theY)
-{
-  if (!mySketch)
+  if (!theFeature)
     return;
 
-  boost::shared_ptr<ModelAPI_AttributeDouble> anAttr;
-  boost::shared_ptr<ModelAPI_Data> aData = mySketch->data();
-
-  boost::shared_ptr<GeomDataAPI_Point> anOrigin = 
-    boost::dynamic_pointer_cast<GeomDataAPI_Point>(aData->attribute(SKETCH_ATTR_ORIGIN));
-
-  boost::shared_ptr<GeomDataAPI_Dir> aX = 
-    boost::dynamic_pointer_cast<GeomDataAPI_Dir>(aData->attribute(SKETCH_ATTR_DIRX));
-  boost::shared_ptr<GeomDataAPI_Dir> anY = 
-    boost::dynamic_pointer_cast<GeomDataAPI_Dir>(aData->attribute(SKETCH_ATTR_DIRY));
+  boost::shared_ptr<ModelAPI_Data> aData = theFeature->data();
+  boost::shared_ptr<GeomDataAPI_Point2D> aPoint =
+        boost::dynamic_pointer_cast<GeomDataAPI_Point2D>(aData->attribute(theAttribute));
 
-  gp_Pnt aVec(thePoint.X() - anOrigin->x(), thePoint.Y() - anOrigin->y(), thePoint.Z() - anOrigin->z());
-  theX = aVec.X() * aX->x() + aVec.Y() * aX->y() + aVec.Z() * aX->z();
-  theY = aVec.X() * anY->x() + aVec.Y() * anY->y() + aVec.Z() * anY->z();
+  aPoint->setValue(aPoint->x() + theDeltaX, aPoint->y() + theDeltaY);
 }