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 #include <PartSet_OperationSketch.h>
8
9 #include <ModuleBase_OperationDescription.h>
10
11 #include <XGUI_ViewerPrs.h>
12
13 #include <SketchPlugin_Feature.h>
14 #include <GeomDataAPI_Point2D.h>
15 #include <ModelAPI_Data.h>
16 #include <ModelAPI_Document.h>
17
18 #include <SketchPlugin_Line.h>
19
20 #include <V3d_View.hxx>
21
22 #ifdef _DEBUG
23 #include <QDebug>
24 #endif
25
26 #include <QMouseEvent>
27
28 using namespace std;
29
30 PartSet_OperationEditLine::PartSet_OperationEditLine(const QString& theId,
31                                                   QObject* theParent,
32                                               boost::shared_ptr<ModelAPI_Feature> theFeature)
33 : PartSet_OperationSketchBase(theId, theParent), mySketch(theFeature)
34 {
35 }
36
37 PartSet_OperationEditLine::~PartSet_OperationEditLine()
38 {
39 }
40
41 bool PartSet_OperationEditLine::isGranted(ModuleBase_IOperation* theOperation) const
42 {
43   return theOperation->getDescription()->operationId().toStdString() == PartSet_OperationSketch::Type();
44 }
45
46 std::list<int> PartSet_OperationEditLine::getSelectionModes(boost::shared_ptr<ModelAPI_Feature> theFeature) const
47 {
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 boost::shared_ptr<ModelAPI_Feature> PartSet_OperationEditLine::sketch() const
59 {
60   return mySketch;
61 }
62
63 void PartSet_OperationEditLine::mousePressed(QMouseEvent* theEvent, Handle(V3d_View) theView)
64 {
65   if (!(theEvent->buttons() &  Qt::LeftButton))
66     return;
67   gp_Pnt aPoint = PartSet_Tools::ConvertClickToPoint(theEvent->pos(), theView);
68   myCurPoint.setPoint(aPoint);
69 }
70
71 void PartSet_OperationEditLine::mouseMoved(QMouseEvent* theEvent, Handle(V3d_View) theView)
72 {
73   if (!(theEvent->buttons() &  Qt::LeftButton))
74     return;
75
76   gp_Pnt aPoint = PartSet_Tools::ConvertClickToPoint(theEvent->pos(), theView);
77
78   if (myCurPoint.myIsInitialized) {
79     double aCurX, aCurY;
80     PartSet_Tools::ConvertTo2D(myCurPoint.myPoint, sketch(), theView, aCurX, aCurY);
81
82     double aX, anY;
83     PartSet_Tools::ConvertTo2D(aPoint, sketch(), theView, aX, anY);
84
85     double aDeltaX = aX - aCurX;
86     double aDeltaY = anY - aCurY;
87
88     moveLinePoint(feature(), aDeltaX, aDeltaY, LINE_ATTR_START);
89     moveLinePoint(feature(), aDeltaX, aDeltaY, LINE_ATTR_END);
90
91     std::list<XGUI_ViewerPrs>::const_iterator anIt = myFeatures.begin(), aLast = myFeatures.end();
92     for (; anIt != aLast; anIt++) {
93       boost::shared_ptr<ModelAPI_Feature> aFeature = (*anIt).feature();
94       if (!aFeature || aFeature == feature())
95         continue;
96       moveLinePoint(aFeature, aDeltaX, aDeltaY, LINE_ATTR_START);
97       moveLinePoint(aFeature, aDeltaX, aDeltaY, LINE_ATTR_END);
98     }
99   }
100   myCurPoint.setPoint(aPoint);
101 }
102
103 void PartSet_OperationEditLine::mouseReleased(QMouseEvent* theEvent, Handle(V3d_View) theView,
104                                               const std::list<XGUI_ViewerPrs>& theSelected)
105 {
106   std::list<XGUI_ViewerPrs> aFeatures = myFeatures;
107   if (myFeatures.size() == 1) {
108     if (theSelected.empty())
109       return;
110
111     boost::shared_ptr<ModelAPI_Feature> aFeature = theSelected.front().feature();
112     commit();
113     emit launchOperation(PartSet_OperationEditLine::Type(), aFeature);
114   }
115   else {
116     commit();
117     std::list<XGUI_ViewerPrs>::const_iterator anIt = aFeatures.begin(), aLast = aFeatures.end();
118     for (; anIt != aLast; anIt++) {
119       boost::shared_ptr<ModelAPI_Feature> aFeature = (*anIt).feature();
120       if (aFeature)
121         emit featureConstructed(aFeature, FM_Deactivation);
122     }
123   }
124 }
125
126 void PartSet_OperationEditLine::startOperation()
127 {
128   // do nothing in order to do not create a new feature
129   emit multiSelectionEnabled(false);
130   emit setSelection(std::list<XGUI_ViewerPrs>());
131   emit stopSelection(myFeatures, true);
132   myCurPoint.clear();
133 }
134
135 void PartSet_OperationEditLine::stopOperation()
136 {
137   emit multiSelectionEnabled(true);
138   bool isSelectFeatures = myFeatures.size() > 1;
139   emit stopSelection(myFeatures, false);
140   if (isSelectFeatures)
141     emit setSelection(myFeatures);
142
143   myFeatures.clear();
144 }
145
146 boost::shared_ptr<ModelAPI_Feature> PartSet_OperationEditLine::createFeature()
147 {
148   // do nothing in order to do not create a new feature
149   return boost::shared_ptr<ModelAPI_Feature>();
150 }
151
152 void  PartSet_OperationEditLine::moveLinePoint(boost::shared_ptr<ModelAPI_Feature> theFeature,
153                                                double theDeltaX, double theDeltaY,
154                                                const std::string& theAttribute)
155 {
156   if (!theFeature || theFeature->getKind() != "SketchLine")
157     return;
158
159   boost::shared_ptr<ModelAPI_Data> aData = theFeature->data();
160   boost::shared_ptr<GeomDataAPI_Point2D> aPoint =
161         boost::dynamic_pointer_cast<GeomDataAPI_Point2D>(aData->attribute(theAttribute));
162
163   aPoint->setValue(aPoint->x() + theDeltaX, aPoint->y() + theDeltaY);
164 }