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