Salome HOME
PLaneGCSSolver: It is not necessary to adjust Angle constraint (issue #1536)
[modules/shaper.git] / src / PartSet / PartSet_WidgetPoint2d.cpp
1 // Copyright (C) 2014-20xx CEA/DEN, EDF R&D
2
3 // File:        PartSet_WidgetPoint2D.cpp
4 // Created:     25 Apr 2014
5 // Author:      Natalia ERMOLAEVA
6
7 #include "PartSet_WidgetPoint2d.h"
8 #include <PartSet_Tools.h>
9 #include <PartSet_Module.h>
10 #include <PartSet_SketcherReetntrantMgr.h>
11
12 #include <XGUI_Tools.h>
13 #include <XGUI_Workshop.h>
14 #include <XGUI_Displayer.h>
15
16 #include <ModuleBase_ParamSpinBox.h>
17 #include <ModuleBase_Tools.h>
18 #include <ModuleBase_IViewer.h>
19 #include <ModuleBase_IViewWindow.h>
20 #include <ModuleBase_ISelection.h>
21 #include <ModuleBase_ViewerPrs.h>
22 #include <ModuleBase_WidgetValidator.h>
23
24 #include <Config_Keywords.h>
25 #include <Config_WidgetAPI.h>
26
27 #include <Events_Loop.h>
28 #include <ModelAPI_Events.h>
29 #include <ModelAPI_AttributeBoolean.h>
30
31 #include <ModelAPI_Feature.h>
32 #include <ModelAPI_Data.h>
33 #include <ModelAPI_Object.h>
34 #include <GeomDataAPI_Point2D.h>
35 #include <GeomAPI_Pnt2d.h>
36
37 #include <GeomAPI_ShapeExplorer.h>
38 #include <GeomAPI_Vertex.h>
39
40 #include <SketchPlugin_Feature.h>
41 #include <SketchPlugin_ConstraintCoincidence.h>
42 #include <SketchPlugin_Line.h>
43 #include <SketchPlugin_Arc.h>
44 #include <SketchPlugin_Circle.h>
45 #include <SketchPlugin_Point.h>
46
47 #include <QGroupBox>
48 #include <QGridLayout>
49 #include <QLabel>
50 #include <QEvent>
51 #include <QMouseEvent>
52 #include <QApplication>
53
54 #include <TopoDS.hxx>
55 #include <TopoDS_Vertex.hxx>
56 #include <BRep_Tool.hxx>
57
58 #include <cfloat>
59 #include <climits>
60
61 const double MaxCoordinate = 1e12;
62
63 static QStringList MyFeaturesForCoincedence;
64
65 PartSet_WidgetPoint2D::PartSet_WidgetPoint2D(QWidget* theParent, 
66                                              ModuleBase_IWorkshop* theWorkshop,
67                                              const Config_WidgetAPI* theData)
68 : ModuleBase_ModelWidget(theParent, theData), myWorkshop(theWorkshop),
69   myValueIsCashed(false), myIsFeatureVisibleInCash(true),
70   myXValueInCash(0), myYValueInCash(0)
71 {
72   if (MyFeaturesForCoincedence.isEmpty()) {
73     MyFeaturesForCoincedence << SketchPlugin_Line::ID().c_str()
74       << SketchPlugin_Arc::ID().c_str()
75       << SketchPlugin_Point::ID().c_str()
76       << SketchPlugin_Circle::ID().c_str();
77   }
78
79   // the control should accept the focus, so the boolean flag is corrected to be true
80   myIsObligatory = true;
81   QString aPageName = QString::fromStdString(theData->getProperty(CONTAINER_PAGE_NAME));
82   myGroupBox = new QGroupBox(aPageName, theParent);
83   myGroupBox->setFlat(false);
84
85   bool aAcceptVariables = theData->getBooleanAttribute(DOUBLE_WDG_ACCEPT_EXPRESSIONS, true);
86
87   QGridLayout* aGroupLay = new QGridLayout(myGroupBox);
88   ModuleBase_Tools::adjustMargins(aGroupLay);
89   aGroupLay->setSpacing(2);
90   aGroupLay->setColumnStretch(1, 1);
91   {
92     QLabel* aLabel = new QLabel(myGroupBox);
93     aLabel->setText(tr("X "));
94     aGroupLay->addWidget(aLabel, 0, 0);
95
96     myXSpin = new ModuleBase_ParamSpinBox(myGroupBox);
97     myXSpin->setAcceptVariables(aAcceptVariables);
98     myXSpin->setMinimum(-DBL_MAX);
99     myXSpin->setMaximum(DBL_MAX);
100     myXSpin->setToolTip(tr("X"));
101     aGroupLay->addWidget(myXSpin, 0, 1);
102
103     connect(myXSpin, SIGNAL(valueChanged(const QString&)), this, SIGNAL(valuesModified()));
104   }
105   {
106     QLabel* aLabel = new QLabel(myGroupBox);
107     aLabel->setText(tr("Y "));
108     aGroupLay->addWidget(aLabel, 1, 0);
109
110     myYSpin = new ModuleBase_ParamSpinBox(myGroupBox);
111     myYSpin->setAcceptVariables(aAcceptVariables);
112     myYSpin->setMinimum(-DBL_MAX);
113     myYSpin->setMaximum(DBL_MAX);
114     myYSpin->setToolTip(tr("Y"));
115     aGroupLay->addWidget(myYSpin, 1, 1);
116
117     connect(myYSpin, SIGNAL(valueChanged(const QString&)), this, SIGNAL(valuesModified()));
118   }
119   QVBoxLayout* aLayout = new QVBoxLayout(this);
120   ModuleBase_Tools::zeroMargins(aLayout);
121   aLayout->addWidget(myGroupBox);
122   setLayout(aLayout);
123
124   myWidgetValidator = new ModuleBase_WidgetValidator(this, myWorkshop);
125 }
126
127 bool PartSet_WidgetPoint2D::isValidSelectionCustom(const ModuleBase_ViewerPrsPtr& theValue)
128 {
129   bool aValid = true;
130
131   PartSet_Module* aModule = dynamic_cast<PartSet_Module*>(myWorkshop->module());
132   if (aModule->sketchReentranceMgr()->isInternalEditActive()) 
133     return true; /// when internal edit is started a new feature is created. I has not results, AIS
134
135   // workaround for feature, where there is no results
136   //if (myFeature->getKind() == "SketchRectangle")
137   //  return true;
138
139   /// the selection is not possible if the current feature has no presentation for the current
140   /// attribute not in AIS not in results. If so, no object in current feature where make
141   /// coincidence, so selection is not necessary
142   std::shared_ptr<ModelAPI_Data> aData = myFeature->data();
143   std::shared_ptr<GeomDataAPI_Point2D> aPointAttr = std::dynamic_pointer_cast<GeomDataAPI_Point2D>(
144       aData->attribute(attributeID()));
145   std::shared_ptr<GeomAPI_Pnt2d> aPoint = aPointAttr->pnt();
146
147   bool aFoundPoint = false;
148   GeomShapePtr anAISShape;
149   GeomPresentablePtr aPrs = std::dynamic_pointer_cast<GeomAPI_IPresentable>(myFeature);
150   if (aPrs.get()) {
151     AISObjectPtr anAIS;
152     anAIS = aPrs->getAISObject(anAIS);
153     if (anAIS.get()) {
154       anAISShape = anAIS->getShape();
155     }
156   }
157   const std::list<std::shared_ptr<ModelAPI_Result> >& aResults = myFeature->results();
158   if (!anAISShape.get() && aResults.empty())
159     return true;
160
161   /// analysis of AIS
162   if (anAISShape.get())
163     aFoundPoint = shapeContainsPoint(anAISShape, aPoint, mySketch);
164
165   /// analysis of results
166   std::list<std::shared_ptr<ModelAPI_Result> >::const_iterator aRIter = aResults.cbegin();
167   for (; aRIter != aResults.cend() && !aFoundPoint; aRIter++) {
168     ResultPtr aResult = *aRIter;
169     if (aResult.get() && aResult->shape().get()) {
170       GeomShapePtr aShape = aResult->shape();
171       aFoundPoint = shapeContainsPoint(aShape, aPoint, mySketch);
172     }
173   }
174   return aFoundPoint;
175 }
176
177 bool PartSet_WidgetPoint2D::resetCustom()
178 {
179   bool aDone = false;
180   if (!isUseReset() || isComputedDefault() || myXSpin->hasVariable() || myYSpin->hasVariable()) {
181     aDone = false;
182   }
183   else {
184     if (myValueIsCashed) {
185       // if the restored value should be hidden, aDone = true to set
186       // reset state for the widget in the parent
187       aDone = restoreCurentValue();
188       emit objectUpdated();
189     }
190     else {
191       bool isOk;
192       double aDefValue = QString::fromStdString(getDefaultValue()).toDouble(&isOk);
193       // it is important to block the spin box control in order to do not through out the
194       // locking of the validating state.
195       ModuleBase_Tools::setSpinValue(myXSpin, isOk ? aDefValue : 0.0);
196       ModuleBase_Tools::setSpinValue(myYSpin, isOk ? aDefValue : 0.0);
197       storeValueCustom();
198       aDone = true;
199     }
200   }
201   return aDone;
202 }
203
204 PartSet_WidgetPoint2D::~PartSet_WidgetPoint2D()
205 {
206 }
207
208 bool PartSet_WidgetPoint2D::setSelection(QList<ModuleBase_ViewerPrsPtr>& theValues,
209                                          const bool theToValidate)
210 {
211   bool isDone = false;
212   if (theValues.empty())
213     return isDone;
214
215   ModuleBase_ViewerPrsPtr aValue = theValues.takeFirst();
216   GeomShapePtr aShape = aValue->shape();
217   if (aShape.get() && !aShape->isNull()) {
218     Handle(V3d_View) aView = myWorkshop->viewer()->activeView();
219     double aX, aY;
220     const TopoDS_Shape& aTDShape = aShape->impl<TopoDS_Shape>();
221     if (getPoint2d(aView, aTDShape, aX, aY)) {
222       isDone = setPoint(aX, aY);
223       PartSet_Tools::setConstraints(mySketch, feature(), attributeID(), aX, aY);
224     }
225   }
226   else if (canBeActivatedByMove()) {
227     if (feature()->getKind() == SketchPlugin_Line::ID()) {
228       FeaturePtr aFeature = std::dynamic_pointer_cast<ModelAPI_Feature>(aValue->object());
229       // Initialize new line with first point equal to end of previous
230       if (aFeature.get()) {
231         std::shared_ptr<ModelAPI_Data> aData = aFeature->data();
232         std::shared_ptr<GeomDataAPI_Point2D> aPoint = 
233           std::dynamic_pointer_cast<GeomDataAPI_Point2D>(
234                                        aData->attribute(SketchPlugin_Line::END_ID()));
235         if (aPoint) {
236           setPoint(aPoint->x(), aPoint->y());
237           PartSet_Tools::setConstraints(mySketch, feature(), attributeID(), aPoint->x(),
238                                         aPoint->y());
239           isDone = true;
240         }
241       }
242     }
243   }
244   return isDone;
245 }
246
247 void PartSet_WidgetPoint2D::selectContent()
248 {
249   myXSpin->selectAll();
250 }
251
252 bool PartSet_WidgetPoint2D::setPoint(double theX, double theY)
253 {
254   if (fabs(theX) >= MaxCoordinate)
255     return false;
256   if (fabs(theY) >= MaxCoordinate)
257     return false;
258
259   ModuleBase_Tools::setSpinValue(myXSpin, theX);
260   ModuleBase_Tools::setSpinValue(myYSpin, theY);
261
262   storeValue();
263   return true;
264 }
265
266 bool PartSet_WidgetPoint2D::storeValueCustom()
267 {
268   std::shared_ptr<ModelAPI_Data> aData = myFeature->data();
269   if (!aData) // can be on abort of sketcher element
270     return false;
271   std::shared_ptr<GeomDataAPI_Point2D> aPoint = std::dynamic_pointer_cast<GeomDataAPI_Point2D>(
272       aData->attribute(attributeID()));
273
274   PartSet_WidgetPoint2D* that = (PartSet_WidgetPoint2D*) this;
275   bool isBlocked = that->blockSignals(true);
276   bool isImmutable = aPoint->setImmutable(true);
277
278   // if text is not empty then setValue will be ignored
279   // so we should set the text at first
280   aPoint->setText(myXSpin->hasVariable() ? myXSpin->text().toStdString() : "",
281                   myYSpin->hasVariable() ? myYSpin->text().toStdString() : "");
282   aPoint->setValue(!myXSpin->hasVariable() ? myXSpin->value() : aPoint->x(),
283                    !myYSpin->hasVariable() ? myYSpin->value() : aPoint->y());
284
285   // after movement the solver will call the update event: optimization
286   moveObject(myFeature);
287   aPoint->setImmutable(isImmutable);
288   that->blockSignals(isBlocked);
289
290   return true;
291 }
292
293 bool PartSet_WidgetPoint2D::restoreValueCustom()
294 {
295   std::shared_ptr<ModelAPI_Data> aData = myFeature->data();
296   std::shared_ptr<GeomDataAPI_Point2D> aPoint = std::dynamic_pointer_cast<GeomDataAPI_Point2D>(
297       aData->attribute(attributeID()));
298   QString aTextX = QString::fromStdString(aPoint->textX());
299   QString aTextY = QString::fromStdString(aPoint->textY());
300
301   bool isDouble = false;
302   double aVal = 0;
303   if (aTextX.isEmpty()) {
304     ModuleBase_Tools::setSpinValue(myXSpin, aPoint->x());
305   } else {
306     aVal = aTextX.toDouble(&isDouble);
307     if (isDouble)
308       ModuleBase_Tools::setSpinValue(myXSpin, aVal);
309     else
310       ModuleBase_Tools::setSpinText(myXSpin, aTextX);
311   }
312   if (aTextY.isEmpty()) {
313     ModuleBase_Tools::setSpinValue(myYSpin, aPoint->y());
314   } else {
315     aVal = aTextY.toDouble(&isDouble);
316     if (isDouble)
317       ModuleBase_Tools::setSpinValue(myYSpin, aVal);
318     else
319       ModuleBase_Tools::setSpinText(myYSpin, aTextY);
320   }
321   //if (aTextX.empty() || aTextY.empty()) {
322   //  ModuleBase_Tools::setSpinValue(myXSpin, aPoint->x());
323   //  ModuleBase_Tools::setSpinValue(myYSpin, aPoint->y());
324   //} else {
325   //  ModuleBase_Tools::setSpinText(myXSpin, QString::fromStdString(aTextX));
326   //  ModuleBase_Tools::setSpinText(myYSpin, QString::fromStdString(aTextY));
327   //}
328   return true;
329 }
330
331 void PartSet_WidgetPoint2D::storeCurentValue()
332 {
333   // do not use cash if a variable is used
334   if (myXSpin->hasVariable() || myYSpin->hasVariable())
335     return;
336
337   myValueIsCashed = true;
338   myIsFeatureVisibleInCash = XGUI_Displayer::isVisible(
339                        XGUI_Tools::workshop(myWorkshop)->displayer(), myFeature);
340   myXValueInCash = myXSpin->value();
341   myYValueInCash = myYSpin->value();
342 }
343
344 bool PartSet_WidgetPoint2D::restoreCurentValue()
345 {
346   bool aRestoredAndHidden = true;
347
348   bool isVisible = myIsFeatureVisibleInCash;
349   // fill the control widgets by the cashed value
350
351   myValueIsCashed = false;
352   myIsFeatureVisibleInCash = true;
353   ModuleBase_Tools::setSpinValue(myXSpin, myXValueInCash);
354   ModuleBase_Tools::setSpinValue(myYSpin, myYValueInCash);
355
356   // store value to the model
357   storeValueCustom();
358   if (isVisible) {
359     setValueState(Stored);
360     aRestoredAndHidden = false;
361   }
362   else
363     aRestoredAndHidden = true;
364
365   return aRestoredAndHidden;
366 }
367
368 QList<QWidget*> PartSet_WidgetPoint2D::getControls() const
369 {
370   QList<QWidget*> aControls;
371   aControls.append(myXSpin);
372   aControls.append(myYSpin);
373   return aControls;
374 }
375
376
377 void PartSet_WidgetPoint2D::activateCustom()
378 {
379   ModuleBase_IViewer* aViewer = myWorkshop->viewer();
380   connect(aViewer, SIGNAL(mouseMove(ModuleBase_IViewWindow*, QMouseEvent*)), 
381           this, SLOT(onMouseMove(ModuleBase_IViewWindow*, QMouseEvent*)));
382   connect(aViewer, SIGNAL(mouseRelease(ModuleBase_IViewWindow*, QMouseEvent*)), 
383           this, SLOT(onMouseRelease(ModuleBase_IViewWindow*, QMouseEvent*)));
384
385   QIntList aModes;
386   aModes << TopAbs_VERTEX;
387   aModes << TopAbs_EDGE;
388   myWorkshop->activateSubShapesSelection(aModes);
389
390   if (!isEditingMode()) {
391     FeaturePtr aFeature = feature();
392     if (aFeature.get() && aFeature->getKind() == SketchPlugin_Point::ID())
393       storeValue();
394   }
395 }
396
397 bool PartSet_WidgetPoint2D::canBeActivatedByMove()
398 {
399   bool aCanBeActivated = false;
400   if (feature()->getKind() == SketchPlugin_Line::ID() &&
401       attributeID() == SketchPlugin_Line::START_ID())
402     aCanBeActivated = true;
403
404   return aCanBeActivated;
405 }
406
407 void PartSet_WidgetPoint2D::deactivate()
408 {
409   // the value of the control should be stored to model if it was not
410   // initialized yet. It is important when we leave this control by Tab key.
411   // It should not be performed by the widget activation as the preview
412   // is visualized with default value. Line point is moved to origin.
413   AttributePtr anAttribute = myFeature->data()->attribute(attributeID());
414   if (anAttribute && !anAttribute->isInitialized())
415     storeValue();
416
417   ModuleBase_ModelWidget::deactivate();
418   ModuleBase_IViewer* aViewer = myWorkshop->viewer();
419   disconnect(aViewer, SIGNAL(mouseMove(ModuleBase_IViewWindow*, QMouseEvent*)),
420              this, SLOT(onMouseMove(ModuleBase_IViewWindow*, QMouseEvent*)));
421   disconnect(aViewer, SIGNAL(mouseRelease(ModuleBase_IViewWindow*, QMouseEvent*)), 
422              this, SLOT(onMouseRelease(ModuleBase_IViewWindow*, QMouseEvent*)));
423
424   myWorkshop->deactivateSubShapesSelection();
425 }
426
427 bool PartSet_WidgetPoint2D::getPoint2d(const Handle(V3d_View)& theView, 
428                                        const TopoDS_Shape& theShape, 
429                                        double& theX, double& theY) const
430 {
431   if (!theShape.IsNull()) {
432     if (theShape.ShapeType() == TopAbs_VERTEX) {
433       const TopoDS_Vertex& aVertex = TopoDS::Vertex(theShape);
434       if (!aVertex.IsNull()) {
435         // A case when point is taken from existing vertex
436         gp_Pnt aPoint = BRep_Tool::Pnt(aVertex);
437         PartSet_Tools::convertTo2D(aPoint, mySketch, theView, theX, theY);
438         return true;
439       }
440     }
441   }
442   return false;
443 }
444
445 bool PartSet_WidgetPoint2D::setConstraintWith(const ObjectPtr& theObject)
446 {
447   std::shared_ptr<GeomDataAPI_Point2D> aFeaturePoint;
448   if (feature()->isMacro()) {
449     AttributePtr aThisAttr = feature()->data()->attribute(attributeID());
450     std::shared_ptr<GeomDataAPI_Point2D> anAttrPoint =
451                                std::dynamic_pointer_cast<GeomDataAPI_Point2D>(aThisAttr);
452     if (anAttrPoint.get()) {
453       // the macro feature will be removed after the operation is stopped, so we need to build
454       // coicidence to possible sub-features
455       aFeaturePoint = PartSet_Tools::findFirstEqualPointInArgumentFeatures(feature(),
456                                                                  anAttrPoint->pnt());
457     }
458   }
459   else {
460     AttributePtr aThisAttr = feature()->data()->attribute(attributeID());
461     aFeaturePoint = std::dynamic_pointer_cast<GeomDataAPI_Point2D>(aThisAttr);
462   }
463   if (!aFeaturePoint.get())
464     return false;
465
466   // Create point-edge coincedence
467   FeaturePtr aFeature = mySketch->addFeature(SketchPlugin_ConstraintCoincidence::ID());
468   std::shared_ptr<ModelAPI_Data> aData = aFeature->data();
469
470   std::shared_ptr<ModelAPI_AttributeRefAttr> aRef1 = std::dynamic_pointer_cast<
471       ModelAPI_AttributeRefAttr>(aData->attribute(SketchPlugin_Constraint::ENTITY_A()));
472
473   aRef1->setAttr(aFeaturePoint);
474
475   std::shared_ptr<ModelAPI_AttributeRefAttr> aRef2 = std::dynamic_pointer_cast<
476       ModelAPI_AttributeRefAttr>(aData->attribute(SketchPlugin_Constraint::ENTITY_B()));
477   aRef2->setObject(theObject);
478
479   // we need to flush created signal in order to coincidence is processed by solver
480   Events_Loop::loop()->flush(Events_Loop::eventByName(EVENT_OBJECT_CREATED));
481
482   return true;
483 }
484
485 void PartSet_WidgetPoint2D::onMouseRelease(ModuleBase_IViewWindow* theWnd, QMouseEvent* theEvent)
486 {
487   // the contex menu release by the right button should not be processed by this widget
488   if (theEvent->button() != Qt::LeftButton)
489     return;
490
491   ModuleBase_ISelection* aSelection = myWorkshop->selection();
492   Handle(V3d_View) aView = theWnd->v3dView();
493
494   QList<ModuleBase_ViewerPrsPtr> aList = aSelection->getSelected(ModuleBase_ISelection::Viewer);
495   ModuleBase_ViewerPrsPtr aFirstValue = aList.size() > 0 ? aList.first() : ModuleBase_ViewerPrsPtr();
496   // if we have selection and use it
497   if (aFirstValue.get() && isValidSelectionCustom(aFirstValue)) {
498     GeomShapePtr aGeomShape = aFirstValue->shape();
499     TopoDS_Shape aShape = aGeomShape->impl<TopoDS_Shape>();
500     ObjectPtr aObject = aFirstValue->object();
501
502     FeaturePtr aSelectedFeature = ModelAPI_Feature::feature(aObject);
503     bool anExternal = false;
504     std::shared_ptr<SketchPlugin_Feature> aSPFeature;
505     if (aSelectedFeature.get() != NULL)
506       aSPFeature = std::dynamic_pointer_cast<SketchPlugin_Feature>(aSelectedFeature);
507       if ((!aSPFeature && !aShape.IsNull()) ||
508           (aSPFeature.get() && aSPFeature->isExternal())) {
509         ResultPtr aFixedObject;
510         anExternal = true;
511         aFixedObject = PartSet_Tools::findFixedObjectByExternal(aShape, aObject, mySketch);
512         if (!aFixedObject.get())
513           aFixedObject = PartSet_Tools::createFixedObjectByExternal(aShape, aObject, mySketch);
514         double aX, aY;
515         if (getPoint2d(aView, aShape, aX, aY) && isFeatureContainsPoint(myFeature, aX, aY)) {
516           // do not create a constraint to the point, which already used by the feature
517           // if the feature contains the point, focus is not switched
518           setPoint(aX, aY);
519         }
520         else {
521           if (getPoint2d(aView, aShape, aX, aY))
522             setPoint(aX, aY);
523           else
524             setValueState(Stored); // in case of edge selection, Apply state should also be updated
525           bool anOrphanPoint = aShape.ShapeType() == TopAbs_VERTEX ||
526                                isOrphanPoint(aSelectedFeature, mySketch, aX, aY);
527           if (anExternal) {
528             anOrphanPoint = true; // we should not stop reentrant operation on external objects because
529             // they are not participate in the contour creation excepting external vertices
530             if (aShape.ShapeType() == TopAbs_VERTEX) {
531               FeaturePtr aFixedFeature = ModelAPI_Feature::feature(aFixedObject);
532               if (aFixedFeature.get() && aFixedFeature->getKind() == SketchPlugin_Point::ID()) {
533                 anOrphanPoint = isOrphanPoint(aFixedFeature, mySketch, aX, aY);
534               }
535             }
536           }
537           if (aFixedObject.get())
538             setConstraintWith(aFixedObject);
539           // fignal updated should be flushed in order to visualize possible created external objects
540           // e.g. selection of trihedron axis when input end arc point
541           updateObject(feature());
542
543           if (!anOrphanPoint)
544             emit vertexSelected(); // it stops the reentrant operation
545
546           emit focusOutWidget(this);
547         }
548       }
549     if (!anExternal) {
550       double aX, aY;
551       bool isProcessed = false;
552       if (getPoint2d(aView, aShape, aX, aY) && isFeatureContainsPoint(myFeature, aX, aY)) {
553         // when the point is selected, the coordinates of the point should be set into the attribute
554         // if the feature contains the point, focus is not switched
555         setPoint(aX, aY);
556       }
557       else {
558         bool anOrphanPoint = isOrphanPoint(aSelectedFeature, mySketch, aX, aY);
559         // do not set a coincidence constraint in the attribute if the feature contains a point
560         // with the same coordinates. It is important for line creation in order to do not set
561         // the same constraints for the same points, oterwise the result line has zero length.
562         bool isAuxiliaryFeature = false;
563         if (getPoint2d(aView, aShape, aX, aY)) {
564           setPoint(aX, aY);
565           feature()->execute();
566           PartSet_Tools::setConstraints(mySketch, feature(), attributeID(), aX, aY);
567         }
568         else if (aShape.ShapeType() == TopAbs_EDGE) {
569           if (!setConstraintWith(aObject)) {
570             gp_Pnt aPoint = PartSet_Tools::convertClickToPoint(theEvent->pos(), theWnd->v3dView());
571             PartSet_Tools::convertTo2D(aPoint, mySketch, aView, aX, aY);
572             setPoint(aX, aY);
573           }
574           setValueState(Stored); // in case of edge selection, Apply state should also be updated
575           isAuxiliaryFeature = PartSet_Tools::isAuxiliarySketchEntity(aObject);
576         }
577         // it is important to perform updateObject() in order to the current value is 
578         // processed by Sketch Solver. Test case: line is created from a previous point
579         // to some distance, but in the area of the highlighting of the point. Constraint
580         // coincidence is created, after the solver is performed, the distance between the
581         // points of the line becomes less than the tolerance. Validator of the line returns
582         // false, the line will be aborted, but sketch stays valid.
583         updateObject(feature());
584         if (!anOrphanPoint && !anExternal && !isAuxiliaryFeature)
585           emit vertexSelected();
586         emit focusOutWidget(this);
587       }
588     }
589   }
590   // End of Bug dependent fragment
591   else {
592     // A case when point is taken from mouse event
593     gp_Pnt aPoint = PartSet_Tools::convertClickToPoint(theEvent->pos(), theWnd->v3dView());
594     double aX, anY;
595     PartSet_Tools::convertTo2D(aPoint, mySketch, aView, aX, anY);
596
597     // if the feature contains the point, focus is not switched
598     if (!setPoint(aX, anY) || isFeatureContainsPoint(myFeature, aX, anY))
599       return;
600
601     /// Start alternative code
602     //std::shared_ptr<GeomDataAPI_Point2D> aFeaturePoint = std::dynamic_pointer_cast<
603     //    GeomDataAPI_Point2D>(feature()->data()->attribute(attributeID()));
604     //QList<FeaturePtr> aIgnore;
605     //aIgnore.append(feature());
606
607     //double aTolerance = aView->Convert(7);
608     //std::shared_ptr<GeomDataAPI_Point2D> aAttrPnt = 
609     //  PartSet_Tools::findAttributePoint(mySketch, aX, anY, aTolerance, aIgnore);
610     //if (aAttrPnt.get() != NULL) {
611     //  aFeaturePoint->setValue(aAttrPnt->pnt());
612     //  PartSet_Tools::createConstraint(mySketch, aAttrPnt, aFeaturePoint);
613     //  emit vertexSelected();
614     //}
615     /// End alternative code
616     emit focusOutWidget(this);
617   }
618 }
619
620
621 void PartSet_WidgetPoint2D::onMouseMove(ModuleBase_IViewWindow* theWnd, QMouseEvent* theEvent)
622 {
623   if (isEditingMode())
624     return;
625
626   gp_Pnt aPoint = PartSet_Tools::convertClickToPoint(theEvent->pos(), theWnd->v3dView());
627
628   double aX, anY;
629   PartSet_Tools::convertTo2D(aPoint, mySketch, theWnd->v3dView(), aX, anY);
630   if (myState != ModifiedInViewer)
631     storeCurentValue();
632   // we need to block the value state change 
633   bool isBlocked = blockValueState(true);
634   setPoint(aX, anY);
635   blockValueState(isBlocked);
636   setValueState(ModifiedInViewer);
637 }
638
639 double PartSet_WidgetPoint2D::x() const
640 {
641   return myXSpin->value();
642 }
643
644 double PartSet_WidgetPoint2D::y() const
645 {
646   return myYSpin->value();
647 }
648
649
650 bool PartSet_WidgetPoint2D::isFeatureContainsPoint(const FeaturePtr& theFeature,
651                                                    double theX, double theY)
652 {
653   bool aPointIsFound = false;
654
655   if (feature()->getKind() != SketchPlugin_Line::ID())
656     return aPointIsFound;
657
658   AttributePtr aWidgetAttribute = myFeature->attribute(attributeID());
659
660   std::shared_ptr<GeomAPI_Pnt2d> aPnt2d = 
661                                     std::shared_ptr<GeomAPI_Pnt2d>(new GeomAPI_Pnt2d(theX, theY));
662   std::list<AttributePtr> anAttributes =
663                                 myFeature->data()->attributes(GeomDataAPI_Point2D::typeId());
664   std::list<AttributePtr>::iterator anIter = anAttributes.begin();
665   for(; anIter != anAttributes.end() && !aPointIsFound; anIter++) {
666     AttributePoint2DPtr aPoint2DAttribute =
667       std::dynamic_pointer_cast<GeomDataAPI_Point2D>(*anIter);
668     if (aPoint2DAttribute == aWidgetAttribute)
669       continue;
670     if (aPoint2DAttribute.get() && aPoint2DAttribute->isInitialized()) {
671       aPointIsFound = aPoint2DAttribute->pnt()->isEqual(aPnt2d);
672     }
673   }
674   return aPointIsFound;
675 }
676
677 void PartSet_WidgetPoint2D::initializeValueByActivate()
678 {
679 }
680
681 /*void PartSet_WidgetPoint2D::onValuesChanged()
682 {
683   emit valuesChanged();
684 }*/
685
686 bool PartSet_WidgetPoint2D::processEnter()
687 {
688   bool isModified = getValueState() == ModifiedInPP;
689   if (isModified) {
690     bool isXModified = myXSpin->hasFocus();
691     emit valuesChanged();
692     if (isXModified)
693       myXSpin->selectAll();
694     else
695       myYSpin->selectAll();
696   }
697   return isModified;
698 }
699
700 bool PartSet_WidgetPoint2D::useSelectedShapes() const
701 {
702   return true;
703 }
704
705 bool PartSet_WidgetPoint2D::isOrphanPoint(const FeaturePtr& theFeature,
706                                           const CompositeFeaturePtr& theSketch,
707                                           double theX, double theY)
708 {
709   bool anOrphanPoint = false;
710   if (theFeature.get()) {
711     std::shared_ptr<GeomDataAPI_Point2D> aPointAttr;
712     std::string aFeatureKind = theFeature->getKind();
713     if (aFeatureKind == SketchPlugin_Point::ID())
714       aPointAttr = std::dynamic_pointer_cast<GeomDataAPI_Point2D>(
715                                        theFeature->attribute(SketchPlugin_Point::COORD_ID()));
716     else if (aFeatureKind == SketchPlugin_Circle::ID())
717       aPointAttr = std::dynamic_pointer_cast<GeomDataAPI_Point2D>(
718                                        theFeature->attribute(SketchPlugin_Circle::CENTER_ID()));
719
720     else if (aFeatureKind == SketchPlugin_Arc::ID())
721       aPointAttr = std::dynamic_pointer_cast<GeomDataAPI_Point2D>(
722                                        theFeature->attribute(SketchPlugin_Arc::CENTER_ID()));
723
724     /// check that the geometry point with the given coordinates is the checked point
725     /// e.g. in arc the (x,y) point can not coicide to the center point and it automatically
726     /// means that this point is not an orphant one.
727     if (aPointAttr.get()) {
728       std::shared_ptr<GeomAPI_Pnt2d> aCheckedPoint = std::shared_ptr<GeomAPI_Pnt2d>(
729                                                     new GeomAPI_Pnt2d(theX, theY));
730       if (!aCheckedPoint->isEqual(aPointAttr->pnt()))
731         return anOrphanPoint;
732     }
733
734     if (aPointAttr.get()) {
735       std::shared_ptr<GeomAPI_Pnt2d> aPoint = aPointAttr->pnt();
736       // we need to find coincidence features in results also, because external object(point)
737       // uses refs to me in another feature.
738       FeaturePtr aCoincidence = PartSet_Tools::findFirstCoincidence(theFeature, aPoint);
739       anOrphanPoint = true;
740       // if there is at least one concident line to the point, the point is not an orphant
741       if (aCoincidence.get()) {
742         QList<FeaturePtr> aCoinsideLines;
743         QList<FeaturePtr> aCoins;
744         PartSet_Tools::findCoincidences(aCoincidence, aCoinsideLines, aCoins,
745                                         SketchPlugin_ConstraintCoincidence::ENTITY_A());
746         PartSet_Tools::findCoincidences(aCoincidence, aCoinsideLines, aCoins,
747                                         SketchPlugin_ConstraintCoincidence::ENTITY_B());
748         QList<FeaturePtr>::const_iterator anIt = aCoinsideLines.begin(),
749                                           aLast = aCoinsideLines.end();
750         for (; anIt != aLast && anOrphanPoint; anIt++) {
751           anOrphanPoint = (*anIt)->getKind() != SketchPlugin_Line::ID();
752         }
753       }
754     }
755   }
756   return anOrphanPoint;
757 }
758
759 bool PartSet_WidgetPoint2D::shapeContainsPoint(const GeomShapePtr& theShape,
760                                                const std::shared_ptr<GeomAPI_Pnt2d>& thePoint,
761                                                const CompositeFeaturePtr& theSketch)
762 {
763   std::shared_ptr<GeomAPI_Pnt> aPoint = PartSet_Tools::point3D(thePoint, theSketch);
764
765   bool aContainPoint = false;
766   GeomAPI_ShapeExplorer anExp(theShape, GeomAPI_Shape::VERTEX);
767   for(; anExp.more() && !aContainPoint; anExp.next()) {
768     std::shared_ptr<GeomAPI_Shape> aVertexInCompSolid = anExp.current();
769     std::shared_ptr<GeomAPI_Vertex> aVertex(new GeomAPI_Vertex(aVertexInCompSolid));
770     if (aVertex.get())
771       aContainPoint = aPoint->isEqual(aVertex->point());
772   }
773   return aContainPoint;
774 }