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