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