]> SALOME platform Git repositories - modules/shaper.git/blob - src/PartSet/PartSet_OperationCreateConstraint.cpp
Salome HOME
a5f54997e252171af91fd78de1d0d90930497737
[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       if (!theSelected.empty()) {
109         XGUI_ViewerPrs aPrs = theSelected.front();
110         FeaturePtr aFeature = aPrs.feature();
111
112         if (myFeaturePrs->setFeature(aFeature, myPointSelectionMode)) {
113           flushUpdated();
114           setPointSelectionMode(SM_SecondPoint);
115         }
116       }
117     }
118     break;
119     case SM_SecondPoint: {
120       double aX, anY;
121       gp_Pnt aPoint = PartSet_Tools::convertClickToPoint(theEvent->pos(), theView);
122       PartSet_Tools::convertTo2D(aPoint, sketch(), theView, aX, anY);
123
124       PartSet_SelectionMode aMode = myFeaturePrs->setPoint(aX, anY, myPointSelectionMode);
125       // show value edit dialog
126       bool isValid;
127       double aValue = PartSet_Tools::featureValue(feature(), CONSTRAINT_ATTR_VALUE, isValid);
128       if (isValid) {
129         showEditor(theEvent, aValue);
130         setPointSelectionMode(SM_ThirdPoint/*aMode*/);
131       }
132     }
133     break;
134     default:
135       break;
136   }
137 }
138
139 void PartSet_OperationCreateConstraint::mouseMoved(QMouseEvent* theEvent, Handle(V3d_View) theView)
140 {
141   // changed
142   switch (myPointSelectionMode)
143   {
144     //case SM_FirstPoint:
145     case SM_SecondPoint:
146     //case SM_ThirdPoint:
147     {
148       double aX, anY;
149       gp_Pnt aPoint = PartSet_Tools::convertClickToPoint(theEvent->pos(), theView);
150       PartSet_Tools::convertTo2D(aPoint, sketch(), theView, aX, anY);
151       if (feature()->getKind() == PartSet_ConstraintRadiusPrs::getKind()) {
152         boost::shared_ptr<PartSet_ConstraintRadiusPrs> anArcPrs =
153                                 boost::dynamic_pointer_cast<PartSet_ConstraintRadiusPrs>(myFeaturePrs);
154         if (anArcPrs) {
155           anArcPrs->projectPointOnFeature(feature(), sketch(), aPoint, theView, aX, anY);
156         }
157       }
158       myFeaturePrs->setPoint(aX, anY, myPointSelectionMode);
159
160       flushUpdated();
161       emit focusActivated(myFeaturePrs->getAttribute(myPointSelectionMode));
162     }
163     break;
164     case SM_DonePoint:
165     {
166       commit();
167       restartOperation(feature()->getKind(), feature());
168     }
169     default:
170       break;
171   }
172 }
173
174 void PartSet_OperationCreateConstraint::keyReleased(std::string theName, QKeyEvent* theEvent)
175 {
176   int aKeyType = theEvent->key();
177   // the second point should be activated by any modification in the property panel
178   if (!theName.empty() /*&& aKeyType == Qt::Key_Return*/)
179   {
180     setPointSelectionMode(myFeaturePrs->getNextMode(theName), false);
181   }
182   keyReleased(theEvent->key());
183 }
184
185 void PartSet_OperationCreateConstraint::keyReleased(const int theKey)
186 {
187   switch (theKey) {
188     case Qt::Key_Return: {
189       if (myPointSelectionMode == SM_DonePoint)
190       {
191         commit();
192         // it start a new line creation at a free point
193         restartOperation(feature()->getKind(), FeaturePtr()/*feature()*/);
194       }
195       // changed
196       // the modification is really need until the focus of editor do not accept the focus
197       if (myPointSelectionMode == SM_ThirdPoint) {
198         if (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     myFeaturePrs->setFeature(myInitFeature, SM_FirstPoint);
261
262   emit featureConstructed(aNewFeature, FM_Activation);
263   if (theFlushMessage)
264     flushCreated();
265   return aNewFeature;
266 }
267
268 void PartSet_OperationCreateConstraint::setPointSelectionMode(const PartSet_SelectionMode& theMode,
269                                                               const bool isToEmitSignal)
270 {
271   myPointSelectionMode = theMode;
272   if (isToEmitSignal) {
273     std::string aName = myFeaturePrs->getAttribute(theMode);
274     if (aName.empty() && theMode == SM_DonePoint) {
275       aName = XGUI::PROP_PANEL_OK;
276     }
277     emit focusActivated(aName);
278   }
279 }
280
281 void PartSet_OperationCreateConstraint::showEditor(QMouseEvent* theEvent, double theValue)
282 {
283   // changed
284   QPoint aPos = theEvent->globalPos();
285   myEditor->start(aPos, theValue);
286 }
287
288 void PartSet_OperationCreateConstraint::onEditStopped(double theValue)
289 {
290   PartSet_Tools::setFeatureValue(feature(), theValue, CONSTRAINT_ATTR_VALUE);
291
292   flushUpdated();
293   commit();
294   restartOperation(feature()->getKind(), FeaturePtr());
295 }