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