]> SALOME platform Git repositories - modules/shaper.git/blob - src/PartSet/PartSet_OperationFeatureCreate.cpp
Salome HOME
Initialization of WidgetPoint2d with point from last feature.
[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 #include <ModuleBase_WidgetPoint2dDistance.h>
27
28 #include <XGUI_ViewerPrs.h>
29 #include <XGUI_Constants.h>
30
31 #include <V3d_View.hxx>
32 #include <TopoDS_Vertex.hxx>
33 #include <TopoDS.hxx>
34 #include <BRep_Tool.hxx>
35
36 #ifdef _DEBUG
37 #include <QDebug>
38 #endif
39
40 #include <QMouseEvent>
41
42 using namespace std;
43
44 PartSet_OperationFeatureCreate::PartSet_OperationFeatureCreate(const QString& theId,
45                                                                QObject* theParent,
46                                                                  FeaturePtr theFeature)
47 : PartSet_OperationSketchBase(theId, theParent), mySketch(theFeature), myActiveWidget(0)
48 {
49 }
50
51 PartSet_OperationFeatureCreate::~PartSet_OperationFeatureCreate()
52 {
53 }
54
55 bool PartSet_OperationFeatureCreate::canProcessKind(const std::string& theId)
56 {
57   return theId == SKETCH_LINE_KIND || theId == SKETCH_POINT_KIND /*||
58          theId == SKETCH_CONSTRAINT_DISTANCE_KIND*/ || theId == SKETCH_CIRCLE_KIND /*||
59          theId == SKETCH_ARC_KIND*/;
60 }
61
62 bool PartSet_OperationFeatureCreate::canBeCommitted() const
63 {
64   return !myActiveWidget;
65 }
66
67 bool PartSet_OperationFeatureCreate::isGranted(ModuleBase_IOperation* theOperation) const
68 {
69   return theOperation->getDescription()->operationId().toStdString() == PartSet_OperationSketch::Type();
70 }
71
72 std::list<int> PartSet_OperationFeatureCreate::getSelectionModes(FeaturePtr theFeature) const
73 {
74   std::list<int> aModes;
75   if (theFeature != feature())
76     aModes = PartSet_OperationSketchBase::getSelectionModes(theFeature);
77   return aModes;
78 }
79
80 void PartSet_OperationFeatureCreate::init(FeaturePtr theFeature,
81                                        const std::list<XGUI_ViewerPrs>& /*theSelected*/,
82                                        const std::list<XGUI_ViewerPrs>& /*theHighlighted*/)
83 {
84   if (!theFeature || theFeature->getKind() != SKETCH_LINE_KIND)
85     return;
86   myInitFeature = theFeature;
87 }
88
89 FeaturePtr PartSet_OperationFeatureCreate::sketch() const
90 {
91   return mySketch;
92 }
93
94 void PartSet_OperationFeatureCreate::mouseReleased(QMouseEvent* theEvent, Handle(V3d_View) theView,
95                                                 const std::list<XGUI_ViewerPrs>& theSelected,
96                                                 const std::list<XGUI_ViewerPrs>& /*theHighlighted*/)
97 {
98   if (canBeCommitted())
99   {
100     // if the point creation is finished, the next mouse release should commit the modification
101     // the next release can happens by double click in the viewer
102     commit();
103     restartOperation(feature()->getKind(), feature());
104     return;
105   }
106
107   double aX, anY;
108
109   gp_Pnt aPoint = PartSet_Tools::convertClickToPoint(theEvent->pos(), theView);
110   if (theSelected.empty()) {
111     PartSet_Tools::convertTo2D(aPoint, sketch(), theView, aX, anY);
112   }
113   else {
114     XGUI_ViewerPrs aPrs = theSelected.front();
115     const TopoDS_Shape& aShape = aPrs.shape();
116     if (!aShape.IsNull()) // the point is selected
117     {
118       if (aShape.ShapeType() == TopAbs_VERTEX) {
119         const TopoDS_Vertex& aVertex = TopoDS::Vertex(aShape);
120         if (!aVertex.IsNull()) {
121           aPoint = BRep_Tool::Pnt(aVertex);
122           PartSet_Tools::convertTo2D(aPoint, sketch(), theView, aX, anY);
123
124           PartSet_Tools::setConstraints(sketch(), feature(), myActiveWidget->attributeID(),
125                                         aX, anY);
126         }
127       }
128       else if (aShape.ShapeType() == TopAbs_EDGE) // the line is selected
129       {
130         PartSet_Tools::convertTo2D(aPoint, sketch(), theView, aX, anY);
131         // move to selected line
132         if (feature()->getKind() == SKETCH_LINE_KIND) {
133           //boost::shared_ptr<PartSet_FeatureLinePrs> aLinePrs =
134           //                       boost::dynamic_pointer_cast<PartSet_FeatureLinePrs>(myFeaturePrs);
135           //if (aLinePrs) {
136           //  FeaturePtr aFeature = aPrs.feature();
137             //aLinePrs->projectPointOnLine(aFeature, myPointSelectionMode, aPoint, theView, aX, anY);
138           //}
139         }
140       }
141     }
142   }
143   /*if (feature()->getKind() == SKETCH_ARC_KIND) {
144     boost::shared_ptr<PartSet_FeatureArcPrs> anArcPrs =
145                               boost::dynamic_pointer_cast<PartSet_FeatureArcPrs>(myFeaturePrs);
146     if (anArcPrs) {
147       anArcPrs->projectPointOnFeature(feature(), sketch(), aPoint, theView, aX, anY);
148     }
149   }*/
150   bool isApplyed = false;
151   if (isPointWidget())
152     isApplyed = setWidgetPoint(aX, anY);
153   else {
154     if (!theSelected.empty()) {
155       XGUI_ViewerPrs aPrs = theSelected.front();
156       FeaturePtr aFeature = aPrs.feature();
157       if (aFeature)
158         isApplyed = setWidgetFeature(aFeature);
159     }
160   }
161   flushUpdated();
162   emit activateNextWidget(myActiveWidget);
163 }
164
165 void PartSet_OperationFeatureCreate::mouseMoved(QMouseEvent* theEvent, Handle(V3d_View) theView)
166 {
167   if (canBeCommitted()) {
168     commit();
169     restartOperation(feature()->getKind(), feature());
170   }
171   else {
172     double aX, anY;
173     gp_Pnt aPoint = PartSet_Tools::convertClickToPoint(theEvent->pos(), theView);
174     PartSet_Tools::convertTo2D(aPoint, sketch(), theView, aX, anY);
175     /*if (myPointSelectionMode == SM_ThirdPoint) {
176       if (feature()->getKind() == SKETCH_ARC_KIND) {
177         boost::shared_ptr<PartSet_FeatureArcPrs> anArcPrs =
178                                 boost::dynamic_pointer_cast<PartSet_FeatureArcPrs>(myFeaturePrs);
179         if (anArcPrs) {
180           anArcPrs->projectPointOnFeature(feature(), sketch(), aPoint, theView, aX, anY);
181         }
182       }
183     }*/
184     setWidgetPoint(aX, anY);
185     flushUpdated();
186   }
187 }
188
189 void PartSet_OperationFeatureCreate::keyReleased(std::string theName, QKeyEvent* theEvent)
190 {
191   int aKeyType = theEvent->key();
192   // the second point should be activated by any modification in the property panel
193   if (!theName.empty())
194   {
195     //setPointSelectionMode(myFeaturePrs->getNextMode(theName), false);
196   }
197   keyReleased(theEvent->key());
198 }
199
200 void PartSet_OperationFeatureCreate::onWidgetActivated(ModuleBase_ModelWidget* theWidget)
201 {
202   myActiveWidget = theWidget;
203
204   if (myInitFeature && myActiveWidget) {
205     ModuleBase_WidgetPoint2D* aWgt = dynamic_cast<ModuleBase_WidgetPoint2D*>(myActiveWidget);
206     if (aWgt)
207       aWgt->initFromPrevious(myInitFeature);
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   boost::shared_ptr<GeomAPI_Pnt2d> aPoint(new GeomAPI_Pnt2d(theX, theY));
305   ModuleBase_WidgetPoint2D* aWidget = dynamic_cast<ModuleBase_WidgetPoint2D*>(myActiveWidget);
306   if (aWidget) {
307     aWidget->setPoint(aPoint);
308     return true;
309   } else {
310     ModuleBase_WidgetPoint2dDistance* aWgt = dynamic_cast<ModuleBase_WidgetPoint2dDistance*>(myActiveWidget);
311     if (aWgt) {
312       aWgt->setPoint(feature(), aPoint);
313       return true;
314     }
315   }
316   return false;
317 }
318
319 bool PartSet_OperationFeatureCreate::setWidgetFeature(const FeaturePtr& theFeature)
320 {
321   ModuleBase_WidgetFeature* aWidget = dynamic_cast<ModuleBase_WidgetFeature*>(myActiveWidget);
322   if (!aWidget)
323     return false;
324
325   return aWidget->setFeature(theFeature);
326 }