Salome HOME
Issue #251. Append Export/Import NewGeom commands in the SALOME desktop.
[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 #include <SketchPlugin_ConstraintRigid.h>
22
23 #include <GeomAPI_Pnt2d.h>
24
25 #include <ModuleBase_OperationDescription.h>
26 #include <ModuleBase_WidgetPoint2D.h>
27 #include <ModuleBase_WidgetValueFeature.h>
28 #include <ModuleBase_ViewerPrs.h>
29 #include <ModuleBase_IPropertyPanel.h>
30 #include <ModuleBase_ISelection.h>
31 #include <ModuleBase_IViewer.h>
32
33 #include <XGUI_Constants.h>
34
35 #include <V3d_View.hxx>
36
37 #ifdef _DEBUG
38 #include <QDebug>
39 #include <iostream>
40 #endif
41
42 #include <QMouseEvent>
43
44 using namespace std;
45
46 PartSet_OperationFeatureCreate::PartSet_OperationFeatureCreate(const QString& theId,
47                                                                QObject* theParent,
48                                                                CompositeFeaturePtr theFeature)
49     : PartSet_OperationFeatureBase(theId, theParent, theFeature)
50 {
51 }
52
53 PartSet_OperationFeatureCreate::~PartSet_OperationFeatureCreate()
54 {
55 }
56
57 bool PartSet_OperationFeatureCreate::canProcessKind(const std::string& theId)
58 {
59   return theId == SketchPlugin_Line::ID() || theId == SketchPlugin_Point::ID()
60       || theId == SketchPlugin_Circle::ID() || theId == SketchPlugin_Arc::ID()
61       || theId == SketchPlugin_ConstraintDistance::ID()
62       || theId == SketchPlugin_ConstraintLength::ID()
63       || theId == SketchPlugin_ConstraintRadius::ID()
64       || theId == SketchPlugin_ConstraintParallel::ID()
65       || theId == SketchPlugin_ConstraintPerpendicular::ID()
66       || theId == SketchPlugin_ConstraintCoincidence::ID()
67       || theId == SketchPlugin_ConstraintRigid::ID();
68 }
69
70 bool PartSet_OperationFeatureCreate::canBeCommitted() const
71 {
72   if (PartSet_OperationSketchBase::canBeCommitted()) {
73     //if(myActiveWidget && !myActiveWidget->isComputedDefault()) {
74     return isValid();
75   }
76   return false;
77 }
78
79 void PartSet_OperationFeatureCreate::mouseMoved(QMouseEvent* theEvent, ModuleBase_IViewer* theViewer)
80 {
81     double aX, anY;
82     Handle(V3d_View) aView = theViewer->activeView();
83     gp_Pnt aPoint = PartSet_Tools::convertClickToPoint(theEvent->pos(), aView);
84     PartSet_Tools::convertTo2D(aPoint, sketch(), aView, aX, anY);
85     setWidgetValue(feature(), aX, anY);
86     flushUpdated();
87 }
88
89 void PartSet_OperationFeatureCreate::keyReleased(const int theKey)
90 {
91   switch (theKey) {
92     case Qt::Key_Return:
93     case Qt::Key_Enter: {
94         // it start a new line creation at a free point
95       if(isValid())
96         restartOperation(feature()->getKind());
97     }
98       break;
99     default:
100       break;
101   }
102 }
103
104 void PartSet_OperationFeatureCreate::mouseReleased(QMouseEvent* theEvent, ModuleBase_IViewer* theViewer,
105                                                    ModuleBase_ISelection* theSelection)
106 {
107   Handle(V3d_View) aView = theViewer->activeView();
108   gp_Pnt aPoint = PartSet_Tools::convertClickToPoint(theEvent->pos(), aView);
109   double aX = aPoint.X(), anY = aPoint.Y();
110   bool isClosedContour = false;
111
112   QList<ModuleBase_ViewerPrs> aSelected = theSelection->getSelected();
113
114   if (aSelected.empty()) {
115     PartSet_Tools::convertTo2D(aPoint, sketch(), aView, aX, anY);
116   } else {
117     ModuleBase_ViewerPrs aPrs = aSelected.first();
118     if (getViewerPoint(aPrs, theViewer, aX, anY)) {
119       ModuleBase_ModelWidget* aActiveWgt = myPropertyPanel->activeWidget();
120       PartSet_Tools::setConstraints(sketch(), feature(), aActiveWgt->attributeID(), aX, anY);
121       isClosedContour = true;
122     }
123     const TopoDS_Shape& aShape = aPrs.shape();
124     if (!aShape.IsNull() && aShape.ShapeType() == TopAbs_EDGE) { // a line is selected
125       ObjectPtr aObject = aPrs.object();
126       if (sketch()->isSub(aObject))
127         PartSet_Tools::convertTo2D(aPoint, sketch(), aView, aX, anY);
128       else {
129         // we have to create the selected edge for the current sketch
130         ResultPtr aRes = PartSet_Tools::createFixedObjectByEdge(aPrs, sketch());
131         aSelected.first().setFeature(aRes);
132       }
133     }
134   }
135   ObjectPtr aFeature;
136   if (!aSelected.empty()) {
137     ModuleBase_ViewerPrs aPrs = aSelected.first();
138     aFeature = aPrs.object();
139   } else {
140     aFeature = feature();  // for the widget distance only
141   }
142
143   bool isApplyed = setWidgetValue(aFeature, aX, anY);
144   if (isApplyed) {
145     flushUpdated();
146     myPropertyPanel->activateNextWidget();
147   }
148
149   if (!myPropertyPanel->activeWidget()) {
150     if(commit() && !isClosedContour) {
151       // if the point creation is finished, the next mouse release should commit the modification
152       // the next release can happens by double click in the viewer
153       restartOperation(feature()->getKind(), feature());
154     }
155   }
156 }
157
158 void PartSet_OperationFeatureCreate::startOperation()
159 {
160   PartSet_OperationSketchBase::startOperation();
161   //emit multiSelectionEnabled(false);
162 }
163
164 void PartSet_OperationFeatureCreate::abortOperation()
165 {
166   emit featureConstructed(feature(), FM_Hide);
167   PartSet_OperationSketchBase::abortOperation();
168 }
169
170 void PartSet_OperationFeatureCreate::stopOperation()
171 {
172   PartSet_OperationSketchBase::stopOperation();
173   //emit multiSelectionEnabled(true);
174 }
175
176 void PartSet_OperationFeatureCreate::afterCommitOperation()
177 {
178   PartSet_OperationSketchBase::afterCommitOperation();
179   emit featureConstructed(feature(), FM_Deactivation);
180 }
181
182 FeaturePtr PartSet_OperationFeatureCreate::createFeature(const bool theFlushMessage,
183   CompositeFeaturePtr theCompositeFeature)
184 {
185   FeaturePtr aNewFeature = ModuleBase_Operation::createFeature(false, sketch());
186
187   if (theFlushMessage)
188     flushCreated();
189   return aNewFeature;
190 }
191
192
193 void PartSet_OperationFeatureCreate::onWidgetActivated(ModuleBase_ModelWidget* theWidget)
194 {
195   PartSet_OperationFeatureBase::onWidgetActivated(theWidget);
196   if (myInitFeature && theWidget) {
197     ModuleBase_WidgetPoint2D* aWgt = dynamic_cast<ModuleBase_WidgetPoint2D*>(theWidget);
198     if (aWgt && aWgt->initFromPrevious(myInitFeature)) {
199       myInitFeature = FeaturePtr();
200       if (myPropertyPanel)
201         myPropertyPanel->activateNextWidget();
202     }
203   }
204 }