]> SALOME platform Git repositories - modules/shaper.git/blob - src/PartSet/PartSet_OperationCreateFeature.cpp
Salome HOME
refs #80 - Sketch base GUI: create/draw point, circle and arc
[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   myFeaturePrs = PartSet_Tools::createFeaturePrs(aKind, theFeature);
46 }
47
48 PartSet_OperationCreateFeature::~PartSet_OperationCreateFeature()
49 {
50 }
51
52 bool PartSet_OperationCreateFeature::canProcessKind(const std::string& theId)
53 {
54   return theId == SKETCH_LINE_KIND || theId == SKETCH_POINT_KIND || theId == SKETCH_CIRCLE_KIND ||
55          theId == SKETCH_ARC_KIND;
56 }
57
58 bool PartSet_OperationCreateFeature::canBeCommitted() const
59 {
60   return myPointSelectionMode == SM_DonePoint;
61 }
62
63 bool PartSet_OperationCreateFeature::isGranted(ModuleBase_IOperation* theOperation) const
64 {
65   return theOperation->getDescription()->operationId().toStdString() == PartSet_OperationSketch::Type();
66 }
67
68 std::list<int> PartSet_OperationCreateFeature::getSelectionModes(FeaturePtr theFeature) const
69 {
70   std::list<int> aModes;
71   if (theFeature != feature())
72     aModes = PartSet_OperationSketchBase::getSelectionModes(theFeature);
73   return aModes;
74 }
75
76 void PartSet_OperationCreateFeature::init(FeaturePtr theFeature,
77                                        const std::list<XGUI_ViewerPrs>& /*theSelected*/,
78                                        const std::list<XGUI_ViewerPrs>& /*theHighlighted*/)
79 {
80   if (!theFeature || theFeature->getKind() != SKETCH_LINE_KIND)
81     return;
82   myInitFeature = theFeature;
83 }
84
85 FeaturePtr PartSet_OperationCreateFeature::sketch() const
86 {
87   return myFeaturePrs->sketch();
88 }
89
90 void PartSet_OperationCreateFeature::mouseReleased(QMouseEvent* theEvent, Handle(V3d_View) theView,
91                                                 const std::list<XGUI_ViewerPrs>& theSelected,
92                                                 const std::list<XGUI_ViewerPrs>& /*theHighlighted*/)
93 {
94   if (myPointSelectionMode == SM_DonePoint)
95   {
96     // if the point creation is finished, the next mouse release should commit the modification
97     // the next release can happens by double click in the viewer
98     commit();
99     restartOperation(feature()->getKind(), feature());
100     return;
101   }
102
103   double aX, anY;
104
105   gp_Pnt aPoint = PartSet_Tools::convertClickToPoint(theEvent->pos(), theView);
106   if (theSelected.empty()) {
107     PartSet_Tools::convertTo2D(aPoint, sketch(), theView, aX, anY);
108   }
109   else {
110     XGUI_ViewerPrs aPrs = theSelected.front();
111     const TopoDS_Shape& aShape = aPrs.shape();
112     if (!aShape.IsNull()) // the point is selected
113     {
114       if (aShape.ShapeType() == TopAbs_VERTEX) {
115         const TopoDS_Vertex& aVertex = TopoDS::Vertex(aShape);
116         if (!aVertex.IsNull()) {
117           aPoint = BRep_Tool::Pnt(aVertex);
118           PartSet_Tools::convertTo2D(aPoint, sketch(), theView, aX, anY);
119
120           //myFeaturePrs->setConstraints(aX, anY, myPointSelectionMode);
121         }
122       }
123       else if (aShape.ShapeType() == TopAbs_EDGE) // the line is selected
124       {
125         PartSet_Tools::convertTo2D(aPoint, sketch(), theView, aX, anY);
126         // move to selected line
127         if (feature()->getKind() == SKETCH_LINE_KIND) {
128           boost::shared_ptr<PartSet_FeatureLinePrs> aLinePrs =
129                                  boost::dynamic_pointer_cast<PartSet_FeatureLinePrs>(myFeaturePrs);
130           if (aLinePrs) {
131             FeaturePtr aFeature = aPrs.feature();
132             aLinePrs->projectPointOnLine(aFeature, myPointSelectionMode, aPoint, theView, aX, anY);
133           }
134         }
135       }
136     }
137   }
138
139   switch (myPointSelectionMode)
140   {
141     case SM_FirstPoint:
142     case SM_SecondPoint:
143     case SM_ThirdPoint: {
144       if (feature()->getKind() == SKETCH_ARC_KIND) {
145         boost::shared_ptr<PartSet_FeatureArcPrs> anArcPrs =
146                                  boost::dynamic_pointer_cast<PartSet_FeatureArcPrs>(myFeaturePrs);
147         if (anArcPrs) {
148           anArcPrs->projectPointOnFeature(feature(), sketch(), aPoint, theView, aX, anY);
149         }
150       }
151       PartSet_SelectionMode aMode = myFeaturePrs->setPoint(aX, anY, myPointSelectionMode);
152       flushUpdated();
153       setPointSelectionMode(aMode);
154     }
155     break;
156     default:
157       break;
158   }
159 }
160
161 void PartSet_OperationCreateFeature::mouseMoved(QMouseEvent* theEvent, Handle(V3d_View) theView)
162 {
163   switch (myPointSelectionMode)
164   {
165     case SM_FirstPoint:
166     case SM_SecondPoint:
167     case SM_ThirdPoint:
168     {
169       double aX, anY;
170       gp_Pnt aPoint = PartSet_Tools::convertClickToPoint(theEvent->pos(), theView);
171       PartSet_Tools::convertTo2D(aPoint, sketch(), theView, aX, anY);
172       if (myPointSelectionMode == SM_ThirdPoint) {
173         if (feature()->getKind() == SKETCH_ARC_KIND) {
174           boost::shared_ptr<PartSet_FeatureArcPrs> anArcPrs =
175                                  boost::dynamic_pointer_cast<PartSet_FeatureArcPrs>(myFeaturePrs);
176           if (anArcPrs) {
177             anArcPrs->projectPointOnFeature(feature(), sketch(), aPoint, theView, aX, anY);
178           }
179         }
180       }
181       myFeaturePrs->setPoint(aX, anY, myPointSelectionMode);
182
183       flushUpdated();
184       emit focusActivated(myFeaturePrs->getAttribute(myPointSelectionMode));
185     }
186     break;
187     case SM_DonePoint:
188     {
189       commit();
190       restartOperation(feature()->getKind(), feature());
191     }
192     default:
193       break;
194   }
195 }
196
197 void PartSet_OperationCreateFeature::keyReleased(std::string theName, QKeyEvent* theEvent)
198 {
199   int aKeyType = theEvent->key();
200   // the second point should be activated by any modification in the property panel
201   if (!theName.empty() /*&& aKeyType == Qt::Key_Return*/)
202   {
203     setPointSelectionMode(myFeaturePrs->getNextMode(theName), false);
204   }
205   keyReleased(theEvent->key());
206 }
207
208 void PartSet_OperationCreateFeature::keyReleased(const int theKey)
209 {
210   switch (theKey) {
211     case Qt::Key_Return: {
212       if (myPointSelectionMode == SM_DonePoint)
213       {
214         commit();
215         // it start a new line creation at a free point
216         restartOperation(feature()->getKind(), FeaturePtr()/*feature()*/);
217       }
218       //else
219       //  abort();
220       //restartOperation(feature()->getKind(), FeaturePtr());
221     }
222     break;
223     case Qt::Key_Escape: {
224       if (myPointSelectionMode == SM_DonePoint)
225       {
226         commit();
227       }
228       else
229       {
230         abort();
231       }
232     }
233     default:
234     break;
235   }
236 }
237
238 void PartSet_OperationCreateFeature::startOperation()
239 {
240   PartSet_OperationSketchBase::startOperation();
241   setPointSelectionMode(!myInitFeature ? SM_FirstPoint : SM_SecondPoint);
242
243   emit multiSelectionEnabled(false);
244 }
245
246 void PartSet_OperationCreateFeature::abortOperation()
247 {
248   emit featureConstructed(feature(), FM_Hide);
249   PartSet_OperationSketchBase::abortOperation();
250 }
251
252 void PartSet_OperationCreateFeature::stopOperation()
253 {
254   PartSet_OperationSketchBase::stopOperation();
255   emit multiSelectionEnabled(true);
256 }
257
258 void PartSet_OperationCreateFeature::afterCommitOperation()
259 {
260   PartSet_OperationSketchBase::afterCommitOperation();  
261   emit featureConstructed(feature(), FM_Deactivation);
262 }
263
264 FeaturePtr PartSet_OperationCreateFeature::createFeature(const bool theFlushMessage)
265 {
266   FeaturePtr aNewFeature = ModuleBase_Operation::createFeature(false);
267   if (sketch()) {
268     boost::shared_ptr<SketchPlugin_Feature> aFeature = 
269                            boost::dynamic_pointer_cast<SketchPlugin_Feature>(sketch());
270
271     aFeature->addSub(aNewFeature);
272   }
273   myFeaturePrs->init(aNewFeature);
274   myFeaturePrs->setFeature(myInitFeature, SM_FirstPoint);
275
276   emit featureConstructed(aNewFeature, FM_Activation);
277   if (theFlushMessage)
278     flushCreated();
279   return aNewFeature;
280 }
281
282 void PartSet_OperationCreateFeature::setPointSelectionMode(const PartSet_SelectionMode& theMode,
283                                                            const bool isToEmitSignal)
284 {
285   myPointSelectionMode = theMode;
286   if (isToEmitSignal) {
287     std::string aName = myFeaturePrs->getAttribute(theMode);
288     if (aName.empty() && theMode == SM_DonePoint) {
289       aName = XGUI::PROP_PANEL_OK;
290     }
291     emit focusActivated(aName);
292   }
293 }