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