1 // File: PartSet_OperationCreateFeature.h
2 // Created: 20 Apr 2014
3 // Author: Natalia ERMOLAEVA
5 #include <PartSet_OperationCreateFeature.h>
7 #include <PartSet_Tools.h>
8 #include <PartSet_OperationSketch.h>
9 #include <PartSet_FeaturePointPrs.h>
10 #include <PartSet_FeatureLinePrs.h>
11 #include <PartSet_FeatureCirclePrs.h>
12 #include <PartSet_FeatureArcPrs.h>
14 #include <SketchPlugin_Feature.h>
15 #include <SketchPlugin_Point.h>
16 #include <SketchPlugin_Line.h>
17 #include <SketchPlugin_Circle.h>
18 #include <SketchPlugin_Arc.h>
20 #include <ModuleBase_OperationDescription.h>
22 #include <XGUI_ViewerPrs.h>
23 #include <XGUI_Constants.h>
25 #include <V3d_View.hxx>
26 #include <TopoDS_Vertex.hxx>
28 #include <BRep_Tool.hxx>
34 #include <QMouseEvent>
38 PartSet_OperationCreateFeature::PartSet_OperationCreateFeature(const QString& theId,
40 FeaturePtr theFeature)
41 : PartSet_OperationSketchBase(theId, theParent),
42 myPointSelectionMode(SM_FirstPoint)
44 std::string aKind = theId.toStdString();
46 if (aKind == SKETCH_POINT_KIND) {
47 myFeaturePrs = new PartSet_FeaturePointPrs(theFeature);
49 if (aKind == SKETCH_LINE_KIND) {
50 myFeaturePrs = new PartSet_FeatureLinePrs(theFeature);
52 else if (aKind == SKETCH_CIRCLE_KIND) {
53 myFeaturePrs = new PartSet_FeatureCirclePrs(theFeature);
55 else if (aKind == SKETCH_ARC_KIND) {
56 myFeaturePrs = new PartSet_FeatureArcPrs(theFeature);
60 PartSet_OperationCreateFeature::~PartSet_OperationCreateFeature()
65 bool PartSet_OperationCreateFeature::canProcessKind(const std::string& theId)
67 return theId == SKETCH_LINE_KIND || theId == SKETCH_POINT_KIND || theId == SKETCH_CIRCLE_KIND ||
68 theId == SKETCH_ARC_KIND;
71 bool PartSet_OperationCreateFeature::canBeCommitted() const
73 return myPointSelectionMode == SM_DonePoint;
76 bool PartSet_OperationCreateFeature::isGranted(ModuleBase_IOperation* theOperation) const
78 return theOperation->getDescription()->operationId().toStdString() == PartSet_OperationSketch::Type();
81 std::list<int> PartSet_OperationCreateFeature::getSelectionModes(FeaturePtr theFeature) const
83 std::list<int> aModes;
84 if (theFeature != feature())
85 aModes = PartSet_OperationSketchBase::getSelectionModes(theFeature);
89 void PartSet_OperationCreateFeature::init(FeaturePtr theFeature,
90 const std::list<XGUI_ViewerPrs>& /*theSelected*/,
91 const std::list<XGUI_ViewerPrs>& /*theHighlighted*/)
93 if (!theFeature || theFeature->getKind() != SKETCH_LINE_KIND)
95 myInitFeature = theFeature;
98 FeaturePtr PartSet_OperationCreateFeature::sketch() const
100 return myFeaturePrs->sketch();
103 void PartSet_OperationCreateFeature::mouseReleased(QMouseEvent* theEvent, Handle(V3d_View) theView,
104 const std::list<XGUI_ViewerPrs>& theSelected,
105 const std::list<XGUI_ViewerPrs>& /*theHighlighted*/)
107 if (myPointSelectionMode == SM_DonePoint)
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
112 restartOperation(feature()->getKind(), feature());
118 gp_Pnt aPoint = PartSet_Tools::convertClickToPoint(theEvent->pos(), theView);
119 if (theSelected.empty()) {
120 PartSet_Tools::convertTo2D(aPoint, sketch(), theView, aX, anY);
123 XGUI_ViewerPrs aPrs = theSelected.front();
124 const TopoDS_Shape& aShape = aPrs.shape();
125 if (!aShape.IsNull()) // the point is selected
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);
133 myFeaturePrs->setConstraints(aX, anY, myPointSelectionMode);
136 else if (aShape.ShapeType() == TopAbs_EDGE) // the line is selected
138 PartSet_Tools::convertTo2D(aPoint, sketch(), theView, aX, anY);
139 // move to selected line
140 if (feature()->getKind() == SKETCH_LINE_KIND) {
141 PartSet_FeatureLinePrs* aLinePrs = dynamic_cast<PartSet_FeatureLinePrs*>(myFeaturePrs);
143 FeaturePtr aFeature = aPrs.feature();
144 aLinePrs->projectPointOnLine(aFeature, myPointSelectionMode, aPoint, theView, aX, anY);
151 switch (myPointSelectionMode)
155 case SM_ThirdPoint: {
156 PartSet_SelectionMode aMode = myFeaturePrs->setPoint(aX, anY, myPointSelectionMode);
158 setPointSelectionMode(aMode);
166 void PartSet_OperationCreateFeature::mouseMoved(QMouseEvent* theEvent, Handle(V3d_View) theView)
168 switch (myPointSelectionMode)
175 gp_Pnt aPoint = PartSet_Tools::convertClickToPoint(theEvent->pos(), theView);
176 PartSet_Tools::convertTo2D(aPoint, sketch(), theView, aX, anY);
177 myFeaturePrs->setPoint(aX, anY, myPointSelectionMode);
180 emit focusActivated(myFeaturePrs->getAttribute(myPointSelectionMode));
186 restartOperation(feature()->getKind(), feature());
193 void PartSet_OperationCreateFeature::keyReleased(std::string theName, QKeyEvent* theEvent)
195 int aKeyType = theEvent->key();
196 // the second point should be activated by any modification in the property panel
197 if (!theName.empty() /*&& aKeyType == Qt::Key_Return*/)
199 setPointSelectionMode(myFeaturePrs->getNextMode(theName), false);
201 keyReleased(theEvent->key());
204 void PartSet_OperationCreateFeature::keyReleased(const int theKey)
207 case Qt::Key_Return: {
208 if (myPointSelectionMode == SM_DonePoint)
211 // it start a new line creation at a free point
212 restartOperation(feature()->getKind(), FeaturePtr()/*feature()*/);
216 //restartOperation(feature()->getKind(), FeaturePtr());
219 case Qt::Key_Escape: {
220 if (myPointSelectionMode == SM_DonePoint)
234 void PartSet_OperationCreateFeature::startOperation()
236 PartSet_OperationSketchBase::startOperation();
237 setPointSelectionMode(!myInitFeature ? SM_FirstPoint : SM_SecondPoint);
239 emit multiSelectionEnabled(false);
242 void PartSet_OperationCreateFeature::abortOperation()
244 emit featureConstructed(feature(), FM_Hide);
245 PartSet_OperationSketchBase::abortOperation();
248 void PartSet_OperationCreateFeature::stopOperation()
250 PartSet_OperationSketchBase::stopOperation();
251 emit multiSelectionEnabled(true);
254 void PartSet_OperationCreateFeature::afterCommitOperation()
256 PartSet_OperationSketchBase::afterCommitOperation();
257 emit featureConstructed(feature(), FM_Deactivation);
260 FeaturePtr PartSet_OperationCreateFeature::createFeature(const bool theFlushMessage)
262 FeaturePtr aNewFeature = ModuleBase_Operation::createFeature(false);
264 boost::shared_ptr<SketchPlugin_Feature> aFeature =
265 boost::dynamic_pointer_cast<SketchPlugin_Feature>(sketch());
267 aFeature->addSub(aNewFeature);
269 myFeaturePrs->init(aNewFeature, myInitFeature);
271 emit featureConstructed(aNewFeature, FM_Activation);
277 void PartSet_OperationCreateFeature::setPointSelectionMode(const PartSet_SelectionMode& theMode,
278 const bool isToEmitSignal)
280 myPointSelectionMode = theMode;
281 if (isToEmitSignal) {
282 std::string aName = myFeaturePrs->getAttribute(theMode);
283 if (aName.empty() && theMode == SM_DonePoint) {
284 aName = XGUI::PROP_PANEL_OK;
286 emit focusActivated(aName);