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