1 // File: PartSet_OperationCreateConstraint.h
2 // Created: 20 Apr 2014
3 // Author: Natalia ERMOLAEVA
5 #include <PartSet_OperationCreateConstraint.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 <SketchPlugin_ConstraintLength.h>
22 #include <ModuleBase_OperationDescription.h>
24 #include <XGUI_ViewerPrs.h>
25 #include <XGUI_Constants.h>
27 #include <V3d_View.hxx>
28 #include <TopoDS_Vertex.hxx>
30 #include <BRep_Tool.hxx>
36 #include <QMouseEvent>
40 PartSet_OperationCreateConstraint::PartSet_OperationCreateConstraint(const QString& theId,
42 FeaturePtr theFeature)
43 : PartSet_OperationSketchBase(theId, theParent),
44 myPointSelectionMode(SM_FirstPoint)
46 std::string aKind = theId.toStdString();
47 myFeaturePrs = PartSet_Tools::createFeaturePrs(aKind, theFeature);
50 PartSet_OperationCreateConstraint::~PartSet_OperationCreateConstraint()
54 bool PartSet_OperationCreateConstraint::canProcessKind(const std::string& theId)
57 return theId == SKETCH_CONSTRAINT_LENGTH_KIND;
60 bool PartSet_OperationCreateConstraint::canBeCommitted() const
62 return myPointSelectionMode == SM_DonePoint;
65 bool PartSet_OperationCreateConstraint::isGranted(ModuleBase_IOperation* theOperation) const
67 return theOperation->getDescription()->operationId().toStdString() == PartSet_OperationSketch::Type();
70 std::list<int> PartSet_OperationCreateConstraint::getSelectionModes(FeaturePtr theFeature) const
72 std::list<int> aModes;
73 if (theFeature != feature())
74 aModes = PartSet_OperationSketchBase::getSelectionModes(theFeature);
78 void PartSet_OperationCreateConstraint::init(FeaturePtr theFeature,
79 const std::list<XGUI_ViewerPrs>& /*theSelected*/,
80 const std::list<XGUI_ViewerPrs>& /*theHighlighted*/)
83 if (!theFeature/* || theFeature->getKind() != SKETCH_LINE_KIND*/)
85 myInitFeature = theFeature;
88 FeaturePtr PartSet_OperationCreateConstraint::sketch() const
90 return myFeaturePrs->sketch();
93 void PartSet_OperationCreateConstraint::mouseReleased(QMouseEvent* theEvent, Handle(V3d_View) theView,
94 const std::list<XGUI_ViewerPrs>& theSelected,
95 const std::list<XGUI_ViewerPrs>& /*theHighlighted*/)
97 switch (myPointSelectionMode)
100 if (!theSelected.empty()) {
101 XGUI_ViewerPrs aPrs = theSelected.front();
102 FeaturePtr aFeature = aPrs.feature();
104 myFeaturePrs->init(feature(), aFeature);
106 setPointSelectionMode(SM_SecondPoint);
110 case SM_SecondPoint: {
112 gp_Pnt aPoint = PartSet_Tools::convertClickToPoint(theEvent->pos(), theView);
113 PartSet_Tools::convertTo2D(aPoint, sketch(), theView, aX, anY);
115 PartSet_SelectionMode aMode = myFeaturePrs->setPoint(aX, anY, myPointSelectionMode);
117 // show value edit dialog
118 //setPointSelectionMode(aMode);
120 restartOperation(feature()->getKind(), FeaturePtr());
128 void PartSet_OperationCreateConstraint::mouseMoved(QMouseEvent* theEvent, Handle(V3d_View) theView)
131 switch (myPointSelectionMode)
133 //case SM_FirstPoint:
135 //case SM_ThirdPoint:
138 gp_Pnt aPoint = PartSet_Tools::convertClickToPoint(theEvent->pos(), theView);
139 PartSet_Tools::convertTo2D(aPoint, sketch(), theView, aX, anY);
140 /*if (myPointSelectionMode == SM_ThirdPoint) {
141 if (feature()->getKind() == SKETCH_ARC_KIND) {
142 boost::shared_ptr<PartSet_FeatureArcPrs> anArcPrs =
143 boost::dynamic_pointer_cast<PartSet_FeatureArcPrs>(myFeaturePrs);
145 anArcPrs->projectPointOnArc(aPoint, theView, aX, anY);
149 myFeaturePrs->setPoint(aX, anY, myPointSelectionMode);
152 emit focusActivated(myFeaturePrs->getAttribute(myPointSelectionMode));
158 restartOperation(feature()->getKind(), feature());
165 void PartSet_OperationCreateConstraint::keyReleased(std::string theName, QKeyEvent* theEvent)
167 int aKeyType = theEvent->key();
168 // the second point should be activated by any modification in the property panel
169 if (!theName.empty() /*&& aKeyType == Qt::Key_Return*/)
171 setPointSelectionMode(myFeaturePrs->getNextMode(theName), false);
173 keyReleased(theEvent->key());
176 void PartSet_OperationCreateConstraint::keyReleased(const int theKey)
179 case Qt::Key_Return: {
180 if (myPointSelectionMode == SM_DonePoint)
183 // it start a new line creation at a free point
184 restartOperation(feature()->getKind(), FeaturePtr()/*feature()*/);
188 //restartOperation(feature()->getKind(), FeaturePtr());
191 case Qt::Key_Escape: {
192 if (myPointSelectionMode == SM_DonePoint)
206 void PartSet_OperationCreateConstraint::startOperation()
208 PartSet_OperationSketchBase::startOperation();
209 setPointSelectionMode(!myInitFeature ? SM_FirstPoint : SM_SecondPoint);
211 emit multiSelectionEnabled(false);
214 void PartSet_OperationCreateConstraint::abortOperation()
216 emit featureConstructed(feature(), FM_Hide);
217 PartSet_OperationSketchBase::abortOperation();
220 void PartSet_OperationCreateConstraint::stopOperation()
222 PartSet_OperationSketchBase::stopOperation();
223 emit multiSelectionEnabled(true);
226 void PartSet_OperationCreateConstraint::afterCommitOperation()
228 PartSet_OperationSketchBase::afterCommitOperation();
229 emit featureConstructed(feature(), FM_Deactivation);
232 FeaturePtr PartSet_OperationCreateConstraint::createFeature(const bool theFlushMessage)
234 FeaturePtr aNewFeature = ModuleBase_Operation::createFeature(false);
236 boost::shared_ptr<SketchPlugin_Feature> aFeature =
237 boost::dynamic_pointer_cast<SketchPlugin_Feature>(sketch());
239 aFeature->addSub(aNewFeature);
241 myFeaturePrs->init(aNewFeature, myInitFeature);
243 emit featureConstructed(aNewFeature, FM_Activation);
249 void PartSet_OperationCreateConstraint::setPointSelectionMode(const PartSet_SelectionMode& theMode,
250 const bool isToEmitSignal)
252 myPointSelectionMode = theMode;
253 if (isToEmitSignal) {
254 std::string aName = myFeaturePrs->getAttribute(theMode);
255 if (aName.empty() && theMode == SM_DonePoint) {
256 aName = XGUI::PROP_PANEL_OK;
258 emit focusActivated(aName);