Salome HOME
29.10.2014.Extrusion feature update.
[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
40 #ifdef _DEBUG
41 #include <QDebug>
42 #include <iostream>
43 #endif
44
45 #include <QMouseEvent>
46
47 using namespace std;
48
49 PartSet_OperationFeatureCreate::PartSet_OperationFeatureCreate(const QString& theId,
50                                                                QObject* theParent,
51                                                                CompositeFeaturePtr theFeature)
52     : PartSet_OperationFeatureBase(theId, theParent, theFeature)
53 {
54 }
55
56 PartSet_OperationFeatureCreate::~PartSet_OperationFeatureCreate()
57 {
58 }
59
60 bool PartSet_OperationFeatureCreate::canProcessKind(const std::string& theId)
61 {
62   return theId == SketchPlugin_Line::ID() || theId == SketchPlugin_Point::ID()
63       || theId == SketchPlugin_Circle::ID() || theId == SketchPlugin_Arc::ID()
64       || theId == SketchPlugin_ConstraintDistance::ID()
65       || theId == SketchPlugin_ConstraintLength::ID()
66       || theId == SketchPlugin_ConstraintRadius::ID()
67       || theId == SketchPlugin_ConstraintParallel::ID()
68       || theId == SketchPlugin_ConstraintPerpendicular::ID()
69       || theId == SketchPlugin_ConstraintCoincidence::ID()
70       || theId == SketchPlugin_ConstraintRigid::ID();
71 }
72
73 bool PartSet_OperationFeatureCreate::canBeCommitted() const
74 {
75   if (PartSet_OperationSketchBase::canBeCommitted()) {
76     //if(myActiveWidget && !myActiveWidget->isComputedDefault()) {
77     return isValid();
78   }
79   return false;
80 }
81
82 void PartSet_OperationFeatureCreate::mouseMoved(QMouseEvent* theEvent, ModuleBase_IViewer* theViewer)
83 {
84     double aX, anY;
85     Handle(V3d_View) aView = theViewer->activeView();
86     gp_Pnt aPoint = PartSet_Tools::convertClickToPoint(theEvent->pos(), aView);
87     PartSet_Tools::convertTo2D(aPoint, sketch(), aView, 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, ModuleBase_IViewer* theViewer,
108                                                    ModuleBase_ISelection* theSelection)
109 {
110   Handle(V3d_View) aView = theViewer->activeView();
111   gp_Pnt aPoint = PartSet_Tools::convertClickToPoint(theEvent->pos(), aView);
112   double aX = aPoint.X(), anY = aPoint.Y();
113   bool isClosedContour = false;
114
115   QList<ModuleBase_ViewerPrs> aSelected = theSelection->getSelected();
116
117   if (aSelected.empty()) {
118     PartSet_Tools::convertTo2D(aPoint, sketch(), aView, aX, anY);
119   } else {
120     ModuleBase_ViewerPrs aPrs = aSelected.first();
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(), aView, aX, anY);
128           ModuleBase_ModelWidget* aActiveWgt = myPropertyPanel->activeWidget();
129           PartSet_Tools::setConstraints(sketch(), feature(), aActiveWgt->attributeID(), aX, anY);
130           isClosedContour = true;
131         }
132       } else if (aShape.ShapeType() == TopAbs_EDGE) { // a line is selected
133         PartSet_Tools::convertTo2D(aPoint, sketch(), aView, aX, anY);
134       }
135     }
136   }
137   ObjectPtr aFeature;
138   if (!aSelected.empty()) {
139     ModuleBase_ViewerPrs aPrs = aSelected.first();
140     aFeature = aPrs.object();
141   } else {
142     aFeature = feature();  // for the widget distance only
143   }
144
145   bool isApplyed = setWidgetValue(aFeature, aX, anY);
146   if (isApplyed) {
147     flushUpdated();
148     myPropertyPanel->activateNextWidget();
149   }
150
151   if (!myPropertyPanel->activeWidget()) {
152     if(commit() && !isClosedContour) {
153       // if the point creation is finished, the next mouse release should commit the modification
154       // the next release can happens by double click in the viewer
155       restartOperation(feature()->getKind(), feature());
156     }
157   }
158 }
159
160 void PartSet_OperationFeatureCreate::startOperation()
161 {
162   PartSet_OperationSketchBase::startOperation();
163   //emit multiSelectionEnabled(false);
164 }
165
166 void PartSet_OperationFeatureCreate::abortOperation()
167 {
168   emit featureConstructed(feature(), FM_Hide);
169   PartSet_OperationSketchBase::abortOperation();
170 }
171
172 void PartSet_OperationFeatureCreate::stopOperation()
173 {
174   PartSet_OperationSketchBase::stopOperation();
175   //emit multiSelectionEnabled(true);
176 }
177
178 void PartSet_OperationFeatureCreate::afterCommitOperation()
179 {
180   PartSet_OperationSketchBase::afterCommitOperation();
181   emit featureConstructed(feature(), FM_Deactivation);
182 }
183
184 FeaturePtr PartSet_OperationFeatureCreate::createFeature(const bool theFlushMessage,
185   CompositeFeaturePtr theCompositeFeature)
186 {
187   FeaturePtr aNewFeature = ModuleBase_Operation::createFeature(false, sketch());
188
189   if (theFlushMessage)
190     flushCreated();
191   return aNewFeature;
192 }
193
194
195 void PartSet_OperationFeatureCreate::onWidgetActivated(ModuleBase_ModelWidget* theWidget)
196 {
197   PartSet_OperationFeatureBase::onWidgetActivated(theWidget);
198   if (myInitFeature && theWidget) {
199     ModuleBase_WidgetPoint2D* aWgt = dynamic_cast<ModuleBase_WidgetPoint2D*>(theWidget);
200     if (aWgt && aWgt->initFromPrevious(myInitFeature)) {
201       myInitFeature = FeaturePtr();
202       if (myPropertyPanel)
203         myPropertyPanel->activateNextWidget();
204     }
205   }
206 }