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_FeatureLengthPrs.h>
23 #include <PartSet_FeatureDistancePrs.h>
24 #include <PartSet_FeatureRadiusPrs.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_FeatureLengthPrs::getKind() ||
68 theId == PartSet_FeatureDistancePrs::getKind() ||
69 theId == PartSet_FeatureRadiusPrs::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 if (PartSet_Tools::featureValue(feature(), CONSTRAINT_ATTR_VALUE, aValue)) {
132 showEditor(theEvent, aValue);
133 setPointSelectionMode(SM_ThirdPoint/*aMode*/);
142 void PartSet_OperationCreateConstraint::mouseMoved(QMouseEvent* theEvent, Handle(V3d_View) theView)
145 switch (myPointSelectionMode)
147 //case SM_FirstPoint:
149 //case SM_ThirdPoint:
152 gp_Pnt aPoint = PartSet_Tools::convertClickToPoint(theEvent->pos(), theView);
153 PartSet_Tools::convertTo2D(aPoint, sketch(), theView, aX, anY);
154 /*if (myPointSelectionMode == SM_ThirdPoint) {
155 if (feature()->getKind() == SKETCH_ARC_KIND) {
156 boost::shared_ptr<PartSet_FeatureArcPrs> anArcPrs =
157 boost::dynamic_pointer_cast<PartSet_FeatureArcPrs>(myFeaturePrs);
159 anArcPrs->projectPointOnArc(aPoint, theView, aX, anY);
163 myFeaturePrs->setPoint(aX, anY, myPointSelectionMode);
166 emit focusActivated(myFeaturePrs->getAttribute(myPointSelectionMode));
172 restartOperation(feature()->getKind(), feature());
179 void PartSet_OperationCreateConstraint::keyReleased(std::string theName, QKeyEvent* theEvent)
181 int aKeyType = theEvent->key();
182 // the second point should be activated by any modification in the property panel
183 if (!theName.empty() /*&& aKeyType == Qt::Key_Return*/)
185 setPointSelectionMode(myFeaturePrs->getNextMode(theName), false);
187 keyReleased(theEvent->key());
190 void PartSet_OperationCreateConstraint::keyReleased(const int theKey)
193 case Qt::Key_Return: {
194 if (myPointSelectionMode == SM_DonePoint)
197 // it start a new line creation at a free point
198 restartOperation(feature()->getKind(), FeaturePtr()/*feature()*/);
201 // the modification is really need until the focus of editor do not accept the focus
202 if (myPointSelectionMode == SM_ThirdPoint) {
203 if (myEditor->isStarted())
206 // it start a new line creation at a free point
207 restartOperation(feature()->getKind(), FeaturePtr()/*feature()*/);
211 case Qt::Key_Escape: {
212 if (myEditor->isStarted())
214 if (myPointSelectionMode == SM_DonePoint)
228 void PartSet_OperationCreateConstraint::startOperation()
230 PartSet_OperationSketchBase::startOperation();
231 setPointSelectionMode(!myInitFeature ? SM_FirstPoint : SM_SecondPoint);
233 emit multiSelectionEnabled(false);
236 void PartSet_OperationCreateConstraint::abortOperation()
238 emit featureConstructed(feature(), FM_Hide);
239 PartSet_OperationSketchBase::abortOperation();
242 void PartSet_OperationCreateConstraint::stopOperation()
244 PartSet_OperationSketchBase::stopOperation();
245 emit multiSelectionEnabled(true);
248 void PartSet_OperationCreateConstraint::afterCommitOperation()
250 PartSet_OperationSketchBase::afterCommitOperation();
251 emit featureConstructed(feature(), FM_Deactivation);
254 FeaturePtr PartSet_OperationCreateConstraint::createFeature(const bool theFlushMessage)
256 FeaturePtr aNewFeature = ModuleBase_Operation::createFeature(false);
258 boost::shared_ptr<SketchPlugin_Feature> aFeature =
259 boost::dynamic_pointer_cast<SketchPlugin_Feature>(sketch());
261 aFeature->addSub(aNewFeature);
263 myFeaturePrs->init(aNewFeature);
265 myFeaturePrs->setFeature(myInitFeature, SM_FirstPoint);
267 emit featureConstructed(aNewFeature, FM_Activation);
273 void PartSet_OperationCreateConstraint::setPointSelectionMode(const PartSet_SelectionMode& theMode,
274 const bool isToEmitSignal)
276 myPointSelectionMode = theMode;
277 if (isToEmitSignal) {
278 std::string aName = myFeaturePrs->getAttribute(theMode);
279 if (aName.empty() && theMode == SM_DonePoint) {
280 aName = XGUI::PROP_PANEL_OK;
282 emit focusActivated(aName);
286 void PartSet_OperationCreateConstraint::showEditor(QMouseEvent* theEvent, double theValue)
289 QPoint aPos = theEvent->globalPos();
290 myEditor->start(aPos, theValue);
293 void PartSet_OperationCreateConstraint::onEditStopped(double theValue)
295 PartSet_Tools::setFeatureValue(feature(), theValue, CONSTRAINT_ATTR_VALUE);
299 restartOperation(feature()->getKind(), FeaturePtr());