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_ConstraintRadiusPrs.h>
24 #include <SketchPlugin_Constraint.h>
26 #include <ModuleBase_OperationDescription.h>
28 #include <XGUI_ViewerPrs.h>
29 #include <XGUI_Constants.h>
31 #include <V3d_View.hxx>
32 #include <TopoDS_Vertex.hxx>
34 #include <BRep_Tool.hxx>
40 #include <QMouseEvent>
44 PartSet_OperationCreateConstraint::PartSet_OperationCreateConstraint(const QString& theId,
46 FeaturePtr theFeature)
47 : PartSet_OperationSketchBase(theId, theParent),
48 myPointSelectionMode(SM_FirstPoint)
50 std::string aKind = theId.toStdString();
51 myFeaturePrs = PartSet_Tools::createFeaturePrs(aKind, theFeature);
54 myEditor = new PartSet_EditLine(0);
55 connect(myEditor, SIGNAL(stopped(double)), this, SLOT(onEditStopped(double)));
58 PartSet_OperationCreateConstraint::~PartSet_OperationCreateConstraint()
62 bool PartSet_OperationCreateConstraint::canProcessKind(const std::string& theId)
65 return PartSet_Tools::isConstraintFeature(theId);
68 bool PartSet_OperationCreateConstraint::canBeCommitted() const
70 return myPointSelectionMode == SM_DonePoint;
73 bool PartSet_OperationCreateConstraint::isGranted(ModuleBase_IOperation* theOperation) const
75 return theOperation->getDescription()->operationId().toStdString() == PartSet_OperationSketch::Type();
78 std::list<int> PartSet_OperationCreateConstraint::getSelectionModes(FeaturePtr theFeature) const
80 std::list<int> aModes;
81 if (theFeature != feature())
82 aModes = PartSet_OperationSketchBase::getSelectionModes(theFeature);
86 void PartSet_OperationCreateConstraint::init(FeaturePtr theFeature,
87 const std::list<XGUI_ViewerPrs>& /*theSelected*/,
88 const std::list<XGUI_ViewerPrs>& /*theHighlighted*/)
91 if (!theFeature/* || theFeature->getKind() != SKETCH_LINE_KIND*/)
93 myInitFeature = theFeature;
96 FeaturePtr PartSet_OperationCreateConstraint::sketch() const
98 return myFeaturePrs->sketch();
101 void PartSet_OperationCreateConstraint::mouseReleased(QMouseEvent* theEvent, Handle(V3d_View) theView,
102 const std::list<XGUI_ViewerPrs>& theSelected,
103 const std::list<XGUI_ViewerPrs>& /*theHighlighted*/)
105 switch (myPointSelectionMode)
108 case SM_SecondPoint: {
109 if (!theSelected.empty()) {
110 XGUI_ViewerPrs aPrs = theSelected.front();
111 FeaturePtr aFeature = aPrs.feature();
113 PartSet_SelectionMode aMode = myFeaturePrs->setFeature(aFeature, myPointSelectionMode);
114 if (aMode != myPointSelectionMode) {
116 setPointSelectionMode(aMode);
123 gp_Pnt aPoint = PartSet_Tools::convertClickToPoint(theEvent->pos(), theView);
124 PartSet_Tools::convertTo2D(aPoint, sketch(), theView, aX, anY);
126 PartSet_SelectionMode aMode = myFeaturePrs->setPoint(aX, anY, myPointSelectionMode);
127 // show value edit dialog
129 double aValue = PartSet_Tools::featureValue(feature(), CONSTRAINT_ATTR_VALUE, isValid);
131 showEditor(theEvent, aValue);
140 void PartSet_OperationCreateConstraint::mouseMoved(QMouseEvent* theEvent, Handle(V3d_View) theView)
143 switch (myPointSelectionMode)
145 //case SM_FirstPoint:
146 //case SM_SecondPoint:
150 gp_Pnt aPoint = PartSet_Tools::convertClickToPoint(theEvent->pos(), theView);
151 PartSet_Tools::convertTo2D(aPoint, sketch(), theView, aX, anY);
152 if (feature()->getKind() == PartSet_ConstraintRadiusPrs::getKind()) {
153 boost::shared_ptr<PartSet_ConstraintRadiusPrs> anArcPrs =
154 boost::dynamic_pointer_cast<PartSet_ConstraintRadiusPrs>(myFeaturePrs);
156 anArcPrs->projectPointOnFeature(feature(), sketch(), aPoint, theView, aX, anY);
159 myFeaturePrs->setPoint(aX, anY, myPointSelectionMode);
162 emit focusActivated(myFeaturePrs->getAttribute(myPointSelectionMode));
168 restartOperation(feature()->getKind(), feature());
175 void PartSet_OperationCreateConstraint::keyReleased(std::string theName, QKeyEvent* theEvent)
177 int aKeyType = theEvent->key();
178 // the second point should be activated by any modification in the property panel
179 if (!theName.empty() /*&& aKeyType == Qt::Key_Return*/)
181 setPointSelectionMode(myFeaturePrs->getNextMode(theName), false);
183 keyReleased(theEvent->key());
186 void PartSet_OperationCreateConstraint::keyReleased(const int theKey)
189 case Qt::Key_Return: {
190 if (myPointSelectionMode == SM_DonePoint)
193 // it start a new line creation at a free point
194 restartOperation(feature()->getKind(), FeaturePtr()/*feature()*/);
197 // the modification is really need until the focus of editor do not accept the focus
198 if (myPointSelectionMode == SM_LastPoint && myEditor->isStarted()) {
201 // it start a new line creation at a free point
202 restartOperation(feature()->getKind(), FeaturePtr()/*feature()*/);
206 case Qt::Key_Escape: {
207 if (myEditor->isStarted())
209 if (myPointSelectionMode == SM_DonePoint)
223 void PartSet_OperationCreateConstraint::startOperation()
225 PartSet_OperationSketchBase::startOperation();
226 //setPointSelectionMode(!myInitFeature ? SM_FirstPoint : SM_SecondPoint);
228 emit multiSelectionEnabled(false);
231 void PartSet_OperationCreateConstraint::abortOperation()
233 emit featureConstructed(feature(), FM_Hide);
234 PartSet_OperationSketchBase::abortOperation();
237 void PartSet_OperationCreateConstraint::stopOperation()
239 PartSet_OperationSketchBase::stopOperation();
240 emit multiSelectionEnabled(true);
243 void PartSet_OperationCreateConstraint::afterCommitOperation()
245 PartSet_OperationSketchBase::afterCommitOperation();
246 emit featureConstructed(feature(), FM_Deactivation);
249 FeaturePtr PartSet_OperationCreateConstraint::createFeature(const bool theFlushMessage)
251 FeaturePtr aNewFeature = ModuleBase_Operation::createFeature(false);
253 boost::shared_ptr<SketchPlugin_Feature> aFeature =
254 boost::dynamic_pointer_cast<SketchPlugin_Feature>(sketch());
256 aFeature->addSub(aNewFeature);
258 myFeaturePrs->init(aNewFeature);
260 PartSet_SelectionMode aMode = myFeaturePrs->setFeature(myInitFeature, SM_FirstPoint);
261 //setPointSelectionMode(aMode);
264 emit featureConstructed(aNewFeature, FM_Activation);
270 void PartSet_OperationCreateConstraint::setPointSelectionMode(const PartSet_SelectionMode& theMode,
271 const bool isToEmitSignal)
273 myPointSelectionMode = theMode;
274 if (isToEmitSignal) {
275 std::string aName = myFeaturePrs->getAttribute(theMode);
276 if (aName.empty() && theMode == SM_DonePoint) {
277 aName = XGUI::PROP_PANEL_OK;
279 emit focusActivated(aName);
283 void PartSet_OperationCreateConstraint::showEditor(QMouseEvent* theEvent, double theValue)
286 QPoint aPos = theEvent->globalPos();
287 myEditor->start(aPos, theValue);
290 void PartSet_OperationCreateConstraint::onEditStopped(double theValue)
292 PartSet_Tools::setFeatureValue(feature(), theValue, CONSTRAINT_ATTR_VALUE);
296 restartOperation(feature()->getKind(), FeaturePtr());