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
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   gp_Pnt aPoint = PartSet_Tools::convertClickToPoint(theEvent->pos(), theView);
119   if (theSelected.empty()) {
120     PartSet_Tools::convertTo2D(aPoint, sketch(), theView, aX, anY);
121   }
122   else {
123     XGUI_ViewerPrs aPrs = theSelected.front();
124     const TopoDS_Shape& aShape = aPrs.shape();
125     if (!aShape.IsNull()) // the point is selected
126     {
127       if (aShape.ShapeType() == TopAbs_VERTEX) {
128         const TopoDS_Vertex& aVertex = TopoDS::Vertex(aShape);
129         if (!aVertex.IsNull()) {
130           aPoint = BRep_Tool::Pnt(aVertex);
131           PartSet_Tools::convertTo2D(aPoint, sketch(), theView, aX, anY);
132
133           myFeaturePrs->setConstraints(aX, anY, myPointSelectionMode);
134         }
135       }
136       else if (aShape.ShapeType() == TopAbs_EDGE) // the line is selected
137       {
138         PartSet_Tools::convertTo2D(aPoint, sketch(), theView, aX, anY);
139         // move to selected line
140         if (feature()->getKind() == SKETCH_LINE_KIND) {
141           PartSet_FeatureLinePrs* aLinePrs = dynamic_cast<PartSet_FeatureLinePrs*>(myFeaturePrs);
142           if (aLinePrs) {
143             FeaturePtr aFeature = aPrs.feature();
144             aLinePrs->projectPointOnLine(aFeature, myPointSelectionMode, aPoint, theView, aX, anY);
145           }
146         }
147       }
148     }
149   }
150
151   switch (myPointSelectionMode)
152   {
153     case SM_FirstPoint:
154     case SM_SecondPoint:
155     case SM_ThirdPoint: {
156       PartSet_SelectionMode aMode = myFeaturePrs->setPoint(aX, anY, myPointSelectionMode);
157       flushUpdated();
158       setPointSelectionMode(aMode);
159     }
160     break;
161     default:
162       break;
163   }
164 }
165
166 void PartSet_OperationCreateFeature::mouseMoved(QMouseEvent* theEvent, Handle(V3d_View) theView)
167 {
168   switch (myPointSelectionMode)
169   {
170     case SM_FirstPoint:
171     case SM_SecondPoint:
172     case SM_ThirdPoint:
173     {
174       double aX, anY;
175       gp_Pnt aPoint = PartSet_Tools::convertClickToPoint(theEvent->pos(), theView);
176       PartSet_Tools::convertTo2D(aPoint, sketch(), theView, aX, anY);
177       myFeaturePrs->setPoint(aX, anY, myPointSelectionMode);
178
179       flushUpdated();
180       emit focusActivated(myFeaturePrs->getAttribute(myPointSelectionMode));
181     }
182     break;
183     case SM_DonePoint:
184     {
185       commit();
186       restartOperation(feature()->getKind(), feature());
187     }
188     default:
189       break;
190   }
191 }
192
193 void PartSet_OperationCreateFeature::keyReleased(std::string theName, QKeyEvent* theEvent)
194 {
195   int aKeyType = theEvent->key();
196   // the second point should be activated by any modification in the property panel
197   if (!theName.empty() /*&& aKeyType == Qt::Key_Return*/)
198   {
199     setPointSelectionMode(myFeaturePrs->getNextMode(theName), false);
200   }
201   keyReleased(theEvent->key());
202 }
203
204 void PartSet_OperationCreateFeature::keyReleased(const int theKey)
205 {
206   switch (theKey) {
207     case Qt::Key_Return: {
208       if (myPointSelectionMode == SM_DonePoint)
209       {
210         commit();
211         // it start a new line creation at a free point
212         restartOperation(feature()->getKind(), FeaturePtr()/*feature()*/);
213       }
214       //else
215       //  abort();
216       //restartOperation(feature()->getKind(), FeaturePtr());
217     }
218     break;
219     case Qt::Key_Escape: {
220       if (myPointSelectionMode == SM_DonePoint)
221       {
222         commit();
223       }
224       else
225       {
226         abort();
227       }
228     }
229     default:
230     break;
231   }
232 }
233
234 void PartSet_OperationCreateFeature::startOperation()
235 {
236   PartSet_OperationSketchBase::startOperation();
237   setPointSelectionMode(!myInitFeature ? SM_FirstPoint : SM_SecondPoint);
238
239   emit multiSelectionEnabled(false);
240 }
241
242 void PartSet_OperationCreateFeature::abortOperation()
243 {
244   emit featureConstructed(feature(), FM_Hide);
245   PartSet_OperationSketchBase::abortOperation();
246 }
247
248 void PartSet_OperationCreateFeature::stopOperation()
249 {
250   PartSet_OperationSketchBase::stopOperation();
251   emit multiSelectionEnabled(true);
252 }
253
254 void PartSet_OperationCreateFeature::afterCommitOperation()
255 {
256   PartSet_OperationSketchBase::afterCommitOperation();  
257   emit featureConstructed(feature(), FM_Deactivation);
258 }
259
260 FeaturePtr PartSet_OperationCreateFeature::createFeature(const bool theFlushMessage)
261 {
262   FeaturePtr aNewFeature = ModuleBase_Operation::createFeature(false);
263   if (sketch()) {
264     boost::shared_ptr<SketchPlugin_Feature> aFeature = 
265                            boost::dynamic_pointer_cast<SketchPlugin_Feature>(sketch());
266
267     aFeature->addSub(aNewFeature);
268   }
269   myFeaturePrs->init(aNewFeature, myInitFeature);
270
271   emit featureConstructed(aNewFeature, FM_Activation);
272   if (theFlushMessage)
273     flushCreated();
274   return aNewFeature;
275 }
276
277 void PartSet_OperationCreateFeature::setPointSelectionMode(const PartSet_SelectionMode& theMode,
278                                                            const bool isToEmitSignal)
279 {
280   myPointSelectionMode = theMode;
281   if (isToEmitSignal) {
282     std::string aName = myFeaturePrs->getAttribute(theMode);
283     if (aName.empty() && theMode == SM_DonePoint) {
284       aName = XGUI::PROP_PANEL_OK;
285     }
286     emit focusActivated(aName);
287   }
288 }