Salome HOME
refs #80 - Sketch base GUI: create/draw point, circle and arc
[modules/shaper.git] / src / PartSet / PartSet_OperationCreateConstraint.cpp
1 // File:        PartSet_OperationCreateConstraint.h
2 // Created:     20 Apr 2014
3 // Author:      Natalia ERMOLAEVA
4
5 #include <PartSet_OperationCreateConstraint.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
20 #include <SketchPlugin_ConstraintLength.h>
21
22 #include <ModuleBase_OperationDescription.h>
23
24 #include <XGUI_ViewerPrs.h>
25 #include <XGUI_Constants.h>
26
27 #include <V3d_View.hxx>
28 #include <TopoDS_Vertex.hxx>
29 #include <TopoDS.hxx>
30 #include <BRep_Tool.hxx>
31
32 #ifdef _DEBUG
33 #include <QDebug>
34 #endif
35
36 #include <QMouseEvent>
37
38 using namespace std;
39
40 PartSet_OperationCreateConstraint::PartSet_OperationCreateConstraint(const QString& theId,
41                                                                QObject* theParent,
42                                                                FeaturePtr theFeature)
43 : PartSet_OperationSketchBase(theId, theParent),
44   myPointSelectionMode(SM_FirstPoint)
45 {
46   std::string aKind = theId.toStdString();
47   myFeaturePrs = PartSet_Tools::createFeaturePrs(aKind, theFeature);
48 }
49
50 PartSet_OperationCreateConstraint::~PartSet_OperationCreateConstraint()
51 {
52 }
53
54 bool PartSet_OperationCreateConstraint::canProcessKind(const std::string& theId)
55 {
56   return /*theId == SKETCH_LINE_KIND || theId == SKETCH_POINT_KIND || theId == SKETCH_CIRCLE_KIND ||
57          theId == SKETCH_ARC_KIND || */theId == SKETCH_CONSTRAINT_LENGTH_KIND;
58 }
59
60 bool PartSet_OperationCreateConstraint::canBeCommitted() const
61 {
62   return myPointSelectionMode == SM_DonePoint;
63 }
64
65 bool PartSet_OperationCreateConstraint::isGranted(ModuleBase_IOperation* theOperation) const
66 {
67   return theOperation->getDescription()->operationId().toStdString() == PartSet_OperationSketch::Type();
68 }
69
70 std::list<int> PartSet_OperationCreateConstraint::getSelectionModes(FeaturePtr theFeature) const
71 {
72   std::list<int> aModes;
73   if (theFeature != feature())
74     aModes = PartSet_OperationSketchBase::getSelectionModes(theFeature);
75   return aModes;
76 }
77
78 void PartSet_OperationCreateConstraint::init(FeaturePtr theFeature,
79                                        const std::list<XGUI_ViewerPrs>& /*theSelected*/,
80                                        const std::list<XGUI_ViewerPrs>& /*theHighlighted*/)
81 {
82   if (!theFeature || theFeature->getKind() != SKETCH_LINE_KIND)
83     return;
84   myInitFeature = theFeature;
85 }
86
87 FeaturePtr PartSet_OperationCreateConstraint::sketch() const
88 {
89   return myFeaturePrs->sketch();
90 }
91
92 void PartSet_OperationCreateConstraint::mouseReleased(QMouseEvent* theEvent, Handle(V3d_View) theView,
93                                                 const std::list<XGUI_ViewerPrs>& theSelected,
94                                                 const std::list<XGUI_ViewerPrs>& /*theHighlighted*/)
95 {
96   if (theSelected.empty()) {
97
98   }
99   else {
100     XGUI_ViewerPrs aPrs = theSelected.front();
101     FeaturePtr aFeature = aPrs.feature();
102
103     myFeaturePrs->init(feature(), aFeature);
104     flushUpdated();
105   }
106 }
107
108 void PartSet_OperationCreateConstraint::mouseMoved(QMouseEvent* theEvent, Handle(V3d_View) theView)
109 {
110   switch (myPointSelectionMode)
111   {
112     case SM_FirstPoint:
113     case SM_SecondPoint:
114     case SM_ThirdPoint:
115     {
116       double aX, anY;
117       gp_Pnt aPoint = PartSet_Tools::convertClickToPoint(theEvent->pos(), theView);
118       PartSet_Tools::convertTo2D(aPoint, sketch(), theView, aX, anY);
119       /*if (myPointSelectionMode == SM_ThirdPoint) {
120         if (feature()->getKind() == SKETCH_ARC_KIND) {
121           boost::shared_ptr<PartSet_FeatureArcPrs> anArcPrs =
122                                  boost::dynamic_pointer_cast<PartSet_FeatureArcPrs>(myFeaturePrs);
123           if (anArcPrs) {
124             anArcPrs->projectPointOnArc(aPoint, theView, aX, anY);
125           }
126         }
127       }*/
128       myFeaturePrs->setPoint(aX, anY, myPointSelectionMode);
129
130       flushUpdated();
131       emit focusActivated(myFeaturePrs->getAttribute(myPointSelectionMode));
132     }
133     break;
134     case SM_DonePoint:
135     {
136       commit();
137       restartOperation(feature()->getKind(), feature());
138     }
139     default:
140       break;
141   }
142 }
143
144 void PartSet_OperationCreateConstraint::keyReleased(std::string theName, QKeyEvent* theEvent)
145 {
146   int aKeyType = theEvent->key();
147   // the second point should be activated by any modification in the property panel
148   if (!theName.empty() /*&& aKeyType == Qt::Key_Return*/)
149   {
150     setPointSelectionMode(myFeaturePrs->getNextMode(theName), false);
151   }
152   keyReleased(theEvent->key());
153 }
154
155 void PartSet_OperationCreateConstraint::keyReleased(const int theKey)
156 {
157   switch (theKey) {
158     case Qt::Key_Return: {
159       if (myPointSelectionMode == SM_DonePoint)
160       {
161         commit();
162         // it start a new line creation at a free point
163         restartOperation(feature()->getKind(), FeaturePtr()/*feature()*/);
164       }
165       //else
166       //  abort();
167       //restartOperation(feature()->getKind(), FeaturePtr());
168     }
169     break;
170     case Qt::Key_Escape: {
171       if (myPointSelectionMode == SM_DonePoint)
172       {
173         commit();
174       }
175       else
176       {
177         abort();
178       }
179     }
180     default:
181     break;
182   }
183 }
184
185 void PartSet_OperationCreateConstraint::startOperation()
186 {
187   PartSet_OperationSketchBase::startOperation();
188   setPointSelectionMode(!myInitFeature ? SM_FirstPoint : SM_SecondPoint);
189
190   emit multiSelectionEnabled(false);
191 }
192
193 void PartSet_OperationCreateConstraint::abortOperation()
194 {
195   emit featureConstructed(feature(), FM_Hide);
196   PartSet_OperationSketchBase::abortOperation();
197 }
198
199 void PartSet_OperationCreateConstraint::stopOperation()
200 {
201   PartSet_OperationSketchBase::stopOperation();
202   emit multiSelectionEnabled(true);
203 }
204
205 void PartSet_OperationCreateConstraint::afterCommitOperation()
206 {
207   PartSet_OperationSketchBase::afterCommitOperation();  
208   emit featureConstructed(feature(), FM_Deactivation);
209 }
210
211 FeaturePtr PartSet_OperationCreateConstraint::createFeature(const bool theFlushMessage)
212 {
213   FeaturePtr aNewFeature = ModuleBase_Operation::createFeature(false);
214   if (sketch()) {
215     boost::shared_ptr<SketchPlugin_Feature> aFeature = 
216                            boost::dynamic_pointer_cast<SketchPlugin_Feature>(sketch());
217
218     aFeature->addSub(aNewFeature);
219   }
220   myFeaturePrs->init(aNewFeature, myInitFeature);
221
222   emit featureConstructed(aNewFeature, FM_Activation);
223   if (theFlushMessage)
224     flushCreated();
225   return aNewFeature;
226 }
227
228 void PartSet_OperationCreateConstraint::setPointSelectionMode(const PartSet_SelectionMode& theMode,
229                                                            const bool isToEmitSignal)
230 {
231   myPointSelectionMode = theMode;
232   if (isToEmitSignal) {
233     std::string aName = myFeaturePrs->getAttribute(theMode);
234     if (aName.empty() && theMode == SM_DonePoint) {
235       aName = XGUI::PROP_PANEL_OK;
236     }
237     emit focusActivated(aName);
238   }
239 }