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