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 #include <ModuleBase_IPropertyPanel.h>
29
30 #include <XGUI_Constants.h>
31
32 #include <V3d_View.hxx>
33 #include <TopoDS_Vertex.hxx>
34 #include <TopoDS.hxx>
35 #include <BRep_Tool.hxx>
36
37 #ifdef _DEBUG
38 #include <QDebug>
39 #include <iostream>
40 #endif
41
42 #include <QMouseEvent>
43
44 using namespace std;
45
46 PartSet_OperationFeatureCreate::PartSet_OperationFeatureCreate(const QString& theId,
47                                                                QObject* theParent,
48                                                                FeaturePtr theFeature)
49     : PartSet_OperationFeatureBase(theId, theParent, theFeature)
50 {
51 }
52
53 PartSet_OperationFeatureCreate::~PartSet_OperationFeatureCreate()
54 {
55 }
56
57 bool PartSet_OperationFeatureCreate::canProcessKind(const std::string& theId)
58 {
59   return theId == SketchPlugin_Line::ID() || theId == SketchPlugin_Point::ID()
60       || theId == SketchPlugin_Circle::ID() || theId == SketchPlugin_Arc::ID()
61       || theId == SketchPlugin_ConstraintDistance::ID()
62       || theId == SketchPlugin_ConstraintLength::ID()
63       || theId == SketchPlugin_ConstraintRadius::ID()
64       || theId == SketchPlugin_ConstraintParallel::ID()
65       || theId == SketchPlugin_ConstraintPerpendicular::ID()
66       || theId == SketchPlugin_ConstraintCoincidence::ID();
67 }
68
69 bool PartSet_OperationFeatureCreate::canBeCommitted() const
70 {
71   if (PartSet_OperationSketchBase::canBeCommitted()) {
72     //if(myActiveWidget && !myActiveWidget->isComputedDefault()) {
73     return isValid();
74   }
75   return false;
76 }
77
78 std::list<int> PartSet_OperationFeatureCreate::getSelectionModes(ObjectPtr theFeature) const
79 {
80   std::list<int> aModes;
81   if (theFeature != feature())
82     aModes = PartSet_OperationSketchBase::getSelectionModes(theFeature);
83   return aModes;
84 }
85
86 void PartSet_OperationFeatureCreate::mouseMoved(QMouseEvent* theEvent, Handle(V3d_View) theView)
87 {
88     double aX, anY;
89     gp_Pnt aPoint = PartSet_Tools::convertClickToPoint(theEvent->pos(), theView);
90     PartSet_Tools::convertTo2D(aPoint, sketch(), theView, aX, anY);
91     setWidgetValue(feature(), aX, anY);
92     flushUpdated();
93 }
94
95 void PartSet_OperationFeatureCreate::keyReleased(const int theKey)
96 {
97   switch (theKey) {
98     case Qt::Key_Return:
99     case Qt::Key_Enter: {
100         // it start a new line creation at a free point
101       if(isValid())
102         restartOperation(feature()->getKind());
103     }
104       break;
105     default:
106       break;
107   }
108 }
109
110 void PartSet_OperationFeatureCreate::mouseReleased(QMouseEvent* theEvent, Handle(V3d_View) theView,
111                                                  const std::list<ModuleBase_ViewerPrs>& theSelected,
112                                                  const std::list<ModuleBase_ViewerPrs>& /*theHighlighted*/)
113 {
114   gp_Pnt aPoint = PartSet_Tools::convertClickToPoint(theEvent->pos(), theView);
115   double aX = aPoint.X(), anY = aPoint.Y();
116   bool isClosedContour = false;
117
118   if (theSelected.empty()) {
119     PartSet_Tools::convertTo2D(aPoint, sketch(), theView, aX, anY);
120   } else {
121     ModuleBase_ViewerPrs aPrs = theSelected.front();
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(), theView, 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         PartSet_Tools::convertTo2D(aPoint, sketch(), theView, aX, anY);
135       }
136     }
137   }
138   ObjectPtr aFeature;
139   if (!theSelected.empty()) {
140     ModuleBase_ViewerPrs aPrs = theSelected.front();
141     aFeature = aPrs.object();
142   } else {
143     aFeature = feature();  // for the widget distance only
144   }
145
146   bool isApplyed = setWidgetValue(aFeature, aX, anY);
147   if (isApplyed) {
148     flushUpdated();
149     myPropertyPanel->activateNextWidget();
150   }
151
152   if (!myPropertyPanel->activeWidget()) {
153     if(commit() && !isClosedContour) {
154       // if the point creation is finished, the next mouse release should commit the modification
155       // the next release can happens by double click in the viewer
156       restartOperation(feature()->getKind(), feature());
157     }
158   }
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
195   if (theFlushMessage)
196     flushCreated();
197   return aNewFeature;
198 }
199
200
201 void PartSet_OperationFeatureCreate::onWidgetActivated(ModuleBase_ModelWidget* theWidget)
202 {
203   PartSet_OperationFeatureBase::onWidgetActivated(theWidget);
204   if (myInitFeature && theWidget) {
205     ModuleBase_WidgetPoint2D* aWgt = dynamic_cast<ModuleBase_WidgetPoint2D*>(theWidget);
206     if (aWgt && aWgt->initFromPrevious(myInitFeature)) {
207       myInitFeature = FeaturePtr();
208       if (myPropertyPanel)
209         myPropertyPanel->activateNextWidget();
210     }
211   }
212 }