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