Salome HOME
#1327 Fatal error when create arc
[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_SketcherReetntrantMgr.h"
9 #include "PartSet_Module.h"
10 #include "PartSet_WidgetPoint2d.h"
11 #include "PartSet_WidgetPoint2dDistance.h"
12 #include "PartSet_Tools.h"
13 #include "PartSet_WidgetSketchLabel.h"
14 #include "PartSet_WidgetEditor.h"
15
16 #include <XGUI_ModuleConnector.h>
17 #include <XGUI_Displayer.h>
18 #include <XGUI_Workshop.h>
19 #include <XGUI_ContextMenuMgr.h>
20 #include <XGUI_Selection.h>
21 #include <XGUI_SelectionMgr.h>
22 #include <XGUI_ModuleConnector.h>
23 #include <XGUI_PropertyPanel.h>
24 #include <XGUI_ViewerProxy.h>
25 #include <XGUI_OperationMgr.h>
26
27 #include <ModuleBase_IPropertyPanel.h>
28 #include <ModuleBase_ISelection.h>
29 #include <ModuleBase_IViewer.h>
30 #include <ModuleBase_IWorkshop.h>
31 #include <ModuleBase_IViewWindow.h>
32 #include <ModuleBase_ModelWidget.h>
33 #include <ModuleBase_Operation.h>
34 #include <ModuleBase_OperationFeature.h>
35 #include <ModuleBase_Operation.h>
36 #include <ModuleBase_WidgetEditor.h>
37
38 #include <GeomDataAPI_Point2D.h>
39
40 #include <Events_Loop.h>
41
42 #include <SketchPlugin_Line.h>
43 #include <SketchPlugin_Sketch.h>
44 #include <SketchPlugin_Point.h>
45 #include <SketchPlugin_Arc.h>
46 #include <SketchPlugin_Circle.h>
47 #include <SketchPlugin_ConstraintLength.h>
48 #include <SketchPlugin_ConstraintDistance.h>
49 #include <SketchPlugin_ConstraintParallel.h>
50 #include <SketchPlugin_ConstraintPerpendicular.h>
51 #include <SketchPlugin_ConstraintRadius.h>
52 #include <SketchPlugin_ConstraintRigid.h>
53 #include <SketchPlugin_ConstraintHorizontal.h>
54 #include <SketchPlugin_ConstraintVertical.h>
55 #include <SketchPlugin_ConstraintEqual.h>
56 #include <SketchPlugin_ConstraintTangent.h>
57 #include <SketchPlugin_ConstraintCoincidence.h>
58 #include <SketchPlugin_ConstraintFillet.h>
59 #include <SketchPlugin_ConstraintMirror.h>
60 #include <SketchPlugin_ConstraintAngle.h>
61 #include <SketchPlugin_ConstraintCollinear.h>
62 #include <SketchPlugin_ConstraintMiddle.h>
63 #include <SketchPlugin_MultiRotation.h>
64 #include <SketchPlugin_MultiTranslation.h>
65 #include <SketchPlugin_IntersectionPoint.h>
66
67 #include <SketcherPrs_Tools.h>
68
69 #include <SelectMgr_IndexedMapOfOwner.hxx>
70 #include <StdSelect_BRepOwner.hxx>
71
72 //#include <AIS_DimensionSelectionMode.hxx>
73 #include <AIS_Shape.hxx>
74 #include <AIS_Dimension.hxx>
75
76 #include <ModelAPI_Events.h>
77 #include <ModelAPI_Session.h>
78 #include <ModelAPI_AttributeString.h>
79
80 #include <QMouseEvent>
81 #include <QApplication>
82 #include <QCursor>
83 #include <QMessageBox>
84
85 //#define DEBUG_DO_NOT_BY_ENTER
86
87 //#define DEBUG_CURSOR
88
89 /// Returns list of unique objects by sum of objects from List1 and List2
90 /*QList<ModuleBase_ViewerPrs> getSumList(const QList<ModuleBase_ViewerPrs>& theList1,
91                                        const QList<ModuleBase_ViewerPrs>& theList2)
92 {
93   QList<ModuleBase_ViewerPrs> aRes;
94   foreach (ModuleBase_ViewerPrs aPrs, theList1) {
95     if (!aRes.contains(aPrs))
96       aRes.append(aPrs);
97   }
98   foreach (ModuleBase_ViewerPrs aPrs, theList2) {
99     if (!aRes.contains(aPrs))
100       aRes.append(aPrs);
101   }
102   return aRes;
103 }*/
104
105 // Fills the list of features the list of selected presentations.
106 // \param theList a list of selected presentations
107 // \param theSketch a sketch to project a vertex shape of a presentation to the plane
108 // and find the corresponded attribute
109 // \param theFeatureList  an output list of features
110 void fillFeatureList(const QList<ModuleBase_ViewerPrs>& theList,
111                      const FeaturePtr theSketch,
112                      QList<FeaturePtr>& theFeatureList)
113 {
114   QList<ModuleBase_ViewerPrs> aRes;
115
116   QList<ModuleBase_ViewerPrs>::const_iterator anIt = theList.begin(),
117                                               aLast = theList.end();
118   for (; anIt != aLast; anIt++)
119   {
120     ModuleBase_ViewerPrs aPrs = *anIt;
121     FeaturePtr aFeature = ModelAPI_Feature::feature(aPrs.object());
122     if (aFeature.get()  && !theFeatureList.contains(aFeature))
123       theFeatureList.append(aFeature);
124   }
125 }
126
127 /// Fills attribute and result lists by the selected owner. In case if the attribute is found,
128 /// by the owner shape, it is put to the list. Otherwise if type of owner shape is edge, put the function
129 /// result as is to the list of results.
130 /// \param theOwner a viewer selected owner
131 /// \param theFeature a feature, where the attribute is searched
132 /// \param theSketch a current sketch
133 /// \param theSelectedAttribute an output list of attributes
134 /// \param theSelectedResults an output list of edge results
135 void getAttributesOrResults(const Handle(SelectMgr_EntityOwner)& theOwner,
136                             const FeaturePtr& theFeature, const FeaturePtr& theSketch,
137                             const ResultPtr& theResult,
138                             std::set<AttributePtr>& aSelectedAttributes,
139                             std::set<ResultPtr>& aSelectedResults)
140 {
141   Handle(StdSelect_BRepOwner) aBRepOwner = Handle(StdSelect_BRepOwner)::DownCast(theOwner);
142   if (aBRepOwner.IsNull())
143     return;
144   Handle(AIS_InteractiveObject) anIO = Handle(AIS_InteractiveObject)::DownCast(
145                                                                     aBRepOwner->Selectable());
146   if (aBRepOwner->HasShape()) {
147     const TopoDS_Shape& aShape = aBRepOwner->Shape();
148     TopAbs_ShapeEnum aShapeType = aShape.ShapeType();
149     if (aShapeType == TopAbs_VERTEX) {
150       AttributePtr aPntAttr = PartSet_Tools::findAttributeBy2dPoint(theFeature,
151                                                                     aShape, theSketch);
152       if (aPntAttr.get() != NULL)
153         aSelectedAttributes.insert(aPntAttr);
154     }
155     else if (aShapeType == TopAbs_EDGE &&
156              aSelectedResults.find(theResult) == aSelectedResults.end()) {
157       aSelectedResults.insert(theResult);
158     }
159   }
160 }
161
162 PartSet_SketcherMgr::PartSet_SketcherMgr(PartSet_Module* theModule)
163   : QObject(theModule), myModule(theModule), myIsDragging(false), myDragDone(false),
164     myIsMouseOverWindow(false),
165     myIsMouseOverViewProcessed(true), myPreviousUpdateViewerEnabled(true),
166     myIsPopupMenuActive(false)
167 {
168   ModuleBase_IWorkshop* anIWorkshop = myModule->workshop();
169   ModuleBase_IViewer* aViewer = anIWorkshop->viewer();
170
171   myPreviousDrawModeEnabled = true;//aViewer->isSelectionEnabled();
172
173   connect(aViewer, SIGNAL(mousePress(ModuleBase_IViewWindow*, QMouseEvent*)),
174           this, SLOT(onMousePressed(ModuleBase_IViewWindow*, QMouseEvent*)));
175
176   connect(aViewer, SIGNAL(mouseRelease(ModuleBase_IViewWindow*, QMouseEvent*)),
177           this, SLOT(onMouseReleased(ModuleBase_IViewWindow*, QMouseEvent*)));
178
179   connect(aViewer, SIGNAL(mouseMove(ModuleBase_IViewWindow*, QMouseEvent*)),
180           this, SLOT(onMouseMoved(ModuleBase_IViewWindow*, QMouseEvent*)));
181
182   connect(aViewer, SIGNAL(mouseDoubleClick(ModuleBase_IViewWindow*, QMouseEvent*)),
183           this, SLOT(onMouseDoubleClick(ModuleBase_IViewWindow*, QMouseEvent*)));
184
185   XGUI_ModuleConnector* aConnector = dynamic_cast<XGUI_ModuleConnector*>(anIWorkshop);
186   XGUI_Workshop* aWorkshop = aConnector->workshop();
187   connect(aWorkshop, SIGNAL(applicationStarted()), this, SLOT(onApplicationStarted()));
188
189   myIsConstraintsShown[PartSet_Tools::Geometrical] = true;
190   myIsConstraintsShown[PartSet_Tools::Dimensional] = true;
191 }
192
193 PartSet_SketcherMgr::~PartSet_SketcherMgr()
194 {
195   if (!myPlaneFilter.IsNull())
196     myPlaneFilter.Nullify();
197 }
198
199 void PartSet_SketcherMgr::onEnterViewPort()
200 {
201   // 1. if the mouse over window, update the next flag. Do not perform update visibility of
202   // created feature because it should be done in onMouseMove(). Some widgets watch
203   // the mouse move and use the cursor position to update own values. If the presentaion is
204   // redisplayed before this update, the feature presentation jumps from reset value to current.
205   myIsMouseOverWindow = true;
206
207   #ifdef DEBUG_DO_NOT_BY_ENTER
208   return;
209   #endif
210
211   if (canChangeCursor(getCurrentOperation())) {
212     QCursor* aCurrentCursor = QApplication::overrideCursor();
213     if (!aCurrentCursor || aCurrentCursor->shape() != Qt::CrossCursor) {
214       QApplication::setOverrideCursor(QCursor(Qt::CrossCursor));
215 #ifdef DEBUG_CURSOR
216       qDebug("onEnterViewPort() : Qt::CrossCursor");
217 #endif
218     }
219   }
220
221   if (!isNestedCreateOperation(getCurrentOperation()))
222     return;
223
224   operationMgr()->onValidateOperation();
225
226   // we need change displayed state of the current operation feature
227   // if the feature is presentable, e.g. distance construction. It has no results, so workshop does
228   // not accept a signal about the result created. Nothing is shown until mouse is moved out/in view
229   // port. If the isDisplayed flag is true, the presentable feature is displayed as soon as the
230   // presentation becomes valid and redisplay happens
231   //ModuleBase_Operation* aOperation = getCurrentOperation();
232   ModuleBase_OperationFeature* aFOperation = dynamic_cast<ModuleBase_OperationFeature*>
233                                                                            (getCurrentOperation());
234   if (aFOperation) {
235     FeaturePtr aFeature = aFOperation->feature();
236     if (aFeature.get() && aFeature->data()->isValid()) {
237       visualizeFeature(aFeature, aFOperation->isEditOperation(), canDisplayObject(aFeature), false);
238     }
239   }
240 }
241
242 void PartSet_SketcherMgr::onLeaveViewPort()
243 {
244   myIsMouseOverViewProcessed = false;
245   myIsMouseOverWindow = false;
246   // it is important to validate operation here only if sketch entity create operation is active
247   // because at this operation we reacts to the mouse leave/enter view port
248   //operationMgr()->onValidateOperation();
249
250   #ifdef DEBUG_DO_NOT_BY_ENTER
251   return;
252   #endif
253
254   if (canChangeCursor(getCurrentOperation())) {
255     QApplication::restoreOverrideCursor();
256 #ifdef DEBUG_CURSOR
257     qDebug("onLeaveViewPort() : None");
258 #endif
259   }
260
261   if (!isNestedCreateOperation(getCurrentOperation()))
262     return;
263
264   // the method should be performed if the popup menu is called,
265   // the reset of the current widget should not happen
266   if (myIsPopupMenuActive)
267     return;
268
269   operationMgr()->onValidateOperation();
270
271   // 2. if the mouse IS NOT over window, reset the active widget value and hide the presentation
272   ModuleBase_IWorkshop* aWorkshop = myModule->workshop();
273   XGUI_ModuleConnector* aConnector = dynamic_cast<XGUI_ModuleConnector*>(aWorkshop);
274   XGUI_Displayer* aDisplayer = aConnector->workshop()->displayer();
275   // disable the viewer update in order to avoid visualization of redisplayed feature in viewer
276   // obtained after reset value
277   bool isEnableUpdateViewer = aDisplayer->enableUpdateViewer(false);
278   ModuleBase_ModelWidget* anActiveWidget = getActiveWidget();
279   if (anActiveWidget)
280     anActiveWidget->reset();
281
282   // hides the presentation of the current operation feature
283   // the feature is to be erased here, but it is correct to call canDisplayObject because
284   // there can be additional check (e.g. editor widget in distance constraint)
285   ModuleBase_OperationFeature* aFOperation = dynamic_cast<ModuleBase_OperationFeature*>
286                                                                            (getCurrentOperation());
287   if (aFOperation) {
288     FeaturePtr aFeature = aFOperation->feature();
289     visualizeFeature(aFeature, aFOperation->isEditOperation(), canDisplayObject(aFeature));
290   }
291   // we should update viewer after the presentation are hidden in the viewer
292   // otherwise the reset presentation(line) appears in the viewer(by quick move from viewer to PP)
293   aDisplayer->enableUpdateViewer(isEnableUpdateViewer);
294 }
295
296 void PartSet_SketcherMgr::onBeforeValuesChangedInPropertyPanel()
297 {
298   if (!isNestedEditOperation(getCurrentOperation()) ||
299       myModule->sketchReentranceMgr()->isInternalEditActive())
300     return;
301   // it is necessary to save current selection in order to restore it after the values are modifed
302   storeSelection();
303
304   ModuleBase_IWorkshop* aWorkshop = myModule->workshop();
305   XGUI_ModuleConnector* aConnector = dynamic_cast<XGUI_ModuleConnector*>(aWorkshop);
306   XGUI_Displayer* aDisplayer = aConnector->workshop()->displayer();
307   myPreviousUpdateViewerEnabled = aDisplayer->enableUpdateViewer(false);
308 }
309
310 void PartSet_SketcherMgr::onAfterValuesChangedInPropertyPanel()
311 {
312   if (!isNestedEditOperation(getCurrentOperation()) ||
313       myModule->sketchReentranceMgr()->isInternalEditActive()) {
314     myModule->sketchReentranceMgr()->updateInternalEditActiveState();
315     return;
316   }
317   // it is necessary to restore current selection in order to restore it after the values are modified
318   restoreSelection();
319   myCurrentSelection.clear();
320
321   // 3. the flag to disable the update viewer should be set in order to avoid blinking in the 
322   // viewer happens by deselect/select the modified objects. The flag should be restored after
323   // the selection processing. The update viewer should be also called.
324   ModuleBase_IWorkshop* aWorkshop = myModule->workshop();
325   XGUI_ModuleConnector* aConnector = dynamic_cast<XGUI_ModuleConnector*>(aWorkshop);
326   XGUI_Displayer* aDisplayer = aConnector->workshop()->displayer();
327   aDisplayer->enableUpdateViewer(myPreviousUpdateViewerEnabled);
328   aDisplayer->updateViewer();
329
330
331 }
332
333 void PartSet_SketcherMgr::onMousePressed(ModuleBase_IViewWindow* theWnd, QMouseEvent* theEvent)
334 {
335   if (myModule->sketchReentranceMgr()->processMousePressed(theWnd, theEvent))
336     return;
337
338   //get2dPoint(theWnd, theEvent, myClickedPoint);
339
340   if (!(theEvent->buttons() & Qt::LeftButton))
341     return;
342
343   // Clear dragging mode
344   myIsDragging = false;
345
346   ModuleBase_IWorkshop* aWorkshop = myModule->workshop();
347   ModuleBase_IViewer* aViewer = aWorkshop->viewer();
348   if (!aViewer->canDragByMouse())
349     return;
350
351   ModuleBase_OperationFeature* aFOperation = dynamic_cast<ModuleBase_OperationFeature*>
352                                                                (getCurrentOperation());
353   if (!aFOperation)
354     return;
355
356   if (aFOperation->isEditOperation()) {
357     // If the current widget is a selector, do nothing, it processes the mouse press
358     ModuleBase_ModelWidget* anActiveWidget = getActiveWidget();
359     if(anActiveWidget && anActiveWidget->isViewerSelector()) {
360       return;
361     }
362   }
363
364   // Use only for sketch operations
365   if (myCurrentSketch) {
366     if (!PartSet_Tools::sketchPlane(myCurrentSketch))
367       return;
368
369     bool isSketcher = isSketchOperation(aFOperation);
370     bool isSketchOpe = isNestedSketchOperation(aFOperation);
371
372     // Avoid non-sketch operations
373     if ((!isSketchOpe) && (!isSketcher))
374       return;
375
376     bool isEditing = aFOperation->isEditOperation();
377
378     // Ignore creation sketch operation
379     if ((!isSketcher) && (!isEditing))
380       return;
381
382     Handle(AIS_InteractiveContext) aContext = aViewer->AISContext();
383     if (!aContext.IsNull()) {
384       // MoveTo in order to highlight current object
385       aContext->MoveTo(theEvent->x(), theEvent->y(), theWnd->v3dView());
386     }
387     // Remember highlighted objects for editing
388     ModuleBase_ISelection* aSelect = aWorkshop->selection();
389
390     bool aHasShift = (theEvent->modifiers() & Qt::ShiftModifier);
391     storeSelection(!aHasShift);
392
393     if (myCurrentSelection.empty()) {
394       if (isSketchOpe && (!isSketcher))
395         // commit previous operation
396         if (!aFOperation->commit())
397           aFOperation->abort();
398       return;
399     }
400     // Init flyout point for radius rotation
401     FeaturePtr aFeature = myCurrentSelection.begin().key();
402
403     if (isSketcher) {
404       myIsDragging = true;
405       get2dPoint(theWnd, theEvent, myCurrentPoint);
406       myDragDone = false;
407
408       myPreviousDrawModeEnabled = aViewer->enableDrawMode(false);
409       launchEditing();
410       if (aFeature.get() != NULL) {
411         std::shared_ptr<SketchPlugin_Feature> aSPFeature = 
412                   std::dynamic_pointer_cast<SketchPlugin_Feature>(aFeature);
413       if (aSPFeature.get() && aSPFeature->getKind() == SketchPlugin_ConstraintRadius::ID()) {
414           DataPtr aData = aSPFeature->data();
415           AttributePtr aAttr = aData->attribute(SketchPlugin_Constraint::FLYOUT_VALUE_PNT());
416           std::shared_ptr<GeomDataAPI_Point2D> aFPAttr = 
417             std::dynamic_pointer_cast<GeomDataAPI_Point2D>(aAttr);
418           aFPAttr->setValue(myCurrentPoint.myCurX, myCurrentPoint.myCurY);
419         }
420       }
421     } else if (isSketchOpe && isEditing) {
422       // If selected another object commit current result
423       aFOperation->commit();
424
425       myIsDragging = true;
426       get2dPoint(theWnd, theEvent, myCurrentPoint);
427       myDragDone = false;
428
429       myPreviousDrawModeEnabled = aViewer->enableDrawMode(false);
430
431       // this is temporary commented in order to avoid the following wrong case:
432       // Distance constraint is under edition, double click on the digit -> nothing happens
433       // because QApplication::processEvents() calls onMouseDoubleClick, which try to show editor
434       // but as the prev edit is commited an new one is not started, editor is not shown.
435       // This is necessary in order to finalize previous operation
436       //QApplication::processEvents();
437       launchEditing();
438     }
439   }
440 }
441
442 void PartSet_SketcherMgr::onMouseReleased(ModuleBase_IViewWindow* theWnd, QMouseEvent* theEvent)
443 {
444   if (myModule->sketchReentranceMgr()->processMouseReleased(theWnd, theEvent))
445     return;
446
447   ModuleBase_IWorkshop* aWorkshop = myModule->workshop();
448   ModuleBase_IViewer* aViewer = aWorkshop->viewer();
449   if (!aViewer->canDragByMouse())
450     return;
451   ModuleBase_Operation* aOp = getCurrentOperation();
452   if (aOp) {
453     if (isNestedSketchOperation(aOp)) {
454       //get2dPoint(theWnd, theEvent, myClickedPoint);
455
456       // Only for sketcher operations
457       if (myIsDragging) {
458         if (myDragDone) {
459           //aOp->commit();
460           myCurrentSelection.clear();
461           /*Handle(AIS_InteractiveContext) aContext = aViewer->AISContext();
462           if (!aContext.IsNull()) {
463           // Reselect edited object
464           aContext->MoveTo(theEvent->x(), theEvent->y(), theWnd->v3dView());
465           if (theEvent->modifiers() & Qt::ShiftModifier)
466             aContext->ShiftSelect();
467           else
468             aContext->Select();
469           */
470         }
471       }
472     }
473   }
474
475   aWorkshop->viewer()->enableDrawMode(myPreviousDrawModeEnabled);
476   myIsDragging = false;
477 }
478
479 void PartSet_SketcherMgr::onMouseMoved(ModuleBase_IViewWindow* theWnd, QMouseEvent* theEvent)
480 {
481   if (myModule->sketchReentranceMgr()->processMouseMoved(theWnd, theEvent))
482     return;
483
484   if (isNestedCreateOperation(getCurrentOperation()) && !myIsMouseOverViewProcessed) {
485     myIsMouseOverViewProcessed = true;
486     // 1. perform the widget mouse move functionality and display the presentation
487     // the mouse move should be processed in the widget, if it can in order to visualize correct
488     // presentation. These widgets correct the feature attribute according to the mouse position
489     ModuleBase_ModelWidget* anActiveWidget = getActiveWidget();
490     PartSet_WidgetPoint2D* aPoint2DWdg = dynamic_cast<PartSet_WidgetPoint2D*>(anActiveWidget);
491     if (aPoint2DWdg) {
492       aPoint2DWdg->onMouseMove(theWnd, theEvent);
493     }
494     PartSet_WidgetPoint2dDistance* aDistanceWdg = dynamic_cast<PartSet_WidgetPoint2dDistance*>
495                                                                 (anActiveWidget);
496     if (aDistanceWdg) {
497       aDistanceWdg->onMouseMove(theWnd, theEvent);
498     }
499     // the feature is to be erased here, but it is correct to call canDisplayObject because
500     // there can be additional check (e.g. editor widget in distance constraint)
501     ModuleBase_OperationFeature* aFOperation = dynamic_cast<ModuleBase_OperationFeature*>
502                                                                              (getCurrentOperation());
503     if (aFOperation) {
504       FeaturePtr aFeature = aFOperation->feature();
505       visualizeFeature(aFeature, aFOperation->isEditOperation(), canDisplayObject(aFeature));
506     }
507   }
508
509   //myClickedPoint.clear();
510
511   if (myIsDragging) {
512     // 1. the current selection is saved in the mouse press method in order to restore it after moving
513     // 2. the enable selection in the viewer should be temporary switched off in order to ignore
514     // mouse press signal in the viewer(it call Select for AIS context and the dragged objects are
515     // deselected). This flag should be restored in the slot, processed the mouse release signal.
516
517     ModuleBase_Operation* aCurrentOperation = getCurrentOperation();
518     if (!aCurrentOperation)
519       return;
520     if (isSketchOperation(aCurrentOperation))
521       return; // No edit operation activated
522
523     Handle(V3d_View) aView = theWnd->v3dView();
524     gp_Pnt aPoint = PartSet_Tools::convertClickToPoint(theEvent->pos(), aView);
525     double aX, aY;
526     PartSet_Tools::convertTo2D(aPoint, myCurrentSketch, aView, aX, aY);
527     double dX =  aX - myCurrentPoint.myCurX;
528     double dY =  aY - myCurrentPoint.myCurY;
529
530     ModuleBase_IWorkshop* aWorkshop = myModule->workshop();
531     XGUI_ModuleConnector* aConnector = dynamic_cast<XGUI_ModuleConnector*>(aWorkshop);
532     XGUI_Displayer* aDisplayer = aConnector->workshop()->displayer();
533     // 3. the flag to disable the update viewer should be set in order to avoid blinking in the 
534     // viewer happens by deselect/select the modified objects. The flag should be restored after
535     // the selection processing. The update viewer should be also called.
536     bool isEnableUpdateViewer = aDisplayer->enableUpdateViewer(false);
537
538     static Events_ID aMoveEvent = Events_Loop::eventByName(EVENT_OBJECT_MOVED);
539     //static Events_ID aUpdateEvent = Events_Loop::eventByName(EVENT_OBJECT_UPDATED);
540     FeatureToSelectionMap::const_iterator anIt = myCurrentSelection.begin(),
541                                           aLast = myCurrentSelection.end();
542     // 4. the features and attributes modification(move)
543     bool isModified = false;
544     for (; anIt != aLast; anIt++) {
545       FeaturePtr aFeature = anIt.key();
546
547       std::set<AttributePtr> anAttributes = anIt.value().first;
548       // Process selection by attribute: the priority to the attribute
549       if (!anAttributes.empty()) {
550         std::set<AttributePtr>::const_iterator anAttIt = anAttributes.begin(),
551                                                anAttLast = anAttributes.end();
552         for (; anAttIt != anAttLast; anAttIt++) {
553           AttributePtr anAttr = *anAttIt;
554           if (anAttr.get() == NULL)
555             continue;
556           std::string aAttrId = anAttr->id();
557           DataPtr aData = aFeature->data();
558           if (aData->isValid()) {
559             std::shared_ptr<GeomDataAPI_Point2D> aPoint = 
560               std::dynamic_pointer_cast<GeomDataAPI_Point2D>(aData->attribute(aAttrId));
561             if (aPoint.get() != NULL) {
562               bool isImmutable = aPoint->setImmutable(true);
563               aPoint->move(dX, dY);
564               isModified = true;
565               ModelAPI_EventCreator::get()->sendUpdated(aFeature, aMoveEvent);
566               aPoint->setImmutable(isImmutable);
567             }
568           }
569         }
570       } else {
571         // Process selection by feature
572         std::shared_ptr<SketchPlugin_Feature> aSketchFeature =
573           std::dynamic_pointer_cast<SketchPlugin_Feature>(aFeature);
574         if (aSketchFeature) {
575           aSketchFeature->move(dX, dY);
576           isModified = true;
577           ModelAPI_EventCreator::get()->sendUpdated(aSketchFeature, aMoveEvent);
578         }
579       }
580     }
581     // the modified state of the current operation should be updated if there are features, which
582     // were changed here
583     if (isModified) {
584       aCurrentOperation->onValuesChanged();
585     }
586     Events_Loop::loop()->flush(aMoveEvent); // up all move events - to be processed in the solver
587     //Events_Loop::loop()->flush(aUpdateEvent); // up update events - to redisplay presentations
588
589     // 5. it is necessary to save current selection in order to restore it after the features moving
590     restoreSelection();
591     // 6. restore the update viewer flag and call this update
592     aDisplayer->enableUpdateViewer(isEnableUpdateViewer);
593     aDisplayer->updateViewer();
594
595     myDragDone = true;
596     myCurrentPoint.setValue(aX, aY);
597   }
598 }
599
600 void PartSet_SketcherMgr::onMouseDoubleClick(ModuleBase_IViewWindow* theWnd, QMouseEvent* theEvent)
601 {
602   ModuleBase_OperationFeature* aFOperation = dynamic_cast<ModuleBase_OperationFeature*>
603                                                                (getCurrentOperation());
604   if (aFOperation && aFOperation->isEditOperation()) {
605     std::string aId = aFOperation->id().toStdString();
606     if (isDistanceOperation(aFOperation))
607     {
608       // Activate dimension value editing on double click
609       ModuleBase_IPropertyPanel* aPanel = aFOperation->propertyPanel();
610       QList<ModuleBase_ModelWidget*> aWidgets = aPanel->modelWidgets();
611       // Find corresponded widget to activate value editing
612       foreach (ModuleBase_ModelWidget* aWgt, aWidgets) {
613         if (aWgt->attributeID() == "ConstraintValue") {
614           PartSet_WidgetEditor* anEditor = dynamic_cast<PartSet_WidgetEditor*>(aWgt);
615           if (anEditor)
616             anEditor->showPopupEditor();
617           return;
618         }
619       }
620     }
621   }
622 }
623
624 void PartSet_SketcherMgr::onApplicationStarted()
625 {
626   ModuleBase_IWorkshop* anIWorkshop = myModule->workshop();
627   XGUI_ModuleConnector* aConnector = dynamic_cast<XGUI_ModuleConnector*>(anIWorkshop);
628   XGUI_Workshop* aWorkshop = aConnector->workshop();
629   PartSet_SketcherReetntrantMgr* aReentranceMgr = myModule->sketchReentranceMgr();
630
631   XGUI_PropertyPanel* aPropertyPanel = aWorkshop->propertyPanel();
632   if (aPropertyPanel) {
633     //connect(aPropertyPanel, SIGNAL(beforeWidgetActivated(ModuleBase_ModelWidget*)),
634     //        this, SLOT(onBeforeWidgetActivated(ModuleBase_ModelWidget*)));
635
636     connect(aPropertyPanel, SIGNAL(noMoreWidgets(const std::string&)),
637             aReentranceMgr, SLOT(onNoMoreWidgets(const std::string&)));
638     connect(aPropertyPanel, SIGNAL(widgetActivated(ModuleBase_ModelWidget*)),
639             aReentranceMgr, SLOT(onWidgetActivated()));
640   }
641
642   XGUI_ViewerProxy* aViewerProxy = aWorkshop->viewer();
643   connect(aViewerProxy, SIGNAL(enterViewPort()), this, SLOT(onEnterViewPort()));
644   connect(aViewerProxy, SIGNAL(leaveViewPort()), this, SLOT(onLeaveViewPort()));
645
646   XGUI_ContextMenuMgr* aContextMenuMgr = aWorkshop->contextMenuMgr();
647   connect(aContextMenuMgr, SIGNAL(beforeContextMenu()), this, SLOT(onBeforeContextMenu()));
648   connect(aContextMenuMgr, SIGNAL(afterContextMenu()), this, SLOT(onAfterContextMenu()));
649 }
650
651 //void PartSet_SketcherMgr::onBeforeWidgetActivated(ModuleBase_ModelWidget* theWidget)
652 //{
653   //if (!myClickedPoint.myIsInitialized)
654   //  return;
655
656   //ModuleBase_Operation* aOperation = getCurrentOperation();
657   // the distance constraint feature should not use the clickedd point
658   // this is workaround in order to don't throw down the flyout point value,
659   // set by execute() method of these type of features
660   //if (isDistanceOperation(aOperation))
661   //  return;
662
663   //PartSet_WidgetPoint2D* aPnt2dWgt = dynamic_cast<PartSet_WidgetPoint2D*>(theWidget);
664   //if (aPnt2dWgt) {
665   //  aPnt2dWgt->setPoint(myClickedPoint.myCurX, myClickedPoint.myCurY);
666   //}
667 //}
668
669 void PartSet_SketcherMgr::onBeforeContextMenu()
670 {
671   myIsPopupMenuActive = true;
672 }
673
674 void PartSet_SketcherMgr::onAfterContextMenu()
675 {
676   myIsPopupMenuActive = false;
677 }
678
679 void PartSet_SketcherMgr::get2dPoint(ModuleBase_IViewWindow* theWnd, QMouseEvent* theEvent, 
680                                      Point& thePoint)
681 {
682   Handle(V3d_View) aView = theWnd->v3dView();
683   gp_Pnt aPoint = PartSet_Tools::convertClickToPoint(theEvent->pos(), aView);
684   double aX, anY;
685   PartSet_Tools::convertTo2D(aPoint, myCurrentSketch, aView, aX, anY);
686   thePoint.setValue(aX, anY);
687 }
688
689 void PartSet_SketcherMgr::launchEditing()
690 {
691   if (!myCurrentSelection.empty()) {
692     FeaturePtr aFeature = myCurrentSelection.begin().key();
693     std::shared_ptr<SketchPlugin_Feature> aSPFeature = 
694               std::dynamic_pointer_cast<SketchPlugin_Feature>(aFeature);
695     if (aSPFeature) {
696       myModule->editFeature(aSPFeature);
697     }
698   }
699 }
700
701 bool PartSet_SketcherMgr::sketchSolverError()
702 {
703   bool anError = false;
704   CompositeFeaturePtr aSketch = activeSketch();
705   if (aSketch.get()) {
706     AttributeStringPtr aAttributeString = aSketch->string(SketchPlugin_Sketch::SOLVER_ERROR());
707     anError = !aAttributeString->value().empty();
708   }
709   return anError;
710 }
711
712 QString PartSet_SketcherMgr::getFeatureError(const FeaturePtr& theFeature)
713 {
714   QString anError = "";
715   if (!theFeature.get() || !theFeature->data()->isValid())
716     return anError;
717
718   CompositeFeaturePtr aSketch = activeSketch();
719   if (aSketch.get() && aSketch == theFeature) {
720     AttributeStringPtr aAttributeString = aSketch->string(SketchPlugin_Sketch::SOLVER_ERROR());
721     anError = aAttributeString->value().c_str();
722   }
723   return anError;
724 }
725
726 void PartSet_SketcherMgr::clearClickedFlags()
727 {
728   //myClickedPoint.clear();
729   myCurrentPoint.clear();
730 }
731
732 const QStringList& PartSet_SketcherMgr::sketchOperationIdList()
733 {
734   static QStringList aIds;
735   if (aIds.size() == 0) {
736     aIds << SketchPlugin_Line::ID().c_str();
737     aIds << SketchPlugin_Point::ID().c_str();
738     aIds << SketchPlugin_Arc::ID().c_str();
739     aIds << SketchPlugin_Circle::ID().c_str();
740     aIds << SketchPlugin_ConstraintFillet::ID().c_str();
741     aIds << SketchPlugin_IntersectionPoint::ID().c_str();
742     // TODO
743     // SketchRectangle is a python feature, so its ID is passed just as a string
744     aIds << "SketchRectangle";
745     aIds.append(constraintsIdList());
746   }
747   return aIds;
748 }
749
750 const QStringList& PartSet_SketcherMgr::constraintsIdList()
751 {
752   static QStringList aIds;
753   if (aIds.size() == 0) {
754     aIds << SketchPlugin_ConstraintLength::ID().c_str();
755     aIds << SketchPlugin_ConstraintDistance::ID().c_str();
756     aIds << SketchPlugin_ConstraintRigid::ID().c_str();
757     aIds << SketchPlugin_ConstraintRadius::ID().c_str();
758     aIds << SketchPlugin_ConstraintPerpendicular::ID().c_str();
759     aIds << SketchPlugin_ConstraintParallel::ID().c_str();
760     aIds << SketchPlugin_ConstraintHorizontal::ID().c_str();
761     aIds << SketchPlugin_ConstraintVertical::ID().c_str();
762     aIds << SketchPlugin_ConstraintEqual::ID().c_str();
763     aIds << SketchPlugin_ConstraintTangent::ID().c_str();
764     aIds << SketchPlugin_ConstraintCoincidence::ID().c_str();
765     aIds << SketchPlugin_ConstraintMirror::ID().c_str();
766     aIds << SketchPlugin_ConstraintAngle::ID().c_str();
767     aIds << SketchPlugin_MultiRotation::ID().c_str();
768     aIds << SketchPlugin_MultiTranslation::ID().c_str();
769     aIds << SketchPlugin_ConstraintCollinear::ID().c_str();
770     aIds << SketchPlugin_ConstraintMiddle::ID().c_str();
771   }
772   return aIds;
773 }
774
775 void PartSet_SketcherMgr::sketchSelectionModes(QIntList& theModes)
776 {
777   theModes.clear();
778
779   theModes.append(SketcherPrs_Tools::Sel_Dimension_Text);
780   theModes.append(SketcherPrs_Tools::Sel_Dimension_Line);
781   theModes.append(SketcherPrs_Tools::Sel_Constraint);
782   theModes.append(TopAbs_VERTEX);
783   theModes.append(TopAbs_EDGE);
784 }
785
786 bool PartSet_SketcherMgr::isSketchOperation(ModuleBase_Operation* theOperation)
787 {
788   return theOperation && theOperation->id().toStdString() == SketchPlugin_Sketch::ID();
789 }
790
791 bool PartSet_SketcherMgr::isNestedSketchOperation(ModuleBase_Operation* theOperation)
792 {
793   return theOperation &&
794          PartSet_SketcherMgr::sketchOperationIdList().contains(theOperation->id());
795 }
796
797 bool PartSet_SketcherMgr::isNestedCreateOperation(ModuleBase_Operation* theOperation)
798 {
799   ModuleBase_OperationFeature* aFOperation = dynamic_cast<ModuleBase_OperationFeature*>
800                                                                (theOperation);
801   return aFOperation && !aFOperation->isEditOperation() && isNestedSketchOperation(aFOperation);
802 }
803
804 bool PartSet_SketcherMgr::isNestedEditOperation(ModuleBase_Operation* theOperation)
805 {
806   ModuleBase_OperationFeature* aFOperation = dynamic_cast<ModuleBase_OperationFeature*>
807                                                                (theOperation);
808   return aFOperation && aFOperation->isEditOperation() && isNestedSketchOperation(aFOperation);
809 }
810
811 bool PartSet_SketcherMgr::isEntity(const std::string& theId)
812 {
813   return (theId == SketchPlugin_Line::ID()) ||
814          (theId == SketchPlugin_Point::ID()) ||
815          (theId == SketchPlugin_Arc::ID()) ||
816          (theId == SketchPlugin_Circle::ID());
817 }
818
819 bool PartSet_SketcherMgr::isDistanceOperation(ModuleBase_Operation* theOperation)
820 {
821   std::string anId = theOperation ? theOperation->id().toStdString() : "";
822
823   return isDistanceKind(anId);
824 }
825
826 bool PartSet_SketcherMgr::isDistanceKind(std::string& theKind)
827 {
828   return (theKind == SketchPlugin_ConstraintLength::ID()) ||
829          (theKind == SketchPlugin_ConstraintDistance::ID()) ||
830          (theKind == SketchPlugin_ConstraintRadius::ID()) ||
831          (theKind == SketchPlugin_ConstraintAngle::ID());
832 }
833
834 void PartSet_SketcherMgr::startSketch(ModuleBase_Operation* theOperation)
835 {
836   ModuleBase_OperationFeature* aFOperation = dynamic_cast<ModuleBase_OperationFeature*>
837                                                                (getCurrentOperation());
838   if (!aFOperation)
839     return;
840
841   myModule->onViewTransformed();
842
843   // Display all sketcher sub-Objects
844   myCurrentSketch = std::dynamic_pointer_cast<ModelAPI_CompositeFeature>(aFOperation->feature());
845   XGUI_ModuleConnector* aConnector = dynamic_cast<XGUI_ModuleConnector*>(myModule->workshop());
846   aConnector->workshop()->displayer()->activateTrihedron(true);
847
848   // Hide sketcher result
849   std::list<ResultPtr> aResults = myCurrentSketch->results();
850   std::list<ResultPtr>::const_iterator aIt;
851   for (aIt = aResults.begin(); aIt != aResults.end(); ++aIt) {
852     (*aIt)->setDisplayed(false);
853   }
854   myCurrentSketch->setDisplayed(false);
855
856   // Display sketcher objects
857   for (int i = 0; i < myCurrentSketch->numberOfSubs(); i++) {
858     FeaturePtr aFeature = myCurrentSketch->subFeature(i);
859     std::list<ResultPtr> aResults = aFeature->results();
860     std::list<ResultPtr>::const_iterator aIt;
861     for (aIt = aResults.begin(); aIt != aResults.end(); ++aIt) {
862       (*aIt)->setDisplayed(true);
863     }
864     aFeature->setDisplayed(true);
865   }
866
867   if (myPlaneFilter.IsNull()) 
868     myPlaneFilter = new ModuleBase_ShapeInPlaneFilter();
869
870   myModule->workshop()->viewer()->addSelectionFilter(myPlaneFilter);
871   bool aHasPlane = false;
872   std::shared_ptr<GeomAPI_Pln> aPln;
873   if (aFOperation->isEditOperation()) {
874     // If it is editing of sketch then it means that plane is already defined
875     aPln = PartSet_Tools::sketchPlane(myCurrentSketch);
876     if (aPln.get())
877       aHasPlane = true;
878   }
879   myPlaneFilter->setPlane(aPln);
880
881   Events_Loop::loop()->flush(Events_Loop::eventByName(EVENT_OBJECT_TO_REDISPLAY));
882   // all sketch objects should be activated in the sketch selection modes by edit operation start
883   // in case of creation operation, there is an active widget, which activates own selection mode
884   if (aFOperation->isEditOperation() && aHasPlane)
885     aConnector->activateModuleSelectionModes();
886 }
887
888 void PartSet_SketcherMgr::stopSketch(ModuleBase_Operation* theOperation)
889 {
890   myIsMouseOverWindow = false;
891   myIsConstraintsShown[PartSet_Tools::Geometrical] = true;
892   myIsConstraintsShown[PartSet_Tools::Dimensional] = true;
893
894   XGUI_ModuleConnector* aConnector = dynamic_cast<XGUI_ModuleConnector*>(myModule->workshop());
895
896   DataPtr aData = myCurrentSketch->data();
897   if (!aData->isValid()) {
898     XGUI_Displayer* aDisplayer = aConnector->workshop()->displayer();
899     // The sketch was aborted
900     myCurrentSketch = CompositeFeaturePtr();
901     myModule->workshop()->viewer()->removeSelectionFilter(myPlaneFilter);
902
903     // Erase all sketcher objects
904     QStringList aSketchIds = sketchOperationIdList();
905     QObjectPtrList aObjects = aDisplayer->displayedObjects();
906     foreach (ObjectPtr aObj, aObjects) {
907       DataPtr aObjData = aObj->data();
908       if (!aObjData->isValid())
909         aObj->setDisplayed(false);
910     }
911   }
912   else {
913     // Hide all sketcher sub-Objects
914     for (int i = 0; i < myCurrentSketch->numberOfSubs(); i++) {
915       FeaturePtr aFeature = myCurrentSketch->subFeature(i);
916       std::list<ResultPtr> aResults = aFeature->results();
917       std::list<ResultPtr>::const_iterator aIt;
918       for (aIt = aResults.begin(); aIt != aResults.end(); ++aIt) {
919         (*aIt)->setDisplayed(false);
920       }
921       aFeature->setDisplayed(false);
922     }
923     // Display sketcher result
924     std::list<ResultPtr> aResults = myCurrentSketch->results();
925     std::list<ResultPtr>::const_iterator aIt;
926     Events_Loop* aLoop = Events_Loop::loop();
927     static Events_ID aDispEvent = aLoop->eventByName(EVENT_OBJECT_TO_REDISPLAY);
928
929     ModuleBase_OperationFeature* aFOperation = dynamic_cast<ModuleBase_OperationFeature*>
930                                                                            (theOperation);
931     for (aIt = aResults.begin(); aIt != aResults.end(); ++aIt) {
932       if (!aFOperation->isDisplayedOnStart(*aIt)) {
933         (*aIt)->setDisplayed(true);
934         // this display event is needed because sketch already may have "displayed" state,
935         // but not displayed while it is still active (issue 613, abort of existing sketch)
936         ModelAPI_EventCreator::get()->sendUpdated(*aIt, aDispEvent);
937       }
938     }
939     if (!aFOperation->isDisplayedOnStart(myCurrentSketch))
940       myCurrentSketch->setDisplayed(true);
941     
942     myCurrentSketch = CompositeFeaturePtr();
943     myModule->workshop()->viewer()->removeSelectionFilter(myPlaneFilter);
944
945     Events_Loop::loop()->flush(aDispEvent);
946   }
947   // restore the module selection modes, which were changed on startSketch
948   aConnector->activateModuleSelectionModes();
949   aConnector->workshop()->displayer()->activateTrihedron(false);
950 }
951
952 void PartSet_SketcherMgr::startNestedSketch(ModuleBase_Operation* theOperation)
953 {
954   if (canChangeCursor(theOperation) && myIsMouseOverWindow) {
955     QCursor* aCurrentCursor = QApplication::overrideCursor();
956     if (!aCurrentCursor || aCurrentCursor->shape() != Qt::CrossCursor) {
957       QApplication::setOverrideCursor(QCursor(Qt::CrossCursor));
958 #ifdef DEBUG_CURSOR
959       qDebug("startNestedSketch() : Qt::CrossCursor");
960 #endif
961     }
962   }
963 }
964
965 void PartSet_SketcherMgr::stopNestedSketch(ModuleBase_Operation* theOperation)
966 {
967   myIsMouseOverViewProcessed = true;
968   operationMgr()->onValidateOperation();
969   if (canChangeCursor(theOperation)) {
970     QApplication::restoreOverrideCursor();
971 #ifdef DEBUG_CURSOR
972     qDebug("stopNestedSketch() : None");
973 #endif
974   }
975 }
976
977 void PartSet_SketcherMgr::commitNestedSketch(ModuleBase_Operation* theOperation)
978 {
979   if (isNestedCreateOperation(theOperation)) {
980     ModuleBase_OperationFeature* aFOperation = dynamic_cast<ModuleBase_OperationFeature*>
981                                                                              (theOperation);
982     if (aFOperation) {
983       FeaturePtr aFeature = aFOperation->feature();
984       // it is necessary to check the the feature data validity because
985       // some kind of features are removed by an operation commit(the macro state of a feature)
986       if (aFeature.get() && aFeature->data()->isValid()) {
987         visualizeFeature(aFeature, aFOperation->isEditOperation(), true);
988       }
989     }
990   }
991 }
992
993 void PartSet_SketcherMgr::activatePlaneFilter(const bool& toActivate)
994 {
995   if (toActivate)
996     myModule->workshop()->viewer()->addSelectionFilter(myPlaneFilter);
997   else
998     myModule->workshop()->viewer()->removeSelectionFilter(myPlaneFilter);
999 }
1000
1001 void PartSet_SketcherMgr::operationActivatedByPreselection()
1002 {
1003   ModuleBase_Operation* anOperation = getCurrentOperation();
1004   if(anOperation && PartSet_SketcherMgr::isNestedSketchOperation(anOperation)) {
1005     // Set final definitions if they are necessary
1006     //propertyPanelDefined(aOperation);
1007     /// Commit sketcher operations automatically
1008     ModuleBase_OperationFeature* aFOperation = dynamic_cast<ModuleBase_OperationFeature*>
1009                                                                             (anOperation);
1010     if (aFOperation) {
1011       if (PartSet_SketcherMgr::isDistanceOperation(aFOperation)) {
1012         FeaturePtr aFeature = aFOperation->feature();
1013         // editor is shown only if all attribute references are filled by preseletion
1014         bool anAllRefAttrInitialized = true;
1015
1016         std::list<AttributePtr> aRefAttrs = aFeature->data()->attributes(
1017                                                     ModelAPI_AttributeRefAttr::typeId());
1018         std::list<AttributePtr>::const_iterator anIt = aRefAttrs.begin(), aLast = aRefAttrs.end();
1019         for (; anIt != aLast && anAllRefAttrInitialized; anIt++) {
1020           anAllRefAttrInitialized = (*anIt)->isInitialized();
1021         }
1022         if (anAllRefAttrInitialized) {
1023           // Activate dimension value editing on double click
1024           ModuleBase_IPropertyPanel* aPanel = aFOperation->propertyPanel();
1025           QList<ModuleBase_ModelWidget*> aWidgets = aPanel->modelWidgets();
1026           // Find corresponded widget to activate value editing
1027           foreach (ModuleBase_ModelWidget* aWgt, aWidgets) {
1028             if (aWgt->attributeID() == "ConstraintValue") {
1029               // the featue should be displayed in order to find the AIS text position,
1030               // the place where the editor will be shown
1031               aFeature->setDisplayed(true);
1032               /// the execute is necessary to perform in the feature compute for flyout position
1033               aFeature->execute();
1034
1035               Events_Loop::loop()->flush(Events_Loop::eventByName(EVENT_OBJECT_CREATED));
1036               Events_Loop::loop()->flush(Events_Loop::eventByName(EVENT_OBJECT_TO_REDISPLAY));
1037
1038               PartSet_WidgetEditor* anEditor = dynamic_cast<PartSet_WidgetEditor*>(aWgt);
1039               if (anEditor) {
1040                 int aX = 0, anY = 0;
1041
1042                 ModuleBase_IWorkshop* aWorkshop = myModule->workshop();
1043                 XGUI_ModuleConnector* aConnector = dynamic_cast<XGUI_ModuleConnector*>(aWorkshop);
1044                 XGUI_Displayer* aDisplayer = aConnector->workshop()->displayer();
1045                 AISObjectPtr anAIS = aDisplayer->getAISObject(aFeature);
1046                 Handle(AIS_InteractiveObject) anAISIO;
1047                 if (anAIS.get() != NULL) {
1048                   anAISIO = anAIS->impl<Handle(AIS_InteractiveObject)>();
1049                 }
1050                 if (anAIS.get() != NULL) {
1051                   Handle(AIS_InteractiveObject) anAISIO = anAIS->impl<Handle(AIS_InteractiveObject)>();
1052
1053                   if (!anAISIO.IsNull()) {
1054                     Handle(AIS_Dimension) aDim = Handle(AIS_Dimension)::DownCast(anAISIO);
1055                     if (!aDim.IsNull()) {
1056                       gp_Pnt aPosition = aDim->GetTextPosition();
1057
1058                       ModuleBase_IViewer* aViewer = aWorkshop->viewer();
1059                       Handle(V3d_View) aView = aViewer->activeView();
1060                       int aCX, aCY;
1061                       aView->Convert(aPosition.X(), aPosition.Y(), aPosition.Z(), aCX, aCY);
1062
1063                       QWidget* aViewPort = aViewer->activeViewPort();
1064                       QPoint aGlPoint = aViewPort->mapToGlobal(QPoint(aCX, aCY));
1065                       aX = aGlPoint.x();
1066                       anY = aGlPoint.y();
1067                     }
1068                   }
1069                   anEditor->setCursorPosition(aX, anY);
1070                   anEditor->showPopupEditor(false);
1071                 }
1072               }
1073             }
1074           }
1075         }
1076       }
1077     }
1078     anOperation->commit();
1079   }
1080 }
1081
1082 bool PartSet_SketcherMgr::canUndo() const
1083 {
1084   return isNestedCreateOperation(getCurrentOperation());
1085 }
1086
1087 bool PartSet_SketcherMgr::canRedo() const
1088 {
1089   return isNestedCreateOperation(getCurrentOperation());
1090 }
1091
1092 bool PartSet_SketcherMgr::canEraseObject(const ObjectPtr& theObject) const
1093 {
1094   bool aCanErase = true;
1095   // when the sketch operation is active, results of sketch sub-feature can not be hidden
1096   if (myCurrentSketch.get()) {
1097     ResultPtr aResult = std::dynamic_pointer_cast<ModelAPI_Result>(theObject);
1098     if (aResult.get()) {
1099       // Display sketcher objects
1100       for (int i = 0; i < myCurrentSketch->numberOfSubs() && aCanErase; i++) {
1101
1102         FeaturePtr aFeature = myCurrentSketch->subFeature(i);
1103         std::list<ResultPtr> aResults = aFeature->results();
1104         std::list<ResultPtr>::const_iterator anIt;
1105         for (anIt = aResults.begin(); anIt != aResults.end() && aCanErase; ++anIt) {
1106           aCanErase = *anIt != aResult;
1107         }
1108       }
1109     }
1110   }
1111   return aCanErase;
1112 }
1113
1114 bool PartSet_SketcherMgr::canDisplayObject(const ObjectPtr& theObject) const
1115 {
1116   bool aCanDisplay = true;
1117
1118   bool aHasActiveSketch = activeSketch().get() != NULL;
1119   if (aHasActiveSketch) {
1120     // 1. the sketch feature should not be displayed during the sketch active operation
1121     // it is hidden by a sketch operation start and shown by a sketch stop, just the sketch 
1122     // nested features can be visualized
1123     FeaturePtr aFeature = ModelAPI_Feature::feature(theObject);
1124     if (aFeature.get() != NULL && aFeature == activeSketch()) {
1125       aCanDisplay = false;
1126     }
1127   }
1128   else { // there are no an active sketch
1129     // 2. sketch sub-features should not be visualized if the sketch operation is not active
1130     FeaturePtr aFeature = ModelAPI_Feature::feature(theObject);
1131     if (aFeature.get() != NULL) {
1132       std::shared_ptr<SketchPlugin_Feature> aSketchFeature =
1133                               std::dynamic_pointer_cast<SketchPlugin_Feature>(aFeature);
1134       if (aSketchFeature.get()) {
1135         aCanDisplay = false;
1136       }
1137     }
1138   }
1139
1140   // 3. the method should not filter the objects, which are not related to the current operation.
1141   // The object is filtered just if it is a current operation feature or this feature result
1142   bool isObjectFound = false;
1143   ModuleBase_OperationFeature* aFOperation = dynamic_cast<ModuleBase_OperationFeature*>
1144                                                                (getCurrentOperation());
1145   if (aFOperation) {
1146     FeaturePtr aFeature = aFOperation->feature();
1147     if (aFeature.get()) {
1148       std::list<ResultPtr> aResults = aFeature->results();
1149       if (theObject == aFeature)
1150         isObjectFound = true;
1151       else {
1152         std::list<ResultPtr>::const_iterator anIt = aResults.begin(), aLast = aResults.end();
1153         for (; anIt != aLast && !isObjectFound; anIt++) {
1154           isObjectFound = *anIt == theObject;
1155         }
1156       }
1157     }
1158   }
1159   if (isObjectFound) {
1160     // 4. For created nested feature operation do not display the created feature if
1161     // the mouse curstor leaves the OCC window.
1162     // The correction cases, which ignores this condition:
1163     // a. the property panel values modification
1164     // b. the popup menu activated
1165     // c. widget editor control
1166     #ifndef DEBUG_DO_NOT_BY_ENTER
1167     if (aCanDisplay && isNestedCreateOperation(getCurrentOperation())) {
1168       ModuleBase_ModelWidget* anActiveWidget = getActiveWidget();
1169       ModuleBase_WidgetEditor* anEditorWdg = anActiveWidget ? dynamic_cast<ModuleBase_WidgetEditor*>(anActiveWidget) : 0;
1170       // the active widget editor should not influence here. The presentation should be visible always
1171       // when this widget is active.
1172       if (!anEditorWdg && !myIsPopupMenuActive) {
1173         // during a nested create operation, the feature is redisplayed only if the mouse over view
1174         // of there was a value modified in the property panel after the mouse left the view
1175         aCanDisplay = canDisplayCurrentCreatedFeature();
1176       }
1177     }
1178     #endif
1179   }
1180
1181   // checks the sketcher constraints visibility according to active sketch check box states
1182   if (aCanDisplay) {
1183     bool aProcessed = false;
1184     FeaturePtr aFeature = ModelAPI_Feature::feature(theObject);
1185     if (aFeature.get()) {
1186       bool aConstraintDisplayed = canDisplayConstraint(aFeature, PartSet_Tools::Any, aProcessed);
1187       if (aProcessed)
1188         aCanDisplay = aConstraintDisplayed;
1189     }
1190   }
1191
1192   return aCanDisplay;
1193 }
1194
1195 bool PartSet_SketcherMgr::canDisplayConstraint(const FeaturePtr& theFeature,
1196                                              const PartSet_Tools::ConstraintVisibleState& theState,
1197                                              bool& isProcessed) const
1198 {
1199   bool aSwitchedOn = true;
1200
1201   const QStringList& aConstrIds = constraintsIdList();
1202
1203   std::string aKind = theFeature->getKind();
1204   if (aConstrIds.contains(QString(aKind.c_str()))) {
1205     bool isTypedConstraint = false;
1206
1207     switch (theState) {
1208       case PartSet_Tools::Dimensional: {
1209         bool isDistance = isDistanceKind(aKind);
1210         if (isDistance) {
1211           isProcessed = true;
1212           aSwitchedOn = myIsConstraintsShown[theState];
1213         }
1214       }
1215       break;
1216       case PartSet_Tools::Geometrical: {
1217         bool isGeometrical = !isDistanceKind(aKind);
1218         if (isGeometrical) {
1219           isProcessed = true;
1220           aSwitchedOn = myIsConstraintsShown[theState];
1221         }
1222       }
1223       break;
1224       case PartSet_Tools::Any: {
1225         isProcessed = true;
1226         bool isDistance = isDistanceKind(aKind);
1227         if (isDistance)
1228           aSwitchedOn = myIsConstraintsShown[PartSet_Tools::Dimensional];
1229         else
1230           aSwitchedOn = myIsConstraintsShown[PartSet_Tools::Geometrical];
1231       }
1232       break;
1233     default:
1234       break;
1235     }
1236   }
1237   return aSwitchedOn;
1238 }
1239
1240 void PartSet_SketcherMgr::processHiddenObject(const std::list<ObjectPtr>& theObjects)
1241 {
1242   ModuleBase_OperationFeature* aFOperation = dynamic_cast<ModuleBase_OperationFeature*>
1243                                                                            (getCurrentOperation());
1244   if (aFOperation && myCurrentSketch.get()) {
1245     // find results of the current operation
1246     // these results should not be proposed to be deleted
1247     FeaturePtr anOperationFeature = aFOperation->feature();
1248     std::list<ResultPtr> anOperationResultList = anOperationFeature->results();
1249     std::set<ResultPtr> anOperationResults;
1250     std::list<ResultPtr>::const_iterator aRIt = anOperationResultList.begin(),
1251                                         aRLast = anOperationResultList.end();
1252     for (; aRIt != aRLast; aRIt++)
1253       anOperationResults.insert(*aRIt);
1254
1255     std::set<FeaturePtr> anObjectsToBeDeleted;
1256     QStringList anObjectsToBeDeletedNames;
1257     std::list<ObjectPtr>::const_iterator anIt = theObjects.begin(), aLast = theObjects.end();
1258     for (; anIt != aLast; anIt++) {
1259       ObjectPtr anObject = *anIt;
1260       bool aCanErase = true;
1261       // when the sketch operation is active, results of sketch sub-feature can not be hidden
1262       ResultPtr aResult = std::dynamic_pointer_cast<ModelAPI_Result>(anObject);
1263       // the result is found between current feature results
1264       if (anOperationResults.find(aResult) != anOperationResults.end())
1265         continue;
1266
1267       if (aResult.get()) {
1268         // Display sketcher objects
1269         for (int i = 0; i < myCurrentSketch->numberOfSubs() && aCanErase; i++) {
1270           FeaturePtr aFeature = myCurrentSketch->subFeature(i);
1271           std::list<ResultPtr> aResults = aFeature->results();
1272           std::list<ResultPtr>::const_iterator anIt;
1273           for (anIt = aResults.begin(); anIt != aResults.end() && aCanErase; ++anIt) {
1274             aCanErase = *anIt != aResult;
1275           }
1276         }
1277       }
1278       if (!aCanErase) {
1279         FeaturePtr aFeature = ModelAPI_Feature::feature(anObject);
1280         if (aFeature.get() && anObjectsToBeDeleted.find(aFeature) == anObjectsToBeDeleted.end()) {
1281           anObjectsToBeDeleted.insert(aFeature);
1282           anObjectsToBeDeletedNames.append(aFeature->name().c_str());
1283         }
1284       }
1285     }
1286     if (!anObjectsToBeDeleted.empty()) {
1287       QString aFeatureNames = anObjectsToBeDeletedNames.join(", ");
1288       QString aMessage = tr("The following features have incorrect presentation and \
1289 will be hidden: %1. Would you like to delete them?")
1290                          .arg(aFeatureNames);
1291       int anAnswer = QMessageBox::question(qApp->activeWindow(), tr("Features hide"),
1292                                            aMessage, QMessageBox::Ok | QMessageBox::Cancel,
1293                                            QMessageBox::Cancel);
1294       if (anAnswer == QMessageBox::Ok) {
1295         QObjectPtrList anObjects;
1296         std::set<FeaturePtr>::const_iterator anIt = anObjectsToBeDeleted.begin(),
1297                                              aLast = anObjectsToBeDeleted.end();
1298         for (; anIt != aLast; anIt++)
1299           anObjects.append(*anIt);
1300         SessionPtr aMgr = ModelAPI_Session::get();
1301         DocumentPtr aDoc = aMgr->activeDocument();
1302         bool aIsOp = aMgr->isOperation();
1303         if (!aIsOp)
1304           aMgr->startOperation();
1305         workshop()->deleteFeatures(anObjects);
1306         //static Events_ID aDeletedEvent = Events_Loop::eventByName(EVENT_OBJECT_DELETED);
1307         //static Events_ID aRedispEvent = Events_Loop::eventByName(EVENT_OBJECT_TO_REDISPLAY);
1308         //Events_Loop::loop()->flush(aDeletedEvent);
1309         //Events_Loop::loop()->flush(aRedispEvent);
1310
1311         if (!aIsOp)
1312           aMgr->finishOperation();
1313       }
1314     }
1315   }
1316 }
1317
1318 bool PartSet_SketcherMgr::canDisplayCurrentCreatedFeature() const
1319 {
1320   bool aCanDisplay = myIsMouseOverWindow;
1321   if (!aCanDisplay) {
1322     ModuleBase_ModelWidget* anActiveWidget = getActiveWidget();
1323     if (anActiveWidget)
1324       aCanDisplay = anActiveWidget->getValueState() == ModuleBase_ModelWidget::Stored;
1325   }
1326   return aCanDisplay;
1327 }
1328
1329 bool PartSet_SketcherMgr::canChangeCursor(ModuleBase_Operation* theOperation) const
1330 {
1331   return isNestedCreateOperation(theOperation) ||
1332          myModule->sketchReentranceMgr()->isInternalEditActive();
1333 }
1334
1335 const QMap<PartSet_Tools::ConstraintVisibleState, bool>& PartSet_SketcherMgr::showConstraintStates()
1336 {
1337   return myIsConstraintsShown;
1338 }
1339
1340 bool PartSet_SketcherMgr::isObjectOfSketch(const ObjectPtr& theObject) const
1341 {
1342   bool isFoundObject = false;
1343
1344   FeaturePtr anObjectFeature = ModelAPI_Feature::feature(theObject);
1345   if (anObjectFeature.get()) {
1346     int aSize = myCurrentSketch->numberOfSubs();
1347     for (int i = 0; i < myCurrentSketch->numberOfSubs() && !isFoundObject; i++) {
1348       FeaturePtr aCurrentFeature = myCurrentSketch->subFeature(i);
1349       isFoundObject = myCurrentSketch->subFeature(i) == anObjectFeature;
1350     }
1351   }
1352   return isFoundObject;
1353 }
1354
1355 void PartSet_SketcherMgr::onPlaneSelected(const std::shared_ptr<GeomAPI_Pln>& thePln)
1356 {
1357   if (myPlaneFilter.IsNull()) 
1358    myPlaneFilter = new ModuleBase_ShapeInPlaneFilter();
1359
1360   myPlaneFilter->setPlane(thePln);
1361 }
1362
1363 void PartSet_SketcherMgr::getCurrentSelection(const FeaturePtr& theFeature,
1364                                               const FeaturePtr& theSketch,
1365                                               ModuleBase_IWorkshop* theWorkshop,
1366                                               FeatureToSelectionMap& theSelection)
1367 {
1368   if (theFeature.get() == NULL)
1369     return;
1370
1371   std::set<AttributePtr> aSelectedAttributes;
1372   std::set<ResultPtr> aSelectedResults;
1373
1374   ModuleBase_IViewer* aViewer = theWorkshop->viewer();
1375   Handle(AIS_InteractiveContext) aContext = aViewer->AISContext();
1376   if (!aContext.IsNull()) {
1377     XGUI_ModuleConnector* aConnector = dynamic_cast<XGUI_ModuleConnector*>(theWorkshop);
1378     XGUI_Displayer* aDisplayer = aConnector->workshop()->displayer();
1379
1380     std::list<ResultPtr> aResults = theFeature->results();
1381     std::list<ResultPtr>::const_iterator aIt;
1382     for (aIt = aResults.begin(); aIt != aResults.end(); ++aIt)
1383     {
1384       ResultPtr aResult = *aIt;
1385       AISObjectPtr aAISObj = aDisplayer->getAISObject(aResult);
1386       if (aAISObj.get() == NULL)
1387         continue;
1388       Handle(AIS_InteractiveObject) anAISIO = aAISObj->impl<Handle(AIS_InteractiveObject)>();
1389       for (aContext->InitSelected(); aContext->MoreSelected(); aContext->NextSelected())
1390       {
1391         Handle(SelectMgr_EntityOwner) anOwner = aContext->SelectedOwner();
1392         if (anOwner->Selectable() != anAISIO)
1393           continue;
1394         getAttributesOrResults(anOwner, theFeature, theSketch, aResult,
1395                                aSelectedAttributes, aSelectedResults);
1396       }
1397       for (aContext->InitDetected(); aContext->MoreDetected(); aContext->NextDetected()) {
1398         Handle(SelectMgr_EntityOwner) anOwner = aContext->DetectedOwner();
1399         if (anOwner.IsNull())
1400           continue;
1401         if (anOwner->Selectable() != anAISIO)
1402           continue;
1403         getAttributesOrResults(anOwner, theFeature, theSketch, aResult,
1404                                aSelectedAttributes, aSelectedResults);
1405       }
1406     }
1407   }
1408   theSelection[theFeature] = std::make_pair(aSelectedAttributes, aSelectedResults);
1409 }
1410
1411 void PartSet_SketcherMgr::getSelectionOwners(const FeaturePtr& theFeature,
1412                                              const FeaturePtr& theSketch,
1413                                              ModuleBase_IWorkshop* theWorkshop,
1414                                              const FeatureToSelectionMap& theSelection,
1415                                              SelectMgr_IndexedMapOfOwner& theOwnersToSelect)
1416 {
1417   if (theFeature.get() == NULL)
1418     return;
1419
1420   FeatureToSelectionMap::const_iterator anIt = theSelection.find(theFeature);
1421   std::set<AttributePtr> aSelectedAttributes = anIt.value().first;
1422   std::set<ResultPtr> aSelectedResults = anIt.value().second;
1423
1424   ModuleBase_IViewer* aViewer = theWorkshop->viewer();
1425
1426   XGUI_ModuleConnector* aConnector = dynamic_cast<XGUI_ModuleConnector*>(theWorkshop);
1427   XGUI_Displayer* aDisplayer = aConnector->workshop()->displayer();
1428
1429   // 1. found the feature's owners. Check the AIS objects of the constructions
1430   AISObjectPtr aAISObj = aDisplayer->getAISObject(theFeature);
1431   if (aAISObj.get() != NULL && aSelectedAttributes.empty() && aSelectedResults.empty()) {
1432     Handle(AIS_InteractiveObject) anAISIO = aAISObj->impl<Handle(AIS_InteractiveObject)>();
1433
1434     SelectMgr_IndexedMapOfOwner aSelectedOwners;
1435     aConnector->workshop()->selector()->selection()->entityOwners(anAISIO, aSelectedOwners);
1436     for  (Standard_Integer i = 1, n = aSelectedOwners.Extent(); i <= n; i++) {
1437       Handle(SelectMgr_EntityOwner) anOwner = aSelectedOwners(i);
1438       if (!anOwner.IsNull())
1439         theOwnersToSelect.Add(anOwner);
1440     }
1441   }
1442
1443   // 2. found the feature results's owners
1444   std::list<ResultPtr> aResults = theFeature->results();
1445   std::list<ResultPtr>::const_iterator aIt;
1446   for (aIt = aResults.begin(); aIt != aResults.end(); ++aIt)
1447   {
1448     ResultPtr aResult = *aIt;
1449     AISObjectPtr aAISObj = aDisplayer->getAISObject(aResult);
1450     if (aAISObj.get() == NULL)
1451       continue; 
1452     Handle(AIS_InteractiveObject) anAISIO = aAISObj->impl<Handle(AIS_InteractiveObject)>();
1453
1454     SelectMgr_IndexedMapOfOwner aSelectedOwners;  
1455     aConnector->workshop()->selector()->selection()->entityOwners(anAISIO, aSelectedOwners);
1456     for  ( Standard_Integer i = 1, n = aSelectedOwners.Extent(); i <= n; i++ ) {
1457       Handle(StdSelect_BRepOwner) anOwner = Handle(StdSelect_BRepOwner)::DownCast(aSelectedOwners(i));
1458       if ( anOwner.IsNull() || !anOwner->HasShape() )
1459         continue;
1460       const TopoDS_Shape& aShape = anOwner->Shape();
1461       TopAbs_ShapeEnum aShapeType = aShape.ShapeType();
1462       if (aShapeType == TopAbs_VERTEX) {
1463         AttributePtr aPntAttr = PartSet_Tools::findAttributeBy2dPoint(theFeature, aShape, theSketch);
1464         if (aPntAttr.get() != NULL &&
1465             aSelectedAttributes.find(aPntAttr) != aSelectedAttributes.end()) {
1466           theOwnersToSelect.Add(anOwner);
1467         }
1468       }
1469       else if (aShapeType == TopAbs_EDGE) {
1470         bool aFound = aSelectedResults.find(aResult) != aSelectedResults.end();
1471         if (aSelectedResults.find(aResult) != aSelectedResults.end() &&
1472             theOwnersToSelect.FindIndex(anOwner) <= 0)
1473           theOwnersToSelect.Add(anOwner);
1474       }
1475     }
1476   }
1477 }
1478
1479 void PartSet_SketcherMgr::connectToPropertyPanel(ModuleBase_ModelWidget* theWidget, const bool isToConnect)
1480 {
1481   if (isToConnect) {
1482     connect(theWidget, SIGNAL(beforeValuesChanged()),
1483             this, SLOT(onBeforeValuesChangedInPropertyPanel()));
1484     connect(theWidget, SIGNAL(afterValuesChanged()),
1485             this, SLOT(onAfterValuesChangedInPropertyPanel()));
1486   }
1487   else {
1488     disconnect(theWidget, SIGNAL(beforeValuesChanged()),
1489                 this, SLOT(onBeforeValuesChangedInPropertyPanel()));
1490     disconnect(theWidget, SIGNAL(afterValuesChanged()),
1491                 this, SLOT(onAfterValuesChangedInPropertyPanel()));
1492   }
1493 }
1494
1495 void PartSet_SketcherMgr::widgetStateChanged(int thePreviousState)
1496 {
1497   ModuleBase_OperationFeature* aFOperation = dynamic_cast<ModuleBase_OperationFeature*>
1498                                                                            (getCurrentOperation());
1499   if (aFOperation) {
1500     if (PartSet_SketcherMgr::isSketchOperation(aFOperation) ||
1501         PartSet_SketcherMgr::isNestedSketchOperation(aFOperation) &&
1502         thePreviousState == ModuleBase_ModelWidget::ModifiedInPP) {
1503       FeaturePtr aFeature = aFOperation->feature();
1504       visualizeFeature(aFeature, aFOperation->isEditOperation(), canDisplayObject(aFeature));
1505     }
1506   }
1507 }
1508
1509 ModuleBase_Operation* PartSet_SketcherMgr::getCurrentOperation() const
1510 {
1511   return myModule->workshop()->currentOperation();
1512 }
1513
1514 //**************************************************************
1515 ModuleBase_ModelWidget* PartSet_SketcherMgr::getActiveWidget() const
1516 {
1517   ModuleBase_ModelWidget* aWidget = 0;
1518   ModuleBase_Operation* anOperation = getCurrentOperation();
1519   if (anOperation) {
1520     ModuleBase_IPropertyPanel* aPanel = anOperation->propertyPanel();
1521     if (aPanel)
1522       aWidget = aPanel->activeWidget();
1523   }
1524   return aWidget;
1525 }
1526
1527 void PartSet_SketcherMgr::visualizeFeature(const FeaturePtr& theFeature,
1528                                            const bool isEditOperation,
1529                                            const bool isToDisplay,
1530                                            const bool isFlushRedisplay)
1531 {
1532   #ifdef DEBUG_DO_NOT_BY_ENTER
1533   return;
1534   #endif
1535
1536   if (isEditOperation || !theFeature.get())
1537     return;
1538
1539   ModuleBase_IWorkshop* aWorkshop = myModule->workshop();
1540   XGUI_ModuleConnector* aConnector = dynamic_cast<XGUI_ModuleConnector*>(aWorkshop);
1541
1542   // 1. change visibility of the object itself, here the presentable object is processed,
1543   // e.g. constraints features
1544   //FeaturePtr aFeature = aFOperation->feature();
1545   std::list<ResultPtr> aResults = theFeature->results();
1546   if (isToDisplay)
1547     theFeature->setDisplayed(true);
1548   else
1549     theFeature->setDisplayed(false);
1550
1551   // change visibility of the object results, e.g. non-constraint features
1552   std::list<ResultPtr>::const_iterator aIt;
1553   for (aIt = aResults.begin(); aIt != aResults.end(); ++aIt) {
1554     if (isToDisplay) {
1555       (*aIt)->setDisplayed(true);
1556     }
1557     else {
1558       (*aIt)->setDisplayed(false);
1559     }
1560   }
1561   if (isFlushRedisplay)
1562     Events_Loop::loop()->flush(Events_Loop::eventByName(EVENT_OBJECT_TO_REDISPLAY));
1563 }
1564
1565 void PartSet_SketcherMgr::storeSelection(const bool theHighlightedOnly)
1566 {
1567   ModuleBase_IWorkshop* aWorkshop = myModule->workshop();
1568   ModuleBase_ISelection* aSelect = aWorkshop->selection();
1569   QList<ModuleBase_ViewerPrs> aHighlighted = aSelect->getHighlighted();
1570
1571   QList<FeaturePtr> aFeatureList;
1572   if (theHighlightedOnly) {
1573     fillFeatureList(aHighlighted, myCurrentSketch, aFeatureList);
1574   }
1575   else {
1576     fillFeatureList(aHighlighted, myCurrentSketch, aFeatureList);
1577
1578     QList<ModuleBase_ViewerPrs> aSelected = aSelect->getSelected(ModuleBase_ISelection::AllControls);
1579     fillFeatureList(aSelected, myCurrentSketch, aFeatureList);
1580   }
1581
1582   // 1. it is necessary to save current selection in order to restore it after the features moving
1583   myCurrentSelection.clear();
1584   QList<FeaturePtr>::const_iterator anIt = aFeatureList.begin(), aLast = aFeatureList.end();
1585   for (; anIt != aLast; anIt++) {
1586     getCurrentSelection(*anIt, myCurrentSketch, aWorkshop, myCurrentSelection);
1587   }
1588   //qDebug(QString("  storeSelection: %1").arg(myCurrentSelection.size()).toStdString().c_str());
1589 }
1590
1591 void PartSet_SketcherMgr::restoreSelection()
1592 {
1593   //qDebug(QString("restoreSelection: %1").arg(myCurrentSelection.size()).toStdString().c_str());
1594   ModuleBase_IWorkshop* aWorkshop = myModule->workshop();
1595   XGUI_ModuleConnector* aConnector = dynamic_cast<XGUI_ModuleConnector*>(aWorkshop);
1596   FeatureToSelectionMap::const_iterator aSIt = myCurrentSelection.begin(),
1597                                         aSLast = myCurrentSelection.end();
1598   SelectMgr_IndexedMapOfOwner anOwnersToSelect;
1599   for (; aSIt != aSLast; aSIt++) {
1600     anOwnersToSelect.Clear();
1601     getSelectionOwners(aSIt.key(), myCurrentSketch, aWorkshop, myCurrentSelection,
1602                         anOwnersToSelect);
1603     aConnector->workshop()->selector()->setSelectedOwners(anOwnersToSelect, false);
1604   }
1605 }
1606
1607 void PartSet_SketcherMgr::onShowConstraintsToggle(int theType, bool theState)
1608 {
1609   PartSet_Tools::ConstraintVisibleState aType = (PartSet_Tools::ConstraintVisibleState)theType;
1610   if (myIsConstraintsShown.contains(aType) && myIsConstraintsShown[aType] == theState)
1611     return;
1612   if (myCurrentSketch.get() == NULL)
1613     return;
1614
1615   myIsConstraintsShown[aType] = theState;
1616
1617   ModuleBase_IWorkshop* aWorkshop = myModule->workshop();
1618   XGUI_ModuleConnector* aConnector = dynamic_cast<XGUI_ModuleConnector*>(aWorkshop);
1619
1620   for (int i = 0; i < myCurrentSketch->numberOfSubs(); i++) {
1621     FeaturePtr aSubFeature = myCurrentSketch->subFeature(i);
1622     bool aProcessed = false;
1623     bool aConstraintDisplayed = canDisplayConstraint(aSubFeature, aType, aProcessed);
1624     if (aProcessed)
1625       aSubFeature->setDisplayed(aConstraintDisplayed);
1626   }
1627   Events_Loop::loop()->flush(Events_Loop::eventByName(EVENT_OBJECT_TO_REDISPLAY));
1628 }
1629
1630 XGUI_Workshop* PartSet_SketcherMgr::workshop() const
1631 {
1632   ModuleBase_IWorkshop* anIWorkshop = myModule->workshop();
1633   XGUI_ModuleConnector* aConnector = dynamic_cast<XGUI_ModuleConnector*>(anIWorkshop);
1634   return aConnector->workshop();
1635 }
1636
1637 XGUI_OperationMgr* PartSet_SketcherMgr::operationMgr() const
1638 {
1639   return workshop()->operationMgr();
1640 }
1641