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