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