Salome HOME
92ae140d2e630a8716f7b6f7d9fa1148a7fa5ed9
[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 #include <V3d_View.hxx>
16
17 #ifdef _DEBUG
18 #include <QDebug>
19 #endif
20
21 #include <QMouseEvent>
22
23 using namespace std;
24
25 PartSet_OperationEditLine::PartSet_OperationEditLine(const QString& theId,
26                                                   QObject* theParent,
27                                               boost::shared_ptr<ModelAPI_Feature> theFeature)
28 : PartSet_OperationSketchBase(theId, theParent), mySketch(theFeature)
29 {
30 }
31
32 PartSet_OperationEditLine::~PartSet_OperationEditLine()
33 {
34 }
35
36 bool PartSet_OperationEditLine::isGranted() const
37 {
38   return true;
39 }
40
41 std::list<int> PartSet_OperationEditLine::getSelectionModes(boost::shared_ptr<ModelAPI_Feature> theFeature) const
42 {
43   std::list<int> aModes;
44   aModes.push_back(TopAbs_VERTEX);
45   aModes.push_back(TopAbs_EDGE);
46   return aModes;
47 }
48
49 void PartSet_OperationEditLine::init(boost::shared_ptr<ModelAPI_Feature> theFeature)
50 {
51   setFeature(theFeature);
52 }
53
54 void PartSet_OperationEditLine::mousePressed(QMouseEvent* theEvent, Handle(V3d_View) theView)
55 {
56   if (!(theEvent->buttons() &  Qt::LeftButton))
57     return;
58   gp_Pnt aPoint = PartSet_Tools::ConvertClickToPoint(theEvent->pos(), theView);
59   myCurPressed = aPoint;
60 }
61
62 void PartSet_OperationEditLine::mouseMoved(QMouseEvent* theEvent, Handle(V3d_View) theView)
63 {
64   if (!(theEvent->buttons() &  Qt::LeftButton))
65     return;
66
67   double aCurX, aCurY;
68   PartSet_Tools::ConvertTo2D(myCurPressed, mySketch, theView, aCurX, aCurY);
69
70   double aX, anY;
71   gp_Pnt aPoint = PartSet_Tools::ConvertClickToPoint(theEvent->pos(), theView);
72   PartSet_Tools::ConvertTo2D(aPoint, mySketch, theView, aX, anY);
73
74   double aDeltaX = aX - aCurX;
75   double aDeltaY = anY - aCurY;
76
77   moveLinePoint(aDeltaX, aDeltaY, LINE_ATTR_START);
78   moveLinePoint(aDeltaX, aDeltaY, LINE_ATTR_END);
79   myCurPressed = aPoint;
80 }
81
82 void PartSet_OperationEditLine::setSelected(boost::shared_ptr<ModelAPI_Feature> theFeature,
83                                             const TopoDS_Shape& theShape)
84 {
85   if (theFeature == feature())
86     return;
87
88   commit();
89
90   if (theFeature)
91     emit launchOperation(PartSet_OperationEditLine::Type(), theFeature);
92 }
93
94 void PartSet_OperationEditLine::startOperation()
95 {
96   // do nothing in order to do not create a new feature
97   emit multiSelectionEnabled(false);
98 }
99
100 void PartSet_OperationEditLine::stopOperation()
101 {
102   emit multiSelectionEnabled(true);
103 }
104
105 boost::shared_ptr<ModelAPI_Feature> PartSet_OperationEditLine::createFeature()
106 {
107   // do nothing in order to do not create a new feature
108   return boost::shared_ptr<ModelAPI_Feature>();
109 }
110
111 void  PartSet_OperationEditLine::moveLinePoint(double theDeltaX, double theDeltaY,
112                                                const std::string& theAttribute)
113 {
114   boost::shared_ptr<ModelAPI_Data> aData = feature()->data();
115   boost::shared_ptr<GeomDataAPI_Point2D> aPoint =
116         boost::dynamic_pointer_cast<GeomDataAPI_Point2D>(aData->attribute(theAttribute));
117
118   aPoint->setValue(aPoint->x() + theDeltaX, aPoint->y() + theDeltaY);
119 }