]> 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
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   // changed
57   return 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   // changed
83   if (!theFeature/* || theFeature->getKind() != SKETCH_LINE_KIND*/)
84     return;
85   myInitFeature = theFeature;
86 }
87
88 FeaturePtr PartSet_OperationCreateConstraint::sketch() const
89 {
90   return myFeaturePrs->sketch();
91 }
92
93 void PartSet_OperationCreateConstraint::mouseReleased(QMouseEvent* theEvent, Handle(V3d_View) theView,
94                                                 const std::list<XGUI_ViewerPrs>& theSelected,
95                                                 const std::list<XGUI_ViewerPrs>& /*theHighlighted*/)
96 {
97   switch (myPointSelectionMode)
98   {
99     case SM_FirstPoint: {
100       if (!theSelected.empty()) {
101         XGUI_ViewerPrs aPrs = theSelected.front();
102         FeaturePtr aFeature = aPrs.feature();
103
104         myFeaturePrs->init(feature(), aFeature);
105         flushUpdated();
106         setPointSelectionMode(SM_SecondPoint);
107       }
108     }
109     break;
110     case SM_SecondPoint: {
111       double aX, anY;
112       gp_Pnt aPoint = PartSet_Tools::convertClickToPoint(theEvent->pos(), theView);
113       PartSet_Tools::convertTo2D(aPoint, sketch(), theView, aX, anY);
114
115       PartSet_SelectionMode aMode = myFeaturePrs->setPoint(aX, anY, myPointSelectionMode);
116       flushUpdated();
117       // show value edit dialog
118       //setPointSelectionMode(aMode);
119       commit();
120       restartOperation(feature()->getKind(), FeaturePtr());
121     }
122     break;
123     default:
124       break;
125   }
126 }
127
128 void PartSet_OperationCreateConstraint::mouseMoved(QMouseEvent* theEvent, Handle(V3d_View) theView)
129 {
130   // changed
131   switch (myPointSelectionMode)
132   {
133     //case SM_FirstPoint:
134     case SM_SecondPoint:
135     //case SM_ThirdPoint:
136     {
137       double aX, anY;
138       gp_Pnt aPoint = PartSet_Tools::convertClickToPoint(theEvent->pos(), theView);
139       PartSet_Tools::convertTo2D(aPoint, sketch(), theView, aX, anY);
140       /*if (myPointSelectionMode == SM_ThirdPoint) {
141         if (feature()->getKind() == SKETCH_ARC_KIND) {
142           boost::shared_ptr<PartSet_FeatureArcPrs> anArcPrs =
143                                  boost::dynamic_pointer_cast<PartSet_FeatureArcPrs>(myFeaturePrs);
144           if (anArcPrs) {
145             anArcPrs->projectPointOnArc(aPoint, theView, aX, anY);
146           }
147         }
148       }*/
149       myFeaturePrs->setPoint(aX, anY, myPointSelectionMode);
150
151       flushUpdated();
152       emit focusActivated(myFeaturePrs->getAttribute(myPointSelectionMode));
153     }
154     break;
155     case SM_DonePoint:
156     {
157       commit();
158       restartOperation(feature()->getKind(), feature());
159     }
160     default:
161       break;
162   }
163 }
164
165 void PartSet_OperationCreateConstraint::keyReleased(std::string theName, QKeyEvent* theEvent)
166 {
167   int aKeyType = theEvent->key();
168   // the second point should be activated by any modification in the property panel
169   if (!theName.empty() /*&& aKeyType == Qt::Key_Return*/)
170   {
171     setPointSelectionMode(myFeaturePrs->getNextMode(theName), false);
172   }
173   keyReleased(theEvent->key());
174 }
175
176 void PartSet_OperationCreateConstraint::keyReleased(const int theKey)
177 {
178   switch (theKey) {
179     case Qt::Key_Return: {
180       if (myPointSelectionMode == SM_DonePoint)
181       {
182         commit();
183         // it start a new line creation at a free point
184         restartOperation(feature()->getKind(), FeaturePtr()/*feature()*/);
185       }
186       //else
187       //  abort();
188       //restartOperation(feature()->getKind(), FeaturePtr());
189     }
190     break;
191     case Qt::Key_Escape: {
192       if (myPointSelectionMode == SM_DonePoint)
193       {
194         commit();
195       }
196       else
197       {
198         abort();
199       }
200     }
201     default:
202     break;
203   }
204 }
205
206 void PartSet_OperationCreateConstraint::startOperation()
207 {
208   PartSet_OperationSketchBase::startOperation();
209   setPointSelectionMode(!myInitFeature ? SM_FirstPoint : SM_SecondPoint);
210
211   emit multiSelectionEnabled(false);
212 }
213
214 void PartSet_OperationCreateConstraint::abortOperation()
215 {
216   emit featureConstructed(feature(), FM_Hide);
217   PartSet_OperationSketchBase::abortOperation();
218 }
219
220 void PartSet_OperationCreateConstraint::stopOperation()
221 {
222   PartSet_OperationSketchBase::stopOperation();
223   emit multiSelectionEnabled(true);
224 }
225
226 void PartSet_OperationCreateConstraint::afterCommitOperation()
227 {
228   PartSet_OperationSketchBase::afterCommitOperation();  
229   emit featureConstructed(feature(), FM_Deactivation);
230 }
231
232 FeaturePtr PartSet_OperationCreateConstraint::createFeature(const bool theFlushMessage)
233 {
234   FeaturePtr aNewFeature = ModuleBase_Operation::createFeature(false);
235   if (sketch()) {
236     boost::shared_ptr<SketchPlugin_Feature> aFeature = 
237                            boost::dynamic_pointer_cast<SketchPlugin_Feature>(sketch());
238
239     aFeature->addSub(aNewFeature);
240   }
241   myFeaturePrs->init(aNewFeature, myInitFeature);
242
243   emit featureConstructed(aNewFeature, FM_Activation);
244   if (theFlushMessage)
245     flushCreated();
246   return aNewFeature;
247 }
248
249 void PartSet_OperationCreateConstraint::setPointSelectionMode(const PartSet_SelectionMode& theMode,
250                                                            const bool isToEmitSignal)
251 {
252   myPointSelectionMode = theMode;
253   if (isToEmitSignal) {
254     std::string aName = myFeaturePrs->getAttribute(theMode);
255     if (aName.empty() && theMode == SM_DonePoint) {
256       aName = XGUI::PROP_PANEL_OK;
257     }
258     emit focusActivated(aName);
259   }
260 }