Salome HOME
a1652fc482c575cebb7274b876077ddd138c7aaf
[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
28 #include <XGUI_ViewerPrs.h>
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), mySketch(theFeature), myActiveWidget(0)
48 {
49 }
50
51 PartSet_OperationFeatureCreate::~PartSet_OperationFeatureCreate()
52 {
53 }
54
55 bool PartSet_OperationFeatureCreate::canProcessKind(const std::string& theId)
56 {
57   return theId == SKETCH_LINE_KIND || theId == SKETCH_POINT_KIND ||
58          theId == SKETCH_CIRCLE_KIND ||
59          theId == SKETCH_ARC_KIND ||
60          theId == SKETCH_CONSTRAINT_DISTANCE_KIND ||
61          theId == SKETCH_CONSTRAINT_LENGTH_KIND ||
62          theId == SKETCH_CONSTRAINT_RADIUS_KIND ||
63          theId == SKETCH_CONSTRAINT_PARALLEL_KIND ||
64          theId == SKETCH_CONSTRAINT_PERPENDICULAR_KIND ||
65          theId == SKETCH_CONSTRAINT_COINCIDENCE_KIND;
66 }
67
68 bool PartSet_OperationFeatureCreate::canBeCommitted() const
69 {
70   return !myActiveWidget;
71 }
72
73 bool PartSet_OperationFeatureCreate::isGranted(ModuleBase_IOperation* theOperation) const
74 {
75   return theOperation->getDescription()->operationId().toStdString() == PartSet_OperationSketch::Type();
76 }
77
78 std::list<int> PartSet_OperationFeatureCreate::getSelectionModes(FeaturePtr 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::initSelection(const std::list<XGUI_ViewerPrs>& theSelected,
87                                                    const std::list<XGUI_ViewerPrs>& /*theHighlighted*/)
88 {
89   myPreSelection = theSelected;
90 }
91
92 void PartSet_OperationFeatureCreate::initFeature(FeaturePtr theFeature)
93 {
94   myInitFeature = theFeature;
95 }
96
97
98 FeaturePtr PartSet_OperationFeatureCreate::sketch() const
99 {
100   return mySketch;
101 }
102
103 void PartSet_OperationFeatureCreate::mouseReleased(QMouseEvent* theEvent, Handle(V3d_View) theView,
104                                                 const std::list<XGUI_ViewerPrs>& theSelected,
105                                                 const std::list<XGUI_ViewerPrs>& /*theHighlighted*/)
106 {
107   if (canBeCommitted())
108   {
109     // if the point creation is finished, the next mouse release should commit the modification
110     // the next release can happens by double click in the viewer
111     commit();
112     restartOperation(feature()->getKind(), feature());
113     return;
114   }
115
116   double aX = 0, anY = 0;
117
118   gp_Pnt aPoint = PartSet_Tools::convertClickToPoint(theEvent->pos(), theView);
119   if (theSelected.empty()) {
120     PartSet_Tools::convertTo2D(aPoint, sketch(), theView, aX, anY);
121   }
122   else {
123     XGUI_ViewerPrs aPrs = theSelected.front();
124     const TopoDS_Shape& aShape = aPrs.shape();
125     if (!aShape.IsNull()) // the point is selected
126     {
127       if (aShape.ShapeType() == TopAbs_VERTEX) {
128         const TopoDS_Vertex& aVertex = TopoDS::Vertex(aShape);
129         if (!aVertex.IsNull()) {
130           aPoint = BRep_Tool::Pnt(aVertex);
131           PartSet_Tools::convertTo2D(aPoint, sketch(), theView, aX, anY);
132
133           PartSet_Tools::setConstraints(sketch(), feature(), myActiveWidget->attributeID(),
134                                         aX, anY);
135         }
136       }
137       else if (aShape.ShapeType() == TopAbs_EDGE) // the line is selected
138       {
139         PartSet_Tools::convertTo2D(aPoint, sketch(), theView, aX, anY);
140         // move to selected line
141         if (feature()->getKind() == SKETCH_LINE_KIND) {
142           //FeaturePtr aFeature = aPrs.feature();
143           //projectPointOnLine(aFeature, myPointSelectionMode, aPoint, theView, aX, anY);
144         }
145       }
146     }
147   }
148   FeaturePtr aFeature;
149   if (!theSelected.empty()) {
150     XGUI_ViewerPrs aPrs = theSelected.front();
151     aFeature = aPrs.feature();
152   }
153   else
154     aFeature = feature(); // for the widget distance only
155
156   bool isApplyed = setWidgetValue(aFeature, aX, anY);
157   if (isApplyed) {
158     flushUpdated();
159     emit activateNextWidget(myActiveWidget);
160   }
161 }
162
163 void PartSet_OperationFeatureCreate::mouseMoved(QMouseEvent* theEvent, Handle(V3d_View) theView)
164 {
165   if (canBeCommitted()) {
166     commit();
167     restartOperation(feature()->getKind(), feature());
168   }
169   else {
170     double aX, anY;
171     gp_Pnt aPoint = PartSet_Tools::convertClickToPoint(theEvent->pos(), theView);
172     PartSet_Tools::convertTo2D(aPoint, sketch(), theView, aX, anY);
173     setWidgetValue(feature(), aX, anY);
174     flushUpdated();
175   }
176 }
177
178 void PartSet_OperationFeatureCreate::keyReleased(std::string theName, QKeyEvent* theEvent)
179 {
180   int aKeyType = theEvent->key();
181   // the second point should be activated by any modification in the property panel
182   if (!theName.empty())
183   {
184     //setPointSelectionMode(myFeaturePrs->getNextMode(theName), false);
185   }
186   keyReleased(theEvent->key());
187 }
188
189 void PartSet_OperationFeatureCreate::onWidgetActivated(ModuleBase_ModelWidget* theWidget)
190 {
191   myActiveWidget = theWidget;
192   if ((myPreSelection.size() > 0) && myActiveWidget) {
193     const XGUI_ViewerPrs& aPrs = myPreSelection.front();
194     ModuleBase_WidgetValueFeature aValue;
195     aValue.setFeature(aPrs.feature());
196     if (myActiveWidget->setValue(&aValue)) {
197       myPreSelection.remove(aPrs);
198       emit activateNextWidget(myActiveWidget);
199     }
200   } 
201   if (myInitFeature && myActiveWidget) {
202     ModuleBase_WidgetPoint2D* aWgt = dynamic_cast<ModuleBase_WidgetPoint2D*>(myActiveWidget);
203     if (aWgt && aWgt->initFromPrevious(myInitFeature)) { 
204       myInitFeature = FeaturePtr();
205       emit activateNextWidget(myActiveWidget);
206     }
207   }
208 }
209
210 void PartSet_OperationFeatureCreate::keyReleased(const int theKey)
211 {
212   switch (theKey) {
213     case Qt::Key_Return: {
214       if (canBeCommitted())
215       {
216         commit();
217         // it start a new line creation at a free point
218         restartOperation(feature()->getKind(), FeaturePtr());
219       }
220     }
221     break;
222     case Qt::Key_Escape: {
223       if (canBeCommitted())
224       {
225         commit();
226       }
227       else
228       {
229         abort();
230       }
231     }
232     default:
233     break;
234   }
235 }
236
237 void PartSet_OperationFeatureCreate::startOperation()
238 {
239   PartSet_OperationSketchBase::startOperation();
240   //setPointSelectionMode(!myInitFeature ? SM_FirstPoint : SM_SecondPoint);
241
242   emit multiSelectionEnabled(false);
243 }
244
245 void PartSet_OperationFeatureCreate::abortOperation()
246 {
247   emit featureConstructed(feature(), FM_Hide);
248   PartSet_OperationSketchBase::abortOperation();
249 }
250
251 void PartSet_OperationFeatureCreate::stopOperation()
252 {
253   PartSet_OperationSketchBase::stopOperation();
254   emit multiSelectionEnabled(true);
255 }
256
257 void PartSet_OperationFeatureCreate::afterCommitOperation()
258 {
259   PartSet_OperationSketchBase::afterCommitOperation();  
260   emit featureConstructed(feature(), FM_Deactivation);
261 }
262
263 FeaturePtr PartSet_OperationFeatureCreate::createFeature(const bool theFlushMessage)
264 {
265   FeaturePtr aNewFeature = ModuleBase_Operation::createFeature(false);
266   if (sketch()) {
267     boost::shared_ptr<SketchPlugin_Feature> aFeature = 
268                            boost::dynamic_pointer_cast<SketchPlugin_Feature>(sketch());
269
270     aFeature->addSub(aNewFeature);
271   }
272   //myFeaturePrs->init(aNewFeature);
273   //myFeaturePrs->setFeature(myInitFeature, SM_FirstPoint);
274
275   emit featureConstructed(aNewFeature, FM_Activation);
276   if (theFlushMessage)
277     flushCreated();
278   return aNewFeature;
279 }
280
281 bool PartSet_OperationFeatureCreate::setWidgetValue(FeaturePtr theFeature, double theX, double theY)
282 {
283   ModuleBase_WidgetValueFeature* aValue = new ModuleBase_WidgetValueFeature();
284   aValue->setFeature(theFeature);
285   aValue->setPoint(boost::shared_ptr<GeomAPI_Pnt2d>(new GeomAPI_Pnt2d(theX, theY)));
286   bool isApplyed = myActiveWidget->setValue(aValue);
287
288   delete aValue;
289   myIsModified = (myIsModified || isApplyed);
290   return isApplyed;
291 }