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