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