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