Salome HOME
Merge branch 'Dev_1.1.0' of newgeom:newgeom into Dev_1.1.0
[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 be 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. the method should not filter the objects, which are not related to the current operation.
810   // The object is filtered just if it is a current operation feature or this feature result
811   bool isObjectFound = false;
812   ModuleBase_Operation* anOperation = getCurrentOperation();
813   if (anOperation) {
814     FeaturePtr aFeature = anOperation->feature();
815     if (aFeature.get()) {
816       std::list<ResultPtr> aResults = aFeature->results();
817       if (theObject == aFeature)
818         isObjectFound = true;
819       else {
820         std::list<ResultPtr>::const_iterator anIt = aResults.begin(), aLast = aResults.end();
821         for (; anIt != aLast; anIt++) {
822           isObjectFound = *anIt == theObject;
823         }
824       }
825     }
826   }
827   if (!isObjectFound)
828     return aCanDisplay;
829
830   // 4. For created nested feature operation do not display the created feature if
831   // the mouse curstor leaves the OCC window.
832   // The correction cases, which ignores this condition:
833   // a. the property panel values modification
834   // b. the popup menu activated
835   // c. widget editor control
836   #ifndef DEBUG_DO_NOT_BY_ENTER
837   if (aCanDisplay && isNestedCreateOperation(getCurrentOperation())) {
838     ModuleBase_Operation* aOperation = getCurrentOperation();
839     ModuleBase_IPropertyPanel* aPanel = aOperation->propertyPanel();
840     ModuleBase_ModelWidget* anActiveWdg = aPanel ? aPanel->activeWidget() : 0;
841     ModuleBase_WidgetEditor* anEditorWdg = anActiveWdg ? dynamic_cast<ModuleBase_WidgetEditor*>(anActiveWdg) : 0;
842     // the active widget editor should not influence here. The presentation should be visible always
843     // when this widget is active.
844     if (!anEditorWdg && !myIsPopupMenuActive) {
845       // during a nested create operation, the feature is redisplayed only if the mouse over view
846       // of there was a value modified in the property panel after the mouse left the view
847       aCanDisplay = myIsPropertyPanelValueChanged || myIsMouseOverWindow;
848     }
849   }
850   #endif
851   return aCanDisplay;
852 }
853
854
855 void PartSet_SketcherMgr::onPlaneSelected(const std::shared_ptr<GeomAPI_Pln>& thePln)
856 {
857   myPlaneFilter->setPlane(thePln->impl<gp_Pln>());
858
859   // after the plane is selected in the sketch, the sketch selection should be activated
860   // it can not be performed in the sketch label widget because, we don't need to switch off
861   // the selection by any label deactivation, but need to switch it off by stop the sketch
862   activateObjectsInSketchMode(true);
863 }
864
865 void PartSet_SketcherMgr::getCurrentSelection(const FeaturePtr& theFeature,
866                                               const FeaturePtr& theSketch,
867                                               ModuleBase_IWorkshop* theWorkshop,
868                                               FeatureToSelectionMap& theSelection)
869 {
870   if (theFeature.get() == NULL)
871     return;
872
873   std::set<AttributePtr> aSelectedAttributes;
874   std::set<ResultPtr> aSelectedResults;
875
876   ModuleBase_IViewer* aViewer = theWorkshop->viewer();
877   Handle(AIS_InteractiveContext) aContext = aViewer->AISContext();
878   XGUI_ModuleConnector* aConnector = dynamic_cast<XGUI_ModuleConnector*>(theWorkshop);
879   XGUI_Displayer* aDisplayer = aConnector->workshop()->displayer();
880
881   std::list<ResultPtr> aResults = theFeature->results();
882   std::list<ResultPtr>::const_iterator aIt;
883   for (aIt = aResults.begin(); aIt != aResults.end(); ++aIt)
884   {
885     ResultPtr aResult = *aIt;
886     AISObjectPtr aAISObj = aDisplayer->getAISObject(aResult);
887     if (aAISObj.get() == NULL)
888       continue;
889     Handle(AIS_InteractiveObject) anAISIO = aAISObj->impl<Handle(AIS_InteractiveObject)>();
890     for (aContext->InitSelected(); aContext->MoreSelected(); aContext->NextSelected())
891     {
892       Handle(StdSelect_BRepOwner) aBRepOwner = Handle(StdSelect_BRepOwner)::DownCast(
893                                                                       aContext->SelectedOwner());
894       if (aBRepOwner.IsNull())
895         continue;
896       Handle(AIS_InteractiveObject) anIO = Handle(AIS_InteractiveObject)::DownCast(
897                                                                         aBRepOwner->Selectable());
898       if (anIO != anAISIO)
899         continue;
900
901       if (aBRepOwner->HasShape()) {
902         const TopoDS_Shape& aShape = aBRepOwner->Shape();
903         TopAbs_ShapeEnum aShapeType = aShape.ShapeType();
904         if (aShapeType == TopAbs_VERTEX) {
905           AttributePtr aPntAttr = PartSet_Tools::findAttributeBy2dPoint(theFeature,
906                                                                         aShape, theSketch);
907           if (aPntAttr.get() != NULL)
908             aSelectedAttributes.insert(aPntAttr);
909         }
910         else if (aShapeType == TopAbs_EDGE &&
911                  aSelectedResults.find(aResult) == aSelectedResults.end()) {
912           aSelectedResults.insert(aResult);
913         }
914       }
915     }
916   }
917   theSelection[theFeature] = std::make_pair(aSelectedAttributes, aSelectedResults);
918 }
919
920 void PartSet_SketcherMgr::getSelectionOwners(const FeaturePtr& theFeature,
921                                              const FeaturePtr& theSketch,
922                                              ModuleBase_IWorkshop* theWorkshop,
923                                              const FeatureToSelectionMap& theSelection,
924                                              SelectMgr_IndexedMapOfOwner& anOwnersToSelect)
925 {
926   if (theFeature.get() == NULL)
927     return;
928
929   FeatureToSelectionMap::const_iterator anIt = theSelection.find(theFeature);
930   std::set<AttributePtr> aSelectedAttributes = anIt.value().first;
931   std::set<ResultPtr> aSelectedResults = anIt.value().second;
932
933   ModuleBase_IViewer* aViewer = theWorkshop->viewer();
934   Handle(AIS_InteractiveContext) aContext = aViewer->AISContext();
935   XGUI_ModuleConnector* aConnector = dynamic_cast<XGUI_ModuleConnector*>(theWorkshop);
936   XGUI_Displayer* aDisplayer = aConnector->workshop()->displayer();
937
938   // 1. found the feature's owners. Check the AIS objects of the constructions
939   AISObjectPtr aAISObj = aDisplayer->getAISObject(theFeature);
940   if (aAISObj.get() != NULL && aSelectedAttributes.empty() && aSelectedResults.empty()) {
941     Handle(AIS_InteractiveObject) anAISIO = aAISObj->impl<Handle(AIS_InteractiveObject)>();
942
943     SelectMgr_IndexedMapOfOwner aSelectedOwners;  
944     aConnector->workshop()->selector()->selection()->entityOwners(anAISIO, aSelectedOwners);
945     for  (Standard_Integer i = 1, n = aSelectedOwners.Extent(); i <= n; i++) {
946       Handle(SelectMgr_EntityOwner) anOwner = aSelectedOwners(i);
947       if (!anOwner.IsNull())
948         anOwnersToSelect.Add(anOwner);
949     }
950   }
951
952   // 2. found the feature results's owners
953   std::list<ResultPtr> aResults = theFeature->results();
954   std::list<ResultPtr>::const_iterator aIt;
955   for (aIt = aResults.begin(); aIt != aResults.end(); ++aIt)
956   {
957     ResultPtr aResult = *aIt;
958     AISObjectPtr aAISObj = aDisplayer->getAISObject(aResult);
959     if (aAISObj.get() == NULL)
960       continue; 
961     Handle(AIS_InteractiveObject) anAISIO = aAISObj->impl<Handle(AIS_InteractiveObject)>();
962
963     SelectMgr_IndexedMapOfOwner aSelectedOwners;  
964     aConnector->workshop()->selector()->selection()->entityOwners(anAISIO, aSelectedOwners);
965     for  ( Standard_Integer i = 1, n = aSelectedOwners.Extent(); i <= n; i++ ) {
966       Handle(StdSelect_BRepOwner) anOwner = Handle(StdSelect_BRepOwner)::DownCast(aSelectedOwners(i));
967       if ( anOwner.IsNull() || !anOwner->HasShape() )
968         continue;
969       const TopoDS_Shape& aShape = anOwner->Shape();
970       TopAbs_ShapeEnum aShapeType = aShape.ShapeType();
971       if (aShapeType == TopAbs_VERTEX) {
972         AttributePtr aPntAttr = PartSet_Tools::findAttributeBy2dPoint(theFeature, aShape, theSketch);
973         if (aPntAttr.get() != NULL &&
974             aSelectedAttributes.find(aPntAttr) != aSelectedAttributes.end()) {
975           anOwnersToSelect.Add(anOwner);
976         }
977       }
978       else if (aShapeType == TopAbs_EDGE) {
979         bool aFound = aSelectedResults.find(aResult) != aSelectedResults.end();
980         if (aSelectedResults.find(aResult) != aSelectedResults.end() &&
981             anOwnersToSelect.FindIndex(anOwner) <= 0)
982           anOwnersToSelect.Add(anOwner);
983       }
984     }
985   }
986 }
987
988 void PartSet_SketcherMgr::connectToPropertyPanel(const bool isToConnect)
989 {
990   ModuleBase_IWorkshop* anIWorkshop = myModule->workshop();
991   XGUI_ModuleConnector* aConnector = dynamic_cast<XGUI_ModuleConnector*>(anIWorkshop);
992   XGUI_Workshop* aWorkshop = aConnector->workshop();
993   XGUI_PropertyPanel* aPropertyPanel = aWorkshop->propertyPanel();
994   if (aPropertyPanel) {
995     const QList<ModuleBase_ModelWidget*>& aWidgets = aPropertyPanel->modelWidgets();
996     foreach (ModuleBase_ModelWidget* aWidget, aWidgets) {
997       if (isToConnect) {
998         connect(aWidget, SIGNAL(beforeValuesChanged()),
999                 this, SLOT(onBeforeValuesChangedInPropertyPanel()));
1000         connect(aWidget, SIGNAL(valuesChanged()), this, SLOT(onValuesChangedInPropertyPanel()));
1001         connect(aWidget, SIGNAL(afterValuesChanged()),
1002                 this, SLOT(onAfterValuesChangedInPropertyPanel()));
1003       }
1004       else {
1005         disconnect(aWidget, SIGNAL(beforeValuesChanged()),
1006                    this, SLOT(onBeforeValuesChangedInPropertyPanel()));
1007         disconnect(aWidget, SIGNAL(valuesChanged()), this, SLOT(onValuesChangedInPropertyPanel()));
1008         disconnect(aWidget, SIGNAL(afterValuesChanged()),
1009                    this, SLOT(onAfterValuesChangedInPropertyPanel()));
1010       }
1011     }
1012   }
1013 }
1014
1015 ModuleBase_Operation* PartSet_SketcherMgr::getCurrentOperation() const
1016 {
1017   return myModule->workshop()->currentOperation();
1018 }
1019
1020 void PartSet_SketcherMgr::visualizeFeature(ModuleBase_Operation* theOperation,
1021                                            const bool isToDisplay)
1022 {
1023   #ifdef DEBUG_DO_NOT_BY_ENTER
1024   return;
1025   #endif
1026
1027   if (!theOperation || theOperation->isEditOperation())
1028     return;
1029
1030   ModuleBase_IWorkshop* aWorkshop = myModule->workshop();
1031   XGUI_ModuleConnector* aConnector = dynamic_cast<XGUI_ModuleConnector*>(aWorkshop);
1032   XGUI_Displayer* aDisplayer = aConnector->workshop()->displayer();
1033
1034   // 1. change visibility of the object itself, here the presentable object is processed,
1035   // e.g. constraints features
1036   FeaturePtr aFeature = theOperation->feature();
1037   std::list<ResultPtr> aResults = aFeature->results();
1038   if (isToDisplay)
1039     aDisplayer->display(aFeature, false);
1040   else
1041     aDisplayer->erase(aFeature, false);
1042
1043   // change visibility of the object results, e.g. non-constraint features
1044   std::list<ResultPtr>::const_iterator aIt;
1045   for (aIt = aResults.begin(); aIt != aResults.end(); ++aIt) {
1046     if (isToDisplay) {
1047       aDisplayer->display(*aIt, false);
1048     }
1049     else {
1050       aDisplayer->erase(*aIt, false);
1051     }
1052   }
1053   aDisplayer->updateViewer();
1054 }
1055
1056 void PartSet_SketcherMgr::activateObjectsInSketchMode(const bool isActive)
1057 {
1058   ModuleBase_IWorkshop* aWorkshop = myModule->workshop();
1059   XGUI_ModuleConnector* aConnector = dynamic_cast<XGUI_ModuleConnector*>(aWorkshop);
1060   XGUI_Displayer* aDisplayer = aConnector->workshop()->displayer();
1061
1062   QIntList aModes;
1063   if (isActive) {
1064     aModes.append(SketcherPrs_Tools::Sel_Dimension_Text);
1065     aModes.append(SketcherPrs_Tools::Sel_Dimension_Line);
1066     aModes.append(SketcherPrs_Tools::Sel_Constraint);
1067     aModes.append(AIS_Shape::SelectionMode((TopAbs_ShapeEnum) TopAbs_VERTEX));
1068     aModes.append(AIS_Shape::SelectionMode((TopAbs_ShapeEnum) TopAbs_EDGE));
1069   }
1070   aDisplayer->activateObjects(aModes);
1071 }
1072
1073 void PartSet_SketcherMgr::storeSelection(const bool theHighlightedOnly)
1074 {
1075   ModuleBase_IWorkshop* aWorkshop = myModule->workshop();
1076   ModuleBase_ISelection* aSelect = aWorkshop->selection();
1077   QList<ModuleBase_ViewerPrs> aHighlighted = aSelect->getHighlighted();
1078     
1079   QMap<FeaturePtr, QList<AttributePtr> > aFeature2AttributeMap;
1080   if (theHighlightedOnly) {
1081     fillFeature2Attribute(aHighlighted, aFeature2AttributeMap, myCurrentSketch);
1082   }
1083   else {
1084     fillFeature2Attribute(aHighlighted, aFeature2AttributeMap, myCurrentSketch);
1085
1086     QList<ModuleBase_ViewerPrs> aSelected = aSelect->getSelected();
1087     fillFeature2Attribute(aSelected, aFeature2AttributeMap, myCurrentSketch);
1088   }
1089
1090   // 1. it is necessary to save current selection in order to restore it after the features moving
1091   myCurrentSelection.clear();
1092   QMap<FeaturePtr, QList<AttributePtr> >::const_iterator anIt = aFeature2AttributeMap.begin(),
1093                                                          aLast = aFeature2AttributeMap.end();
1094   for (; anIt != aLast; anIt++) {
1095     FeaturePtr aFeature = anIt.key();
1096     getCurrentSelection(aFeature, myCurrentSketch, aWorkshop, myCurrentSelection);
1097   }
1098   //qDebug(QString("  storeSelection: %1").arg(myCurrentSelection.size()).toStdString().c_str());
1099 }
1100
1101 void PartSet_SketcherMgr::restoreSelection()
1102 {
1103   //qDebug(QString("restoreSelection: %1").arg(myCurrentSelection.size()).toStdString().c_str());
1104   ModuleBase_IWorkshop* aWorkshop = myModule->workshop();
1105   XGUI_ModuleConnector* aConnector = dynamic_cast<XGUI_ModuleConnector*>(aWorkshop);
1106   FeatureToSelectionMap::const_iterator aSIt = myCurrentSelection.begin(),
1107                                         aSLast = myCurrentSelection.end();
1108   SelectMgr_IndexedMapOfOwner anOwnersToSelect;
1109   for (; aSIt != aSLast; aSIt++) {
1110     anOwnersToSelect.Clear();
1111     getSelectionOwners(aSIt.key(), myCurrentSketch, aWorkshop, myCurrentSelection,
1112                         anOwnersToSelect);
1113     aConnector->workshop()->selector()->setSelectedOwners(anOwnersToSelect, false);
1114   }
1115 }
1116
1117 void PartSet_SketcherMgr::onShowConstraintsToggle(bool theOn)
1118 {
1119   if (myIsConstraintsShown == theOn)
1120     return;
1121   if (myCurrentSketch.get() == NULL)
1122     return;
1123
1124   myIsConstraintsShown = theOn;
1125
1126   ModuleBase_IWorkshop* aWorkshop = myModule->workshop();
1127   XGUI_ModuleConnector* aConnector = dynamic_cast<XGUI_ModuleConnector*>(aWorkshop);
1128   XGUI_Displayer* aDisplayer = aConnector->workshop()->displayer();
1129
1130   const QStringList& aConstrIds = constraintsIdList();
1131   for (int i = 0; i < myCurrentSketch->numberOfSubs(); i++) {
1132     FeaturePtr aSubFeature = myCurrentSketch->subFeature(i);
1133     if (aConstrIds.contains(QString(aSubFeature->getKind().c_str()))) {
1134       if (myIsConstraintsShown) 
1135         aDisplayer->display(aSubFeature, false);
1136       else
1137         aDisplayer->erase(aSubFeature, false);
1138     }
1139   }
1140   aDisplayer->updateViewer();
1141 }