Salome HOME
b1d45d56fe5ad54f1b2fef14617ea0af3a4c7c1c
[modules/shaper.git] / src / PartSet / PartSet_SketcherMgr.cpp
1 // Copyright (C) 2014-20xx CEA/DEN, EDF R&D
2
3 // File:        PartSet_SketcherMgr.cpp
4 // Created:     19 Dec 2014
5 // Author:      Vitaly SMETANNIKOV
6
7 #include "PartSet_SketcherMgr.h"
8 #include "PartSet_Module.h"
9 #include "PartSet_WidgetPoint2d.h"
10 #include "PartSet_WidgetPoint2dDistance.h"
11 #include "PartSet_Tools.h"
12 #include "PartSet_WidgetSketchLabel.h"
13
14 #include <ModuleBase_WidgetEditor.h>
15
16 #include <XGUI_ModuleConnector.h>
17 #include <XGUI_Displayer.h>
18 #include <XGUI_Workshop.h>
19 #include <XGUI_ContextMenuMgr.h>
20 #include <XGUI_Selection.h>
21 #include <XGUI_SelectionMgr.h>
22 #include <ModuleBase_ModelWidget.h>
23 #include <XGUI_ModuleConnector.h>
24 #include <XGUI_PropertyPanel.h>
25 #include <XGUI_ViewerProxy.h>
26
27 #include <ModuleBase_IViewer.h>
28 #include <ModuleBase_IWorkshop.h>
29 #include <ModuleBase_IViewWindow.h>
30 #include <ModuleBase_Operation.h>
31 #include <ModuleBase_ISelection.h>
32 #include <ModuleBase_IPropertyPanel.h>
33 #include <ModuleBase_Operation.h>
34
35 #include <GeomDataAPI_Point2D.h>
36
37 #include <Events_Loop.h>
38
39 #include <SketchPlugin_Line.h>
40 #include <SketchPlugin_Sketch.h>
41 #include <SketchPlugin_Point.h>
42 #include <SketchPlugin_Arc.h>
43 #include <SketchPlugin_Circle.h>
44 #include <SketchPlugin_ConstraintLength.h>
45 #include <SketchPlugin_ConstraintDistance.h>
46 #include <SketchPlugin_ConstraintParallel.h>
47 #include <SketchPlugin_ConstraintPerpendicular.h>
48 #include <SketchPlugin_ConstraintRadius.h>
49 #include <SketchPlugin_ConstraintRigid.h>
50 #include <SketchPlugin_ConstraintHorizontal.h>
51 #include <SketchPlugin_ConstraintVertical.h>
52 #include <SketchPlugin_ConstraintEqual.h>
53 #include <SketchPlugin_ConstraintTangent.h>
54 #include <SketchPlugin_ConstraintCoincidence.h>
55 #include <SketchPlugin_ConstraintFillet.h>
56 #include <SketchPlugin_ConstraintMirror.h>
57 #include <SketchPlugin_MultiRotation.h>
58 #include <SketchPlugin_MultiTranslation.h>
59
60 #include <SketcherPrs_Tools.h>
61
62 #include <SelectMgr_IndexedMapOfOwner.hxx>
63 #include <StdSelect_BRepOwner.hxx>
64
65 //#include <AIS_DimensionSelectionMode.hxx>
66 #include <AIS_Shape.hxx>
67
68 #include <ModelAPI_Events.h>
69 #include <ModelAPI_Session.h>
70
71 #include <QMouseEvent>
72 #include <QApplication>
73
74 //#define DEBUG_DO_NOT_BY_ENTER
75 //#define DEBUG_MOUSE_OVER_WINDOW_FLAGS
76
77 /// Returns list of unique objects by sum of objects from List1 and List2
78 /*QList<ModuleBase_ViewerPrs> getSumList(const QList<ModuleBase_ViewerPrs>& theList1,
79                                        const QList<ModuleBase_ViewerPrs>& theList2)
80 {
81   QList<ModuleBase_ViewerPrs> aRes;
82   foreach (ModuleBase_ViewerPrs aPrs, theList1) {
83     if (!aRes.contains(aPrs))
84       aRes.append(aPrs);
85   }
86   foreach (ModuleBase_ViewerPrs aPrs, theList2) {
87     if (!aRes.contains(aPrs))
88       aRes.append(aPrs);
89   }
90   return aRes;
91 }*/
92
93 void fillFeature2Attribute(const QList<ModuleBase_ViewerPrs>& theList,
94                            QMap<FeaturePtr, QList<AttributePtr> >& theFeature2AttributeMap,
95                            const FeaturePtr theSketch)
96 {
97   QList<ModuleBase_ViewerPrs> aRes;
98
99   QList<ModuleBase_ViewerPrs>::const_iterator anIt = theList.begin(),
100                                               aLast = theList.end();
101   for (; anIt != aLast; anIt++)
102   {
103     ModuleBase_ViewerPrs aPrs = *anIt;
104     FeaturePtr aFeature = ModelAPI_Feature::feature(aPrs.object());
105     if (aFeature.get() == NULL)
106       continue;
107
108     QList<AttributePtr> anAttributes;
109     if (theFeature2AttributeMap.contains(aFeature)) {
110       anAttributes = theFeature2AttributeMap[aFeature];
111     }
112     AttributePtr anAttr;
113     TopoDS_Shape aShape = aPrs.shape();
114     if (!aShape.IsNull()) {
115       if (aShape.ShapeType() == TopAbs_VERTEX) {
116         anAttr = PartSet_Tools::findAttributeBy2dPoint(aFeature, aShape, theSketch);
117         if (anAttr.get() != NULL && !anAttributes.contains(anAttr))
118           anAttributes.push_back(anAttr);
119       }
120     }
121     theFeature2AttributeMap[aFeature] = anAttributes;
122   }
123 }
124
125
126
127
128 PartSet_SketcherMgr::PartSet_SketcherMgr(PartSet_Module* theModule)
129   : QObject(theModule), myModule(theModule), myIsDragging(false), myDragDone(false),
130     myIsPropertyPanelValueChanged(false), myIsMouseOverWindow(false),
131     myIsMouseOverViewProcessed(true), myPreviousUpdateViewerEnabled(true),
132     myIsPopupMenuActive(false), myIsConstraintsShown(true)
133 {
134   ModuleBase_IWorkshop* anIWorkshop = myModule->workshop();
135   ModuleBase_IViewer* aViewer = anIWorkshop->viewer();
136
137   myPreviousSelectionEnabled = true;//aViewer->isSelectionEnabled();
138
139   connect(aViewer, SIGNAL(mousePress(ModuleBase_IViewWindow*, QMouseEvent*)),
140           this, SLOT(onMousePressed(ModuleBase_IViewWindow*, QMouseEvent*)));
141
142   connect(aViewer, SIGNAL(mouseRelease(ModuleBase_IViewWindow*, QMouseEvent*)),
143           this, SLOT(onMouseReleased(ModuleBase_IViewWindow*, QMouseEvent*)));
144
145   connect(aViewer, SIGNAL(mouseMove(ModuleBase_IViewWindow*, QMouseEvent*)),
146           this, SLOT(onMouseMoved(ModuleBase_IViewWindow*, QMouseEvent*)));
147
148   connect(aViewer, SIGNAL(mouseDoubleClick(ModuleBase_IViewWindow*, QMouseEvent*)),
149           this, SLOT(onMouseDoubleClick(ModuleBase_IViewWindow*, QMouseEvent*)));
150
151   XGUI_ModuleConnector* aConnector = dynamic_cast<XGUI_ModuleConnector*>(anIWorkshop);
152   XGUI_Workshop* aWorkshop = aConnector->workshop();
153   connect(aWorkshop, SIGNAL(applicationStarted()), this, SLOT(onApplicationStarted()));
154 }
155
156 PartSet_SketcherMgr::~PartSet_SketcherMgr()
157 {
158   if (!myPlaneFilter.IsNull())
159     myPlaneFilter.Nullify();
160   if (!myFilterInfinite.IsNull())
161     myFilterInfinite.Nullify();
162
163 }
164
165 void PartSet_SketcherMgr::onEnterViewPort()
166 {
167   // 1. if the mouse over window, update the next flag. Do not perform update visibility of
168   // created feature because it should be done in onMouseMove(). Some widgets watch
169   // the mouse move and use the cursor position to update own values. If the presentaion is
170   // redisplayed before this update, the feature presentation jumps from reset value to current.
171   myIsMouseOverWindow = true;
172   myIsPropertyPanelValueChanged = false;
173 #ifdef DEBUG_MOUSE_OVER_WINDOW_FLAGS
174   qDebug(QString("onEnterViewPort: %1").arg(mouseOverWindowFlagsInfo()).toStdString().c_str());
175 #endif
176
177   #ifdef DEBUG_DO_NOT_BY_ENTER
178   return;
179   #endif
180
181   if (!isNestedCreateOperation(getCurrentOperation()))
182     return;
183   // we need change displayed state of the current operation feature
184   // if the feature is presentable, e.g. distance construction. It has no results, so workshop does
185   // not accept a signal about the result created. Nothing is shown until mouse is moved out/in view
186   // port. If the isDisplayed flag is true, the presentable feature is displayed as soon as the
187   // presentation becomes valid and redisplay happens
188   ModuleBase_Operation* aOperation = getCurrentOperation();
189   if (aOperation) {
190     FeaturePtr aFeature = aOperation->feature();
191     if (aFeature.get() && aFeature->data()->isValid()) {
192       visualizeFeature(aOperation, canDisplayObject(aFeature), false);
193     }
194   }
195 }
196
197 void PartSet_SketcherMgr::onLeaveViewPort()
198 {
199   myIsMouseOverViewProcessed = false;
200   myIsMouseOverWindow = false;
201   myIsPropertyPanelValueChanged = false;
202 #ifdef DEBUG_MOUSE_OVER_WINDOW_FLAGS
203   qDebug(QString("onLeaveViewPort: %1").arg(mouseOverWindowFlagsInfo()).toStdString().c_str());
204 #endif
205
206   #ifdef DEBUG_DO_NOT_BY_ENTER
207   return;
208   #endif
209
210   if (!isNestedCreateOperation(getCurrentOperation()))
211     return;
212   // the method should be performed if the popup menu is called,
213   // the reset of the current widget should not happen
214   if (myIsPopupMenuActive)
215     return;
216
217   // 2. if the mouse IS NOT over window, reset the active widget value and hide the presentation
218   ModuleBase_IWorkshop* aWorkshop = myModule->workshop();
219   XGUI_ModuleConnector* aConnector = dynamic_cast<XGUI_ModuleConnector*>(aWorkshop);
220   XGUI_Displayer* aDisplayer = aConnector->workshop()->displayer();
221   // disable the viewer update in order to avoid visualization of redisplayed feature in viewer
222   // obtained after reset value
223   bool isEnableUpdateViewer = aDisplayer->enableUpdateViewer(false);
224   ModuleBase_Operation* aOperation = getCurrentOperation();
225   ModuleBase_IPropertyPanel* aPanel = aOperation->propertyPanel();
226   ModuleBase_ModelWidget* aActiveWgt = aPanel->activeWidget();
227   if (aActiveWgt) {
228     aActiveWgt->reset();
229   }
230   aDisplayer->enableUpdateViewer(isEnableUpdateViewer);
231
232   // hides the presentation of the current operation feature
233   //myIsPropertyPanelValueChanged = false;
234   // the feature is to be erased here, but it is correct to call canDisplayObject because
235   // there can be additional check (e.g. editor widget in distance constraint)
236   FeaturePtr aFeature = getCurrentOperation()->feature();
237   visualizeFeature(aOperation, canDisplayObject(aFeature));
238 }
239
240 void PartSet_SketcherMgr::onBeforeValuesChangedInPropertyPanel()
241 {
242   if (isNestedCreateOperation(getCurrentOperation()))
243     return;
244   // it is necessary to save current selection in order to restore it after the values are modifed
245   storeSelection();
246
247   ModuleBase_IWorkshop* aWorkshop = myModule->workshop();
248   XGUI_ModuleConnector* aConnector = dynamic_cast<XGUI_ModuleConnector*>(aWorkshop);
249   XGUI_Displayer* aDisplayer = aConnector->workshop()->displayer();
250   myPreviousUpdateViewerEnabled = aDisplayer->enableUpdateViewer(false);
251 }
252
253 void PartSet_SketcherMgr::onAfterValuesChangedInPropertyPanel()
254 {
255   if (isNestedCreateOperation(getCurrentOperation()))
256     return;
257   // it is necessary to restore current selection in order to restore it after the values are modified
258   restoreSelection();
259   myCurrentSelection.clear();
260
261   // 3. the flag to disable the update viewer should be set in order to avoid blinking in the 
262   // viewer happens by deselect/select the modified objects. The flag should be restored after
263   // the selection processing. The update viewer should be also called.
264   ModuleBase_IWorkshop* aWorkshop = myModule->workshop();
265   XGUI_ModuleConnector* aConnector = dynamic_cast<XGUI_ModuleConnector*>(aWorkshop);
266   XGUI_Displayer* aDisplayer = aConnector->workshop()->displayer();
267   aDisplayer->enableUpdateViewer(myPreviousUpdateViewerEnabled);
268   aDisplayer->updateViewer();
269 }
270
271 void PartSet_SketcherMgr::onValuesChangedInPropertyPanel()
272 {
273   if (!isNestedCreateOperation(getCurrentOperation()))
274     return;
275
276   // visualize the current operation feature
277   myIsPropertyPanelValueChanged = true;
278   ModuleBase_Operation* aOperation = getCurrentOperation();
279   // the feature is to be erased here, but it is correct to call canDisplayObject because
280   // there can be additional check (e.g. editor widget in distance constraint)
281   FeaturePtr aFeature = getCurrentOperation()->feature();
282   visualizeFeature(aOperation, canDisplayObject(aFeature));
283 }
284
285 void PartSet_SketcherMgr::onMousePressed(ModuleBase_IViewWindow* theWnd, QMouseEvent* theEvent)
286 {
287   get2dPoint(theWnd, theEvent, myClickedPoint);
288
289   if (!(theEvent->buttons() & Qt::LeftButton))
290     return;
291
292   // Clear dragging mode
293   myIsDragging = false;
294
295   ModuleBase_IWorkshop* aWorkshop = myModule->workshop();
296   ModuleBase_IViewer* aViewer = aWorkshop->viewer();
297   if (!aViewer->canDragByMouse())
298     return;
299
300   ModuleBase_Operation* aOperation = getCurrentOperation();
301   if (aOperation && aOperation->isEditOperation()) {
302     ModuleBase_IPropertyPanel* aPanel = aOperation->propertyPanel();
303     ModuleBase_ModelWidget* aActiveWgt = aPanel->activeWidget();
304     // If the current widget is a selector, do nothing, it processes the mouse press
305     if(aActiveWgt && aActiveWgt->isViewerSelector()) {
306       return;
307     }
308   }
309
310   // Use only for sketch operations
311   if (aOperation && myCurrentSketch) {
312     if (!PartSet_Tools::sketchPlane(myCurrentSketch))
313       return;
314
315     bool isSketcher = isSketchOperation(aOperation);
316     bool isSketchOpe = isNestedSketchOperation(aOperation);
317
318     // Avoid non-sketch operations
319     if ((!isSketchOpe) && (!isSketcher))
320       return;
321
322     bool isEditing = aOperation->isEditOperation();
323
324     // Ignore creation sketch operation
325     if ((!isSketcher) && (!isEditing))
326       return;
327
328     // MoveTo in order to highlight current object
329     aViewer->AISContext()->MoveTo(theEvent->x(), theEvent->y(), theWnd->v3dView());
330
331     // Remember highlighted objects for editing
332     ModuleBase_ISelection* aSelect = aWorkshop->selection();
333
334     bool aHasShift = (theEvent->modifiers() & Qt::ShiftModifier);
335     storeSelection(!aHasShift);
336
337     if (myCurrentSelection.empty()) {
338       if (isSketchOpe && (!isSketcher))
339         // commit previous operation
340         if (!aOperation->commit())
341           aOperation->abort();
342       return;
343     }
344     // Init flyout point for radius rotation
345     FeaturePtr aFeature = myCurrentSelection.begin().key();
346
347     if (isSketcher) {
348       myIsDragging = true;
349       get2dPoint(theWnd, theEvent, myCurrentPoint);
350       myDragDone = false;
351       launchEditing();
352       if (aFeature.get() != NULL) {
353         std::shared_ptr<SketchPlugin_Feature> aSPFeature = 
354                   std::dynamic_pointer_cast<SketchPlugin_Feature>(aFeature);
355       if (aSPFeature.get() && aSPFeature->getKind() == SketchPlugin_ConstraintRadius::ID()) {
356           DataPtr aData = aSPFeature->data();
357           AttributePtr aAttr = aData->attribute(SketchPlugin_Constraint::FLYOUT_VALUE_PNT());
358           std::shared_ptr<GeomDataAPI_Point2D> aFPAttr = 
359             std::dynamic_pointer_cast<GeomDataAPI_Point2D>(aAttr);
360           aFPAttr->setValue(myCurrentPoint.myCurX, myCurrentPoint.myCurY);
361         }
362       }
363     } else if (isSketchOpe && isEditing) {
364       // If selected another object commit current result
365       aOperation->commit();
366
367       myIsDragging = true;
368       get2dPoint(theWnd, theEvent, myCurrentPoint);
369       myDragDone = false;
370
371       // This is necessary in order to finalize previous operation
372       QApplication::processEvents();
373       launchEditing();
374     }
375   }
376 }
377
378 void PartSet_SketcherMgr::onMouseReleased(ModuleBase_IViewWindow* theWnd, QMouseEvent* theEvent)
379 {
380   ModuleBase_IWorkshop* aWorkshop = myModule->workshop();
381   ModuleBase_IViewer* aViewer = aWorkshop->viewer();
382   if (!aViewer->canDragByMouse())
383     return;
384   ModuleBase_Operation* aOp = getCurrentOperation();
385   if (aOp) {
386     if (isNestedSketchOperation(aOp)) {
387       get2dPoint(theWnd, theEvent, myClickedPoint);
388
389       // Only for sketcher operations
390       if (myIsDragging) {
391         if (myDragDone) {
392           //aOp->commit();
393           myCurrentSelection.clear();
394           // Reselect edited object
395           /*aViewer->AISContext()->MoveTo(theEvent->x(), theEvent->y(), theWnd->v3dView());
396           if (theEvent->modifiers() & Qt::ShiftModifier)
397             aViewer->AISContext()->ShiftSelect();
398           else
399             aViewer->AISContext()->Select();
400             */
401         }
402       }
403     }
404   }
405   aWorkshop->viewer()->enableSelection(myPreviousSelectionEnabled);
406   myIsDragging = false;
407 }
408
409 void PartSet_SketcherMgr::onMouseMoved(ModuleBase_IViewWindow* theWnd, QMouseEvent* theEvent)
410 {
411   if (isNestedCreateOperation(getCurrentOperation()) && !myIsMouseOverViewProcessed) {
412     myIsMouseOverViewProcessed = true;
413     // 1. perform the widget mouse move functionality and display the presentation
414     ModuleBase_Operation* aOperation = getCurrentOperation();
415     ModuleBase_IPropertyPanel* aPanel = aOperation->propertyPanel();
416     ModuleBase_ModelWidget* anActiveWdg = aPanel->activeWidget();
417     // the mouse move should be processed in the widget, if it can in order to visualize correct
418     // presentation. These widgets correct the feature attribute according to the mouse position
419     PartSet_WidgetPoint2D* aPoint2DWdg = dynamic_cast<PartSet_WidgetPoint2D*>(anActiveWdg);
420     if (aPoint2DWdg) {
421       aPoint2DWdg->onMouseMove(theWnd, theEvent);
422     }
423     PartSet_WidgetPoint2dDistance* aDistanceWdg = dynamic_cast<PartSet_WidgetPoint2dDistance*>
424                                                                 (anActiveWdg);
425     if (aDistanceWdg) {
426       aDistanceWdg->onMouseMove(theWnd, theEvent);
427     }
428     // the feature is to be erased here, but it is correct to call canDisplayObject because
429     // there can be additional check (e.g. editor widget in distance constraint)
430     FeaturePtr aFeature = getCurrentOperation()->feature();
431     visualizeFeature(aOperation, canDisplayObject(aFeature));
432   }
433
434   myClickedPoint.clear();
435
436   if (myIsDragging) {
437     // 1. the current selection is saved in the mouse press method in order to restore it after moving
438     // 2. the enable selection in the viewer should be temporary switched off in order to ignore
439     // mouse press signal in the viewer(it call Select for AIS context and the dragged objects are
440     // deselected). This flag should be restored in the slot, processed the mouse release signal.
441     ModuleBase_IViewer* aViewer = myModule->workshop()->viewer();
442     aViewer->enableSelection(false);
443
444     ModuleBase_Operation* aOperation = getCurrentOperation();
445     if (!aOperation)
446       return;
447     if (isSketchOperation(aOperation))
448       return; // No edit operation activated
449
450     Handle(V3d_View) aView = theWnd->v3dView();
451     gp_Pnt aPoint = PartSet_Tools::convertClickToPoint(theEvent->pos(), aView);
452     double aX, aY;
453     PartSet_Tools::convertTo2D(aPoint, myCurrentSketch, aView, aX, aY);
454     double dX =  aX - myCurrentPoint.myCurX;
455     double dY =  aY - myCurrentPoint.myCurY;
456
457     ModuleBase_IWorkshop* aWorkshop = myModule->workshop();
458     XGUI_ModuleConnector* aConnector = dynamic_cast<XGUI_ModuleConnector*>(aWorkshop);
459     XGUI_Displayer* aDisplayer = aConnector->workshop()->displayer();
460     // 3. the flag to disable the update viewer should be set in order to avoid blinking in the 
461     // viewer happens by deselect/select the modified objects. The flag should be restored after
462     // the selection processing. The update viewer should be also called.
463     bool isEnableUpdateViewer = aDisplayer->enableUpdateViewer(false);
464
465     static Events_ID aMoveEvent = Events_Loop::eventByName(EVENT_OBJECT_MOVED);
466     //static Events_ID aUpdateEvent = Events_Loop::eventByName(EVENT_OBJECT_UPDATED);
467     FeatureToSelectionMap::const_iterator anIt = myCurrentSelection.begin(),
468                                           aLast = myCurrentSelection.end();
469     // 4. the features and attributes modification(move)
470     for (; anIt != aLast; anIt++) {
471       FeaturePtr aFeature = anIt.key();
472
473       std::set<AttributePtr> anAttributes = anIt.value().first;
474       // Process selection by attribute: the priority to the attribute
475       if (!anAttributes.empty()) {
476         std::set<AttributePtr>::const_iterator anAttIt = anAttributes.begin(),
477                                                anAttLast = anAttributes.end();
478         for (; anAttIt != anAttLast; anAttIt++) {
479           AttributePtr anAttr = *anAttIt;
480           if (anAttr.get() == NULL)
481             continue;
482           std::string aAttrId = anAttr->id();
483           DataPtr aData = aFeature->data();
484           if (aData->isValid()) {
485             std::shared_ptr<GeomDataAPI_Point2D> aPoint = 
486               std::dynamic_pointer_cast<GeomDataAPI_Point2D>(aData->attribute(aAttrId));
487             if (aPoint.get() != NULL) {
488               bool isImmutable = aPoint->setImmutable(true);
489               aPoint->move(dX, dY);
490               ModelAPI_EventCreator::get()->sendUpdated(aFeature, aMoveEvent);
491               aPoint->setImmutable(isImmutable);
492             }
493           }
494         }
495       } else {
496         // Process selection by feature
497         std::shared_ptr<SketchPlugin_Feature> aSketchFeature =
498           std::dynamic_pointer_cast<SketchPlugin_Feature>(aFeature);
499         if (aSketchFeature) {
500           aSketchFeature->move(dX, dY);
501           ModelAPI_EventCreator::get()->sendUpdated(aSketchFeature, aMoveEvent);
502         }
503       }
504     }
505     Events_Loop::loop()->flush(aMoveEvent); // up all move events - to be processed in the solver
506     //Events_Loop::loop()->flush(aUpdateEvent); // up update events - to redisplay presentations
507
508     // 5. it is necessary to save current selection in order to restore it after the features moving
509     restoreSelection();
510     // 6. restore the update viewer flag and call this update
511     aDisplayer->enableUpdateViewer(isEnableUpdateViewer);
512     aDisplayer->updateViewer();
513
514     myDragDone = true;
515     myCurrentPoint.setValue(aX, aY);
516   }
517 }
518
519 void PartSet_SketcherMgr::onMouseDoubleClick(ModuleBase_IViewWindow* theWnd, QMouseEvent* theEvent)
520 {
521   ModuleBase_Operation* aOperation = getCurrentOperation();
522   if (aOperation && aOperation->isEditOperation()) {
523     std::string aId = aOperation->id().toStdString();
524     if (isDistanceOperation(aOperation))
525     {
526       // Activate dimension value editing on double click
527       ModuleBase_IPropertyPanel* aPanel = aOperation->propertyPanel();
528       QList<ModuleBase_ModelWidget*> aWidgets = aPanel->modelWidgets();
529       // Find corresponded widget to activate value editing
530       foreach (ModuleBase_ModelWidget* aWgt, aWidgets) {
531         if (aWgt->attributeID() == "ConstraintValue") {
532           aWgt->focusTo();
533           return;
534         }
535       }
536     }
537   }
538 }
539
540 void PartSet_SketcherMgr::onApplicationStarted()
541 {
542   ModuleBase_IWorkshop* anIWorkshop = myModule->workshop();
543   XGUI_ModuleConnector* aConnector = dynamic_cast<XGUI_ModuleConnector*>(anIWorkshop);
544   XGUI_Workshop* aWorkshop = aConnector->workshop();
545   XGUI_PropertyPanel* aPropertyPanel = aWorkshop->propertyPanel();
546   if (aPropertyPanel) {
547     connect(aPropertyPanel, SIGNAL(beforeWidgetActivated(ModuleBase_ModelWidget*)),
548             this, SLOT(onBeforeWidgetActivated(ModuleBase_ModelWidget*)));
549   }
550
551   XGUI_ViewerProxy* aViewerProxy = aWorkshop->viewer();
552   connect(aViewerProxy, SIGNAL(enterViewPort()), this, SLOT(onEnterViewPort()));
553   connect(aViewerProxy, SIGNAL(leaveViewPort()), this, SLOT(onLeaveViewPort()));
554
555   XGUI_ContextMenuMgr* aContextMenuMgr = aWorkshop->contextMenuMgr();
556   connect(aContextMenuMgr, SIGNAL(beforeContextMenu()), this, SLOT(onBeforeContextMenu()));
557   connect(aContextMenuMgr, SIGNAL(afterContextMenu()), this, SLOT(onAfterContextMenu()));
558 }
559
560 void PartSet_SketcherMgr::onBeforeWidgetActivated(ModuleBase_ModelWidget* theWidget)
561 {
562   if (!myClickedPoint.myIsInitialized)
563     return;
564
565   ModuleBase_Operation* aOperation = getCurrentOperation();
566   // the distance constraint feature should not use the clickedd point
567   // this is workaround in order to don't throw down the flyout point value,
568   // set by execute() method of these type of features
569   if (isDistanceOperation(aOperation))
570     return;
571
572   PartSet_WidgetPoint2D* aPnt2dWgt = dynamic_cast<PartSet_WidgetPoint2D*>(theWidget);
573   if (aPnt2dWgt) {
574     aPnt2dWgt->setPoint(myClickedPoint.myCurX, myClickedPoint.myCurY);
575   }
576 }
577
578 void PartSet_SketcherMgr::onBeforeContextMenu()
579 {
580   myIsPopupMenuActive = true;
581 }
582
583 void PartSet_SketcherMgr::onAfterContextMenu()
584 {
585   myIsPopupMenuActive = false;
586 }
587
588 void PartSet_SketcherMgr::get2dPoint(ModuleBase_IViewWindow* theWnd, QMouseEvent* theEvent, 
589                                      Point& thePoint)
590 {
591   Handle(V3d_View) aView = theWnd->v3dView();
592   gp_Pnt aPoint = PartSet_Tools::convertClickToPoint(theEvent->pos(), aView);
593   double aX, anY;
594   PartSet_Tools::convertTo2D(aPoint, myCurrentSketch, aView, aX, anY);
595   thePoint.setValue(aX, anY);
596 }
597
598 void PartSet_SketcherMgr::launchEditing()
599 {
600   if (!myCurrentSelection.empty()) {
601     FeaturePtr aFeature = myCurrentSelection.begin().key();
602     std::shared_ptr<SketchPlugin_Feature> aSPFeature = 
603               std::dynamic_pointer_cast<SketchPlugin_Feature>(aFeature);
604     if (aSPFeature) {
605       myModule->editFeature(aSPFeature);
606     }
607   }
608 }
609
610
611 const QStringList& PartSet_SketcherMgr::sketchOperationIdList()
612 {
613   static QStringList aIds;
614   if (aIds.size() == 0) {
615     aIds << SketchPlugin_Line::ID().c_str();
616     aIds << SketchPlugin_Point::ID().c_str();
617     aIds << SketchPlugin_Arc::ID().c_str();
618     aIds << SketchPlugin_Circle::ID().c_str();
619     aIds << SketchPlugin_ConstraintFillet::ID().c_str();
620     aIds.append(constraintsIdList());
621   }
622   return aIds;
623 }
624
625 const QStringList& PartSet_SketcherMgr::constraintsIdList()
626 {
627   static QStringList aIds;
628   if (aIds.size() == 0) {
629     aIds << SketchPlugin_ConstraintLength::ID().c_str();
630     aIds << SketchPlugin_ConstraintDistance::ID().c_str();
631     aIds << SketchPlugin_ConstraintRigid::ID().c_str();
632     aIds << SketchPlugin_ConstraintRadius::ID().c_str();
633     aIds << SketchPlugin_ConstraintPerpendicular::ID().c_str();
634     aIds << SketchPlugin_ConstraintParallel::ID().c_str();
635     aIds << SketchPlugin_ConstraintHorizontal::ID().c_str();
636     aIds << SketchPlugin_ConstraintVertical::ID().c_str();
637     aIds << SketchPlugin_ConstraintEqual::ID().c_str();
638     aIds << SketchPlugin_ConstraintTangent::ID().c_str();
639     aIds << SketchPlugin_ConstraintCoincidence::ID().c_str();
640     aIds << SketchPlugin_ConstraintMirror::ID().c_str();
641     aIds << SketchPlugin_MultiRotation::ID().c_str();
642     aIds << SketchPlugin_MultiTranslation::ID().c_str();
643   }
644   return aIds;
645 }
646
647 void PartSet_SketcherMgr::sketchSelectionModes(QIntList& theModes)
648 {
649   theModes.clear();
650
651   theModes.append(SketcherPrs_Tools::Sel_Dimension_Text);
652   theModes.append(SketcherPrs_Tools::Sel_Dimension_Line);
653   theModes.append(SketcherPrs_Tools::Sel_Constraint);
654   theModes.append(TopAbs_VERTEX);
655   theModes.append(TopAbs_EDGE);
656 }
657
658 bool PartSet_SketcherMgr::isSketchOperation(ModuleBase_Operation* theOperation)
659 {
660   return theOperation && theOperation->id().toStdString() == SketchPlugin_Sketch::ID();
661 }
662
663 bool PartSet_SketcherMgr::isNestedSketchOperation(ModuleBase_Operation* theOperation)
664 {
665   return theOperation &&
666          PartSet_SketcherMgr::sketchOperationIdList().contains(theOperation->id());
667 }
668
669 bool PartSet_SketcherMgr::isNestedCreateOperation(ModuleBase_Operation* theOperation)
670 {
671   return theOperation && !theOperation->isEditOperation() && isNestedSketchOperation(theOperation);
672 }
673
674 bool PartSet_SketcherMgr::isEntity(const std::string& theId)
675 {
676   return (theId == SketchPlugin_Line::ID()) ||
677          (theId == SketchPlugin_Point::ID()) ||
678          (theId == SketchPlugin_Arc::ID()) ||
679          (theId == SketchPlugin_Circle::ID());
680 }
681
682 bool PartSet_SketcherMgr::isDistanceOperation(ModuleBase_Operation* theOperation)
683 {
684   std::string aId = theOperation ? theOperation->id().toStdString() : "";
685
686   return (aId == SketchPlugin_ConstraintLength::ID()) ||
687          (aId == SketchPlugin_ConstraintDistance::ID()) ||
688          (aId == SketchPlugin_ConstraintRadius::ID());
689 }
690
691 void PartSet_SketcherMgr::startSketch(ModuleBase_Operation* theOperation)
692 {
693   myModule->onViewTransformed();
694
695   // Display all sketcher sub-Objects
696   myCurrentSketch = std::dynamic_pointer_cast<ModelAPI_CompositeFeature>(theOperation->feature());
697   XGUI_ModuleConnector* aConnector = dynamic_cast<XGUI_ModuleConnector*>(myModule->workshop());
698
699   // Hide sketcher result
700   std::list<ResultPtr> aResults = myCurrentSketch->results();
701   std::list<ResultPtr>::const_iterator aIt;
702   for (aIt = aResults.begin(); aIt != aResults.end(); ++aIt) {
703     (*aIt)->setDisplayed(false);
704   }
705   myCurrentSketch->setDisplayed(false);
706
707   // Display sketcher objects
708   for (int i = 0; i < myCurrentSketch->numberOfSubs(); i++) {
709     FeaturePtr aFeature = myCurrentSketch->subFeature(i);
710     std::list<ResultPtr> aResults = aFeature->results();
711     std::list<ResultPtr>::const_iterator aIt;
712     for (aIt = aResults.begin(); aIt != aResults.end(); ++aIt) {
713       (*aIt)->setDisplayed(true);
714     }
715     aFeature->setDisplayed(true);
716   }
717
718   if (myPlaneFilter.IsNull()) 
719     myPlaneFilter = new ModuleBase_ShapeInPlaneFilter();
720
721   if (myFilterInfinite.IsNull())
722     myFilterInfinite = new PartSet_FilterInfinite();
723
724   myModule->workshop()->viewer()->addSelectionFilter(myPlaneFilter);
725   myModule->workshop()->viewer()->addSelectionFilter(myFilterInfinite);
726
727   bool aHasPlane = false;
728   if (theOperation->isEditOperation()) {
729     // If it is editing of sketch then it means that plane is already defined
730     std::shared_ptr<GeomAPI_Pln> aPln = PartSet_Tools::sketchPlane(myCurrentSketch);
731     if (aPln.get()) {
732       myPlaneFilter->setPlane(aPln->impl<gp_Pln>());
733       aHasPlane = true;
734     }
735   }
736   Events_Loop::loop()->flush(Events_Loop::eventByName(EVENT_OBJECT_TO_REDISPLAY));
737   // all sketch objects should be activated in the sketch selection modes by edit operation start
738   // in case of creation operation, there is an active widget, which activates own selection mode
739   if (theOperation->isEditOperation() && aHasPlane)
740     aConnector->activateModuleSelectionModes();
741 }
742
743 void PartSet_SketcherMgr::stopSketch(ModuleBase_Operation* theOperation)
744 {
745   myIsMouseOverWindow = false;
746   myIsConstraintsShown = true;
747 #ifdef DEBUG_MOUSE_OVER_WINDOW_FLAGS
748   qDebug(QString("stopSketch: %1").arg(mouseOverWindowFlagsInfo()).toStdString().c_str());
749 #endif
750   XGUI_ModuleConnector* aConnector = dynamic_cast<XGUI_ModuleConnector*>(myModule->workshop());
751
752   DataPtr aData = myCurrentSketch->data();
753   if (!aData->isValid()) {
754     XGUI_Displayer* aDisplayer = aConnector->workshop()->displayer();
755     // The sketch was aborted
756     myCurrentSketch = CompositeFeaturePtr();
757     myModule->workshop()->viewer()->removeSelectionFilter(myPlaneFilter);
758     myModule->workshop()->viewer()->removeSelectionFilter(myFilterInfinite);
759
760     // Erase all sketcher objects
761     QStringList aSketchIds = sketchOperationIdList();
762     QObjectPtrList aObjects = aDisplayer->displayedObjects();
763     foreach (ObjectPtr aObj, aObjects) {
764       DataPtr aObjData = aObj->data();
765       if (!aObjData->isValid())
766         aObj->setDisplayed(false);
767     }
768   }
769   else {
770   // Hide all sketcher sub-Objects
771   for (int i = 0; i < myCurrentSketch->numberOfSubs(); i++) {
772     FeaturePtr aFeature = myCurrentSketch->subFeature(i);
773     std::list<ResultPtr> aResults = aFeature->results();
774     std::list<ResultPtr>::const_iterator aIt;
775     for (aIt = aResults.begin(); aIt != aResults.end(); ++aIt) {
776       (*aIt)->setDisplayed(false);
777     }
778     aFeature->setDisplayed(false);
779   }
780   // Display sketcher result
781   std::list<ResultPtr> aResults = myCurrentSketch->results();
782   std::list<ResultPtr>::const_iterator aIt;
783   for (aIt = aResults.begin(); aIt != aResults.end(); ++aIt) {
784     (*aIt)->setDisplayed(true);
785   }
786   myCurrentSketch->setDisplayed(true);
787     
788   myCurrentSketch = CompositeFeaturePtr();
789   myModule->workshop()->viewer()->removeSelectionFilter(myPlaneFilter);
790   myModule->workshop()->viewer()->removeSelectionFilter(myFilterInfinite);
791
792   Events_Loop::loop()->flush(Events_Loop::eventByName(EVENT_OBJECT_TO_REDISPLAY));
793   }
794   // restore the module selection modes, which were changed on startSketch
795   aConnector->activateModuleSelectionModes();
796 }
797
798 void PartSet_SketcherMgr::startNestedSketch(ModuleBase_Operation* theOperation)
799 {
800   if (constraintsIdList().contains(theOperation->id())) {
801     // Show constraints if a constraint was created
802     onShowConstraintsToggle(true);
803   }
804   connectToPropertyPanel(true);
805 }
806
807 void PartSet_SketcherMgr::stopNestedSketch(ModuleBase_Operation* theOp)
808 {
809   connectToPropertyPanel(false);
810   myIsPropertyPanelValueChanged = false;
811   myIsMouseOverViewProcessed = true;
812 }
813
814 void PartSet_SketcherMgr::commitNestedSketch(ModuleBase_Operation* theOperation)
815 {
816   if (isNestedCreateOperation(theOperation)) {
817     FeaturePtr aFeature = theOperation->feature();
818     // it is necessary to check the the feature data validity because
819     // some kind of features are removed by an operation commit(the macro state of a feature)
820     if (aFeature.get() && aFeature->data()->isValid()) {
821       visualizeFeature(theOperation, true);
822     }
823   }
824 }
825
826 bool PartSet_SketcherMgr::canUndo() const
827 {
828   return isNestedCreateOperation(getCurrentOperation());
829 }
830
831 bool PartSet_SketcherMgr::canRedo() const
832 {
833   return isNestedCreateOperation(getCurrentOperation());
834 }
835
836 bool PartSet_SketcherMgr::canDisplayObject(const ObjectPtr& theObject) const
837 {
838   bool aCanDisplay = true;
839
840   bool aHasActiveSketch = activeSketch().get() != NULL;
841   if (aHasActiveSketch) {
842     // 1. the sketch feature should not be displayed during the sketch active operation
843     // it is hidden by a sketch operation start and shown by a sketch stop, just the sketch 
844     // nested features can be visualized
845     FeaturePtr aFeature = ModelAPI_Feature::feature(theObject);
846     if (aFeature.get() != NULL && aFeature == activeSketch()) {
847       aCanDisplay = false;
848     }
849   }
850   else { // there are no an active sketch
851     // 2. sketch sub-features should not be visualized if the sketch operation is not active
852     FeaturePtr aFeature = ModelAPI_Feature::feature(theObject);
853     if (aFeature.get() != NULL) {
854       std::shared_ptr<SketchPlugin_Feature> aSketchFeature =
855                               std::dynamic_pointer_cast<SketchPlugin_Feature>(aFeature);
856       if (aSketchFeature.get()) {
857         aCanDisplay = false;
858       }
859     }
860   }
861
862   // 3. the method should not filter the objects, which are not related to the current operation.
863   // The object is filtered just if it is a current operation feature or this feature result
864   bool isObjectFound = false;
865   ModuleBase_Operation* anOperation = getCurrentOperation();
866   if (anOperation) {
867     FeaturePtr aFeature = anOperation->feature();
868     if (aFeature.get()) {
869       std::list<ResultPtr> aResults = aFeature->results();
870       if (theObject == aFeature)
871         isObjectFound = true;
872       else {
873         std::list<ResultPtr>::const_iterator anIt = aResults.begin(), aLast = aResults.end();
874         for (; anIt != aLast; anIt++) {
875           isObjectFound = *anIt == theObject;
876         }
877       }
878     }
879   }
880   if (!isObjectFound) 
881     return aCanDisplay;
882   
883   // 4. For created nested feature operation do not display the created feature if
884   // the mouse curstor leaves the OCC window.
885   // The correction cases, which ignores this condition:
886   // a. the property panel values modification
887   // b. the popup menu activated
888   // c. widget editor control
889   #ifndef DEBUG_DO_NOT_BY_ENTER
890   if (aCanDisplay && isNestedCreateOperation(getCurrentOperation())) {
891     ModuleBase_Operation* aOperation = getCurrentOperation();
892     ModuleBase_IPropertyPanel* aPanel = aOperation->propertyPanel();
893     ModuleBase_ModelWidget* anActiveWdg = aPanel ? aPanel->activeWidget() : 0;
894     ModuleBase_WidgetEditor* anEditorWdg = anActiveWdg ? dynamic_cast<ModuleBase_WidgetEditor*>(anActiveWdg) : 0;
895     // the active widget editor should not influence here. The presentation should be visible always
896     // when this widget is active.
897     if (!anEditorWdg && !myIsPopupMenuActive) {
898       // during a nested create operation, the feature is redisplayed only if the mouse over view
899       // of there was a value modified in the property panel after the mouse left the view
900       aCanDisplay = canDisplayCurrentCreatedFeature();
901     }
902   }
903   #endif
904   return aCanDisplay;
905 }
906
907 bool PartSet_SketcherMgr::canDisplayCurrentCreatedFeature() const
908 {
909   return myIsPropertyPanelValueChanged || myIsMouseOverWindow;
910 #ifdef DEBUG_MOUSE_OVER_WINDOW_FLAGS
911   qDebug(QString("canDisplayCurrentCreatedFeature: %1").arg(mouseOverWindowFlagsInfo()).toStdString().c_str());
912 #endif
913 }
914
915 bool PartSet_SketcherMgr::isObjectOfSketch(const ObjectPtr& theObject) const
916 {
917   bool isFoundObject = false;
918
919   FeaturePtr anObjectFeature = ModelAPI_Feature::feature(theObject);
920   if (anObjectFeature.get()) {
921     int aSize = myCurrentSketch->numberOfSubs();
922     for (int i = 0; i < myCurrentSketch->numberOfSubs() && !isFoundObject; i++) {
923       FeaturePtr aCurrentFeature = myCurrentSketch->subFeature(i);
924       isFoundObject = myCurrentSketch->subFeature(i) == anObjectFeature;
925     }
926   }
927   return isFoundObject;
928 }
929
930 void PartSet_SketcherMgr::onPlaneSelected(const std::shared_ptr<GeomAPI_Pln>& thePln)
931 {
932   myPlaneFilter->setPlane(thePln->impl<gp_Pln>());
933 }
934
935 void PartSet_SketcherMgr::getCurrentSelection(const FeaturePtr& theFeature,
936                                               const FeaturePtr& theSketch,
937                                               ModuleBase_IWorkshop* theWorkshop,
938                                               FeatureToSelectionMap& theSelection)
939 {
940   if (theFeature.get() == NULL)
941     return;
942
943   std::set<AttributePtr> aSelectedAttributes;
944   std::set<ResultPtr> aSelectedResults;
945
946   ModuleBase_IViewer* aViewer = theWorkshop->viewer();
947   Handle(AIS_InteractiveContext) aContext = aViewer->AISContext();
948   XGUI_ModuleConnector* aConnector = dynamic_cast<XGUI_ModuleConnector*>(theWorkshop);
949   XGUI_Displayer* aDisplayer = aConnector->workshop()->displayer();
950
951   std::list<ResultPtr> aResults = theFeature->results();
952   std::list<ResultPtr>::const_iterator aIt;
953   for (aIt = aResults.begin(); aIt != aResults.end(); ++aIt)
954   {
955     ResultPtr aResult = *aIt;
956     AISObjectPtr aAISObj = aDisplayer->getAISObject(aResult);
957     if (aAISObj.get() == NULL)
958       continue;
959     Handle(AIS_InteractiveObject) anAISIO = aAISObj->impl<Handle(AIS_InteractiveObject)>();
960     for (aContext->InitSelected(); aContext->MoreSelected(); aContext->NextSelected())
961     {
962       Handle(StdSelect_BRepOwner) aBRepOwner = Handle(StdSelect_BRepOwner)::DownCast(
963                                                                       aContext->SelectedOwner());
964       if (aBRepOwner.IsNull())
965         continue;
966       Handle(AIS_InteractiveObject) anIO = Handle(AIS_InteractiveObject)::DownCast(
967                                                                         aBRepOwner->Selectable());
968       if (anIO != anAISIO)
969         continue;
970
971       if (aBRepOwner->HasShape()) {
972         const TopoDS_Shape& aShape = aBRepOwner->Shape();
973         TopAbs_ShapeEnum aShapeType = aShape.ShapeType();
974         if (aShapeType == TopAbs_VERTEX) {
975           AttributePtr aPntAttr = PartSet_Tools::findAttributeBy2dPoint(theFeature,
976                                                                         aShape, theSketch);
977           if (aPntAttr.get() != NULL)
978             aSelectedAttributes.insert(aPntAttr);
979         }
980         else if (aShapeType == TopAbs_EDGE &&
981                  aSelectedResults.find(aResult) == aSelectedResults.end()) {
982           aSelectedResults.insert(aResult);
983         }
984       }
985     }
986   }
987   theSelection[theFeature] = std::make_pair(aSelectedAttributes, aSelectedResults);
988 }
989
990 void PartSet_SketcherMgr::getSelectionOwners(const FeaturePtr& theFeature,
991                                              const FeaturePtr& theSketch,
992                                              ModuleBase_IWorkshop* theWorkshop,
993                                              const FeatureToSelectionMap& theSelection,
994                                              SelectMgr_IndexedMapOfOwner& anOwnersToSelect)
995 {
996   if (theFeature.get() == NULL)
997     return;
998
999   FeatureToSelectionMap::const_iterator anIt = theSelection.find(theFeature);
1000   std::set<AttributePtr> aSelectedAttributes = anIt.value().first;
1001   std::set<ResultPtr> aSelectedResults = anIt.value().second;
1002
1003   ModuleBase_IViewer* aViewer = theWorkshop->viewer();
1004   Handle(AIS_InteractiveContext) aContext = aViewer->AISContext();
1005   XGUI_ModuleConnector* aConnector = dynamic_cast<XGUI_ModuleConnector*>(theWorkshop);
1006   XGUI_Displayer* aDisplayer = aConnector->workshop()->displayer();
1007
1008   // 1. found the feature's owners. Check the AIS objects of the constructions
1009   AISObjectPtr aAISObj = aDisplayer->getAISObject(theFeature);
1010   if (aAISObj.get() != NULL && aSelectedAttributes.empty() && aSelectedResults.empty()) {
1011     Handle(AIS_InteractiveObject) anAISIO = aAISObj->impl<Handle(AIS_InteractiveObject)>();
1012
1013     SelectMgr_IndexedMapOfOwner aSelectedOwners;  
1014     aConnector->workshop()->selector()->selection()->entityOwners(anAISIO, aSelectedOwners);
1015     for  (Standard_Integer i = 1, n = aSelectedOwners.Extent(); i <= n; i++) {
1016       Handle(SelectMgr_EntityOwner) anOwner = aSelectedOwners(i);
1017       if (!anOwner.IsNull())
1018         anOwnersToSelect.Add(anOwner);
1019     }
1020   }
1021
1022   // 2. found the feature results's owners
1023   std::list<ResultPtr> aResults = theFeature->results();
1024   std::list<ResultPtr>::const_iterator aIt;
1025   for (aIt = aResults.begin(); aIt != aResults.end(); ++aIt)
1026   {
1027     ResultPtr aResult = *aIt;
1028     AISObjectPtr aAISObj = aDisplayer->getAISObject(aResult);
1029     if (aAISObj.get() == NULL)
1030       continue; 
1031     Handle(AIS_InteractiveObject) anAISIO = aAISObj->impl<Handle(AIS_InteractiveObject)>();
1032
1033     SelectMgr_IndexedMapOfOwner aSelectedOwners;  
1034     aConnector->workshop()->selector()->selection()->entityOwners(anAISIO, aSelectedOwners);
1035     for  ( Standard_Integer i = 1, n = aSelectedOwners.Extent(); i <= n; i++ ) {
1036       Handle(StdSelect_BRepOwner) anOwner = Handle(StdSelect_BRepOwner)::DownCast(aSelectedOwners(i));
1037       if ( anOwner.IsNull() || !anOwner->HasShape() )
1038         continue;
1039       const TopoDS_Shape& aShape = anOwner->Shape();
1040       TopAbs_ShapeEnum aShapeType = aShape.ShapeType();
1041       if (aShapeType == TopAbs_VERTEX) {
1042         AttributePtr aPntAttr = PartSet_Tools::findAttributeBy2dPoint(theFeature, aShape, theSketch);
1043         if (aPntAttr.get() != NULL &&
1044             aSelectedAttributes.find(aPntAttr) != aSelectedAttributes.end()) {
1045           anOwnersToSelect.Add(anOwner);
1046         }
1047       }
1048       else if (aShapeType == TopAbs_EDGE) {
1049         bool aFound = aSelectedResults.find(aResult) != aSelectedResults.end();
1050         if (aSelectedResults.find(aResult) != aSelectedResults.end() &&
1051             anOwnersToSelect.FindIndex(anOwner) <= 0)
1052           anOwnersToSelect.Add(anOwner);
1053       }
1054     }
1055   }
1056 }
1057
1058 void PartSet_SketcherMgr::connectToPropertyPanel(const bool isToConnect)
1059 {
1060   ModuleBase_IWorkshop* anIWorkshop = myModule->workshop();
1061   XGUI_ModuleConnector* aConnector = dynamic_cast<XGUI_ModuleConnector*>(anIWorkshop);
1062   XGUI_Workshop* aWorkshop = aConnector->workshop();
1063   XGUI_PropertyPanel* aPropertyPanel = aWorkshop->propertyPanel();
1064   if (aPropertyPanel) {
1065     const QList<ModuleBase_ModelWidget*>& aWidgets = aPropertyPanel->modelWidgets();
1066     foreach (ModuleBase_ModelWidget* aWidget, aWidgets) {
1067       if (isToConnect) {
1068         connect(aWidget, SIGNAL(beforeValuesChanged()),
1069                 this, SLOT(onBeforeValuesChangedInPropertyPanel()));
1070         connect(aWidget, SIGNAL(valuesChanged()), this, SLOT(onValuesChangedInPropertyPanel()));
1071         connect(aWidget, SIGNAL(afterValuesChanged()),
1072                 this, SLOT(onAfterValuesChangedInPropertyPanel()));
1073       }
1074       else {
1075         disconnect(aWidget, SIGNAL(beforeValuesChanged()),
1076                    this, SLOT(onBeforeValuesChangedInPropertyPanel()));
1077         disconnect(aWidget, SIGNAL(valuesChanged()), this, SLOT(onValuesChangedInPropertyPanel()));
1078         disconnect(aWidget, SIGNAL(afterValuesChanged()),
1079                    this, SLOT(onAfterValuesChangedInPropertyPanel()));
1080       }
1081     }
1082   }
1083 }
1084
1085 ModuleBase_Operation* PartSet_SketcherMgr::getCurrentOperation() const
1086 {
1087   return myModule->workshop()->currentOperation();
1088 }
1089
1090 void PartSet_SketcherMgr::visualizeFeature(ModuleBase_Operation* theOperation,
1091                                            const bool isToDisplay,
1092                                            const bool isFlushRedisplay)
1093 {
1094   #ifdef DEBUG_DO_NOT_BY_ENTER
1095   return;
1096   #endif
1097
1098   if (!theOperation || theOperation->isEditOperation())
1099     return;
1100
1101   ModuleBase_IWorkshop* aWorkshop = myModule->workshop();
1102   XGUI_ModuleConnector* aConnector = dynamic_cast<XGUI_ModuleConnector*>(aWorkshop);
1103
1104   // 1. change visibility of the object itself, here the presentable object is processed,
1105   // e.g. constraints features
1106   FeaturePtr aFeature = theOperation->feature();
1107   std::list<ResultPtr> aResults = aFeature->results();
1108   if (isToDisplay)
1109     aFeature->setDisplayed(true);
1110   else
1111     aFeature->setDisplayed(false);
1112
1113   // change visibility of the object results, e.g. non-constraint features
1114   std::list<ResultPtr>::const_iterator aIt;
1115   for (aIt = aResults.begin(); aIt != aResults.end(); ++aIt) {
1116     if (isToDisplay) {
1117       (*aIt)->setDisplayed(true);
1118     }
1119     else {
1120       (*aIt)->setDisplayed(false);
1121     }
1122   }
1123   if (isFlushRedisplay)
1124     Events_Loop::loop()->flush(Events_Loop::eventByName(EVENT_OBJECT_TO_REDISPLAY));
1125 }
1126
1127 void PartSet_SketcherMgr::storeSelection(const bool theHighlightedOnly)
1128 {
1129   ModuleBase_IWorkshop* aWorkshop = myModule->workshop();
1130   ModuleBase_ISelection* aSelect = aWorkshop->selection();
1131   QList<ModuleBase_ViewerPrs> aHighlighted = aSelect->getHighlighted();
1132     
1133   QMap<FeaturePtr, QList<AttributePtr> > aFeature2AttributeMap;
1134   if (theHighlightedOnly) {
1135     fillFeature2Attribute(aHighlighted, aFeature2AttributeMap, myCurrentSketch);
1136   }
1137   else {
1138     fillFeature2Attribute(aHighlighted, aFeature2AttributeMap, myCurrentSketch);
1139
1140     QList<ModuleBase_ViewerPrs> aSelected = aSelect->getSelected();
1141     fillFeature2Attribute(aSelected, aFeature2AttributeMap, myCurrentSketch);
1142   }
1143
1144   // 1. it is necessary to save current selection in order to restore it after the features moving
1145   myCurrentSelection.clear();
1146   QMap<FeaturePtr, QList<AttributePtr> >::const_iterator anIt = aFeature2AttributeMap.begin(),
1147                                                          aLast = aFeature2AttributeMap.end();
1148   for (; anIt != aLast; anIt++) {
1149     FeaturePtr aFeature = anIt.key();
1150     getCurrentSelection(aFeature, myCurrentSketch, aWorkshop, myCurrentSelection);
1151   }
1152   //qDebug(QString("  storeSelection: %1").arg(myCurrentSelection.size()).toStdString().c_str());
1153 }
1154
1155 void PartSet_SketcherMgr::restoreSelection()
1156 {
1157   //qDebug(QString("restoreSelection: %1").arg(myCurrentSelection.size()).toStdString().c_str());
1158   ModuleBase_IWorkshop* aWorkshop = myModule->workshop();
1159   XGUI_ModuleConnector* aConnector = dynamic_cast<XGUI_ModuleConnector*>(aWorkshop);
1160   FeatureToSelectionMap::const_iterator aSIt = myCurrentSelection.begin(),
1161                                         aSLast = myCurrentSelection.end();
1162   SelectMgr_IndexedMapOfOwner anOwnersToSelect;
1163   for (; aSIt != aSLast; aSIt++) {
1164     anOwnersToSelect.Clear();
1165     getSelectionOwners(aSIt.key(), myCurrentSketch, aWorkshop, myCurrentSelection,
1166                         anOwnersToSelect);
1167     aConnector->workshop()->selector()->setSelectedOwners(anOwnersToSelect, false);
1168   }
1169 }
1170
1171 void PartSet_SketcherMgr::onShowConstraintsToggle(bool theOn)
1172 {
1173   if (myIsConstraintsShown == theOn)
1174     return;
1175   if (myCurrentSketch.get() == NULL)
1176     return;
1177
1178   myIsConstraintsShown = theOn;
1179
1180   ModuleBase_IWorkshop* aWorkshop = myModule->workshop();
1181   XGUI_ModuleConnector* aConnector = dynamic_cast<XGUI_ModuleConnector*>(aWorkshop);
1182
1183   const QStringList& aConstrIds = constraintsIdList();
1184   for (int i = 0; i < myCurrentSketch->numberOfSubs(); i++) {
1185     FeaturePtr aSubFeature = myCurrentSketch->subFeature(i);
1186     if (aConstrIds.contains(QString(aSubFeature->getKind().c_str()))) {
1187       if (myIsConstraintsShown) 
1188         aSubFeature->setDisplayed(true);
1189       else
1190         aSubFeature->setDisplayed(false);
1191     }
1192   }
1193   Events_Loop::loop()->flush(Events_Loop::eventByName(EVENT_OBJECT_TO_REDISPLAY));
1194 }
1195
1196 QString PartSet_SketcherMgr::mouseOverWindowFlagsInfo() const
1197 {
1198   return QString("myIsPropertyPanelValueChanged = %1,    myIsMouseOverWindow = %2")
1199      .arg(myIsPropertyPanelValueChanged).arg(myIsMouseOverWindow);
1200 }