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