Salome HOME
Merge branch 'master' of newgeom:newgeom
[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(-1);
47   return aModes;
48   //return PartSet_OperationSketchBase::getSelectionModes(theFeature);
49 }
50
51 void PartSet_OperationEditLine::init(boost::shared_ptr<ModelAPI_Feature> theFeature,
52                                      const std::list<XGUI_ViewerPrs>& thePresentations)
53 {
54   setFeature(theFeature);
55   myFeatures = thePresentations;
56 }
57
58 void PartSet_OperationEditLine::mousePressed(QMouseEvent* theEvent, Handle(V3d_View) theView)
59 {
60   if (!(theEvent->buttons() &  Qt::LeftButton))
61     return;
62   gp_Pnt aPoint = PartSet_Tools::ConvertClickToPoint(theEvent->pos(), theView);
63   myCurPoint.setPoint(aPoint);
64 }
65
66 void PartSet_OperationEditLine::mouseMoved(QMouseEvent* theEvent, Handle(V3d_View) theView)
67 {
68   if (!(theEvent->buttons() &  Qt::LeftButton))
69     return;
70
71   gp_Pnt aPoint = PartSet_Tools::ConvertClickToPoint(theEvent->pos(), theView);
72
73   if (myCurPoint.myIsInitialized) {
74     double aCurX, aCurY;
75     PartSet_Tools::ConvertTo2D(myCurPoint.myPoint, mySketch, theView, aCurX, aCurY);
76
77     double aX, anY;
78     PartSet_Tools::ConvertTo2D(aPoint, mySketch, theView, aX, anY);
79
80     double aDeltaX = aX - aCurX;
81     double aDeltaY = anY - aCurY;
82
83     moveLinePoint(feature(), aDeltaX, aDeltaY, LINE_ATTR_START);
84     moveLinePoint(feature(), aDeltaX, aDeltaY, LINE_ATTR_END);
85
86     std::list<XGUI_ViewerPrs>::const_iterator anIt = myFeatures.begin(), aLast = myFeatures.end();
87     for (; anIt != aLast; anIt++) {
88       boost::shared_ptr<ModelAPI_Feature> aFeature = (*anIt).feature();
89       if (!aFeature || aFeature == feature())
90         continue;
91       moveLinePoint(aFeature, aDeltaX, aDeltaY, LINE_ATTR_START);
92       moveLinePoint(aFeature, aDeltaX, aDeltaY, LINE_ATTR_END);
93     }
94   }
95   myCurPoint.setPoint(aPoint);
96 }
97
98 void PartSet_OperationEditLine::mouseReleased(QMouseEvent* theEvent, Handle(V3d_View) theView,
99                                               const std::list<XGUI_ViewerPrs>& theSelected)
100 {
101   std::list<XGUI_ViewerPrs> aFeatures = myFeatures;
102   if (myFeatures.size() == 1) {
103     boost::shared_ptr<ModelAPI_Feature> aFeature;
104     if (!theSelected.empty())
105       aFeature = theSelected.front().feature();
106
107     if (aFeature == feature())
108       return;
109   
110    commit();
111    if (aFeature)
112      emit launchOperation(PartSet_OperationEditLine::Type(), aFeature);
113   }
114   else {
115     commit();
116     std::list<XGUI_ViewerPrs>::const_iterator anIt = aFeatures.begin(), aLast = aFeatures.end();
117     for (; anIt != aLast; anIt++) {
118       boost::shared_ptr<ModelAPI_Feature> aFeature = (*anIt).feature();
119       if (aFeature)
120         emit featureConstructed(aFeature, FM_Deactivation);
121     }
122   }
123 }
124
125 void PartSet_OperationEditLine::startOperation()
126 {
127   // do nothing in order to do not create a new feature
128   emit multiSelectionEnabled(false);
129   emit stopSelection(myFeatures, true);
130   myCurPoint.clear();
131 }
132
133 void PartSet_OperationEditLine::stopOperation()
134 {
135   emit multiSelectionEnabled(true);
136   emit stopSelection(myFeatures, false);
137   myFeatures.clear();
138 }
139
140 boost::shared_ptr<ModelAPI_Feature> PartSet_OperationEditLine::createFeature()
141 {
142   // do nothing in order to do not create a new feature
143   return boost::shared_ptr<ModelAPI_Feature>();
144 }
145
146 void  PartSet_OperationEditLine::moveLinePoint(boost::shared_ptr<ModelAPI_Feature> theFeature,
147                                                double theDeltaX, double theDeltaY,
148                                                const std::string& theAttribute)
149 {
150   if (!theFeature || theFeature->getKind() != "SketchLine")
151     return;
152
153   boost::shared_ptr<ModelAPI_Data> aData = theFeature->data();
154   boost::shared_ptr<GeomDataAPI_Point2D> aPoint =
155         boost::dynamic_pointer_cast<GeomDataAPI_Point2D>(aData->attribute(theAttribute));
156
157   aPoint->setValue(aPoint->x() + theDeltaX, aPoint->y() + theDeltaY);
158 }