Salome HOME
Added history branch into data tree (issue #22)
[modules/shaper.git] / src / PartSet / PartSet_OperationSketchLine.cpp
1 // File:        PartSet_OperationSketchLine.h
2 // Created:     20 Apr 2014
3 // Author:      Natalia ERMOLAEVA
4
5 #include <PartSet_OperationSketchLine.h>
6
7 #include <PartSet_Tools.h>
8
9 #include <SketchPlugin_Feature.h>
10 #include <GeomDataAPI_Point2D.h>
11 #include <ModelAPI_Data.h>
12 #include <ModelAPI_Document.h>
13
14 #include <SketchPlugin_Line.h>
15
16 #ifdef _DEBUG
17 #include <QDebug>
18 #endif
19
20 using namespace std;
21
22 PartSet_OperationSketchLine::PartSet_OperationSketchLine(const QString& theId,
23                                                   QObject* theParent,
24                                               boost::shared_ptr<ModelAPI_Feature> theFeature)
25 : PartSet_OperationSketchBase(theId, theParent), mySketch(theFeature),
26   myPointSelectionMode(SM_FirstPoint)
27 {
28 }
29
30 PartSet_OperationSketchLine::~PartSet_OperationSketchLine()
31 {
32 }
33
34 bool PartSet_OperationSketchLine::isGranted() const
35 {
36   return true;
37 }
38
39 std::list<int> PartSet_OperationSketchLine::getSelectionModes(boost::shared_ptr<ModelAPI_Feature> theFeature) const
40 {
41   return std::list<int>();
42 }
43
44 void PartSet_OperationSketchLine::init(boost::shared_ptr<ModelAPI_Feature> theFeature)
45 {
46   if (!theFeature)
47     return;
48   // use the last point of the previous feature as the first of the new one
49   boost::shared_ptr<ModelAPI_Data> aData = theFeature->data();
50   myInitPoint = boost::dynamic_pointer_cast<GeomDataAPI_Point2D>(aData->attribute(LINE_ATTR_END));
51 }
52
53 void PartSet_OperationSketchLine::mouseReleased(const gp_Pnt& thePoint, QMouseEvent* /*theEvent*/)
54 {
55   switch (myPointSelectionMode)
56   {
57     case SM_FirstPoint: {
58       setLinePoint(thePoint, LINE_ATTR_START);
59       myPointSelectionMode = SM_SecondPoint;
60     }
61     break;
62     case SM_SecondPoint: {
63       setLinePoint(thePoint, LINE_ATTR_END);
64       commit();
65       emit featureConstructed(feature(), FM_Deactivation);
66       emit launchOperation(PartSet_OperationSketchLine::Type(), feature());
67     }
68     break;
69     default:
70       break;
71   }
72 }
73
74 void PartSet_OperationSketchLine::mouseMoved(const gp_Pnt& thePoint, QMouseEvent* /*theEvent*/)
75 {
76   switch (myPointSelectionMode)
77   {
78     case SM_SecondPoint:
79       setLinePoint(thePoint, LINE_ATTR_END);
80       break;
81     default:
82       break;
83   }
84 }
85
86 void PartSet_OperationSketchLine::keyReleased(const int theKey)
87 {
88   switch (theKey) {
89     case Qt::Key_Escape: {
90       abort();
91     }
92     break;
93     case Qt::Key_Return: {
94       abort();
95       emit launchOperation(PartSet_OperationSketchLine::Type(), boost::shared_ptr<ModelAPI_Feature>());
96     }
97     break;
98     default:
99     break;
100   }
101 }
102
103 void PartSet_OperationSketchLine::startOperation()
104 {
105   PartSet_OperationSketchBase::startOperation();
106   myPointSelectionMode = !myInitPoint ? SM_FirstPoint : SM_SecondPoint;
107 }
108
109 void PartSet_OperationSketchLine::abortOperation()
110 {
111   emit featureConstructed(feature(), FM_Abort);
112   PartSet_OperationSketchBase::abortOperation();
113 }
114
115 boost::shared_ptr<ModelAPI_Feature> PartSet_OperationSketchLine::createFeature()
116 {
117   boost::shared_ptr<ModelAPI_Feature> aNewFeature = ModuleBase_Operation::createFeature();
118   if (mySketch) {
119     boost::shared_ptr<SketchPlugin_Feature> aFeature = 
120                            boost::dynamic_pointer_cast<SketchPlugin_Feature>(mySketch);
121
122     aFeature->addSub(aNewFeature);
123   }
124   if (myInitPoint) {
125     boost::shared_ptr<ModelAPI_Data> aData = aNewFeature->data();
126     boost::shared_ptr<GeomDataAPI_Point2D> aPoint = boost::dynamic_pointer_cast<GeomDataAPI_Point2D>
127                                                                 (aData->attribute(LINE_ATTR_START));
128     aPoint->setValue(myInitPoint->x(), myInitPoint->y());
129   }
130
131   emit featureConstructed(aNewFeature, FM_Activation);
132   return aNewFeature;
133 }
134
135 void PartSet_OperationSketchLine::setLinePoint(const gp_Pnt& thePoint,
136                                                const std::string& theAttribute)
137 {
138   boost::shared_ptr<ModelAPI_Data> aData = feature()->data();
139   boost::shared_ptr<GeomDataAPI_Point2D> aPoint =
140         boost::dynamic_pointer_cast<GeomDataAPI_Point2D>(aData->attribute(theAttribute));
141
142   double aX, anY;
143   PartSet_Tools::ConvertTo2D(thePoint, mySketch, aX, anY);
144   aPoint->setValue(aX, anY);
145 }