Salome HOME
fb36405b09dca3ccf9e07c284beddf7526a734ff
[modules/shaper.git] / src / PartSet / PartSet_OperationFeatureCreate.cpp
1 // File:        PartSet_OperationFeatureCreate.h
2 // Created:     20 Apr 2014
3 // Author:      Natalia ERMOLAEVA
4
5 #include <PartSet_OperationFeatureCreate.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 #include <SketchPlugin_ConstraintDistance.h>
20 #include <SketchPlugin_ConstraintLength.h>
21 #include <SketchPlugin_ConstraintRadius.h>
22 #include <SketchPlugin_ConstraintParallel.h>
23 #include <SketchPlugin_ConstraintPerpendicular.h>
24
25 #include <GeomAPI_Pnt2d.h>
26
27 #include <ModuleBase_OperationDescription.h>
28 #include <ModuleBase_WidgetPoint2D.h>
29 #include <ModuleBase_WidgetFeature.h>
30 #include <ModuleBase_WidgetPoint2dDistance.h>
31
32 #include <XGUI_ViewerPrs.h>
33 #include <XGUI_Constants.h>
34
35 #include <V3d_View.hxx>
36 #include <TopoDS_Vertex.hxx>
37 #include <TopoDS.hxx>
38 #include <BRep_Tool.hxx>
39
40 #ifdef _DEBUG
41 #include <QDebug>
42 #endif
43
44 #include <QMouseEvent>
45
46 using namespace std;
47
48 PartSet_OperationFeatureCreate::PartSet_OperationFeatureCreate(const QString& theId,
49                                                                QObject* theParent,
50                                                                  FeaturePtr theFeature)
51 : PartSet_OperationSketchBase(theId, theParent), mySketch(theFeature), myActiveWidget(0)
52 {
53 }
54
55 PartSet_OperationFeatureCreate::~PartSet_OperationFeatureCreate()
56 {
57 }
58
59 bool PartSet_OperationFeatureCreate::canProcessKind(const std::string& theId)
60 {
61   return theId == SKETCH_LINE_KIND || theId == SKETCH_POINT_KIND ||
62          theId == SKETCH_CIRCLE_KIND /*||
63          theId == SKETCH_ARC_KIND*/ ||
64          theId == SKETCH_CONSTRAINT_DISTANCE_KIND ||
65          theId == SKETCH_CONSTRAINT_LENGTH_KIND ||
66          theId == SKETCH_CONSTRAINT_RADIUS_KIND ||
67          theId == SKETCH_CONSTRAINT_PARALLEL_KIND ||
68          theId == SKETCH_CONSTRAINT_PERPENDICULAR_KIND;
69 }
70
71 bool PartSet_OperationFeatureCreate::canBeCommitted() const
72 {
73   return !myActiveWidget;
74 }
75
76 bool PartSet_OperationFeatureCreate::isGranted(ModuleBase_IOperation* theOperation) const
77 {
78   return theOperation->getDescription()->operationId().toStdString() == PartSet_OperationSketch::Type();
79 }
80
81 std::list<int> PartSet_OperationFeatureCreate::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_OperationFeatureCreate::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_OperationFeatureCreate::sketch() const
99 {
100   return mySketch;
101 }
102
103 void PartSet_OperationFeatureCreate::mouseReleased(QMouseEvent* theEvent, Handle(V3d_View) theView,
104                                                 const std::list<XGUI_ViewerPrs>& theSelected,
105                                                 const std::list<XGUI_ViewerPrs>& /*theHighlighted*/)
106 {
107   if (canBeCommitted())
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           PartSet_Tools::setConstraints(sketch(), feature(), myActiveWidget->attributeID(),
134                                         aX, anY);
135         }
136       }
137       else if (aShape.ShapeType() == TopAbs_EDGE) // the line is selected
138       {
139         PartSet_Tools::convertTo2D(aPoint, sketch(), theView, aX, anY);
140         // move to selected line
141         if (feature()->getKind() == SKETCH_LINE_KIND) {
142           //boost::shared_ptr<PartSet_FeatureLinePrs> aLinePrs =
143           //                       boost::dynamic_pointer_cast<PartSet_FeatureLinePrs>(myFeaturePrs);
144           //if (aLinePrs) {
145           //  FeaturePtr aFeature = aPrs.feature();
146             //aLinePrs->projectPointOnLine(aFeature, myPointSelectionMode, aPoint, theView, aX, anY);
147           //}
148         }
149       }
150     }
151   }
152   /*if (feature()->getKind() == SKETCH_ARC_KIND) {
153     boost::shared_ptr<PartSet_FeatureArcPrs> anArcPrs =
154                               boost::dynamic_pointer_cast<PartSet_FeatureArcPrs>(myFeaturePrs);
155     if (anArcPrs) {
156       anArcPrs->projectPointOnFeature(feature(), sketch(), aPoint, theView, aX, anY);
157     }
158   }*/
159   bool isApplyed = false;
160   if (isPointWidget())
161     isApplyed = setWidgetPoint(aX, anY);
162   else {
163     if (!theSelected.empty()) {
164       XGUI_ViewerPrs aPrs = theSelected.front();
165       FeaturePtr aFeature = aPrs.feature();
166       if (aFeature)
167         isApplyed = setWidgetFeature(aFeature);
168     }
169   }
170   flushUpdated();
171   emit activateNextWidget(myActiveWidget);
172 }
173
174 void PartSet_OperationFeatureCreate::mouseMoved(QMouseEvent* theEvent, Handle(V3d_View) theView)
175 {
176   if (canBeCommitted()) {
177     commit();
178     restartOperation(feature()->getKind(), feature());
179   }
180   else {
181     double aX, anY;
182     gp_Pnt aPoint = PartSet_Tools::convertClickToPoint(theEvent->pos(), theView);
183     PartSet_Tools::convertTo2D(aPoint, sketch(), theView, aX, anY);
184     /*if (myPointSelectionMode == SM_ThirdPoint) {
185       if (feature()->getKind() == SKETCH_ARC_KIND) {
186         boost::shared_ptr<PartSet_FeatureArcPrs> anArcPrs =
187                                 boost::dynamic_pointer_cast<PartSet_FeatureArcPrs>(myFeaturePrs);
188         if (anArcPrs) {
189           anArcPrs->projectPointOnFeature(feature(), sketch(), aPoint, theView, aX, anY);
190         }
191       }
192     }*/
193     setWidgetPoint(aX, anY);
194     flushUpdated();
195   }
196 }
197
198 void PartSet_OperationFeatureCreate::keyReleased(std::string theName, QKeyEvent* theEvent)
199 {
200   int aKeyType = theEvent->key();
201   // the second point should be activated by any modification in the property panel
202   if (!theName.empty())
203   {
204     //setPointSelectionMode(myFeaturePrs->getNextMode(theName), false);
205   }
206   keyReleased(theEvent->key());
207 }
208
209 void PartSet_OperationFeatureCreate::onWidgetActivated(ModuleBase_ModelWidget* theWidget)
210 {
211   myActiveWidget = theWidget;
212
213   if (myInitFeature && myActiveWidget) {
214     // TODO: to be realized in the custom point selector. The last point values of the init feature
215     // should be to to the start point of a new feature
216     //myActiveWidget->init(myInitFeature);
217     //PartSet_FeatureLinePrs::setFeature(myInitFeature, SM_FirstPoint);
218     myInitFeature = FeaturePtr();
219     emit activateNextWidget(myActiveWidget);
220   }
221 }
222
223 void PartSet_OperationFeatureCreate::keyReleased(const int theKey)
224 {
225   switch (theKey) {
226     case Qt::Key_Return: {
227       if (canBeCommitted())
228       {
229         commit();
230         // it start a new line creation at a free point
231         restartOperation(feature()->getKind(), FeaturePtr());
232       }
233     }
234     break;
235     case Qt::Key_Escape: {
236       if (canBeCommitted())
237       {
238         commit();
239       }
240       else
241       {
242         abort();
243       }
244     }
245     default:
246     break;
247   }
248 }
249
250 void PartSet_OperationFeatureCreate::startOperation()
251 {
252   PartSet_OperationSketchBase::startOperation();
253   //setPointSelectionMode(!myInitFeature ? SM_FirstPoint : SM_SecondPoint);
254
255   emit multiSelectionEnabled(false);
256 }
257
258 void PartSet_OperationFeatureCreate::abortOperation()
259 {
260   emit featureConstructed(feature(), FM_Hide);
261   PartSet_OperationSketchBase::abortOperation();
262 }
263
264 void PartSet_OperationFeatureCreate::stopOperation()
265 {
266   PartSet_OperationSketchBase::stopOperation();
267   emit multiSelectionEnabled(true);
268 }
269
270 void PartSet_OperationFeatureCreate::afterCommitOperation()
271 {
272   PartSet_OperationSketchBase::afterCommitOperation();  
273   emit featureConstructed(feature(), FM_Deactivation);
274 }
275
276 FeaturePtr PartSet_OperationFeatureCreate::createFeature(const bool theFlushMessage)
277 {
278   FeaturePtr aNewFeature = ModuleBase_Operation::createFeature(false);
279   if (sketch()) {
280     boost::shared_ptr<SketchPlugin_Feature> aFeature = 
281                            boost::dynamic_pointer_cast<SketchPlugin_Feature>(sketch());
282
283     aFeature->addSub(aNewFeature);
284   }
285   //myFeaturePrs->init(aNewFeature);
286   //myFeaturePrs->setFeature(myInitFeature, SM_FirstPoint);
287
288   emit featureConstructed(aNewFeature, FM_Activation);
289   if (theFlushMessage)
290     flushCreated();
291   return aNewFeature;
292 }
293
294 /*void PartSet_OperationFeatureCreate::setPointSelectionMode(const PartSet_SelectionMode& theMode,
295                                                            const bool isToEmitSignal)
296 {
297   myPointSelectionMode = theMode;
298   if (isToEmitSignal) {
299     std::string aName = myFeaturePrs->getAttribute(theMode);
300     if (aName.empty() && theMode == SM_DonePoint) {
301       aName = XGUI::PROP_PANEL_OK;
302     }
303     emit focusActivated(aName);
304   }
305 }*/
306
307 bool PartSet_OperationFeatureCreate::isPointWidget() const
308 {
309   return dynamic_cast<ModuleBase_WidgetPoint2D*>(myActiveWidget);
310 }
311
312 bool PartSet_OperationFeatureCreate::setWidgetPoint(double theX, double theY)
313 {
314   boost::shared_ptr<GeomAPI_Pnt2d> aPoint(new GeomAPI_Pnt2d(theX, theY));
315   ModuleBase_WidgetPoint2D* aWidget = dynamic_cast<ModuleBase_WidgetPoint2D*>(myActiveWidget);
316   if (aWidget) {
317     aWidget->setPoint(aPoint);
318     return true;
319   } else {
320     ModuleBase_WidgetPoint2dDistance* aWgt = dynamic_cast<ModuleBase_WidgetPoint2dDistance*>(myActiveWidget);
321     if (aWgt) {
322       aWgt->setPoint(feature(), aPoint);
323       return true;
324     }
325   }
326   return false;
327 }
328
329 bool PartSet_OperationFeatureCreate::setWidgetFeature(const FeaturePtr& theFeature)
330 {
331   ModuleBase_WidgetFeature* aWidget = dynamic_cast<ModuleBase_WidgetFeature*>(myActiveWidget);
332   if (!aWidget)
333     return false;
334
335   return aWidget->setFeature(theFeature);
336 }