]> SALOME platform Git repositories - modules/shaper.git/blob - src/PartSet/PartSet_OperationFeatureCreate.cpp
Salome HOME
Issue #101: Fixed editing of nested features
[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 #endif
39
40 #include <QMouseEvent>
41
42 using namespace std;
43
44 PartSet_OperationFeatureCreate::PartSet_OperationFeatureCreate(const QString& theId,
45                                                                QObject* theParent,
46                                                                FeaturePtr theFeature)
47     : PartSet_OperationSketchBase(theId, theParent),
48       mySketch(theFeature),
49       myActiveWidget(0)
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     return !myActiveWidget;
73   return false;
74 }
75
76 std::list<int> PartSet_OperationFeatureCreate::getSelectionModes(ObjectPtr theFeature) const
77 {
78   std::list<int> aModes;
79   if (theFeature != feature())
80     aModes = PartSet_OperationSketchBase::getSelectionModes(theFeature);
81   return aModes;
82 }
83
84 void PartSet_OperationFeatureCreate::initSelection(
85     const std::list<ModuleBase_ViewerPrs>& theSelected,
86     const std::list<ModuleBase_ViewerPrs>& /*theHighlighted*/)
87 {
88   myPreSelection = theSelected;
89 }
90
91 void PartSet_OperationFeatureCreate::initFeature(FeaturePtr theFeature)
92 {
93   myInitFeature = theFeature;
94 }
95
96 FeaturePtr PartSet_OperationFeatureCreate::sketch() const
97 {
98   return mySketch;
99 }
100
101 void PartSet_OperationFeatureCreate::mouseReleased(
102     QMouseEvent* theEvent, Handle(V3d_View) theView,
103     const std::list<ModuleBase_ViewerPrs>& theSelected,
104     const std::list<ModuleBase_ViewerPrs>& /*theHighlighted*/)
105 {
106   if (commit()) {
107     // if the point creation is finished, the next mouse release should commit the modification
108     // the next release can happens by double click in the viewer
109     restartOperation(feature()->getKind(), feature());
110     return;
111   }
112
113   gp_Pnt aPoint = PartSet_Tools::convertClickToPoint(theEvent->pos(), theView);
114   double aX = aPoint.X(), anY = aPoint.Y();
115
116   if (theSelected.empty()) {
117     PartSet_Tools::convertTo2D(aPoint, sketch(), theView, aX, anY);
118   } else {
119     ModuleBase_ViewerPrs aPrs = theSelected.front();
120     const TopoDS_Shape& aShape = aPrs.shape();
121     if (!aShape.IsNull())  // the point is selected
122     {
123       if (aShape.ShapeType() == TopAbs_VERTEX) {
124         const TopoDS_Vertex& aVertex = TopoDS::Vertex(aShape);
125         if (!aVertex.IsNull()) {
126           aPoint = BRep_Tool::Pnt(aVertex);
127           PartSet_Tools::convertTo2D(aPoint, sketch(), theView, aX, anY);
128
129           PartSet_Tools::setConstraints(sketch(), feature(), myActiveWidget->attributeID(), aX,
130                                         anY);
131         }
132       } else if (aShape.ShapeType() == TopAbs_EDGE)  // the line is selected
133           {
134         PartSet_Tools::convertTo2D(aPoint, sketch(), theView, aX, anY);
135         // move to selected line
136         if (feature()->getKind() == SketchPlugin_Line::ID()) {
137           //FeaturePtr aFeature = aPrs.feature();
138           //projectPointOnLine(aFeature, myPointSelectionMode, aPoint, theView, aX, anY);
139         }
140       }
141     }
142   }
143   ObjectPtr aFeature;
144   if (!theSelected.empty()) {
145     ModuleBase_ViewerPrs aPrs = theSelected.front();
146     aFeature = aPrs.object();
147   } else
148     aFeature = feature();  // for the widget distance only
149
150   bool isApplyed = setWidgetValue(aFeature, aX, anY);
151   if (isApplyed) {
152     flushUpdated();
153     emit activateNextWidget(myActiveWidget);
154   }
155 }
156
157 void PartSet_OperationFeatureCreate::mouseMoved(QMouseEvent* theEvent, Handle(V3d_View) theView)
158 {
159   if (commit()) {
160     restartOperation(feature()->getKind(), feature());
161   } else {
162     double aX, anY;
163     gp_Pnt aPoint = PartSet_Tools::convertClickToPoint(theEvent->pos(), theView);
164     PartSet_Tools::convertTo2D(aPoint, sketch(), theView, aX, anY);
165     setWidgetValue(feature(), aX, anY);
166     flushUpdated();
167   }
168 }
169
170 void PartSet_OperationFeatureCreate::keyReleased(std::string theName, QKeyEvent* theEvent)
171 {
172   int aKeyType = theEvent->key();
173   // the second point should be activated by any modification in the property panel
174   if (!theName.empty()) {
175     //setPointSelectionMode(myFeaturePrs->getNextMode(theName), false);
176   }
177   keyReleased(theEvent->key());
178 }
179
180 void PartSet_OperationFeatureCreate::onWidgetActivated(ModuleBase_ModelWidget* theWidget)
181 {
182   myActiveWidget = theWidget;
183   if ((myPreSelection.size() > 0) && myActiveWidget) {
184     const ModuleBase_ViewerPrs& aPrs = myPreSelection.front();
185     ModuleBase_WidgetValueFeature aValue;
186     aValue.setObject(aPrs.object());
187     if (myActiveWidget->setValue(&aValue)) {
188       myPreSelection.remove(aPrs);
189       emit activateNextWidget(myActiveWidget);
190     }
191   }
192   if (myInitFeature && myActiveWidget) {
193     ModuleBase_WidgetPoint2D* aWgt = dynamic_cast<ModuleBase_WidgetPoint2D*>(myActiveWidget);
194     if (aWgt && aWgt->initFromPrevious(myInitFeature)) {
195       myInitFeature = FeaturePtr();
196       emit activateNextWidget(myActiveWidget);
197     }
198   }
199 }
200
201 void PartSet_OperationFeatureCreate::keyReleased(const int theKey)
202 {
203   switch (theKey) {
204     case Qt::Key_Return: {
205       if (commit()) {
206         // it start a new line creation at a free point
207         restartOperation(feature()->getKind(), FeaturePtr());
208       }
209     }
210       break;
211     case Qt::Key_Escape: {
212       if (!commit()) {
213         abort();
214       }
215     }
216     default:
217       break;
218   }
219 }
220
221 void PartSet_OperationFeatureCreate::startOperation()
222 {
223   PartSet_OperationSketchBase::startOperation();
224   //setPointSelectionMode(!myInitFeature ? SM_FirstPoint : SM_SecondPoint);
225
226   emit multiSelectionEnabled(false);
227 }
228
229 void PartSet_OperationFeatureCreate::abortOperation()
230 {
231   emit featureConstructed(feature(), FM_Hide);
232   PartSet_OperationSketchBase::abortOperation();
233 }
234
235 void PartSet_OperationFeatureCreate::stopOperation()
236 {
237   PartSet_OperationSketchBase::stopOperation();
238   emit multiSelectionEnabled(true);
239 }
240
241 void PartSet_OperationFeatureCreate::afterCommitOperation()
242 {
243   PartSet_OperationSketchBase::afterCommitOperation();
244   emit featureConstructed(feature(), FM_Deactivation);
245 }
246
247 FeaturePtr PartSet_OperationFeatureCreate::createFeature(const bool theFlushMessage)
248 {
249   FeaturePtr aNewFeature = ModuleBase_Operation::createFeature(false);
250   if (sketch()) {
251     boost::shared_ptr<SketchPlugin_Feature> aFeature = boost::dynamic_pointer_cast<
252         SketchPlugin_Feature>(sketch());
253
254     aFeature->addSub(aNewFeature);
255   }
256   //myFeaturePrs->init(aNewFeature);
257   //myFeaturePrs->setFeature(myInitFeature, SM_FirstPoint);
258
259 //TODO  emit featureConstructed(aNewFeature, FM_Activation);
260   if (theFlushMessage)
261     flushCreated();
262   return aNewFeature;
263 }
264
265 bool PartSet_OperationFeatureCreate::setWidgetValue(ObjectPtr theFeature, double theX, double theY)
266 {
267   if (!myActiveWidget)
268     return false;
269   ModuleBase_WidgetValueFeature* aValue = new ModuleBase_WidgetValueFeature();
270   aValue->setObject(theFeature);
271   aValue->setPoint(boost::shared_ptr<GeomAPI_Pnt2d>(new GeomAPI_Pnt2d(theX, theY)));
272   bool isApplyed = myActiveWidget->setValue(aValue);
273
274   delete aValue;
275   myIsModified = (myIsModified || isApplyed);
276   return isApplyed;
277 }