Salome HOME
7841945757e335b9f7c0955075537b1bc4ec9925
[modules/shaper.git] / src / PartSet / PartSet_OperationEditLine.cpp
1 // File:        PartSet_OperationEditLine.h
2 // Created:     05 May 2014
3 // Author:      Natalia ERMOLAEVA
4
5 #include <PartSet_OperationEditLine.h>
6 #include <PartSet_Tools.h>
7
8 #include <SketchPlugin_Feature.h>
9 #include <GeomDataAPI_Point2D.h>
10 #include <ModelAPI_Data.h>
11 #include <ModelAPI_Document.h>
12
13 #include <SketchPlugin_Line.h>
14
15 #ifdef _DEBUG
16 #include <QDebug>
17 #endif
18
19 #include <QMouseEvent>
20
21 using namespace std;
22
23 PartSet_OperationEditLine::PartSet_OperationEditLine(const QString& theId,
24                                                   QObject* theParent,
25                                               boost::shared_ptr<ModelAPI_Feature> theFeature)
26 : PartSet_OperationSketchBase(theId, theParent), mySketch(theFeature)
27 {
28 }
29
30 PartSet_OperationEditLine::~PartSet_OperationEditLine()
31 {
32 }
33
34 bool PartSet_OperationEditLine::isGranted() const
35 {
36   return true;
37 }
38
39 std::list<int> PartSet_OperationEditLine::getSelectionModes(boost::shared_ptr<ModelAPI_Feature> theFeature) const
40 {
41   std::list<int> aModes;
42   aModes.push_back(TopAbs_VERTEX);
43   aModes.push_back(TopAbs_EDGE);
44   return aModes;
45 }
46
47 void PartSet_OperationEditLine::init(boost::shared_ptr<ModelAPI_Feature> theFeature)
48 {
49   setFeature(theFeature);
50 }
51
52 void PartSet_OperationEditLine::mousePressed(const gp_Pnt& thePoint, QMouseEvent* theEvent)
53 {
54   if (!(theEvent->buttons() &  Qt::LeftButton))
55     return;
56   myCurPressed = thePoint;
57 }
58
59 void PartSet_OperationEditLine::mouseMoved(const gp_Pnt& thePoint, QMouseEvent* theEvent)
60 {
61   if (!(theEvent->buttons() &  Qt::LeftButton))
62     return;
63
64   double aCurX, aCurY;
65   PartSet_Tools::ConvertTo2D(myCurPressed, mySketch, aCurX, aCurY);
66
67   double aX, anY;
68   PartSet_Tools::ConvertTo2D(thePoint, mySketch, aX, anY);
69
70   double aDeltaX = aX - aCurX;
71   double aDeltaY = anY - aCurY;
72
73   moveLinePoint(aDeltaX, aDeltaY, LINE_ATTR_START);
74   moveLinePoint(aDeltaX, aDeltaY, LINE_ATTR_END);
75   myCurPressed = thePoint;
76 }
77
78 void PartSet_OperationEditLine::setSelected(boost::shared_ptr<ModelAPI_Feature> theFeature,
79                                             const TopoDS_Shape& theShape)
80 {
81   if (theFeature == feature())
82     return;
83
84   commit();
85
86   if (theFeature)
87     emit launchOperation(PartSet_OperationEditLine::Type(), theFeature);
88 }
89
90 void PartSet_OperationEditLine::startOperation()
91 {
92   // do nothing in order to do not create a new feature
93 }
94
95 boost::shared_ptr<ModelAPI_Feature> PartSet_OperationEditLine::createFeature()
96 {
97   // do nothing in order to do not create a new feature
98   return boost::shared_ptr<ModelAPI_Feature>();
99 }
100
101 void  PartSet_OperationEditLine::moveLinePoint(double theDeltaX, double theDeltaY,
102                                                const std::string& theAttribute)
103 {
104   boost::shared_ptr<ModelAPI_Data> aData = feature()->data();
105   boost::shared_ptr<GeomDataAPI_Point2D> aPoint =
106         boost::dynamic_pointer_cast<GeomDataAPI_Point2D>(aData->attribute(theAttribute));
107
108   aPoint->setValue(aPoint->x() + theDeltaX, aPoint->y() + theDeltaY);
109 }