]> SALOME platform Git repositories - modules/shaper.git/blob - src/PartSet/PartSet_OperationFeatureCreate.cpp
Salome HOME
Merge branch 'master' of newgeom:newgeom
[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 #include <iostream>
39 #endif
40
41 #include <QMouseEvent>
42
43 using namespace std;
44
45 PartSet_OperationFeatureCreate::PartSet_OperationFeatureCreate(const QString& theId,
46                                                                QObject* theParent,
47                                                                FeaturePtr theFeature)
48     : PartSet_OperationFeatureBase(theId, theParent, theFeature)
49 {
50 }
51
52 PartSet_OperationFeatureCreate::~PartSet_OperationFeatureCreate()
53 {
54 }
55
56 bool PartSet_OperationFeatureCreate::canProcessKind(const std::string& theId)
57 {
58   return theId == SketchPlugin_Line::ID() || theId == SketchPlugin_Point::ID()
59       || theId == SketchPlugin_Circle::ID() || theId == SketchPlugin_Arc::ID()
60       || theId == SketchPlugin_ConstraintDistance::ID()
61       || theId == SketchPlugin_ConstraintLength::ID()
62       || theId == SketchPlugin_ConstraintRadius::ID()
63       || theId == SketchPlugin_ConstraintParallel::ID()
64       || theId == SketchPlugin_ConstraintPerpendicular::ID()
65       || theId == SketchPlugin_ConstraintCoincidence::ID();
66 }
67
68 bool PartSet_OperationFeatureCreate::canBeCommitted() const
69 {
70   if (PartSet_OperationSketchBase::canBeCommitted())
71     return !myActiveWidget;
72   return false;
73 }
74
75 std::list<int> PartSet_OperationFeatureCreate::getSelectionModes(ObjectPtr theFeature) const
76 {
77   std::list<int> aModes;
78   if (theFeature != feature())
79     aModes = PartSet_OperationSketchBase::getSelectionModes(theFeature);
80   return aModes;
81 }
82
83 void PartSet_OperationFeatureCreate::mouseMoved(QMouseEvent* theEvent, Handle(V3d_View) theView)
84 {
85     double aX, anY;
86     gp_Pnt aPoint = PartSet_Tools::convertClickToPoint(theEvent->pos(), theView);
87     PartSet_Tools::convertTo2D(aPoint, sketch(), theView, aX, anY);
88     setWidgetValue(feature(), aX, anY);
89     flushUpdated();
90 }
91
92 void PartSet_OperationFeatureCreate::keyReleased(const int theKey)
93 {
94   switch (theKey) {
95     case Qt::Key_Return:
96     case Qt::Key_Enter: {
97         // it start a new line creation at a free point
98       if(isValid())
99         restartOperation(feature()->getKind());
100     }
101       break;
102     default:
103       break;
104   }
105 }
106
107 void PartSet_OperationFeatureCreate::mouseReleased(QMouseEvent* theEvent, Handle(V3d_View) theView,
108                                                  const std::list<ModuleBase_ViewerPrs>& theSelected,
109                                                  const std::list<ModuleBase_ViewerPrs>& /*theHighlighted*/)
110 {
111   gp_Pnt aPoint = PartSet_Tools::convertClickToPoint(theEvent->pos(), theView);
112   double aX = aPoint.X(), anY = aPoint.Y();
113   bool isClosedContour = false;
114
115   if (theSelected.empty()) {
116     PartSet_Tools::convertTo2D(aPoint, sketch(), theView, aX, anY);
117   } else {
118     ModuleBase_ViewerPrs aPrs = theSelected.front();
119     const TopoDS_Shape& aShape = aPrs.shape();
120     if (!aShape.IsNull()) {
121       if (aShape.ShapeType() == TopAbs_VERTEX) { // a point is selected
122         const TopoDS_Vertex& aVertex = TopoDS::Vertex(aShape);
123         if (!aVertex.IsNull()) {
124           aPoint = BRep_Tool::Pnt(aVertex);
125           PartSet_Tools::convertTo2D(aPoint, sketch(), theView, aX, anY);
126           PartSet_Tools::setConstraints(sketch(), feature(), myActiveWidget->attributeID(), aX, anY);
127           isClosedContour = true;
128         }
129       } else if (aShape.ShapeType() == TopAbs_EDGE) { // a line is selected
130         PartSet_Tools::convertTo2D(aPoint, sketch(), theView, aX, anY);
131       }
132     }
133   }
134   ObjectPtr aFeature;
135   if (!theSelected.empty()) {
136     ModuleBase_ViewerPrs aPrs = theSelected.front();
137     aFeature = aPrs.object();
138   } else {
139     aFeature = feature();  // for the widget distance only
140   }
141
142   bool isApplyed = setWidgetValue(aFeature, aX, anY);
143   if (isApplyed) {
144     flushUpdated();
145     emit activateNextWidget(myActiveWidget);
146   }
147
148   if (commit() && !isClosedContour) {
149     // if the point creation is finished, the next mouse release should commit the modification
150     // the next release can happens by double click in the viewer
151     restartOperation(feature()->getKind(), feature());
152     return;
153   }
154 }
155
156 void PartSet_OperationFeatureCreate::activateNextToCurrentWidget()
157 {
158   emit activateNextWidget(myActiveWidget);
159 }
160
161 void PartSet_OperationFeatureCreate::startOperation()
162 {
163   PartSet_OperationSketchBase::startOperation();
164   emit multiSelectionEnabled(false);
165 }
166
167 void PartSet_OperationFeatureCreate::abortOperation()
168 {
169   emit featureConstructed(feature(), FM_Hide);
170   PartSet_OperationSketchBase::abortOperation();
171 }
172
173 void PartSet_OperationFeatureCreate::stopOperation()
174 {
175   PartSet_OperationSketchBase::stopOperation();
176   emit multiSelectionEnabled(true);
177 }
178
179 void PartSet_OperationFeatureCreate::afterCommitOperation()
180 {
181   PartSet_OperationSketchBase::afterCommitOperation();
182   emit featureConstructed(feature(), FM_Deactivation);
183 }
184
185 FeaturePtr PartSet_OperationFeatureCreate::createFeature(const bool theFlushMessage)
186 {
187   FeaturePtr aNewFeature = ModuleBase_Operation::createFeature(false);
188   if (sketch()) {
189     boost::shared_ptr<SketchPlugin_Feature> aFeature = boost::dynamic_pointer_cast<
190         SketchPlugin_Feature>(sketch());
191
192     aFeature->addSub(aNewFeature);
193   }
194   //myFeaturePrs->init(aNewFeature);
195   //myFeaturePrs->setFeature(myInitFeature, SM_FirstPoint);
196
197 //TODO  emit featureConstructed(aNewFeature, FM_Activation);
198   if (theFlushMessage)
199     flushCreated();
200   return aNewFeature;
201 }