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