]> SALOME platform Git repositories - modules/shaper.git/blob - src/PartSet/PartSet_OperationCreateConstraint.cpp
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 #include <PartSet_EditLine.h>
14
15 #include <SketchPlugin_Feature.h>
16 /*#include <SketchPlugin_Point.h>
17 #include <SketchPlugin_Line.h>
18 #include <SketchPlugin_Circle.h>
19 #include <SketchPlugin_Arc.h>
20 */
21
22 #include <PartSet_ConstraintLengthPrs.h>
23 #include <PartSet_ConstraintDistancePrs.h>
24 #include <PartSet_ConstraintRadiusPrs.h>
25
26 #include <SketchPlugin_Constraint.h>
27
28 #include <ModuleBase_OperationDescription.h>
29
30 #include <XGUI_ViewerPrs.h>
31 #include <XGUI_Constants.h>
32
33 #include <V3d_View.hxx>
34 #include <TopoDS_Vertex.hxx>
35 #include <TopoDS.hxx>
36 #include <BRep_Tool.hxx>
37
38 #ifdef _DEBUG
39 #include <QDebug>
40 #endif
41
42 #include <QMouseEvent>
43
44 using namespace std;
45
46 PartSet_OperationCreateConstraint::PartSet_OperationCreateConstraint(const QString& theId,
47                                                                QObject* theParent,
48                                                                FeaturePtr theFeature)
49 : PartSet_OperationSketchBase(theId, theParent),
50   myPointSelectionMode(SM_FirstPoint)
51 {
52   std::string aKind = theId.toStdString();
53   myFeaturePrs = PartSet_Tools::createFeaturePrs(aKind, theFeature);
54
55   // changed
56   myEditor = new PartSet_EditLine(0);
57   connect(myEditor, SIGNAL(stopped(double)), this, SLOT(onEditStopped(double)));
58 }
59
60 PartSet_OperationCreateConstraint::~PartSet_OperationCreateConstraint()
61 {
62 }
63
64 bool PartSet_OperationCreateConstraint::canProcessKind(const std::string& theId)
65 {
66   // changed
67   return theId == PartSet_ConstraintLengthPrs::getKind() ||
68          theId == PartSet_ConstraintDistancePrs::getKind() ||
69          theId == PartSet_ConstraintRadiusPrs::getKind();
70 }
71
72 bool PartSet_OperationCreateConstraint::canBeCommitted() const
73 {
74   return myPointSelectionMode == SM_DonePoint;
75 }
76
77 bool PartSet_OperationCreateConstraint::isGranted(ModuleBase_IOperation* theOperation) const
78 {
79   return theOperation->getDescription()->operationId().toStdString() == PartSet_OperationSketch::Type();
80 }
81
82 std::list<int> PartSet_OperationCreateConstraint::getSelectionModes(FeaturePtr theFeature) const
83 {
84   std::list<int> aModes;
85   if (theFeature != feature())
86     aModes = PartSet_OperationSketchBase::getSelectionModes(theFeature);
87   return aModes;
88 }
89
90 void PartSet_OperationCreateConstraint::init(FeaturePtr theFeature,
91                                        const std::list<XGUI_ViewerPrs>& /*theSelected*/,
92                                        const std::list<XGUI_ViewerPrs>& /*theHighlighted*/)
93 {
94   // changed
95   if (!theFeature/* || theFeature->getKind() != SKETCH_LINE_KIND*/)
96     return;
97   myInitFeature = theFeature;
98 }
99
100 FeaturePtr PartSet_OperationCreateConstraint::sketch() const
101 {
102   return myFeaturePrs->sketch();
103 }
104
105 void PartSet_OperationCreateConstraint::mouseReleased(QMouseEvent* theEvent, Handle(V3d_View) theView,
106                                                 const std::list<XGUI_ViewerPrs>& theSelected,
107                                                 const std::list<XGUI_ViewerPrs>& /*theHighlighted*/)
108 {
109   switch (myPointSelectionMode)
110   {
111     case SM_FirstPoint: {
112       if (!theSelected.empty()) {
113         XGUI_ViewerPrs aPrs = theSelected.front();
114         FeaturePtr aFeature = aPrs.feature();
115
116         if (myFeaturePrs->setFeature(aFeature, myPointSelectionMode)) {
117           flushUpdated();
118           setPointSelectionMode(SM_SecondPoint);
119         }
120       }
121     }
122     break;
123     case SM_SecondPoint: {
124       double aX, anY;
125       gp_Pnt aPoint = PartSet_Tools::convertClickToPoint(theEvent->pos(), theView);
126       PartSet_Tools::convertTo2D(aPoint, sketch(), theView, aX, anY);
127
128       PartSet_SelectionMode aMode = myFeaturePrs->setPoint(aX, anY, myPointSelectionMode);
129       // show value edit dialog
130       bool isValid;
131       double aValue = PartSet_Tools::featureValue(feature(), CONSTRAINT_ATTR_VALUE, isValid);
132       if (isValid) {
133         showEditor(theEvent, aValue);
134         setPointSelectionMode(SM_ThirdPoint/*aMode*/);
135       }
136     }
137     break;
138     default:
139       break;
140   }
141 }
142
143 void PartSet_OperationCreateConstraint::mouseMoved(QMouseEvent* theEvent, Handle(V3d_View) theView)
144 {
145   // changed
146   switch (myPointSelectionMode)
147   {
148     //case SM_FirstPoint:
149     case SM_SecondPoint:
150     //case SM_ThirdPoint:
151     {
152       double aX, anY;
153       gp_Pnt aPoint = PartSet_Tools::convertClickToPoint(theEvent->pos(), theView);
154       PartSet_Tools::convertTo2D(aPoint, sketch(), theView, aX, anY);
155       if (feature()->getKind() == PartSet_ConstraintRadiusPrs::getKind()) {
156         boost::shared_ptr<PartSet_ConstraintRadiusPrs> anArcPrs =
157                                 boost::dynamic_pointer_cast<PartSet_ConstraintRadiusPrs>(myFeaturePrs);
158         if (anArcPrs) {
159           anArcPrs->projectPointOnFeature(feature(), sketch(), aPoint, theView, aX, anY);
160         }
161       }
162       myFeaturePrs->setPoint(aX, anY, myPointSelectionMode);
163
164       flushUpdated();
165       emit focusActivated(myFeaturePrs->getAttribute(myPointSelectionMode));
166     }
167     break;
168     case SM_DonePoint:
169     {
170       commit();
171       restartOperation(feature()->getKind(), feature());
172     }
173     default:
174       break;
175   }
176 }
177
178 void PartSet_OperationCreateConstraint::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() /*&& aKeyType == Qt::Key_Return*/)
183   {
184     setPointSelectionMode(myFeaturePrs->getNextMode(theName), false);
185   }
186   keyReleased(theEvent->key());
187 }
188
189 void PartSet_OperationCreateConstraint::keyReleased(const int theKey)
190 {
191   switch (theKey) {
192     case Qt::Key_Return: {
193       if (myPointSelectionMode == SM_DonePoint)
194       {
195         commit();
196         // it start a new line creation at a free point
197         restartOperation(feature()->getKind(), FeaturePtr()/*feature()*/);
198       }
199       // changed
200       // the modification is really need until the focus of editor do not accept the focus
201       if (myPointSelectionMode == SM_ThirdPoint) {
202         if (myEditor->isStarted())
203           myEditor->stop();
204         commit();
205         // it start a new line creation at a free point
206         restartOperation(feature()->getKind(), FeaturePtr()/*feature()*/);
207       }
208     }
209     break;
210     case Qt::Key_Escape: {
211       if (myEditor->isStarted())
212         myEditor->stop();
213       if (myPointSelectionMode == SM_DonePoint)
214       {
215         commit();
216       }
217       else
218       {
219         abort();
220       }
221     }
222     default:
223     break;
224   }
225 }
226
227 void PartSet_OperationCreateConstraint::startOperation()
228 {
229   PartSet_OperationSketchBase::startOperation();
230   setPointSelectionMode(!myInitFeature ? SM_FirstPoint : SM_SecondPoint);
231
232   emit multiSelectionEnabled(false);
233 }
234
235 void PartSet_OperationCreateConstraint::abortOperation()
236 {
237   emit featureConstructed(feature(), FM_Hide);
238   PartSet_OperationSketchBase::abortOperation();
239 }
240
241 void PartSet_OperationCreateConstraint::stopOperation()
242 {
243   PartSet_OperationSketchBase::stopOperation();
244   emit multiSelectionEnabled(true);
245 }
246
247 void PartSet_OperationCreateConstraint::afterCommitOperation()
248 {
249   PartSet_OperationSketchBase::afterCommitOperation();  
250   emit featureConstructed(feature(), FM_Deactivation);
251 }
252
253 FeaturePtr PartSet_OperationCreateConstraint::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   if (myInitFeature)
264     myFeaturePrs->setFeature(myInitFeature, SM_FirstPoint);
265
266   emit featureConstructed(aNewFeature, FM_Activation);
267   if (theFlushMessage)
268     flushCreated();
269   return aNewFeature;
270 }
271
272 void PartSet_OperationCreateConstraint::setPointSelectionMode(const PartSet_SelectionMode& theMode,
273                                                               const bool isToEmitSignal)
274 {
275   myPointSelectionMode = theMode;
276   if (isToEmitSignal) {
277     std::string aName = myFeaturePrs->getAttribute(theMode);
278     if (aName.empty() && theMode == SM_DonePoint) {
279       aName = XGUI::PROP_PANEL_OK;
280     }
281     emit focusActivated(aName);
282   }
283 }
284
285 void PartSet_OperationCreateConstraint::showEditor(QMouseEvent* theEvent, double theValue)
286 {
287   // changed
288   QPoint aPos = theEvent->globalPos();
289   myEditor->start(aPos, theValue);
290 }
291
292 void PartSet_OperationCreateConstraint::onEditStopped(double theValue)
293 {
294   PartSet_Tools::setFeatureValue(feature(), theValue, CONSTRAINT_ATTR_VALUE);
295
296   flushUpdated();
297   commit();
298   restartOperation(feature()->getKind(), FeaturePtr());
299 }