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