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