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