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