Salome HOME
Issue #144 Corrections in immediate commit
[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_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 #include <iostream>
39 #endif
40
41 #include <QMouseEvent>
42
43 using namespace std;
44
45 PartSet_OperationFeatureBase::PartSet_OperationFeatureBase(const QString& theId,
46                                                                QObject* theParent,
47                                                                FeaturePtr theFeature)
48     : PartSet_OperationSketchBase(theId, theParent),
49       mySketch(theFeature),
50       myActiveWidget(NULL)
51 {
52 }
53
54 PartSet_OperationFeatureBase::~PartSet_OperationFeatureBase()
55 {
56 }
57
58 void PartSet_OperationFeatureBase::initSelection(
59     const std::list<ModuleBase_ViewerPrs>& theSelected,
60     const std::list<ModuleBase_ViewerPrs>& /*theHighlighted*/)
61 {
62   myPreSelection = theSelected;
63 }
64
65 void PartSet_OperationFeatureBase::initFeature(FeaturePtr theFeature)
66 {
67   myInitFeature = theFeature;
68 }
69
70 FeaturePtr PartSet_OperationFeatureBase::sketch() const
71 {
72   return mySketch;
73 }
74
75 void PartSet_OperationFeatureBase::mouseReleased(QMouseEvent* theEvent, Handle(V3d_View) theView,
76                                                  const std::list<ModuleBase_ViewerPrs>& theSelected,
77                                                  const std::list<ModuleBase_ViewerPrs>& /*theHighlighted*/)
78 {
79   gp_Pnt aPoint = PartSet_Tools::convertClickToPoint(theEvent->pos(), theView);
80   double aX = aPoint.X(), anY = aPoint.Y();
81
82   if (theSelected.empty()) {
83     PartSet_Tools::convertTo2D(aPoint, sketch(), theView, aX, anY);
84   } else {
85     ModuleBase_ViewerPrs aPrs = theSelected.front();
86     const TopoDS_Shape& aShape = aPrs.shape();
87     if (!aShape.IsNull()) {
88       if (aShape.ShapeType() == TopAbs_VERTEX) { // a point is selected
89         const TopoDS_Vertex& aVertex = TopoDS::Vertex(aShape);
90         if (!aVertex.IsNull()) {
91           aPoint = BRep_Tool::Pnt(aVertex);
92           PartSet_Tools::convertTo2D(aPoint, sketch(), theView, aX, anY);
93           PartSet_Tools::setConstraints(sketch(), feature(), myActiveWidget->attributeID(), aX, anY);
94         }
95       } else if (aShape.ShapeType() == TopAbs_EDGE) { // a line is selected
96         PartSet_Tools::convertTo2D(aPoint, sketch(), theView, aX, anY);
97       }
98     }
99   }
100   ObjectPtr aFeature;
101   if (!theSelected.empty()) {
102     ModuleBase_ViewerPrs aPrs = theSelected.front();
103     aFeature = aPrs.object();
104   } else {
105     aFeature = feature();  // for the widget distance only
106   }
107
108   bool isApplyed = setWidgetValue(aFeature, aX, anY);
109   if (isApplyed) {
110     flushUpdated();
111     emit activateNextWidget(myActiveWidget);
112   }
113   commit();
114 }
115
116 void PartSet_OperationFeatureBase::onWidgetActivated(ModuleBase_ModelWidget* theWidget)
117 {
118   myActiveWidget = theWidget;
119   activateByPreselection();
120   if (myInitFeature && myActiveWidget) {
121     ModuleBase_WidgetPoint2D* aWgt = dynamic_cast<ModuleBase_WidgetPoint2D*>(myActiveWidget);
122     if (aWgt && aWgt->initFromPrevious(myInitFeature)) {
123       myInitFeature = FeaturePtr();
124       emit activateNextWidget(myActiveWidget);
125     }
126   }
127 }
128
129 void PartSet_OperationFeatureBase::activateByPreselection()
130 {
131   if ((myPreSelection.size() > 0) && myActiveWidget) {
132     const ModuleBase_ViewerPrs& aPrs = myPreSelection.front();
133     ModuleBase_WidgetValueFeature aValue;
134     aValue.setObject(aPrs.object());
135     if (myActiveWidget->setValue(&aValue)) {
136       myPreSelection.remove(aPrs);
137       if(isValid()) {
138         myActiveWidget = NULL;
139         commit();
140       } else {
141         emit activateNextWidget(myActiveWidget);
142       }
143     }
144     // If preselection is enough to make a valid feature - apply it immediately
145   }
146 }
147
148 bool PartSet_OperationFeatureBase::setWidgetValue(ObjectPtr theFeature, double theX, double theY)
149 {
150   if (!myActiveWidget)
151     return false;
152   ModuleBase_WidgetValueFeature* aValue = new ModuleBase_WidgetValueFeature();
153   aValue->setObject(theFeature);
154   aValue->setPoint(boost::shared_ptr<GeomAPI_Pnt2d>(new GeomAPI_Pnt2d(theX, theY)));
155   bool isApplyed = myActiveWidget->setValue(aValue);
156
157   delete aValue;
158   myIsModified = (myIsModified || isApplyed);
159   return isApplyed;
160 }