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