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