Salome HOME
4c2b63a6f5f44305823c6c5518be0e9e4faaa240
[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(myCirclePointFilter.IsNull()) {
868     myCirclePointFilter = new PartSet_CirclePointFilter(myModule->workshop());
869   }
870
871   myModule->workshop()->viewer()->addSelectionFilter(myCirclePointFilter);
872
873   if (myPlaneFilter.IsNull()) 
874     myPlaneFilter = new ModuleBase_ShapeInPlaneFilter();
875
876   myModule->workshop()->viewer()->addSelectionFilter(myPlaneFilter);
877   bool aHasPlane = false;
878   std::shared_ptr<GeomAPI_Pln> aPln;
879   if (aFOperation->isEditOperation()) {
880     // If it is editing of sketch then it means that plane is already defined
881     aPln = PartSet_Tools::sketchPlane(myCurrentSketch);
882     if (aPln.get())
883       aHasPlane = true;
884   }
885   myPlaneFilter->setPlane(aPln);
886
887   Events_Loop::loop()->flush(Events_Loop::eventByName(EVENT_OBJECT_TO_REDISPLAY));
888   // all sketch objects should be activated in the sketch selection modes by edit operation start
889   // in case of creation operation, there is an active widget, which activates own selection mode
890   if (aFOperation->isEditOperation() && aHasPlane)
891     aConnector->activateModuleSelectionModes();
892 }
893
894 void PartSet_SketcherMgr::stopSketch(ModuleBase_Operation* theOperation)
895 {
896   myIsMouseOverWindow = false;
897   myIsConstraintsShown[PartSet_Tools::Geometrical] = true;
898   myIsConstraintsShown[PartSet_Tools::Dimensional] = true;
899
900   XGUI_ModuleConnector* aConnector = dynamic_cast<XGUI_ModuleConnector*>(myModule->workshop());
901
902   DataPtr aData = myCurrentSketch->data();
903   if (!aData->isValid()) {
904     XGUI_Displayer* aDisplayer = aConnector->workshop()->displayer();
905     // The sketch was aborted
906     myCurrentSketch = CompositeFeaturePtr();
907     // TODO: move this outside of if-else
908     myModule->workshop()->viewer()->removeSelectionFilter(myCirclePointFilter);
909     myModule->workshop()->viewer()->removeSelectionFilter(myPlaneFilter);
910
911     // Erase all sketcher objects
912     QStringList aSketchIds = sketchOperationIdList();
913     QObjectPtrList aObjects = aDisplayer->displayedObjects();
914     foreach (ObjectPtr aObj, aObjects) {
915       DataPtr aObjData = aObj->data();
916       if (!aObjData->isValid())
917         aObj->setDisplayed(false);
918     }
919   }
920   else {
921     // Hide all sketcher sub-Objects
922     for (int i = 0; i < myCurrentSketch->numberOfSubs(); i++) {
923       FeaturePtr aFeature = myCurrentSketch->subFeature(i);
924       std::list<ResultPtr> aResults = aFeature->results();
925       std::list<ResultPtr>::const_iterator aIt;
926       for (aIt = aResults.begin(); aIt != aResults.end(); ++aIt) {
927         (*aIt)->setDisplayed(false);
928       }
929       aFeature->setDisplayed(false);
930     }
931     // Display sketcher result
932     std::list<ResultPtr> aResults = myCurrentSketch->results();
933     std::list<ResultPtr>::const_iterator aIt;
934     Events_Loop* aLoop = Events_Loop::loop();
935     static Events_ID aDispEvent = aLoop->eventByName(EVENT_OBJECT_TO_REDISPLAY);
936
937     ModuleBase_OperationFeature* aFOperation = dynamic_cast<ModuleBase_OperationFeature*>
938                                                                            (theOperation);
939     for (aIt = aResults.begin(); aIt != aResults.end(); ++aIt) {
940       if (!aFOperation->isDisplayedOnStart(*aIt)) {
941         (*aIt)->setDisplayed(true);
942         // this display event is needed because sketch already may have "displayed" state,
943         // but not displayed while it is still active (issue 613, abort of existing sketch)
944         ModelAPI_EventCreator::get()->sendUpdated(*aIt, aDispEvent);
945       }
946     }
947     if (!aFOperation->isDisplayedOnStart(myCurrentSketch))
948       myCurrentSketch->setDisplayed(true);
949     
950     myCurrentSketch = CompositeFeaturePtr();
951
952     myModule->workshop()->viewer()->removeSelectionFilter(myCirclePointFilter);
953     myModule->workshop()->viewer()->removeSelectionFilter(myPlaneFilter);
954
955     Events_Loop::loop()->flush(aDispEvent);
956   }
957   // restore the module selection modes, which were changed on startSketch
958   aConnector->activateModuleSelectionModes();
959   aConnector->workshop()->displayer()->activateTrihedron(false);
960 }
961
962 void PartSet_SketcherMgr::startNestedSketch(ModuleBase_Operation* theOperation)
963 {
964   if (canChangeCursor(theOperation) && myIsMouseOverWindow) {
965     QCursor* aCurrentCursor = QApplication::overrideCursor();
966     if (!aCurrentCursor || aCurrentCursor->shape() != Qt::CrossCursor) {
967       QApplication::setOverrideCursor(QCursor(Qt::CrossCursor));
968 #ifdef DEBUG_CURSOR
969       qDebug("startNestedSketch() : Qt::CrossCursor");
970 #endif
971     }
972   }
973 }
974
975 void PartSet_SketcherMgr::stopNestedSketch(ModuleBase_Operation* theOperation)
976 {
977   myIsMouseOverViewProcessed = true;
978   operationMgr()->onValidateOperation();
979   if (canChangeCursor(theOperation)) {
980     QApplication::restoreOverrideCursor();
981 #ifdef DEBUG_CURSOR
982     qDebug("stopNestedSketch() : None");
983 #endif
984   }
985 }
986
987 void PartSet_SketcherMgr::commitNestedSketch(ModuleBase_Operation* theOperation)
988 {
989   if (isNestedCreateOperation(theOperation)) {
990     ModuleBase_OperationFeature* aFOperation = dynamic_cast<ModuleBase_OperationFeature*>
991                                                                              (theOperation);
992     if (aFOperation) {
993       FeaturePtr aFeature = aFOperation->feature();
994       // it is necessary to check the the feature data validity because
995       // some kind of features are removed by an operation commit(the macro state of a feature)
996       if (aFeature.get() && aFeature->data()->isValid()) {
997         visualizeFeature(aFeature, aFOperation->isEditOperation(), true);
998       }
999     }
1000   }
1001 }
1002
1003 void PartSet_SketcherMgr::activatePlaneFilter(const bool& toActivate)
1004 {
1005   if (toActivate)
1006     myModule->workshop()->viewer()->addSelectionFilter(myPlaneFilter);
1007   else
1008     myModule->workshop()->viewer()->removeSelectionFilter(myPlaneFilter);
1009 }
1010
1011 void PartSet_SketcherMgr::operationActivatedByPreselection()
1012 {
1013   ModuleBase_Operation* anOperation = getCurrentOperation();
1014   if(anOperation && PartSet_SketcherMgr::isNestedSketchOperation(anOperation)) {
1015     // Set final definitions if they are necessary
1016     //propertyPanelDefined(aOperation);
1017     /// Commit sketcher operations automatically
1018     ModuleBase_OperationFeature* aFOperation = dynamic_cast<ModuleBase_OperationFeature*>
1019                                                                             (anOperation);
1020     if (aFOperation) {
1021       if (PartSet_SketcherMgr::isDistanceOperation(aFOperation)) {
1022         FeaturePtr aFeature = aFOperation->feature();
1023         // editor is shown only if all attribute references are filled by preseletion
1024         bool anAllRefAttrInitialized = true;
1025
1026         std::list<AttributePtr> aRefAttrs = aFeature->data()->attributes(
1027                                                     ModelAPI_AttributeRefAttr::typeId());
1028         std::list<AttributePtr>::const_iterator anIt = aRefAttrs.begin(), aLast = aRefAttrs.end();
1029         for (; anIt != aLast && anAllRefAttrInitialized; anIt++) {
1030           anAllRefAttrInitialized = (*anIt)->isInitialized();
1031         }
1032         if (anAllRefAttrInitialized) {
1033           // Activate dimension value editing on double click
1034           ModuleBase_IPropertyPanel* aPanel = aFOperation->propertyPanel();
1035           QList<ModuleBase_ModelWidget*> aWidgets = aPanel->modelWidgets();
1036           // Find corresponded widget to activate value editing
1037           foreach (ModuleBase_ModelWidget* aWgt, aWidgets) {
1038             if (aWgt->attributeID() == "ConstraintValue") {
1039               // the featue should be displayed in order to find the AIS text position,
1040               // the place where the editor will be shown
1041               aFeature->setDisplayed(true);
1042               /// the execute is necessary to perform in the feature compute for flyout position
1043               aFeature->execute();
1044
1045               Events_Loop::loop()->flush(Events_Loop::eventByName(EVENT_OBJECT_CREATED));
1046               Events_Loop::loop()->flush(Events_Loop::eventByName(EVENT_OBJECT_TO_REDISPLAY));
1047
1048               PartSet_WidgetEditor* anEditor = dynamic_cast<PartSet_WidgetEditor*>(aWgt);
1049               if (anEditor) {
1050                 int aX = 0, anY = 0;
1051
1052                 ModuleBase_IWorkshop* aWorkshop = myModule->workshop();
1053                 XGUI_ModuleConnector* aConnector = dynamic_cast<XGUI_ModuleConnector*>(aWorkshop);
1054                 XGUI_Displayer* aDisplayer = aConnector->workshop()->displayer();
1055                 AISObjectPtr anAIS = aDisplayer->getAISObject(aFeature);
1056                 Handle(AIS_InteractiveObject) anAISIO;
1057                 if (anAIS.get() != NULL) {
1058                   anAISIO = anAIS->impl<Handle(AIS_InteractiveObject)>();
1059                 }
1060                 if (anAIS.get() != NULL) {
1061                   Handle(AIS_InteractiveObject) anAISIO = anAIS->impl<Handle(AIS_InteractiveObject)>();
1062
1063                   if (!anAISIO.IsNull()) {
1064                     Handle(AIS_Dimension) aDim = Handle(AIS_Dimension)::DownCast(anAISIO);
1065                     if (!aDim.IsNull()) {
1066                       gp_Pnt aPosition = aDim->GetTextPosition();
1067
1068                       ModuleBase_IViewer* aViewer = aWorkshop->viewer();
1069                       Handle(V3d_View) aView = aViewer->activeView();
1070                       int aCX, aCY;
1071                       aView->Convert(aPosition.X(), aPosition.Y(), aPosition.Z(), aCX, aCY);
1072
1073                       QWidget* aViewPort = aViewer->activeViewPort();
1074                       QPoint aGlPoint = aViewPort->mapToGlobal(QPoint(aCX, aCY));
1075                       aX = aGlPoint.x();
1076                       anY = aGlPoint.y();
1077                     }
1078                   }
1079                   anEditor->setCursorPosition(aX, anY);
1080                   anEditor->showPopupEditor(false);
1081                 }
1082               }
1083             }
1084           }
1085         }
1086       }
1087     }
1088     anOperation->commit();
1089   }
1090 }
1091
1092 bool PartSet_SketcherMgr::canUndo() const
1093 {
1094   return isNestedCreateOperation(getCurrentOperation());
1095 }
1096
1097 bool PartSet_SketcherMgr::canRedo() const
1098 {
1099   return isNestedCreateOperation(getCurrentOperation());
1100 }
1101
1102 bool PartSet_SketcherMgr::canEraseObject(const ObjectPtr& theObject) const
1103 {
1104   bool aCanErase = true;
1105   // when the sketch operation is active, results of sketch sub-feature can not be hidden
1106   if (myCurrentSketch.get()) {
1107     ResultPtr aResult = std::dynamic_pointer_cast<ModelAPI_Result>(theObject);
1108     if (aResult.get()) {
1109       // Display sketcher objects
1110       for (int i = 0; i < myCurrentSketch->numberOfSubs() && aCanErase; i++) {
1111
1112         FeaturePtr aFeature = myCurrentSketch->subFeature(i);
1113         std::list<ResultPtr> aResults = aFeature->results();
1114         std::list<ResultPtr>::const_iterator anIt;
1115         for (anIt = aResults.begin(); anIt != aResults.end() && aCanErase; ++anIt) {
1116           aCanErase = *anIt != aResult;
1117         }
1118       }
1119     }
1120   }
1121   return aCanErase;
1122 }
1123
1124 bool PartSet_SketcherMgr::canDisplayObject(const ObjectPtr& theObject) const
1125 {
1126   bool aCanDisplay = true;
1127
1128   bool aHasActiveSketch = activeSketch().get() != NULL;
1129   if (aHasActiveSketch) {
1130     // 1. the sketch feature should not be displayed during the sketch active operation
1131     // it is hidden by a sketch operation start and shown by a sketch stop, just the sketch 
1132     // nested features can be visualized
1133     FeaturePtr aFeature = ModelAPI_Feature::feature(theObject);
1134     if (aFeature.get() != NULL && aFeature == activeSketch()) {
1135       aCanDisplay = false;
1136     }
1137   }
1138   else { // there are no an active sketch
1139     // 2. sketch sub-features should not be visualized if the sketch operation is not active
1140     FeaturePtr aFeature = ModelAPI_Feature::feature(theObject);
1141     if (aFeature.get() != NULL) {
1142       std::shared_ptr<SketchPlugin_Feature> aSketchFeature =
1143                               std::dynamic_pointer_cast<SketchPlugin_Feature>(aFeature);
1144       if (aSketchFeature.get()) {
1145         aCanDisplay = false;
1146       }
1147     }
1148   }
1149
1150   // 3. the method should not filter the objects, which are not related to the current operation.
1151   // The object is filtered just if it is a current operation feature or this feature result
1152   bool isObjectFound = false;
1153   ModuleBase_OperationFeature* aFOperation = dynamic_cast<ModuleBase_OperationFeature*>
1154                                                                (getCurrentOperation());
1155   if (aFOperation) {
1156     FeaturePtr aFeature = aFOperation->feature();
1157     if (aFeature.get()) {
1158       std::list<ResultPtr> aResults = aFeature->results();
1159       if (theObject == aFeature)
1160         isObjectFound = true;
1161       else {
1162         std::list<ResultPtr>::const_iterator anIt = aResults.begin(), aLast = aResults.end();
1163         for (; anIt != aLast && !isObjectFound; anIt++) {
1164           isObjectFound = *anIt == theObject;
1165         }
1166       }
1167     }
1168   }
1169   if (isObjectFound) {
1170     // 4. For created nested feature operation do not display the created feature if
1171     // the mouse curstor leaves the OCC window.
1172     // The correction cases, which ignores this condition:
1173     // a. the property panel values modification
1174     // b. the popup menu activated
1175     // c. widget editor control
1176     #ifndef DEBUG_DO_NOT_BY_ENTER
1177     if (aCanDisplay && isNestedCreateOperation(getCurrentOperation())) {
1178       ModuleBase_ModelWidget* anActiveWidget = getActiveWidget();
1179       ModuleBase_WidgetEditor* anEditorWdg = anActiveWidget ? dynamic_cast<ModuleBase_WidgetEditor*>(anActiveWidget) : 0;
1180       // the active widget editor should not influence here. The presentation should be visible always
1181       // when this widget is active.
1182       if (!anEditorWdg && !myIsPopupMenuActive) {
1183         // during a nested create operation, the feature is redisplayed only if the mouse over view
1184         // of there was a value modified in the property panel after the mouse left the view
1185         aCanDisplay = canDisplayCurrentCreatedFeature();
1186       }
1187     }
1188     #endif
1189   }
1190
1191   // checks the sketcher constraints visibility according to active sketch check box states
1192   if (aCanDisplay) {
1193     bool aProcessed = false;
1194     FeaturePtr aFeature = ModelAPI_Feature::feature(theObject);
1195     if (aFeature.get()) {
1196       bool aConstraintDisplayed = canDisplayConstraint(aFeature, PartSet_Tools::Any, aProcessed);
1197       if (aProcessed)
1198         aCanDisplay = aConstraintDisplayed;
1199     }
1200   }
1201
1202   return aCanDisplay;
1203 }
1204
1205 bool PartSet_SketcherMgr::canDisplayConstraint(const FeaturePtr& theFeature,
1206                                              const PartSet_Tools::ConstraintVisibleState& theState,
1207                                              bool& isProcessed) const
1208 {
1209   bool aSwitchedOn = true;
1210
1211   const QStringList& aConstrIds = constraintsIdList();
1212
1213   std::string aKind = theFeature->getKind();
1214   if (aConstrIds.contains(QString(aKind.c_str()))) {
1215     bool isTypedConstraint = false;
1216
1217     switch (theState) {
1218       case PartSet_Tools::Dimensional: {
1219         bool isDistance = isDistanceKind(aKind);
1220         if (isDistance) {
1221           isProcessed = true;
1222           aSwitchedOn = myIsConstraintsShown[theState];
1223         }
1224       }
1225       break;
1226       case PartSet_Tools::Geometrical: {
1227         bool isGeometrical = !isDistanceKind(aKind);
1228         if (isGeometrical) {
1229           isProcessed = true;
1230           aSwitchedOn = myIsConstraintsShown[theState];
1231         }
1232       }
1233       break;
1234       case PartSet_Tools::Any: {
1235         isProcessed = true;
1236         bool isDistance = isDistanceKind(aKind);
1237         if (isDistance)
1238           aSwitchedOn = myIsConstraintsShown[PartSet_Tools::Dimensional];
1239         else
1240           aSwitchedOn = myIsConstraintsShown[PartSet_Tools::Geometrical];
1241       }
1242       break;
1243     default:
1244       break;
1245     }
1246   }
1247   return aSwitchedOn;
1248 }
1249
1250 void PartSet_SketcherMgr::processHiddenObject(const std::list<ObjectPtr>& theObjects)
1251 {
1252   ModuleBase_OperationFeature* aFOperation = dynamic_cast<ModuleBase_OperationFeature*>
1253                                                                            (getCurrentOperation());
1254   if (aFOperation && myCurrentSketch.get()) {
1255     // find results of the current operation
1256     // these results should not be proposed to be deleted
1257     FeaturePtr anOperationFeature = aFOperation->feature();
1258     std::list<ResultPtr> anOperationResultList = anOperationFeature->results();
1259     std::set<ResultPtr> anOperationResults;
1260     std::list<ResultPtr>::const_iterator aRIt = anOperationResultList.begin(),
1261                                         aRLast = anOperationResultList.end();
1262     for (; aRIt != aRLast; aRIt++)
1263       anOperationResults.insert(*aRIt);
1264
1265     std::set<FeaturePtr> anObjectsToBeDeleted;
1266     QStringList anObjectsToBeDeletedNames;
1267     std::list<ObjectPtr>::const_iterator anIt = theObjects.begin(), aLast = theObjects.end();
1268     for (; anIt != aLast; anIt++) {
1269       ObjectPtr anObject = *anIt;
1270       bool aCanErase = true;
1271       // when the sketch operation is active, results of sketch sub-feature can not be hidden
1272       ResultPtr aResult = std::dynamic_pointer_cast<ModelAPI_Result>(anObject);
1273       // the result is found between current feature results
1274       if (anOperationResults.find(aResult) != anOperationResults.end())
1275         continue;
1276
1277       if (aResult.get()) {
1278         // Display sketcher objects
1279         for (int i = 0; i < myCurrentSketch->numberOfSubs() && aCanErase; i++) {
1280           FeaturePtr aFeature = myCurrentSketch->subFeature(i);
1281           std::list<ResultPtr> aResults = aFeature->results();
1282           std::list<ResultPtr>::const_iterator anIt;
1283           for (anIt = aResults.begin(); anIt != aResults.end() && aCanErase; ++anIt) {
1284             aCanErase = *anIt != aResult;
1285           }
1286         }
1287       }
1288       if (!aCanErase) {
1289         FeaturePtr aFeature = ModelAPI_Feature::feature(anObject);
1290         if (aFeature.get() && anObjectsToBeDeleted.find(aFeature) == anObjectsToBeDeleted.end()) {
1291           anObjectsToBeDeleted.insert(aFeature);
1292           anObjectsToBeDeletedNames.append(aFeature->name().c_str());
1293         }
1294       }
1295     }
1296     if (!anObjectsToBeDeleted.empty()) {
1297       QString aFeatureNames = anObjectsToBeDeletedNames.join(", ");
1298       QString aMessage = tr("The following features have incorrect presentation and \
1299 will be hidden: %1. Would you like to delete them?")
1300                          .arg(aFeatureNames);
1301       int anAnswer = QMessageBox::question(qApp->activeWindow(), tr("Features hide"),
1302                                            aMessage, QMessageBox::Ok | QMessageBox::Cancel,
1303                                            QMessageBox::Cancel);
1304       if (anAnswer == QMessageBox::Ok) {
1305         QObjectPtrList anObjects;
1306         std::set<FeaturePtr>::const_iterator anIt = anObjectsToBeDeleted.begin(),
1307                                              aLast = anObjectsToBeDeleted.end();
1308         for (; anIt != aLast; anIt++)
1309           anObjects.append(*anIt);
1310         SessionPtr aMgr = ModelAPI_Session::get();
1311         DocumentPtr aDoc = aMgr->activeDocument();
1312         bool aIsOp = aMgr->isOperation();
1313         if (!aIsOp)
1314           aMgr->startOperation();
1315         workshop()->deleteFeatures(anObjects);
1316         //static Events_ID aDeletedEvent = Events_Loop::eventByName(EVENT_OBJECT_DELETED);
1317         //static Events_ID aRedispEvent = Events_Loop::eventByName(EVENT_OBJECT_TO_REDISPLAY);
1318         //Events_Loop::loop()->flush(aDeletedEvent);
1319         //Events_Loop::loop()->flush(aRedispEvent);
1320
1321         if (!aIsOp)
1322           aMgr->finishOperation();
1323       }
1324     }
1325   }
1326 }
1327
1328 bool PartSet_SketcherMgr::canDisplayCurrentCreatedFeature() const
1329 {
1330   bool aCanDisplay = myIsMouseOverWindow;
1331   if (!aCanDisplay) {
1332     ModuleBase_ModelWidget* anActiveWidget = getActiveWidget();
1333     if (anActiveWidget)
1334       aCanDisplay = anActiveWidget->getValueState() == ModuleBase_ModelWidget::Stored;
1335   }
1336   return aCanDisplay;
1337 }
1338
1339 bool PartSet_SketcherMgr::canChangeCursor(ModuleBase_Operation* theOperation) const
1340 {
1341   return isNestedCreateOperation(theOperation) ||
1342          myModule->sketchReentranceMgr()->isInternalEditActive();
1343 }
1344
1345 const QMap<PartSet_Tools::ConstraintVisibleState, bool>& PartSet_SketcherMgr::showConstraintStates()
1346 {
1347   return myIsConstraintsShown;
1348 }
1349
1350 bool PartSet_SketcherMgr::isObjectOfSketch(const ObjectPtr& theObject) const
1351 {
1352   bool isFoundObject = false;
1353
1354   FeaturePtr anObjectFeature = ModelAPI_Feature::feature(theObject);
1355   if (anObjectFeature.get()) {
1356     int aSize = myCurrentSketch->numberOfSubs();
1357     for (int i = 0; i < myCurrentSketch->numberOfSubs() && !isFoundObject; i++) {
1358       FeaturePtr aCurrentFeature = myCurrentSketch->subFeature(i);
1359       isFoundObject = myCurrentSketch->subFeature(i) == anObjectFeature;
1360     }
1361   }
1362   return isFoundObject;
1363 }
1364
1365 void PartSet_SketcherMgr::onPlaneSelected(const std::shared_ptr<GeomAPI_Pln>& thePln)
1366 {
1367   if (myPlaneFilter.IsNull()) 
1368    myPlaneFilter = new ModuleBase_ShapeInPlaneFilter();
1369
1370   myPlaneFilter->setPlane(thePln);
1371 }
1372
1373 void PartSet_SketcherMgr::getCurrentSelection(const FeaturePtr& theFeature,
1374                                               const FeaturePtr& theSketch,
1375                                               ModuleBase_IWorkshop* theWorkshop,
1376                                               FeatureToSelectionMap& theSelection)
1377 {
1378   if (theFeature.get() == NULL)
1379     return;
1380
1381   std::set<AttributePtr> aSelectedAttributes;
1382   std::set<ResultPtr> aSelectedResults;
1383
1384   ModuleBase_IViewer* aViewer = theWorkshop->viewer();
1385   Handle(AIS_InteractiveContext) aContext = aViewer->AISContext();
1386   if (!aContext.IsNull()) {
1387     XGUI_ModuleConnector* aConnector = dynamic_cast<XGUI_ModuleConnector*>(theWorkshop);
1388     XGUI_Displayer* aDisplayer = aConnector->workshop()->displayer();
1389
1390     std::list<ResultPtr> aResults = theFeature->results();
1391     std::list<ResultPtr>::const_iterator aIt;
1392     for (aIt = aResults.begin(); aIt != aResults.end(); ++aIt)
1393     {
1394       ResultPtr aResult = *aIt;
1395       AISObjectPtr aAISObj = aDisplayer->getAISObject(aResult);
1396       if (aAISObj.get() == NULL)
1397         continue;
1398       Handle(AIS_InteractiveObject) anAISIO = aAISObj->impl<Handle(AIS_InteractiveObject)>();
1399       for (aContext->InitSelected(); aContext->MoreSelected(); aContext->NextSelected())
1400       {
1401         Handle(SelectMgr_EntityOwner) anOwner = aContext->SelectedOwner();
1402         if (anOwner->Selectable() != anAISIO)
1403           continue;
1404         getAttributesOrResults(anOwner, theFeature, theSketch, aResult,
1405                                aSelectedAttributes, aSelectedResults);
1406       }
1407       for (aContext->InitDetected(); aContext->MoreDetected(); aContext->NextDetected()) {
1408         Handle(SelectMgr_EntityOwner) anOwner = aContext->DetectedOwner();
1409         if (anOwner.IsNull())
1410           continue;
1411         if (anOwner->Selectable() != anAISIO)
1412           continue;
1413         getAttributesOrResults(anOwner, theFeature, theSketch, aResult,
1414                                aSelectedAttributes, aSelectedResults);
1415       }
1416     }
1417   }
1418   theSelection[theFeature] = std::make_pair(aSelectedAttributes, aSelectedResults);
1419 }
1420
1421 void PartSet_SketcherMgr::getSelectionOwners(const FeaturePtr& theFeature,
1422                                              const FeaturePtr& theSketch,
1423                                              ModuleBase_IWorkshop* theWorkshop,
1424                                              const FeatureToSelectionMap& theSelection,
1425                                              SelectMgr_IndexedMapOfOwner& theOwnersToSelect)
1426 {
1427   if (theFeature.get() == NULL)
1428     return;
1429
1430   FeatureToSelectionMap::const_iterator anIt = theSelection.find(theFeature);
1431   std::set<AttributePtr> aSelectedAttributes = anIt.value().first;
1432   std::set<ResultPtr> aSelectedResults = anIt.value().second;
1433
1434   ModuleBase_IViewer* aViewer = theWorkshop->viewer();
1435
1436   XGUI_ModuleConnector* aConnector = dynamic_cast<XGUI_ModuleConnector*>(theWorkshop);
1437   XGUI_Displayer* aDisplayer = aConnector->workshop()->displayer();
1438
1439   // 1. found the feature's owners. Check the AIS objects of the constructions
1440   AISObjectPtr aAISObj = aDisplayer->getAISObject(theFeature);
1441   if (aAISObj.get() != NULL && aSelectedAttributes.empty() && aSelectedResults.empty()) {
1442     Handle(AIS_InteractiveObject) anAISIO = aAISObj->impl<Handle(AIS_InteractiveObject)>();
1443
1444     SelectMgr_IndexedMapOfOwner aSelectedOwners;
1445     aConnector->workshop()->selector()->selection()->entityOwners(anAISIO, aSelectedOwners);
1446     for  (Standard_Integer i = 1, n = aSelectedOwners.Extent(); i <= n; i++) {
1447       Handle(SelectMgr_EntityOwner) anOwner = aSelectedOwners(i);
1448       if (!anOwner.IsNull())
1449         theOwnersToSelect.Add(anOwner);
1450     }
1451   }
1452
1453   // 2. found the feature results's owners
1454   std::list<ResultPtr> aResults = theFeature->results();
1455   std::list<ResultPtr>::const_iterator aIt;
1456   for (aIt = aResults.begin(); aIt != aResults.end(); ++aIt)
1457   {
1458     ResultPtr aResult = *aIt;
1459     AISObjectPtr aAISObj = aDisplayer->getAISObject(aResult);
1460     if (aAISObj.get() == NULL)
1461       continue; 
1462     Handle(AIS_InteractiveObject) anAISIO = aAISObj->impl<Handle(AIS_InteractiveObject)>();
1463
1464     SelectMgr_IndexedMapOfOwner aSelectedOwners;  
1465     aConnector->workshop()->selector()->selection()->entityOwners(anAISIO, aSelectedOwners);
1466     for  ( Standard_Integer i = 1, n = aSelectedOwners.Extent(); i <= n; i++ ) {
1467       Handle(StdSelect_BRepOwner) anOwner = Handle(StdSelect_BRepOwner)::DownCast(aSelectedOwners(i));
1468       if ( anOwner.IsNull() || !anOwner->HasShape() )
1469         continue;
1470       const TopoDS_Shape& aShape = anOwner->Shape();
1471       TopAbs_ShapeEnum aShapeType = aShape.ShapeType();
1472       if (aShapeType == TopAbs_VERTEX) {
1473         AttributePtr aPntAttr = PartSet_Tools::findAttributeBy2dPoint(theFeature, aShape, theSketch);
1474         if (aPntAttr.get() != NULL &&
1475             aSelectedAttributes.find(aPntAttr) != aSelectedAttributes.end()) {
1476           theOwnersToSelect.Add(anOwner);
1477         }
1478       }
1479       else if (aShapeType == TopAbs_EDGE) {
1480         bool aFound = aSelectedResults.find(aResult) != aSelectedResults.end();
1481         if (aSelectedResults.find(aResult) != aSelectedResults.end() &&
1482             theOwnersToSelect.FindIndex(anOwner) <= 0)
1483           theOwnersToSelect.Add(anOwner);
1484       }
1485     }
1486   }
1487 }
1488
1489 void PartSet_SketcherMgr::connectToPropertyPanel(ModuleBase_ModelWidget* theWidget, const bool isToConnect)
1490 {
1491   if (isToConnect) {
1492     connect(theWidget, SIGNAL(beforeValuesChanged()),
1493             this, SLOT(onBeforeValuesChangedInPropertyPanel()));
1494     connect(theWidget, SIGNAL(afterValuesChanged()),
1495             this, SLOT(onAfterValuesChangedInPropertyPanel()));
1496   }
1497   else {
1498     disconnect(theWidget, SIGNAL(beforeValuesChanged()),
1499                 this, SLOT(onBeforeValuesChangedInPropertyPanel()));
1500     disconnect(theWidget, SIGNAL(afterValuesChanged()),
1501                 this, SLOT(onAfterValuesChangedInPropertyPanel()));
1502   }
1503 }
1504
1505 void PartSet_SketcherMgr::widgetStateChanged(int thePreviousState)
1506 {
1507   ModuleBase_OperationFeature* aFOperation = dynamic_cast<ModuleBase_OperationFeature*>
1508                                                                            (getCurrentOperation());
1509   if (aFOperation) {
1510     if (PartSet_SketcherMgr::isSketchOperation(aFOperation) ||
1511         PartSet_SketcherMgr::isNestedSketchOperation(aFOperation) &&
1512         thePreviousState == ModuleBase_ModelWidget::ModifiedInPP) {
1513       FeaturePtr aFeature = aFOperation->feature();
1514       visualizeFeature(aFeature, aFOperation->isEditOperation(), canDisplayObject(aFeature));
1515     }
1516   }
1517 }
1518
1519 ModuleBase_Operation* PartSet_SketcherMgr::getCurrentOperation() const
1520 {
1521   return myModule->workshop()->currentOperation();
1522 }
1523
1524 //**************************************************************
1525 ModuleBase_ModelWidget* PartSet_SketcherMgr::getActiveWidget() const
1526 {
1527   ModuleBase_ModelWidget* aWidget = 0;
1528   ModuleBase_Operation* anOperation = getCurrentOperation();
1529   if (anOperation) {
1530     ModuleBase_IPropertyPanel* aPanel = anOperation->propertyPanel();
1531     if (aPanel)
1532       aWidget = aPanel->activeWidget();
1533   }
1534   return aWidget;
1535 }
1536
1537 void PartSet_SketcherMgr::visualizeFeature(const FeaturePtr& theFeature,
1538                                            const bool isEditOperation,
1539                                            const bool isToDisplay,
1540                                            const bool isFlushRedisplay)
1541 {
1542   #ifdef DEBUG_DO_NOT_BY_ENTER
1543   return;
1544   #endif
1545
1546   if (isEditOperation || !theFeature.get())
1547     return;
1548
1549   ModuleBase_IWorkshop* aWorkshop = myModule->workshop();
1550   XGUI_ModuleConnector* aConnector = dynamic_cast<XGUI_ModuleConnector*>(aWorkshop);
1551
1552   // 1. change visibility of the object itself, here the presentable object is processed,
1553   // e.g. constraints features
1554   //FeaturePtr aFeature = aFOperation->feature();
1555   std::list<ResultPtr> aResults = theFeature->results();
1556   if (isToDisplay)
1557     theFeature->setDisplayed(true);
1558   else
1559     theFeature->setDisplayed(false);
1560
1561   // change visibility of the object results, e.g. non-constraint features
1562   std::list<ResultPtr>::const_iterator aIt;
1563   for (aIt = aResults.begin(); aIt != aResults.end(); ++aIt) {
1564     if (isToDisplay) {
1565       (*aIt)->setDisplayed(true);
1566     }
1567     else {
1568       (*aIt)->setDisplayed(false);
1569     }
1570   }
1571   if (isFlushRedisplay)
1572     Events_Loop::loop()->flush(Events_Loop::eventByName(EVENT_OBJECT_TO_REDISPLAY));
1573 }
1574
1575 void PartSet_SketcherMgr::storeSelection(const bool theHighlightedOnly)
1576 {
1577   ModuleBase_IWorkshop* aWorkshop = myModule->workshop();
1578   ModuleBase_ISelection* aSelect = aWorkshop->selection();
1579   QList<ModuleBase_ViewerPrs> aHighlighted = aSelect->getHighlighted();
1580
1581   QList<FeaturePtr> aFeatureList;
1582   if (theHighlightedOnly) {
1583     fillFeatureList(aHighlighted, myCurrentSketch, aFeatureList);
1584   }
1585   else {
1586     fillFeatureList(aHighlighted, myCurrentSketch, aFeatureList);
1587
1588     QList<ModuleBase_ViewerPrs> aSelected = aSelect->getSelected(ModuleBase_ISelection::AllControls);
1589     fillFeatureList(aSelected, myCurrentSketch, aFeatureList);
1590   }
1591
1592   // 1. it is necessary to save current selection in order to restore it after the features moving
1593   myCurrentSelection.clear();
1594   QList<FeaturePtr>::const_iterator anIt = aFeatureList.begin(), aLast = aFeatureList.end();
1595   for (; anIt != aLast; anIt++) {
1596     getCurrentSelection(*anIt, myCurrentSketch, aWorkshop, myCurrentSelection);
1597   }
1598   //qDebug(QString("  storeSelection: %1").arg(myCurrentSelection.size()).toStdString().c_str());
1599 }
1600
1601 void PartSet_SketcherMgr::restoreSelection()
1602 {
1603   //qDebug(QString("restoreSelection: %1").arg(myCurrentSelection.size()).toStdString().c_str());
1604   ModuleBase_IWorkshop* aWorkshop = myModule->workshop();
1605   XGUI_ModuleConnector* aConnector = dynamic_cast<XGUI_ModuleConnector*>(aWorkshop);
1606   FeatureToSelectionMap::const_iterator aSIt = myCurrentSelection.begin(),
1607                                         aSLast = myCurrentSelection.end();
1608   SelectMgr_IndexedMapOfOwner anOwnersToSelect;
1609   for (; aSIt != aSLast; aSIt++) {
1610     anOwnersToSelect.Clear();
1611     getSelectionOwners(aSIt.key(), myCurrentSketch, aWorkshop, myCurrentSelection,
1612                         anOwnersToSelect);
1613     aConnector->workshop()->selector()->setSelectedOwners(anOwnersToSelect, false);
1614   }
1615 }
1616
1617 void PartSet_SketcherMgr::onShowConstraintsToggle(int theType, bool theState)
1618 {
1619   PartSet_Tools::ConstraintVisibleState aType = (PartSet_Tools::ConstraintVisibleState)theType;
1620   if (myIsConstraintsShown.contains(aType) && myIsConstraintsShown[aType] == theState)
1621     return;
1622   if (myCurrentSketch.get() == NULL)
1623     return;
1624
1625   myIsConstraintsShown[aType] = theState;
1626
1627   ModuleBase_IWorkshop* aWorkshop = myModule->workshop();
1628   XGUI_ModuleConnector* aConnector = dynamic_cast<XGUI_ModuleConnector*>(aWorkshop);
1629
1630   for (int i = 0; i < myCurrentSketch->numberOfSubs(); i++) {
1631     FeaturePtr aSubFeature = myCurrentSketch->subFeature(i);
1632     bool aProcessed = false;
1633     bool aConstraintDisplayed = canDisplayConstraint(aSubFeature, aType, aProcessed);
1634     if (aProcessed)
1635       aSubFeature->setDisplayed(aConstraintDisplayed);
1636   }
1637   Events_Loop::loop()->flush(Events_Loop::eventByName(EVENT_OBJECT_TO_REDISPLAY));
1638 }
1639
1640 XGUI_Workshop* PartSet_SketcherMgr::workshop() const
1641 {
1642   ModuleBase_IWorkshop* anIWorkshop = myModule->workshop();
1643   XGUI_ModuleConnector* aConnector = dynamic_cast<XGUI_ModuleConnector*>(anIWorkshop);
1644   return aConnector->workshop();
1645 }
1646
1647 XGUI_OperationMgr* PartSet_SketcherMgr::operationMgr() const
1648 {
1649   return workshop()->operationMgr();
1650 }
1651