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