Salome HOME
2d53041c0fefa5ef2e8bd84cb707fbfb3e644e53
[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 <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                                                                CompositeFeaturePtr theFeature)
48     : PartSet_OperationSketchBase(theId, theParent),
49       mySketch(theFeature)
50 {
51 }
52
53 PartSet_OperationFeatureBase::~PartSet_OperationFeatureBase()
54 {
55 }
56
57 CompositeFeaturePtr PartSet_OperationFeatureBase::sketch() const
58 {
59   return mySketch;
60 }
61
62 void PartSet_OperationFeatureBase::mouseReleased(QMouseEvent* theEvent, ModuleBase_IViewer* theViewer,
63                                                  ModuleBase_ISelection* theSelection)
64 {
65   Handle(V3d_View) aView = theViewer->activeView();
66   gp_Pnt aPoint = PartSet_Tools::convertClickToPoint(theEvent->pos(), aView);
67   double aX = aPoint.X(), anY = aPoint.Y();
68   QList<ModuleBase_ViewerPrs> aSelected = theSelection->getSelected();
69
70   if (aSelected.empty()) {
71     PartSet_Tools::convertTo2D(aPoint, sketch(), aView, aX, anY);
72   } else {
73     ModuleBase_ViewerPrs aPrs = aSelected.first();
74     if (getViewerPoint(aPrs, theViewer, aX, anY)) {
75       ModuleBase_ModelWidget* aActiveWgt = myPropertyPanel->activeWidget();
76       PartSet_Tools::setConstraints(sketch(), feature(), aActiveWgt->attributeID(), aX, anY);
77     }
78     const TopoDS_Shape& aShape = aPrs.shape();
79     if (!aShape.IsNull() && aShape.ShapeType() == TopAbs_EDGE) { // a line is selected
80       PartSet_Tools::convertTo2D(aPoint, sketch(), aView, aX, anY);
81     }
82   }
83   ObjectPtr aFeature;
84   if (!aSelected.empty()) {
85     ModuleBase_ViewerPrs aPrs = aSelected.first();
86     aFeature = aPrs.object();
87   } else {
88     aFeature = feature();  // for the widget distance only
89   }
90
91   bool isApplyed = setWidgetValue(aFeature, aX, anY);
92   if (isApplyed) {
93     flushUpdated();
94     myPropertyPanel->activateNextWidget();
95   }
96   // the operation can be committed only when there is no an active widget anymore
97   // if this check is absent, the edit operation for constraint perpendicular is stopped
98   // after the first object selection in the viewer(there are two objects to be selected)
99   // the second case is the constraint distance, the edit is stopped after any mouse click
100   // in the viewer whenever it is applyed or not to the selection control
101   if (!myPropertyPanel->activeWidget())
102     commit();
103 }
104
105 bool PartSet_OperationFeatureBase::getViewerPoint(ModuleBase_ViewerPrs thePrs,
106                                                        ModuleBase_IViewer* theViewer,
107                                                        double& theX, double& theY)
108 {
109   return PartSet_Tools::hasVertexShape(thePrs, sketch(), theViewer->activeView(),
110                                        theX, theY);
111 }
112
113 /*bool PartSet_OperationFeatureBase::setWidgetValue(ObjectPtr theFeature, double theX, double theY)
114 {
115   ModuleBase_ModelWidget* aActiveWgt = myPropertyPanel->activeWidget();
116   if (!aActiveWgt)
117     return false;
118   ModuleBase_WidgetValueFeature* aValue = new ModuleBase_WidgetValueFeature();
119   aValue->setObject(theFeature);
120   aValue->setPoint(boost::shared_ptr<GeomAPI_Pnt2d>(new GeomAPI_Pnt2d(theX, theY)));
121   bool isApplyed = aActiveWgt->setValue(aValue);
122
123   delete aValue;
124   myIsModified = (myIsModified || isApplyed);
125   return isApplyed;
126 }*/