]> SALOME platform Git repositories - modules/shaper.git/blob - src/PartSet/PartSet_OperationFeatureCreate.cpp
Salome HOME
Sources formated according to the codeing standards
[modules/shaper.git] / src / PartSet / PartSet_OperationFeatureCreate.cpp
1 // File:        PartSet_OperationFeatureCreate.h
2 // Created:     20 Apr 2014
3 // Author:      Natalia ERMOLAEVA
4
5 #include <PartSet_OperationFeatureCreate.h>
6
7 #include <PartSet_Tools.h>
8 #include <PartSet_OperationSketch.h>
9
10 #include <SketchPlugin_Feature.h>
11 #include <SketchPlugin_Point.h>
12 #include <SketchPlugin_Line.h>
13 #include <SketchPlugin_Circle.h>
14 #include <SketchPlugin_Arc.h>
15 #include <SketchPlugin_ConstraintDistance.h>
16 #include <SketchPlugin_ConstraintLength.h>
17 #include <SketchPlugin_ConstraintRadius.h>
18 #include <SketchPlugin_ConstraintParallel.h>
19 #include <SketchPlugin_ConstraintPerpendicular.h>
20 #include <SketchPlugin_ConstraintCoincidence.h>
21
22 #include <GeomAPI_Pnt2d.h>
23
24 #include <ModuleBase_OperationDescription.h>
25 #include <ModuleBase_WidgetPoint2D.h>
26 #include <ModuleBase_WidgetValueFeature.h>
27 #include <ModuleBase_ViewerPrs.h>
28
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_OperationFeatureCreate::PartSet_OperationFeatureCreate(const QString& theId,
45                                                                QObject* theParent,
46                                                                FeaturePtr theFeature)
47     : PartSet_OperationSketchBase(theId, theParent),
48       mySketch(theFeature),
49       myActiveWidget(0)
50 {
51 }
52
53 PartSet_OperationFeatureCreate::~PartSet_OperationFeatureCreate()
54 {
55 }
56
57 bool PartSet_OperationFeatureCreate::canProcessKind(const std::string& theId)
58 {
59   return theId == SketchPlugin_Line::ID() || theId == SketchPlugin_Point::ID()
60       || theId == SketchPlugin_Circle::ID() || theId == SketchPlugin_Arc::ID()
61       || theId == SketchPlugin_ConstraintDistance::ID()
62       || theId == SketchPlugin_ConstraintLength::ID()
63       || theId == SketchPlugin_ConstraintRadius::ID()
64       || theId == SketchPlugin_ConstraintParallel::ID()
65       || theId == SketchPlugin_ConstraintPerpendicular::ID()
66       || theId == SketchPlugin_ConstraintCoincidence::ID();
67 }
68
69 bool PartSet_OperationFeatureCreate::canBeCommitted() const
70 {
71   if (PartSet_OperationSketchBase::canBeCommitted())
72     return !myActiveWidget;
73   return false;
74 }
75
76 bool PartSet_OperationFeatureCreate::isGranted(ModuleBase_IOperation* theOperation) const
77 {
78   return theOperation->getDescription()->operationId().toStdString()
79       == PartSet_OperationSketch::Type();
80 }
81
82 std::list<int> PartSet_OperationFeatureCreate::getSelectionModes(ObjectPtr 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_OperationFeatureCreate::initSelection(
91     const std::list<ModuleBase_ViewerPrs>& theSelected,
92     const std::list<ModuleBase_ViewerPrs>& /*theHighlighted*/)
93 {
94   myPreSelection = theSelected;
95 }
96
97 void PartSet_OperationFeatureCreate::initFeature(FeaturePtr theFeature)
98 {
99   myInitFeature = theFeature;
100 }
101
102 FeaturePtr PartSet_OperationFeatureCreate::sketch() const
103 {
104   return mySketch;
105 }
106
107 void PartSet_OperationFeatureCreate::mouseReleased(
108     QMouseEvent* theEvent, Handle(V3d_View) theView,
109     const std::list<ModuleBase_ViewerPrs>& theSelected,
110     const std::list<ModuleBase_ViewerPrs>& /*theHighlighted*/)
111 {
112   if (commit()) {
113     // if the point creation is finished, the next mouse release should commit the modification
114     // the next release can happens by double click in the viewer
115     restartOperation(feature()->getKind(), feature());
116     return;
117   }
118
119   gp_Pnt aPoint = PartSet_Tools::convertClickToPoint(theEvent->pos(), theView);
120   double aX = aPoint.X(), anY = aPoint.Y();
121
122   if (theSelected.empty()) {
123     PartSet_Tools::convertTo2D(aPoint, sketch(), theView, aX, anY);
124   } else {
125     ModuleBase_ViewerPrs aPrs = theSelected.front();
126     const TopoDS_Shape& aShape = aPrs.shape();
127     if (!aShape.IsNull())  // the point is selected
128     {
129       if (aShape.ShapeType() == TopAbs_VERTEX) {
130         const TopoDS_Vertex& aVertex = TopoDS::Vertex(aShape);
131         if (!aVertex.IsNull()) {
132           aPoint = BRep_Tool::Pnt(aVertex);
133           PartSet_Tools::convertTo2D(aPoint, sketch(), theView, aX, anY);
134
135           PartSet_Tools::setConstraints(sketch(), feature(), myActiveWidget->attributeID(), aX,
136                                         anY);
137         }
138       } else if (aShape.ShapeType() == TopAbs_EDGE)  // the line is selected
139           {
140         PartSet_Tools::convertTo2D(aPoint, sketch(), theView, aX, anY);
141         // move to selected line
142         if (feature()->getKind() == SketchPlugin_Line::ID()) {
143           //FeaturePtr aFeature = aPrs.feature();
144           //projectPointOnLine(aFeature, myPointSelectionMode, aPoint, theView, aX, anY);
145         }
146       }
147     }
148   }
149   ObjectPtr aFeature;
150   if (!theSelected.empty()) {
151     ModuleBase_ViewerPrs aPrs = theSelected.front();
152     aFeature = aPrs.object();
153   } else
154     aFeature = feature();  // for the widget distance only
155
156   bool isApplyed = setWidgetValue(aFeature, aX, anY);
157   if (isApplyed) {
158     flushUpdated();
159     emit activateNextWidget(myActiveWidget);
160   }
161 }
162
163 void PartSet_OperationFeatureCreate::mouseMoved(QMouseEvent* theEvent, Handle(V3d_View) theView)
164 {
165   if (commit()) {
166     restartOperation(feature()->getKind(), feature());
167   } else {
168     double aX, anY;
169     gp_Pnt aPoint = PartSet_Tools::convertClickToPoint(theEvent->pos(), theView);
170     PartSet_Tools::convertTo2D(aPoint, sketch(), theView, aX, anY);
171     setWidgetValue(feature(), aX, anY);
172     flushUpdated();
173   }
174 }
175
176 void PartSet_OperationFeatureCreate::keyReleased(std::string theName, QKeyEvent* theEvent)
177 {
178   int aKeyType = theEvent->key();
179   // the second point should be activated by any modification in the property panel
180   if (!theName.empty()) {
181     //setPointSelectionMode(myFeaturePrs->getNextMode(theName), false);
182   }
183   keyReleased(theEvent->key());
184 }
185
186 void PartSet_OperationFeatureCreate::onWidgetActivated(ModuleBase_ModelWidget* theWidget)
187 {
188   myActiveWidget = theWidget;
189   if ((myPreSelection.size() > 0) && myActiveWidget) {
190     const ModuleBase_ViewerPrs& aPrs = myPreSelection.front();
191     ModuleBase_WidgetValueFeature aValue;
192     aValue.setObject(aPrs.object());
193     if (myActiveWidget->setValue(&aValue)) {
194       myPreSelection.remove(aPrs);
195       emit activateNextWidget(myActiveWidget);
196     }
197   }
198   if (myInitFeature && myActiveWidget) {
199     ModuleBase_WidgetPoint2D* aWgt = dynamic_cast<ModuleBase_WidgetPoint2D*>(myActiveWidget);
200     if (aWgt && aWgt->initFromPrevious(myInitFeature)) {
201       myInitFeature = FeaturePtr();
202       emit activateNextWidget(myActiveWidget);
203     }
204   }
205 }
206
207 void PartSet_OperationFeatureCreate::keyReleased(const int theKey)
208 {
209   switch (theKey) {
210     case Qt::Key_Return: {
211       if (commit()) {
212         // it start a new line creation at a free point
213         restartOperation(feature()->getKind(), FeaturePtr());
214       }
215     }
216       break;
217     case Qt::Key_Escape: {
218       if (!commit()) {
219         abort();
220       }
221     }
222     default:
223       break;
224   }
225 }
226
227 void PartSet_OperationFeatureCreate::startOperation()
228 {
229   PartSet_OperationSketchBase::startOperation();
230   //setPointSelectionMode(!myInitFeature ? SM_FirstPoint : SM_SecondPoint);
231
232   emit multiSelectionEnabled(false);
233 }
234
235 void PartSet_OperationFeatureCreate::abortOperation()
236 {
237   emit featureConstructed(feature(), FM_Hide);
238   PartSet_OperationSketchBase::abortOperation();
239 }
240
241 void PartSet_OperationFeatureCreate::stopOperation()
242 {
243   PartSet_OperationSketchBase::stopOperation();
244   emit multiSelectionEnabled(true);
245 }
246
247 void PartSet_OperationFeatureCreate::afterCommitOperation()
248 {
249   PartSet_OperationSketchBase::afterCommitOperation();
250   emit featureConstructed(feature(), FM_Deactivation);
251 }
252
253 FeaturePtr PartSet_OperationFeatureCreate::createFeature(const bool theFlushMessage)
254 {
255   FeaturePtr aNewFeature = ModuleBase_Operation::createFeature(false);
256   if (sketch()) {
257     boost::shared_ptr<SketchPlugin_Feature> aFeature = boost::dynamic_pointer_cast<
258         SketchPlugin_Feature>(sketch());
259
260     aFeature->addSub(aNewFeature);
261   }
262   //myFeaturePrs->init(aNewFeature);
263   //myFeaturePrs->setFeature(myInitFeature, SM_FirstPoint);
264
265 //TODO  emit featureConstructed(aNewFeature, FM_Activation);
266   if (theFlushMessage)
267     flushCreated();
268   return aNewFeature;
269 }
270
271 bool PartSet_OperationFeatureCreate::setWidgetValue(ObjectPtr theFeature, double theX, double theY)
272 {
273   if (!myActiveWidget)
274     return false;
275   ModuleBase_WidgetValueFeature* aValue = new ModuleBase_WidgetValueFeature();
276   aValue->setObject(theFeature);
277   aValue->setPoint(boost::shared_ptr<GeomAPI_Pnt2d>(new GeomAPI_Pnt2d(theX, theY)));
278   bool isApplyed = myActiveWidget->setValue(aValue);
279
280   delete aValue;
281   myIsModified = (myIsModified || isApplyed);
282   return isApplyed;
283 }