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