Salome HOME
f08d83972f67e4fd554625036240b6962221400a
[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), 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 == SketchPlugin_Line::ID() || theId == SketchPlugin_Point::ID() ||
58          theId == SketchPlugin_Circle::ID() ||
59          theId == SketchPlugin_Arc::ID() ||
60          theId == SketchPlugin_ConstraintDistance::ID() ||
61          theId == SketchPlugin_ConstraintLength::ID() ||
62          theId == SketchPlugin_ConstraintRadius::ID() ||
63          theId == SketchPlugin_ConstraintParallel::ID() ||
64          theId == SketchPlugin_ConstraintPerpendicular::ID() ||
65          theId == SketchPlugin_ConstraintCoincidence::ID();
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<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
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<ModuleBase_ViewerPrs>& theSelected,
105                                                 const std::list<ModuleBase_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   gp_Pnt aPoint = PartSet_Tools::convertClickToPoint(theEvent->pos(), theView);
117   double aX = aPoint.X(), anY = aPoint.Y();
118
119   if (theSelected.empty()) {
120     PartSet_Tools::convertTo2D(aPoint, sketch(), theView, aX, anY);
121   }
122   else {
123     ModuleBase_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() == SketchPlugin_Line::ID()) {
142           //FeaturePtr aFeature = aPrs.feature();
143           //projectPointOnLine(aFeature, myPointSelectionMode, aPoint, theView, aX, anY);
144         }
145       }
146     }
147   }
148   ResultPtr aFeature;
149   if (!theSelected.empty()) {
150     ModuleBase_ViewerPrs aPrs = theSelected.front();
151     aFeature = aPrs.result();
152   }
153   // TODO
154   //else
155   //  aFeature = feature(); // for the widget distance only
156
157   // TODO
158   /*bool isApplyed = setWidgetValue(aFeature, aX, anY);
159   if (isApplyed) {
160     flushUpdated();
161     emit activateNextWidget(myActiveWidget);
162   }*/
163 }
164
165 void PartSet_OperationFeatureCreate::mouseMoved(QMouseEvent* theEvent, Handle(V3d_View) theView)
166 {
167   if (canBeCommitted()) {
168     commit();
169     restartOperation(feature()->getKind(), feature());
170   }
171   else {
172     double aX, anY;
173     gp_Pnt aPoint = PartSet_Tools::convertClickToPoint(theEvent->pos(), theView);
174     PartSet_Tools::convertTo2D(aPoint, sketch(), theView, aX, anY);
175     setWidgetValue(feature(), aX, anY);
176     flushUpdated();
177   }
178 }
179
180 void PartSet_OperationFeatureCreate::keyReleased(std::string theName, QKeyEvent* theEvent)
181 {
182   int aKeyType = theEvent->key();
183   // the second point should be activated by any modification in the property panel
184   if (!theName.empty())
185   {
186     //setPointSelectionMode(myFeaturePrs->getNextMode(theName), false);
187   }
188   keyReleased(theEvent->key());
189 }
190
191 void PartSet_OperationFeatureCreate::onWidgetActivated(ModuleBase_ModelWidget* theWidget)
192 {
193   myActiveWidget = theWidget;
194   if ((myPreSelection.size() > 0) && myActiveWidget) {
195     const ModuleBase_ViewerPrs& aPrs = myPreSelection.front();
196     ModuleBase_WidgetValueFeature aValue;
197     aValue.setResult(aPrs.result());
198     if (myActiveWidget->setValue(&aValue)) {
199       myPreSelection.remove(aPrs);
200       emit activateNextWidget(myActiveWidget);
201     }
202   } 
203   if (myInitFeature && myActiveWidget) {
204     ModuleBase_WidgetPoint2D* aWgt = dynamic_cast<ModuleBase_WidgetPoint2D*>(myActiveWidget);
205     if (aWgt && aWgt->initFromPrevious(myInitFeature)) { 
206       myInitFeature = FeaturePtr();
207       emit activateNextWidget(myActiveWidget);
208     }
209   }
210 }
211
212 void PartSet_OperationFeatureCreate::keyReleased(const int theKey)
213 {
214   switch (theKey) {
215     case Qt::Key_Return: {
216       if (canBeCommitted())
217       {
218         commit();
219         // it start a new line creation at a free point
220         restartOperation(feature()->getKind(), FeaturePtr());
221       }
222     }
223     break;
224     case Qt::Key_Escape: {
225       if (canBeCommitted())
226       {
227         commit();
228       }
229       else
230       {
231         abort();
232       }
233     }
234     default:
235     break;
236   }
237 }
238
239 void PartSet_OperationFeatureCreate::startOperation()
240 {
241   PartSet_OperationSketchBase::startOperation();
242   //setPointSelectionMode(!myInitFeature ? SM_FirstPoint : SM_SecondPoint);
243
244   emit multiSelectionEnabled(false);
245 }
246
247 void PartSet_OperationFeatureCreate::abortOperation()
248 {
249   emit featureConstructed(feature(), FM_Hide);
250   PartSet_OperationSketchBase::abortOperation();
251 }
252
253 void PartSet_OperationFeatureCreate::stopOperation()
254 {
255   PartSet_OperationSketchBase::stopOperation();
256   emit multiSelectionEnabled(true);
257 }
258
259 void PartSet_OperationFeatureCreate::afterCommitOperation()
260 {
261   PartSet_OperationSketchBase::afterCommitOperation();  
262   emit featureConstructed(feature(), FM_Deactivation);
263 }
264
265 FeaturePtr PartSet_OperationFeatureCreate::createFeature(const bool theFlushMessage)
266 {
267   FeaturePtr aNewFeature = ModuleBase_Operation::createFeature(false);
268   if (sketch()) {
269     boost::shared_ptr<SketchPlugin_Feature> aFeature = 
270                            boost::dynamic_pointer_cast<SketchPlugin_Feature>(sketch());
271
272     aFeature->addSub(aNewFeature);
273   }
274   //myFeaturePrs->init(aNewFeature);
275   //myFeaturePrs->setFeature(myInitFeature, SM_FirstPoint);
276
277   emit featureConstructed(aNewFeature, FM_Activation);
278   if (theFlushMessage)
279     flushCreated();
280   return aNewFeature;
281 }
282
283 bool PartSet_OperationFeatureCreate::setWidgetValue(FeaturePtr theFeature, double theX, double theY)
284 {
285   ModuleBase_WidgetValueFeature* aValue = new ModuleBase_WidgetValueFeature();
286   aValue->setResult(theFeature->firstResult());
287   aValue->setPoint(boost::shared_ptr<GeomAPI_Pnt2d>(new GeomAPI_Pnt2d(theX, theY)));
288   bool isApplyed = myActiveWidget->setValue(aValue);
289
290   delete aValue;
291   myIsModified = (myIsModified || isApplyed);
292   return isApplyed;
293 }