]> SALOME platform Git repositories - modules/shaper.git/blob - src/PartSet/PartSet_OperationCreateFeature.cpp
Salome HOME
2bfa875d6ad59adb8582bdacc3a814536253e9ab
[modules/shaper.git] / src / PartSet / PartSet_OperationCreateFeature.cpp
1 // File:        PartSet_OperationCreateFeature.h
2 // Created:     20 Apr 2014
3 // Author:      Natalia ERMOLAEVA
4
5 #include <PartSet_OperationCreateFeature.h>
6
7 #include <PartSet_Tools.h>
8 #include <PartSet_OperationSketch.h>
9 #include <PartSet_FeaturePointPrs.h>
10 #include <PartSet_FeatureLinePrs.h>
11 #include <PartSet_FeatureCirclePrs.h>
12 #include <PartSet_FeatureArcPrs.h>
13
14 #include <SketchPlugin_Feature.h>
15 #include <SketchPlugin_Point.h>
16 #include <SketchPlugin_Line.h>
17 #include <SketchPlugin_Circle.h>
18 #include <SketchPlugin_Arc.h>
19
20 #include <ModuleBase_OperationDescription.h>
21
22 #include <XGUI_ViewerPrs.h>
23 #include <XGUI_Constants.h>
24
25 #include <V3d_View.hxx>
26 #include <TopoDS_Vertex.hxx>
27 #include <TopoDS.hxx>
28 #include <BRep_Tool.hxx>
29
30 #ifdef _DEBUG
31 #include <QDebug>
32 #endif
33
34 #include <QMouseEvent>
35
36 using namespace std;
37
38 PartSet_OperationCreateFeature::PartSet_OperationCreateFeature(const QString& theId,
39                                                                QObject* theParent,
40                                                                FeaturePtr theFeature)
41 : PartSet_OperationSketchBase(theId, theParent),
42   myPointSelectionMode(SM_FirstPoint)
43 {
44   std::string aKind = theId.toStdString();
45
46   if (aKind == SKETCH_POINT_KIND) {
47     myFeaturePrs = new PartSet_FeaturePointPrs(theFeature);
48   }
49   if (aKind == SKETCH_LINE_KIND) {
50     myFeaturePrs = new PartSet_FeatureLinePrs(theFeature);
51   }
52   else if (aKind == SKETCH_CIRCLE_KIND) {
53     myFeaturePrs = new PartSet_FeatureCirclePrs(theFeature);
54   }
55   else if (aKind == SKETCH_ARC_KIND) {
56     myFeaturePrs = new PartSet_FeatureArcPrs(theFeature);
57   }
58 }
59
60 PartSet_OperationCreateFeature::~PartSet_OperationCreateFeature()
61 {
62   delete myFeaturePrs;
63 }
64
65 bool PartSet_OperationCreateFeature::canProcessKind(const std::string& theId)
66 {
67   return theId == SKETCH_LINE_KIND || theId == SKETCH_POINT_KIND || theId == SKETCH_CIRCLE_KIND ||
68          theId == SKETCH_ARC_KIND;
69 }
70
71 bool PartSet_OperationCreateFeature::canBeCommitted() const
72 {
73   return myPointSelectionMode == SM_DonePoint;
74 }
75
76 bool PartSet_OperationCreateFeature::isGranted(ModuleBase_IOperation* theOperation) const
77 {
78   return theOperation->getDescription()->operationId().toStdString() == PartSet_OperationSketch::Type();
79 }
80
81 std::list<int> PartSet_OperationCreateFeature::getSelectionModes(FeaturePtr theFeature) const
82 {
83   std::list<int> aModes;
84   if (theFeature != feature())
85     aModes = PartSet_OperationSketchBase::getSelectionModes(theFeature);
86   return aModes;
87 }
88
89 void PartSet_OperationCreateFeature::init(FeaturePtr theFeature,
90                                        const std::list<XGUI_ViewerPrs>& /*theSelected*/,
91                                        const std::list<XGUI_ViewerPrs>& /*theHighlighted*/)
92 {
93   if (!theFeature || theFeature->getKind() != SKETCH_LINE_KIND)
94     return;
95   myInitFeature = theFeature;
96 }
97
98 FeaturePtr PartSet_OperationCreateFeature::sketch() const
99 {
100   return myFeaturePrs->sketch();
101 }
102
103 void PartSet_OperationCreateFeature::mouseReleased(QMouseEvent* theEvent, Handle(V3d_View) theView,
104                                                 const std::list<XGUI_ViewerPrs>& theSelected,
105                                                 const std::list<XGUI_ViewerPrs>& /*theHighlighted*/)
106 {
107   if (myPointSelectionMode == SM_DonePoint)
108   {
109     // if the point creation is finished, the next mouse release should commit the modification
110     // the next release can happens by double click in the viewer
111     commit();
112     restartOperation(feature()->getKind(), feature());
113     return;
114   }
115
116   double aX, anY;
117
118   bool isFoundPoint = false;
119   gp_Pnt aPoint = PartSet_Tools::convertClickToPoint(theEvent->pos(), theView);
120   if (theSelected.empty()) {
121     PartSet_Tools::convertTo2D(aPoint, sketch(), theView, aX, anY);
122     isFoundPoint = true;
123   }
124   else {
125     XGUI_ViewerPrs aPrs = theSelected.front();
126     const TopoDS_Shape& aShape = aPrs.shape();
127     if (!aShape.IsNull()) // the point is selected
128     {
129       if (aShape.ShapeType() == TopAbs_VERTEX) {
130         const TopoDS_Vertex& aVertex = TopoDS::Vertex(aShape);
131         if (!aVertex.IsNull()) {
132           aPoint = BRep_Tool::Pnt(aVertex);
133           PartSet_Tools::convertTo2D(aPoint, sketch(), theView, aX, anY);
134           isFoundPoint = true;
135
136           myFeaturePrs->setConstraints(aX, anY, myPointSelectionMode);
137         }
138       }
139       else if (aShape.ShapeType() == TopAbs_EDGE) // the line is selected
140       {
141         PartSet_Tools::convertTo2D(aPoint, sketch(), theView, aX, anY);
142         isFoundPoint = true;
143         /*
144         FeaturePtr aFeature = aPrs.feature();
145         if (aFeature) {
146           double X0, X1, X2, X3;
147           double Y0, Y1, Y2, Y3;
148           PartSet_Tools::getLinePoint(aFeature, LINE_ATTR_START, X2, Y2);
149           PartSet_Tools::getLinePoint(aFeature, LINE_ATTR_END, X3, Y3);
150           PartSet_Tools::convertTo2D(aPoint, sketch(), theView, X1, Y1);
151
152           switch (myPointSelectionMode) {
153             case SM_FirstPoint:
154               PartSet_Tools::projectPointOnLine(X2, Y2, X3, Y3, X1, Y1, aX, anY);
155             break;
156             case SM_SecondPoint: {
157               PartSet_Tools::getLinePoint(feature(), LINE_ATTR_START, X0, Y0);
158               PartSet_Tools::intersectLines(X0, Y0, X1, Y1, X2, Y2, X3, Y3, aX, anY);
159             }
160             break;
161             default:
162             break;
163           }
164           isFoundPoint = true;
165         }
166         */
167       }
168     }
169   }
170
171   switch (myPointSelectionMode)
172   {
173     case SM_FirstPoint:
174     case SM_SecondPoint: {
175       PartSet_SelectionMode aMode = myFeaturePrs->setPoint(aX, anY, myPointSelectionMode);
176       flushUpdated();
177       setPointSelectionMode(aMode);
178     }
179     break;
180     default:
181       break;
182   }
183 }
184
185 void PartSet_OperationCreateFeature::mouseMoved(QMouseEvent* theEvent, Handle(V3d_View) theView)
186 {
187   switch (myPointSelectionMode)
188   {
189     case SM_FirstPoint:
190     case SM_SecondPoint:
191     {
192       double aX, anY;
193       gp_Pnt aPoint = PartSet_Tools::convertClickToPoint(theEvent->pos(), theView);
194       PartSet_Tools::convertTo2D(aPoint, sketch(), theView, aX, anY);
195       myFeaturePrs->setPoint(aX, anY, myPointSelectionMode);
196
197       flushUpdated();
198       emit focusActivated(myFeaturePrs->getAttribute(myPointSelectionMode));
199     }
200     break;
201     case SM_DonePoint:
202     {
203       commit();
204       restartOperation(feature()->getKind(), feature());
205     }
206     default:
207       break;
208   }
209 }
210
211 void PartSet_OperationCreateFeature::keyReleased(std::string theName, QKeyEvent* theEvent)
212 {
213   int aKeyType = theEvent->key();
214   // the second point should be activated by any modification in the property panel
215   if (!theName.empty() /*&& aKeyType == Qt::Key_Return*/)
216   {
217     setPointSelectionMode(myFeaturePrs->getNextMode(theName), false);
218   }
219   keyReleased(theEvent->key());
220 }
221
222 void PartSet_OperationCreateFeature::keyReleased(const int theKey)
223 {
224   switch (theKey) {
225     case Qt::Key_Return: {
226       if (myPointSelectionMode == SM_DonePoint)
227       {
228         commit();
229         // it start a new line creation at a free point
230         restartOperation(feature()->getKind(), FeaturePtr()/*feature()*/);
231       }
232       //else
233       //  abort();
234       //restartOperation(feature()->getKind(), FeaturePtr());
235     }
236     break;
237     case Qt::Key_Escape: {
238       if (myPointSelectionMode == SM_DonePoint)
239       {
240         commit();
241       }
242       else
243       {
244         abort();
245       }
246     }
247     default:
248     break;
249   }
250 }
251
252 void PartSet_OperationCreateFeature::startOperation()
253 {
254   PartSet_OperationSketchBase::startOperation();
255   setPointSelectionMode(!myInitFeature ? SM_FirstPoint : SM_SecondPoint);
256
257   emit multiSelectionEnabled(false);
258 }
259
260 void PartSet_OperationCreateFeature::abortOperation()
261 {
262   emit featureConstructed(feature(), FM_Hide);
263   PartSet_OperationSketchBase::abortOperation();
264 }
265
266 void PartSet_OperationCreateFeature::stopOperation()
267 {
268   PartSet_OperationSketchBase::stopOperation();
269   emit multiSelectionEnabled(true);
270 }
271
272 void PartSet_OperationCreateFeature::afterCommitOperation()
273 {
274   PartSet_OperationSketchBase::afterCommitOperation();  
275   emit featureConstructed(feature(), FM_Deactivation);
276 }
277
278 FeaturePtr PartSet_OperationCreateFeature::createFeature(const bool theFlushMessage)
279 {
280   FeaturePtr aNewFeature = ModuleBase_Operation::createFeature(false);
281   if (sketch()) {
282     boost::shared_ptr<SketchPlugin_Feature> aFeature = 
283                            boost::dynamic_pointer_cast<SketchPlugin_Feature>(sketch());
284
285     aFeature->addSub(aNewFeature);
286   }
287   myFeaturePrs->init(aNewFeature, myInitFeature);
288
289   emit featureConstructed(aNewFeature, FM_Activation);
290   if (theFlushMessage)
291     flushCreated();
292   return aNewFeature;
293 }
294
295 void PartSet_OperationCreateFeature::setPointSelectionMode(const PartSet_SelectionMode& theMode,
296                                                            const bool isToEmitSignal)
297 {
298   myPointSelectionMode = theMode;
299   if (isToEmitSignal) {
300     std::string aName = myFeaturePrs->getAttribute(theMode);
301     if (aName.empty() && theMode == SM_DonePoint) {
302       aName = XGUI::PROP_PANEL_OK;
303     }
304     emit focusActivated(aName);
305   }
306 }