Salome HOME
refs #156: Behavior of the Sketch during edition
[modules/shaper.git] / src / PartSet / PartSet_OperationFeatureBase.cpp
1 // File:        PartSet_OperationFeatureBase.h
2 // Created:     20 Apr 2014
3 // Author:      Natalia ERMOLAEVA
4
5 #include <PartSet_OperationFeatureBase.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_IPropertyPanel.h"
28 #include "ModuleBase_ISelection.h"
29 #include "ModuleBase_IViewer.h"
30
31 #include <XGUI_Constants.h>
32
33 #include <V3d_View.hxx>
34 #include <TopoDS_Vertex.hxx>
35 #include <TopoDS.hxx>
36 #include <BRep_Tool.hxx>
37
38 #ifdef _DEBUG
39 #include <QDebug>
40 #include <iostream>
41 #endif
42
43 #include <QMouseEvent>
44
45 using namespace std;
46
47 PartSet_OperationFeatureBase::PartSet_OperationFeatureBase(const QString& theId,
48                                                                QObject* theParent,
49                                                                CompositeFeaturePtr theFeature)
50     : PartSet_OperationSketchBase(theId, theParent),
51       mySketch(theFeature)
52 {
53 }
54
55 PartSet_OperationFeatureBase::~PartSet_OperationFeatureBase()
56 {
57 }
58
59 CompositeFeaturePtr PartSet_OperationFeatureBase::sketch() const
60 {
61   return mySketch;
62 }
63
64 void PartSet_OperationFeatureBase::mouseReleased(QMouseEvent* theEvent, ModuleBase_IViewer* theViewer,
65                                                  ModuleBase_ISelection* theSelection)
66 {
67   Handle(V3d_View) aView = theViewer->activeView();
68   gp_Pnt aPoint = PartSet_Tools::convertClickToPoint(theEvent->pos(), aView);
69   double aX = aPoint.X(), anY = aPoint.Y();
70   QList<ModuleBase_ViewerPrs> aSelected = theSelection->getSelected();
71
72   if (aSelected.empty()) {
73     PartSet_Tools::convertTo2D(aPoint, sketch(), aView, aX, anY);
74   } else {
75     ModuleBase_ViewerPrs aPrs = aSelected.first();
76     const TopoDS_Shape& aShape = aPrs.shape();
77     if (!aShape.IsNull()) {
78       if (aShape.ShapeType() == TopAbs_VERTEX) { // a point is selected
79         const TopoDS_Vertex& aVertex = TopoDS::Vertex(aShape);
80         if (!aVertex.IsNull()) {
81           aPoint = BRep_Tool::Pnt(aVertex);
82           PartSet_Tools::convertTo2D(aPoint, sketch(), aView, aX, anY);
83           ModuleBase_ModelWidget* aActiveWgt = myPropertyPanel->activeWidget();
84           PartSet_Tools::setConstraints(sketch(), feature(), aActiveWgt->attributeID(), aX, anY);
85         }
86       } else if (aShape.ShapeType() == TopAbs_EDGE) { // a line is selected
87         PartSet_Tools::convertTo2D(aPoint, sketch(), aView, aX, anY);
88       }
89     }
90   }
91   ObjectPtr aFeature;
92   if (!aSelected.empty()) {
93     ModuleBase_ViewerPrs aPrs = aSelected.first();
94     aFeature = aPrs.object();
95   } else {
96     aFeature = feature();  // for the widget distance only
97   }
98
99   bool isApplyed = setWidgetValue(aFeature, aX, anY);
100   if (isApplyed) {
101     flushUpdated();
102     myPropertyPanel->activateNextWidget();
103   }
104   commit();
105 }
106
107 /*bool PartSet_OperationFeatureBase::setWidgetValue(ObjectPtr theFeature, double theX, double theY)
108 {
109   ModuleBase_ModelWidget* aActiveWgt = myPropertyPanel->activeWidget();
110   if (!aActiveWgt)
111     return false;
112   ModuleBase_WidgetValueFeature* aValue = new ModuleBase_WidgetValueFeature();
113   aValue->setObject(theFeature);
114   aValue->setPoint(boost::shared_ptr<GeomAPI_Pnt2d>(new GeomAPI_Pnt2d(theX, theY)));
115   bool isApplyed = aActiveWgt->setValue(aValue);
116
117   delete aValue;
118   myIsModified = (myIsModified || isApplyed);
119   return isApplyed;
120 }*/