]> SALOME platform Git repositories - modules/shaper.git/blob - src/PartSet/PartSet_OperationFeatureCreate.cpp
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
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_OperationSketchBase(theId, theParent),
49       mySketch(theFeature),
50       myActiveWidget(0)
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 }
69
70 bool PartSet_OperationFeatureCreate::canBeCommitted() const
71 {
72   if (PartSet_OperationSketchBase::canBeCommitted())
73     return !myActiveWidget;
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::initSelection(
86     const std::list<ModuleBase_ViewerPrs>& theSelected,
87     const std::list<ModuleBase_ViewerPrs>& /*theHighlighted*/)
88 {
89   myPreSelection = theSelected;
90 }
91
92 void PartSet_OperationFeatureCreate::initFeature(FeaturePtr theFeature)
93 {
94   myInitFeature = theFeature;
95 }
96
97 FeaturePtr PartSet_OperationFeatureCreate::sketch() const
98 {
99   return mySketch;
100 }
101
102 void PartSet_OperationFeatureCreate::mouseReleased(
103     QMouseEvent* theEvent, Handle(V3d_View) theView,
104     const std::list<ModuleBase_ViewerPrs>& theSelected,
105     const std::list<ModuleBase_ViewerPrs>& /*theHighlighted*/)
106 {
107   gp_Pnt aPoint = PartSet_Tools::convertClickToPoint(theEvent->pos(), theView);
108   double aX = aPoint.X(), anY = aPoint.Y();
109   bool isClosedContour = false;
110
111   if (theSelected.empty()) {
112     PartSet_Tools::convertTo2D(aPoint, sketch(), theView, aX, anY);
113   } else {
114     ModuleBase_ViewerPrs aPrs = theSelected.front();
115     const TopoDS_Shape& aShape = aPrs.shape();
116     if (!aShape.IsNull())
117     {
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           PartSet_Tools::setConstraints(sketch(), feature(), myActiveWidget->attributeID(), aX, anY);
124           isClosedContour = true;
125         }
126       } else if (aShape.ShapeType() == TopAbs_EDGE) { // a line is selected
127         PartSet_Tools::convertTo2D(aPoint, sketch(), theView, aX, anY);
128         // move to selected line
129         if (feature()->getKind() == SketchPlugin_Line::ID()) {
130           //FeaturePtr aFeature = aPrs.feature();
131           //projectPointOnLine(aFeature, myPointSelectionMode, aPoint, theView, aX, anY);
132         }
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   bool isApplyed = setWidgetValue(aFeature, aX, anY);
144   if (isApplyed) {
145     flushUpdated();
146     emit activateNextWidget(myActiveWidget);
147   }
148
149   if (commit() && !isClosedContour) {
150     // if the point creation is finished, the next mouse release should commit the modification
151     // the next release can happens by double click in the viewer
152     restartOperation(feature()->getKind(), feature());
153     return;
154   }
155 }
156
157 void PartSet_OperationFeatureCreate::mouseMoved(QMouseEvent* theEvent, Handle(V3d_View) theView)
158 {
159     double aX, anY;
160     gp_Pnt aPoint = PartSet_Tools::convertClickToPoint(theEvent->pos(), theView);
161     PartSet_Tools::convertTo2D(aPoint, sketch(), theView, aX, anY);
162     setWidgetValue(feature(), aX, anY);
163     flushUpdated();
164 }
165
166 void PartSet_OperationFeatureCreate::onWidgetActivated(ModuleBase_ModelWidget* theWidget)
167 {
168   myActiveWidget = theWidget;
169   if ((myPreSelection.size() > 0) && myActiveWidget) {
170     const ModuleBase_ViewerPrs& aPrs = myPreSelection.front();
171     ModuleBase_WidgetValueFeature aValue;
172     aValue.setObject(aPrs.object());
173     if (myActiveWidget->setValue(&aValue)) {
174       myPreSelection.remove(aPrs);
175       emit activateNextWidget(myActiveWidget);
176     }
177   }
178   if (myInitFeature && myActiveWidget) {
179     ModuleBase_WidgetPoint2D* aWgt = dynamic_cast<ModuleBase_WidgetPoint2D*>(myActiveWidget);
180     if (aWgt && aWgt->initFromPrevious(myInitFeature)) {
181       myInitFeature = FeaturePtr();
182       emit activateNextWidget(myActiveWidget);
183     }
184   }
185 }
186
187 void PartSet_OperationFeatureCreate::keyReleased(const int theKey)
188 {
189   switch (theKey) {
190     case Qt::Key_Return:
191     case Qt::Key_Enter: {
192       if (commit()) {
193         // it start a new line creation at a free point
194         restartOperation(feature()->getKind());
195       }
196     }
197       break;
198     default:
199       break;
200   }
201 }
202
203 void PartSet_OperationFeatureCreate::activateNextToCurrentWidget()
204 {
205   emit activateNextWidget(myActiveWidget);
206 }
207
208 void PartSet_OperationFeatureCreate::startOperation()
209 {
210   PartSet_OperationSketchBase::startOperation();
211   //setPointSelectionMode(!myInitFeature ? SM_FirstPoint : SM_SecondPoint);
212
213   emit multiSelectionEnabled(false);
214 }
215
216 void PartSet_OperationFeatureCreate::abortOperation()
217 {
218   emit featureConstructed(feature(), FM_Hide);
219   PartSet_OperationSketchBase::abortOperation();
220 }
221
222 void PartSet_OperationFeatureCreate::stopOperation()
223 {
224   PartSet_OperationSketchBase::stopOperation();
225   emit multiSelectionEnabled(true);
226 }
227
228 void PartSet_OperationFeatureCreate::afterCommitOperation()
229 {
230   PartSet_OperationSketchBase::afterCommitOperation();
231   emit featureConstructed(feature(), FM_Deactivation);
232 }
233
234 FeaturePtr PartSet_OperationFeatureCreate::createFeature(const bool theFlushMessage)
235 {
236   FeaturePtr aNewFeature = ModuleBase_Operation::createFeature(false);
237   if (sketch()) {
238     boost::shared_ptr<SketchPlugin_Feature> aFeature = boost::dynamic_pointer_cast<
239         SketchPlugin_Feature>(sketch());
240
241     aFeature->addSub(aNewFeature);
242   }
243   //myFeaturePrs->init(aNewFeature);
244   //myFeaturePrs->setFeature(myInitFeature, SM_FirstPoint);
245
246 //TODO  emit featureConstructed(aNewFeature, FM_Activation);
247   if (theFlushMessage)
248     flushCreated();
249   return aNewFeature;
250 }
251
252 bool PartSet_OperationFeatureCreate::setWidgetValue(ObjectPtr theFeature, double theX, double theY)
253 {
254   if (!myActiveWidget)
255     return false;
256   ModuleBase_WidgetValueFeature* aValue = new ModuleBase_WidgetValueFeature();
257   aValue->setObject(theFeature);
258   aValue->setPoint(boost::shared_ptr<GeomAPI_Pnt2d>(new GeomAPI_Pnt2d(theX, theY)));
259   bool isApplyed = myActiveWidget->setValue(aValue);
260
261   delete aValue;
262   myIsModified = (myIsModified || isApplyed);
263   return isApplyed;
264 }