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