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