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