Salome HOME
Constraint icons added
[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::onWidgetActivated(ModuleBase_ModelWidget* theWidget)
171 {
172   myActiveWidget = theWidget;
173   if ((myPreSelection.size() > 0) && myActiveWidget) {
174     const ModuleBase_ViewerPrs& aPrs = myPreSelection.front();
175     ModuleBase_WidgetValueFeature aValue;
176     aValue.setObject(aPrs.object());
177     if (myActiveWidget->setValue(&aValue)) {
178       myPreSelection.remove(aPrs);
179       emit activateNextWidget(myActiveWidget);
180     }
181   }
182   if (myInitFeature && myActiveWidget) {
183     ModuleBase_WidgetPoint2D* aWgt = dynamic_cast<ModuleBase_WidgetPoint2D*>(myActiveWidget);
184     if (aWgt && aWgt->initFromPrevious(myInitFeature)) {
185       myInitFeature = FeaturePtr();
186       emit activateNextWidget(myActiveWidget);
187     }
188   }
189 }
190
191 void PartSet_OperationFeatureCreate::keyReleased(const int theKey)
192 {
193   switch (theKey) {
194     case Qt::Key_Return:
195     case Qt::Key_Enter: {
196       if (commit()) {
197         // it start a new line creation at a free point
198         restartOperation(feature()->getKind());
199       }
200     }
201       break;
202     case Qt::Key_Escape: {
203       if (!commit()) {
204         abort();
205       }
206     }
207       break;
208     default:
209       break;
210   }
211 }
212
213 void PartSet_OperationFeatureCreate::startOperation()
214 {
215   PartSet_OperationSketchBase::startOperation();
216   //setPointSelectionMode(!myInitFeature ? SM_FirstPoint : SM_SecondPoint);
217
218   emit multiSelectionEnabled(false);
219 }
220
221 void PartSet_OperationFeatureCreate::abortOperation()
222 {
223   emit featureConstructed(feature(), FM_Hide);
224   PartSet_OperationSketchBase::abortOperation();
225 }
226
227 void PartSet_OperationFeatureCreate::stopOperation()
228 {
229   PartSet_OperationSketchBase::stopOperation();
230   emit multiSelectionEnabled(true);
231 }
232
233 void PartSet_OperationFeatureCreate::afterCommitOperation()
234 {
235   PartSet_OperationSketchBase::afterCommitOperation();
236   emit featureConstructed(feature(), FM_Deactivation);
237 }
238
239 FeaturePtr PartSet_OperationFeatureCreate::createFeature(const bool theFlushMessage)
240 {
241   FeaturePtr aNewFeature = ModuleBase_Operation::createFeature(false);
242   if (sketch()) {
243     boost::shared_ptr<SketchPlugin_Feature> aFeature = boost::dynamic_pointer_cast<
244         SketchPlugin_Feature>(sketch());
245
246     aFeature->addSub(aNewFeature);
247   }
248   //myFeaturePrs->init(aNewFeature);
249   //myFeaturePrs->setFeature(myInitFeature, SM_FirstPoint);
250
251 //TODO  emit featureConstructed(aNewFeature, FM_Activation);
252   if (theFlushMessage)
253     flushCreated();
254   return aNewFeature;
255 }
256
257 bool PartSet_OperationFeatureCreate::setWidgetValue(ObjectPtr theFeature, double theX, double theY)
258 {
259   if (!myActiveWidget)
260     return false;
261   ModuleBase_WidgetValueFeature* aValue = new ModuleBase_WidgetValueFeature();
262   aValue->setObject(theFeature);
263   aValue->setPoint(boost::shared_ptr<GeomAPI_Pnt2d>(new GeomAPI_Pnt2d(theX, theY)));
264   bool isApplyed = myActiveWidget->setValue(aValue);
265
266   delete aValue;
267   myIsModified = (myIsModified || isApplyed);
268   return isApplyed;
269 }