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