]> 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_FeatureLengthPrs.h>
23 #include <PartSet_FeatureDistancePrs.h>
24 #include <PartSet_FeatureRadiusPrs.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_FeatureLengthPrs::getKind() ||
68          theId == PartSet_FeatureDistancePrs::getKind() ||
69          theId == PartSet_FeatureRadiusPrs::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       double aValue;
131       if (PartSet_Tools::featureValue(feature(), CONSTRAINT_ATTR_VALUE, aValue)) {
132         showEditor(theEvent, aValue);
133         setPointSelectionMode(SM_ThirdPoint/*aMode*/);
134       }
135     }
136     break;
137     default:
138       break;
139   }
140 }
141
142 void PartSet_OperationCreateConstraint::mouseMoved(QMouseEvent* theEvent, Handle(V3d_View) theView)
143 {
144   // changed
145   switch (myPointSelectionMode)
146   {
147     //case SM_FirstPoint:
148     case SM_SecondPoint:
149     //case SM_ThirdPoint:
150     {
151       double aX, anY;
152       gp_Pnt aPoint = PartSet_Tools::convertClickToPoint(theEvent->pos(), theView);
153       PartSet_Tools::convertTo2D(aPoint, sketch(), theView, aX, anY);
154       /*if (myPointSelectionMode == SM_ThirdPoint) {
155         if (feature()->getKind() == SKETCH_ARC_KIND) {
156           boost::shared_ptr<PartSet_FeatureArcPrs> anArcPrs =
157                                  boost::dynamic_pointer_cast<PartSet_FeatureArcPrs>(myFeaturePrs);
158           if (anArcPrs) {
159             anArcPrs->projectPointOnArc(aPoint, theView, aX, anY);
160           }
161         }
162       }*/
163       myFeaturePrs->setPoint(aX, anY, myPointSelectionMode);
164
165       flushUpdated();
166       emit focusActivated(myFeaturePrs->getAttribute(myPointSelectionMode));
167     }
168     break;
169     case SM_DonePoint:
170     {
171       commit();
172       restartOperation(feature()->getKind(), feature());
173     }
174     default:
175       break;
176   }
177 }
178
179 void PartSet_OperationCreateConstraint::keyReleased(std::string theName, QKeyEvent* theEvent)
180 {
181   int aKeyType = theEvent->key();
182   // the second point should be activated by any modification in the property panel
183   if (!theName.empty() /*&& aKeyType == Qt::Key_Return*/)
184   {
185     setPointSelectionMode(myFeaturePrs->getNextMode(theName), false);
186   }
187   keyReleased(theEvent->key());
188 }
189
190 void PartSet_OperationCreateConstraint::keyReleased(const int theKey)
191 {
192   switch (theKey) {
193     case Qt::Key_Return: {
194       if (myPointSelectionMode == SM_DonePoint)
195       {
196         commit();
197         // it start a new line creation at a free point
198         restartOperation(feature()->getKind(), FeaturePtr()/*feature()*/);
199       }
200       // changed
201       // the modification is really need until the focus of editor do not accept the focus
202       if (myPointSelectionMode == SM_ThirdPoint) {
203         if (myEditor->isStarted())
204           myEditor->stop();
205         commit();
206         // it start a new line creation at a free point
207         restartOperation(feature()->getKind(), FeaturePtr()/*feature()*/);
208       }
209     }
210     break;
211     case Qt::Key_Escape: {
212       if (myEditor->isStarted())
213         myEditor->stop();
214       if (myPointSelectionMode == SM_DonePoint)
215       {
216         commit();
217       }
218       else
219       {
220         abort();
221       }
222     }
223     default:
224     break;
225   }
226 }
227
228 void PartSet_OperationCreateConstraint::startOperation()
229 {
230   PartSet_OperationSketchBase::startOperation();
231   setPointSelectionMode(!myInitFeature ? SM_FirstPoint : SM_SecondPoint);
232
233   emit multiSelectionEnabled(false);
234 }
235
236 void PartSet_OperationCreateConstraint::abortOperation()
237 {
238   emit featureConstructed(feature(), FM_Hide);
239   PartSet_OperationSketchBase::abortOperation();
240 }
241
242 void PartSet_OperationCreateConstraint::stopOperation()
243 {
244   PartSet_OperationSketchBase::stopOperation();
245   emit multiSelectionEnabled(true);
246 }
247
248 void PartSet_OperationCreateConstraint::afterCommitOperation()
249 {
250   PartSet_OperationSketchBase::afterCommitOperation();  
251   emit featureConstructed(feature(), FM_Deactivation);
252 }
253
254 FeaturePtr PartSet_OperationCreateConstraint::createFeature(const bool theFlushMessage)
255 {
256   FeaturePtr aNewFeature = ModuleBase_Operation::createFeature(false);
257   if (sketch()) {
258     boost::shared_ptr<SketchPlugin_Feature> aFeature = 
259                            boost::dynamic_pointer_cast<SketchPlugin_Feature>(sketch());
260
261     aFeature->addSub(aNewFeature);
262   }
263   myFeaturePrs->init(aNewFeature);
264   if (myInitFeature)
265     myFeaturePrs->setFeature(myInitFeature, SM_FirstPoint);
266
267   emit featureConstructed(aNewFeature, FM_Activation);
268   if (theFlushMessage)
269     flushCreated();
270   return aNewFeature;
271 }
272
273 void PartSet_OperationCreateConstraint::setPointSelectionMode(const PartSet_SelectionMode& theMode,
274                                                               const bool isToEmitSignal)
275 {
276   myPointSelectionMode = theMode;
277   if (isToEmitSignal) {
278     std::string aName = myFeaturePrs->getAttribute(theMode);
279     if (aName.empty() && theMode == SM_DonePoint) {
280       aName = XGUI::PROP_PANEL_OK;
281     }
282     emit focusActivated(aName);
283   }
284 }
285
286 void PartSet_OperationCreateConstraint::showEditor(QMouseEvent* theEvent, double theValue)
287 {
288   // changed
289   QPoint aPos = theEvent->globalPos();
290   myEditor->start(aPos, theValue);
291 }
292
293 void PartSet_OperationCreateConstraint::onEditStopped(double theValue)
294 {
295   PartSet_Tools::setFeatureValue(feature(), theValue, CONSTRAINT_ATTR_VALUE);
296
297   flushUpdated();
298   commit();
299   restartOperation(feature()->getKind(), FeaturePtr());
300 }