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