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