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