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