]> SALOME platform Git repositories - modules/shaper.git/blob - src/PartSet/PartSet_OperationFeatureCreate.cpp
Salome HOME
5b22f3face29eb2bd73c5150340e21611d237e30
[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 = 0, anY = 0;
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   if (isApplyed) {
171     flushUpdated();
172     emit activateNextWidget(myActiveWidget);
173   }
174 }
175
176 void PartSet_OperationFeatureCreate::mouseMoved(QMouseEvent* theEvent, Handle(V3d_View) theView)
177 {
178   if (canBeCommitted()) {
179     commit();
180     restartOperation(feature()->getKind(), feature());
181   }
182   else {
183     double aX, anY;
184     gp_Pnt aPoint = PartSet_Tools::convertClickToPoint(theEvent->pos(), theView);
185     PartSet_Tools::convertTo2D(aPoint, sketch(), theView, aX, anY);
186     /*if (myPointSelectionMode == SM_ThirdPoint) {
187       if (feature()->getKind() == SKETCH_ARC_KIND) {
188         boost::shared_ptr<PartSet_FeatureArcPrs> anArcPrs =
189                                 boost::dynamic_pointer_cast<PartSet_FeatureArcPrs>(myFeaturePrs);
190         if (anArcPrs) {
191           anArcPrs->projectPointOnFeature(feature(), sketch(), aPoint, theView, aX, anY);
192         }
193       }
194     }*/
195     setWidgetPoint(aX, anY);
196     flushUpdated();
197   }
198 }
199
200 void PartSet_OperationFeatureCreate::keyReleased(std::string theName, QKeyEvent* theEvent)
201 {
202   int aKeyType = theEvent->key();
203   // the second point should be activated by any modification in the property panel
204   if (!theName.empty())
205   {
206     //setPointSelectionMode(myFeaturePrs->getNextMode(theName), false);
207   }
208   keyReleased(theEvent->key());
209 }
210
211 void PartSet_OperationFeatureCreate::onWidgetActivated(ModuleBase_ModelWidget* theWidget)
212 {
213   myActiveWidget = theWidget;
214
215   if (myInitFeature && myActiveWidget) {
216     ModuleBase_WidgetPoint2D* aWgt = dynamic_cast<ModuleBase_WidgetPoint2D*>(myActiveWidget);
217     if (aWgt)
218       aWgt->initFromPrevious(myInitFeature);
219     myInitFeature = FeaturePtr();
220     emit activateNextWidget(myActiveWidget);
221   }
222 }
223
224 void PartSet_OperationFeatureCreate::keyReleased(const int theKey)
225 {
226   switch (theKey) {
227     case Qt::Key_Return: {
228       if (canBeCommitted())
229       {
230         commit();
231         // it start a new line creation at a free point
232         restartOperation(feature()->getKind(), FeaturePtr());
233       }
234     }
235     break;
236     case Qt::Key_Escape: {
237       if (canBeCommitted())
238       {
239         commit();
240       }
241       else
242       {
243         abort();
244       }
245     }
246     default:
247     break;
248   }
249 }
250
251 void PartSet_OperationFeatureCreate::startOperation()
252 {
253   PartSet_OperationSketchBase::startOperation();
254   //setPointSelectionMode(!myInitFeature ? SM_FirstPoint : SM_SecondPoint);
255
256   emit multiSelectionEnabled(false);
257 }
258
259 void PartSet_OperationFeatureCreate::abortOperation()
260 {
261   emit featureConstructed(feature(), FM_Hide);
262   PartSet_OperationSketchBase::abortOperation();
263 }
264
265 void PartSet_OperationFeatureCreate::stopOperation()
266 {
267   PartSet_OperationSketchBase::stopOperation();
268   emit multiSelectionEnabled(true);
269 }
270
271 void PartSet_OperationFeatureCreate::afterCommitOperation()
272 {
273   PartSet_OperationSketchBase::afterCommitOperation();  
274   emit featureConstructed(feature(), FM_Deactivation);
275 }
276
277 FeaturePtr PartSet_OperationFeatureCreate::createFeature(const bool theFlushMessage)
278 {
279   FeaturePtr aNewFeature = ModuleBase_Operation::createFeature(false);
280   if (sketch()) {
281     boost::shared_ptr<SketchPlugin_Feature> aFeature = 
282                            boost::dynamic_pointer_cast<SketchPlugin_Feature>(sketch());
283
284     aFeature->addSub(aNewFeature);
285   }
286   //myFeaturePrs->init(aNewFeature);
287   //myFeaturePrs->setFeature(myInitFeature, SM_FirstPoint);
288
289   emit featureConstructed(aNewFeature, FM_Activation);
290   if (theFlushMessage)
291     flushCreated();
292   return aNewFeature;
293 }
294
295 /*void PartSet_OperationFeatureCreate::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 }*/
307
308 bool PartSet_OperationFeatureCreate::isPointWidget() const
309 {
310   return dynamic_cast<ModuleBase_WidgetPoint2D*>(myActiveWidget);
311 }
312
313 bool PartSet_OperationFeatureCreate::setWidgetPoint(double theX, double theY)
314 {
315   boost::shared_ptr<GeomAPI_Pnt2d> aPoint(new GeomAPI_Pnt2d(theX, theY));
316   ModuleBase_WidgetPoint2D* aWidget = dynamic_cast<ModuleBase_WidgetPoint2D*>(myActiveWidget);
317   if (aWidget) {
318     aWidget->setPoint(aPoint);
319     return true;
320   } else {
321     ModuleBase_WidgetPoint2dDistance* aWgt = dynamic_cast<ModuleBase_WidgetPoint2dDistance*>(myActiveWidget);
322     if (aWgt) {
323       aWgt->setPoint(feature(), aPoint);
324       return true;
325     }
326   }
327   return false;
328 }
329
330 bool PartSet_OperationFeatureCreate::setWidgetFeature(const FeaturePtr& theFeature)
331 {
332   ModuleBase_WidgetFeature* aWidget = dynamic_cast<ModuleBase_WidgetFeature*>(myActiveWidget);
333   if (!aWidget)
334     return false;
335
336   return aWidget->setFeature(theFeature);
337 }