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>
13 #include <PartSet_EditLine.h>
15 #include <SketchPlugin_Feature.h>
16 /*#include <SketchPlugin_Point.h>
17 #include <SketchPlugin_Line.h>
18 #include <SketchPlugin_Circle.h>
19 #include <SketchPlugin_Arc.h>
22 #include <PartSet_ConstraintLengthPrs.h>
23 #include <PartSet_ConstraintDistancePrs.h>
24 #include <PartSet_ConstraintRadiusPrs.h>
26 #include <SketchPlugin_Constraint.h>
28 #include <ModuleBase_OperationDescription.h>
30 #include <XGUI_ViewerPrs.h>
31 #include <XGUI_Constants.h>
33 #include <V3d_View.hxx>
34 #include <TopoDS_Vertex.hxx>
36 #include <BRep_Tool.hxx>
42 #include <QMouseEvent>
46 PartSet_OperationCreateConstraint::PartSet_OperationCreateConstraint(const QString& theId,
48 FeaturePtr theFeature)
49 : PartSet_OperationSketchBase(theId, theParent),
50 myPointSelectionMode(SM_FirstPoint)
52 std::string aKind = theId.toStdString();
53 myFeaturePrs = PartSet_Tools::createFeaturePrs(aKind, theFeature);
56 myEditor = new PartSet_EditLine(0);
57 connect(myEditor, SIGNAL(stopped(double)), this, SLOT(onEditStopped(double)));
60 PartSet_OperationCreateConstraint::~PartSet_OperationCreateConstraint()
64 bool PartSet_OperationCreateConstraint::canProcessKind(const std::string& theId)
67 return theId == PartSet_ConstraintLengthPrs::getKind() ||
68 theId == PartSet_ConstraintDistancePrs::getKind() ||
69 theId == PartSet_ConstraintRadiusPrs::getKind();
72 bool PartSet_OperationCreateConstraint::canBeCommitted() const
74 return myPointSelectionMode == SM_DonePoint;
77 bool PartSet_OperationCreateConstraint::isGranted(ModuleBase_IOperation* theOperation) const
79 return theOperation->getDescription()->operationId().toStdString() == PartSet_OperationSketch::Type();
82 std::list<int> PartSet_OperationCreateConstraint::getSelectionModes(FeaturePtr theFeature) const
84 std::list<int> aModes;
85 if (theFeature != feature())
86 aModes = PartSet_OperationSketchBase::getSelectionModes(theFeature);
90 void PartSet_OperationCreateConstraint::init(FeaturePtr theFeature,
91 const std::list<XGUI_ViewerPrs>& /*theSelected*/,
92 const std::list<XGUI_ViewerPrs>& /*theHighlighted*/)
95 if (!theFeature/* || theFeature->getKind() != SKETCH_LINE_KIND*/)
97 myInitFeature = theFeature;
100 FeaturePtr PartSet_OperationCreateConstraint::sketch() const
102 return myFeaturePrs->sketch();
105 void PartSet_OperationCreateConstraint::mouseReleased(QMouseEvent* theEvent, Handle(V3d_View) theView,
106 const std::list<XGUI_ViewerPrs>& theSelected,
107 const std::list<XGUI_ViewerPrs>& /*theHighlighted*/)
109 switch (myPointSelectionMode)
111 case SM_FirstPoint: {
112 if (!theSelected.empty()) {
113 XGUI_ViewerPrs aPrs = theSelected.front();
114 FeaturePtr aFeature = aPrs.feature();
116 if (myFeaturePrs->setFeature(aFeature, myPointSelectionMode)) {
118 setPointSelectionMode(SM_SecondPoint);
123 case SM_SecondPoint: {
125 gp_Pnt aPoint = PartSet_Tools::convertClickToPoint(theEvent->pos(), theView);
126 PartSet_Tools::convertTo2D(aPoint, sketch(), theView, aX, anY);
128 PartSet_SelectionMode aMode = myFeaturePrs->setPoint(aX, anY, myPointSelectionMode);
129 // show value edit dialog
131 double aValue = PartSet_Tools::featureValue(feature(), CONSTRAINT_ATTR_VALUE, isValid);
133 showEditor(theEvent, aValue);
134 setPointSelectionMode(SM_ThirdPoint/*aMode*/);
143 void PartSet_OperationCreateConstraint::mouseMoved(QMouseEvent* theEvent, Handle(V3d_View) theView)
146 switch (myPointSelectionMode)
148 //case SM_FirstPoint:
150 //case SM_ThirdPoint:
153 gp_Pnt aPoint = PartSet_Tools::convertClickToPoint(theEvent->pos(), theView);
154 PartSet_Tools::convertTo2D(aPoint, sketch(), theView, aX, anY);
155 if (feature()->getKind() == PartSet_ConstraintRadiusPrs::getKind()) {
156 boost::shared_ptr<PartSet_ConstraintRadiusPrs> anArcPrs =
157 boost::dynamic_pointer_cast<PartSet_ConstraintRadiusPrs>(myFeaturePrs);
159 anArcPrs->projectPointOnFeature(feature(), sketch(), aPoint, theView, aX, anY);
162 myFeaturePrs->setPoint(aX, anY, myPointSelectionMode);
165 emit focusActivated(myFeaturePrs->getAttribute(myPointSelectionMode));
171 restartOperation(feature()->getKind(), feature());
178 void PartSet_OperationCreateConstraint::keyReleased(std::string theName, QKeyEvent* theEvent)
180 int aKeyType = theEvent->key();
181 // the second point should be activated by any modification in the property panel
182 if (!theName.empty() /*&& aKeyType == Qt::Key_Return*/)
184 setPointSelectionMode(myFeaturePrs->getNextMode(theName), false);
186 keyReleased(theEvent->key());
189 void PartSet_OperationCreateConstraint::keyReleased(const int theKey)
192 case Qt::Key_Return: {
193 if (myPointSelectionMode == SM_DonePoint)
196 // it start a new line creation at a free point
197 restartOperation(feature()->getKind(), FeaturePtr()/*feature()*/);
200 // the modification is really need until the focus of editor do not accept the focus
201 if (myPointSelectionMode == SM_ThirdPoint) {
202 if (myEditor->isStarted())
205 // it start a new line creation at a free point
206 restartOperation(feature()->getKind(), FeaturePtr()/*feature()*/);
210 case Qt::Key_Escape: {
211 if (myEditor->isStarted())
213 if (myPointSelectionMode == SM_DonePoint)
227 void PartSet_OperationCreateConstraint::startOperation()
229 PartSet_OperationSketchBase::startOperation();
230 setPointSelectionMode(!myInitFeature ? SM_FirstPoint : SM_SecondPoint);
232 emit multiSelectionEnabled(false);
235 void PartSet_OperationCreateConstraint::abortOperation()
237 emit featureConstructed(feature(), FM_Hide);
238 PartSet_OperationSketchBase::abortOperation();
241 void PartSet_OperationCreateConstraint::stopOperation()
243 PartSet_OperationSketchBase::stopOperation();
244 emit multiSelectionEnabled(true);
247 void PartSet_OperationCreateConstraint::afterCommitOperation()
249 PartSet_OperationSketchBase::afterCommitOperation();
250 emit featureConstructed(feature(), FM_Deactivation);
253 FeaturePtr PartSet_OperationCreateConstraint::createFeature(const bool theFlushMessage)
255 FeaturePtr aNewFeature = ModuleBase_Operation::createFeature(false);
257 boost::shared_ptr<SketchPlugin_Feature> aFeature =
258 boost::dynamic_pointer_cast<SketchPlugin_Feature>(sketch());
260 aFeature->addSub(aNewFeature);
262 myFeaturePrs->init(aNewFeature);
264 myFeaturePrs->setFeature(myInitFeature, SM_FirstPoint);
266 emit featureConstructed(aNewFeature, FM_Activation);
272 void PartSet_OperationCreateConstraint::setPointSelectionMode(const PartSet_SelectionMode& theMode,
273 const bool isToEmitSignal)
275 myPointSelectionMode = theMode;
276 if (isToEmitSignal) {
277 std::string aName = myFeaturePrs->getAttribute(theMode);
278 if (aName.empty() && theMode == SM_DonePoint) {
279 aName = XGUI::PROP_PANEL_OK;
281 emit focusActivated(aName);
285 void PartSet_OperationCreateConstraint::showEditor(QMouseEvent* theEvent, double theValue)
288 QPoint aPos = theEvent->globalPos();
289 myEditor->start(aPos, theValue);
292 void PartSet_OperationCreateConstraint::onEditStopped(double theValue)
294 PartSet_Tools::setFeatureValue(feature(), theValue, CONSTRAINT_ATTR_VALUE);
298 restartOperation(feature()->getKind(), FeaturePtr());