]> SALOME platform Git repositories - modules/shaper.git/blob - src/PartSet/PartSet_OperationFeatureCreate.cpp
Salome HOME
Merge with master
[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   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     ModuleBase_WidgetPoint2D* aWgt = dynamic_cast<ModuleBase_WidgetPoint2D*>(myActiveWidget);
215     if (aWgt)
216       aWgt->initFromPrevious(myInitFeature);
217     myInitFeature = FeaturePtr();
218     emit activateNextWidget(myActiveWidget);
219   }
220 }
221
222 void PartSet_OperationFeatureCreate::keyReleased(const int theKey)
223 {
224   switch (theKey) {
225     case Qt::Key_Return: {
226       if (canBeCommitted())
227       {
228         commit();
229         // it start a new line creation at a free point
230         restartOperation(feature()->getKind(), FeaturePtr());
231       }
232     }
233     break;
234     case Qt::Key_Escape: {
235       if (canBeCommitted())
236       {
237         commit();
238       }
239       else
240       {
241         abort();
242       }
243     }
244     default:
245     break;
246   }
247 }
248
249 void PartSet_OperationFeatureCreate::startOperation()
250 {
251   PartSet_OperationSketchBase::startOperation();
252   //setPointSelectionMode(!myInitFeature ? SM_FirstPoint : SM_SecondPoint);
253
254   emit multiSelectionEnabled(false);
255 }
256
257 void PartSet_OperationFeatureCreate::abortOperation()
258 {
259   emit featureConstructed(feature(), FM_Hide);
260   PartSet_OperationSketchBase::abortOperation();
261 }
262
263 void PartSet_OperationFeatureCreate::stopOperation()
264 {
265   PartSet_OperationSketchBase::stopOperation();
266   emit multiSelectionEnabled(true);
267 }
268
269 void PartSet_OperationFeatureCreate::afterCommitOperation()
270 {
271   PartSet_OperationSketchBase::afterCommitOperation();  
272   emit featureConstructed(feature(), FM_Deactivation);
273 }
274
275 FeaturePtr PartSet_OperationFeatureCreate::createFeature(const bool theFlushMessage)
276 {
277   FeaturePtr aNewFeature = ModuleBase_Operation::createFeature(false);
278   if (sketch()) {
279     boost::shared_ptr<SketchPlugin_Feature> aFeature = 
280                            boost::dynamic_pointer_cast<SketchPlugin_Feature>(sketch());
281
282     aFeature->addSub(aNewFeature);
283   }
284   //myFeaturePrs->init(aNewFeature);
285   //myFeaturePrs->setFeature(myInitFeature, SM_FirstPoint);
286
287   emit featureConstructed(aNewFeature, FM_Activation);
288   if (theFlushMessage)
289     flushCreated();
290   return aNewFeature;
291 }
292
293 /*void PartSet_OperationFeatureCreate::setPointSelectionMode(const PartSet_SelectionMode& theMode,
294                                                            const bool isToEmitSignal)
295 {
296   myPointSelectionMode = theMode;
297   if (isToEmitSignal) {
298     std::string aName = myFeaturePrs->getAttribute(theMode);
299     if (aName.empty() && theMode == SM_DonePoint) {
300       aName = XGUI::PROP_PANEL_OK;
301     }
302     emit focusActivated(aName);
303   }
304 }*/
305
306 bool PartSet_OperationFeatureCreate::isPointWidget() const
307 {
308   return dynamic_cast<ModuleBase_WidgetPoint2D*>(myActiveWidget);
309 }
310
311 bool PartSet_OperationFeatureCreate::setWidgetPoint(double theX, double theY)
312 {
313   boost::shared_ptr<GeomAPI_Pnt2d> aPoint(new GeomAPI_Pnt2d(theX, theY));
314   ModuleBase_WidgetPoint2D* aWidget = dynamic_cast<ModuleBase_WidgetPoint2D*>(myActiveWidget);
315   if (aWidget) {
316     aWidget->setPoint(aPoint);
317     return true;
318   } else {
319     ModuleBase_WidgetPoint2dDistance* aWgt = dynamic_cast<ModuleBase_WidgetPoint2dDistance*>(myActiveWidget);
320     if (aWgt) {
321       aWgt->setPoint(feature(), aPoint);
322       return true;
323     }
324   }
325   return false;
326 }
327
328 bool PartSet_OperationFeatureCreate::setWidgetFeature(const FeaturePtr& theFeature)
329 {
330   ModuleBase_WidgetFeature* aWidget = dynamic_cast<ModuleBase_WidgetFeature*>(myActiveWidget);
331   if (!aWidget)
332     return false;
333
334   return aWidget->setFeature(theFeature);
335 }