Salome HOME
5dd9eaf20a7ff8260d453f40dc5e8437ea1d03b4
[modules/shaper.git] / src / PartSet / PartSet_SketcherMgr.cpp
1 // Copyright (C) 2014-20xx CEA/DEN, EDF R&D
2
3 // File:        PartSet_SketcherMgr.cpp
4 // Created:     19 Dec 2014
5 // Author:      Vitaly SMETANNIKOV
6
7 #include "PartSet_SketcherMgr.h"
8 #include "PartSet_Module.h"
9 #include "PartSet_WidgetPoint2d.h"
10 #include "PartSet_WidgetPoint2dDistance.h"
11 #include "PartSet_Tools.h"
12 #include "PartSet_WidgetSketchLabel.h"
13
14 #include <ModuleBase_WidgetEditor.h>
15 #include <ModuleBase_ModelWidget.h>
16
17 #include <XGUI_ModuleConnector.h>
18 #include <XGUI_Displayer.h>
19 #include <XGUI_Workshop.h>
20 #include <XGUI_ContextMenuMgr.h>
21 #include <XGUI_Selection.h>
22 #include <XGUI_SelectionMgr.h>
23 #include <XGUI_ModuleConnector.h>
24 #include <XGUI_PropertyPanel.h>
25 #include <XGUI_ViewerProxy.h>
26 #include <XGUI_OperationMgr.h>
27
28 #include <ModuleBase_IViewer.h>
29 #include <ModuleBase_IWorkshop.h>
30 #include <ModuleBase_IViewWindow.h>
31 #include <ModuleBase_Operation.h>
32 #include <ModuleBase_ISelection.h>
33 #include <ModuleBase_IPropertyPanel.h>
34 #include <ModuleBase_Operation.h>
35
36 #include <GeomDataAPI_Point2D.h>
37
38 #include <Events_Loop.h>
39
40 #include <SketchPlugin_Line.h>
41 #include <SketchPlugin_Sketch.h>
42 #include <SketchPlugin_Point.h>
43 #include <SketchPlugin_Arc.h>
44 #include <SketchPlugin_Circle.h>
45 #include <SketchPlugin_ConstraintLength.h>
46 #include <SketchPlugin_ConstraintDistance.h>
47 #include <SketchPlugin_ConstraintParallel.h>
48 #include <SketchPlugin_ConstraintPerpendicular.h>
49 #include <SketchPlugin_ConstraintRadius.h>
50 #include <SketchPlugin_ConstraintRigid.h>
51 #include <SketchPlugin_ConstraintHorizontal.h>
52 #include <SketchPlugin_ConstraintVertical.h>
53 #include <SketchPlugin_ConstraintEqual.h>
54 #include <SketchPlugin_ConstraintTangent.h>
55 #include <SketchPlugin_ConstraintCoincidence.h>
56 #include <SketchPlugin_ConstraintFillet.h>
57 #include <SketchPlugin_ConstraintMirror.h>
58 #include <SketchPlugin_MultiRotation.h>
59 #include <SketchPlugin_MultiTranslation.h>
60
61 #include <SketcherPrs_Tools.h>
62
63 #include <SelectMgr_IndexedMapOfOwner.hxx>
64 #include <StdSelect_BRepOwner.hxx>
65
66 //#include <AIS_DimensionSelectionMode.hxx>
67 #include <AIS_Shape.hxx>
68
69 #include <ModelAPI_Events.h>
70 #include <ModelAPI_Session.h>
71
72 #include <QMouseEvent>
73 #include <QApplication>
74
75 //#define DEBUG_DO_NOT_BY_ENTER
76 //#define DEBUG_MOUSE_OVER_WINDOW_FLAGS
77
78 /// Returns list of unique objects by sum of objects from List1 and List2
79 /*QList<ModuleBase_ViewerPrs> getSumList(const QList<ModuleBase_ViewerPrs>& theList1,
80                                        const QList<ModuleBase_ViewerPrs>& theList2)
81 {
82   QList<ModuleBase_ViewerPrs> aRes;
83   foreach (ModuleBase_ViewerPrs aPrs, theList1) {
84     if (!aRes.contains(aPrs))
85       aRes.append(aPrs);
86   }
87   foreach (ModuleBase_ViewerPrs aPrs, theList2) {
88     if (!aRes.contains(aPrs))
89       aRes.append(aPrs);
90   }
91   return aRes;
92 }*/
93
94 // Fills the list of features the list of selected presentations.
95 // \param theList a list of selected presentations
96 // \param theSketch a sketch to project a vertex shape of a presentation to the plane
97 // and find the corresponded attribute
98 // \param theFeatureList  an output list of features
99 void fillFeatureList(const QList<ModuleBase_ViewerPrs>& theList,
100                      const FeaturePtr theSketch,
101                      QList<FeaturePtr>& theFeatureList)
102 {
103   QList<ModuleBase_ViewerPrs> aRes;
104
105   QList<ModuleBase_ViewerPrs>::const_iterator anIt = theList.begin(),
106                                               aLast = theList.end();
107   for (; anIt != aLast; anIt++)
108   {
109     ModuleBase_ViewerPrs aPrs = *anIt;
110     FeaturePtr aFeature = ModelAPI_Feature::feature(aPrs.object());
111     if (aFeature.get()  && !theFeatureList.contains(aFeature))
112       theFeatureList.append(aFeature);
113   }
114 }
115
116 /// Fills attribute and result lists by the selected owner. In case if the attribute is found,
117 /// by the owner shape, it is put to the list. Otherwise if type of owner shape is edge, put the function
118 /// result as is to the list of results.
119 /// \param theOwner a viewer selected owner
120 /// \param theFeature a feature, where the attribute is searched
121 /// \param theSketch a current sketch
122 /// \param theSelectedAttribute an output list of attributes
123 /// \param theSelectedResults an output list of edge results
124 void getAttributesOrResults(const Handle(SelectMgr_EntityOwner)& theOwner,
125                             const FeaturePtr& theFeature, const FeaturePtr& theSketch,
126                             const ResultPtr& theResult,
127                             std::set<AttributePtr>& aSelectedAttributes,
128                             std::set<ResultPtr>& aSelectedResults)
129 {
130   Handle(StdSelect_BRepOwner) aBRepOwner = Handle(StdSelect_BRepOwner)::DownCast(theOwner);
131   if (aBRepOwner.IsNull())
132     return;
133   Handle(AIS_InteractiveObject) anIO = Handle(AIS_InteractiveObject)::DownCast(
134                                                                     aBRepOwner->Selectable());
135   if (aBRepOwner->HasShape()) {
136     const TopoDS_Shape& aShape = aBRepOwner->Shape();
137     TopAbs_ShapeEnum aShapeType = aShape.ShapeType();
138     if (aShapeType == TopAbs_VERTEX) {
139       AttributePtr aPntAttr = PartSet_Tools::findAttributeBy2dPoint(theFeature,
140                                                                     aShape, theSketch);
141       if (aPntAttr.get() != NULL)
142         aSelectedAttributes.insert(aPntAttr);
143     }
144     else if (aShapeType == TopAbs_EDGE &&
145              aSelectedResults.find(theResult) == aSelectedResults.end()) {
146       aSelectedResults.insert(theResult);
147     }
148   }
149 }
150
151 PartSet_SketcherMgr::PartSet_SketcherMgr(PartSet_Module* theModule)
152   : QObject(theModule), myModule(theModule), myIsDragging(false), myDragDone(false),
153     myIsResetCurrentValue(false), myIsMouseOverWindow(false),
154     myIsMouseOverViewProcessed(true), myPreviousUpdateViewerEnabled(true),
155     myIsPopupMenuActive(false), myIsConstraintsShown(true)
156 {
157   ModuleBase_IWorkshop* anIWorkshop = myModule->workshop();
158   ModuleBase_IViewer* aViewer = anIWorkshop->viewer();
159
160   myPreviousSelectionEnabled = true;//aViewer->isSelectionEnabled();
161
162   connect(aViewer, SIGNAL(mousePress(ModuleBase_IViewWindow*, QMouseEvent*)),
163           this, SLOT(onMousePressed(ModuleBase_IViewWindow*, QMouseEvent*)));
164
165   connect(aViewer, SIGNAL(mouseRelease(ModuleBase_IViewWindow*, QMouseEvent*)),
166           this, SLOT(onMouseReleased(ModuleBase_IViewWindow*, QMouseEvent*)));
167
168   connect(aViewer, SIGNAL(mouseMove(ModuleBase_IViewWindow*, QMouseEvent*)),
169           this, SLOT(onMouseMoved(ModuleBase_IViewWindow*, QMouseEvent*)));
170
171   connect(aViewer, SIGNAL(mouseDoubleClick(ModuleBase_IViewWindow*, QMouseEvent*)),
172           this, SLOT(onMouseDoubleClick(ModuleBase_IViewWindow*, QMouseEvent*)));
173
174   XGUI_ModuleConnector* aConnector = dynamic_cast<XGUI_ModuleConnector*>(anIWorkshop);
175   XGUI_Workshop* aWorkshop = aConnector->workshop();
176   connect(aWorkshop, SIGNAL(applicationStarted()), this, SLOT(onApplicationStarted()));
177 }
178
179 PartSet_SketcherMgr::~PartSet_SketcherMgr()
180 {
181   if (!myPlaneFilter.IsNull())
182     myPlaneFilter.Nullify();
183 }
184
185 void PartSet_SketcherMgr::onEnterViewPort()
186 {
187   // 1. if the mouse over window, update the next flag. Do not perform update visibility of
188   // created feature because it should be done in onMouseMove(). Some widgets watch
189   // the mouse move and use the cursor position to update own values. If the presentaion is
190   // redisplayed before this update, the feature presentation jumps from reset value to current.
191   myIsMouseOverWindow = true;
192   myIsResetCurrentValue = false;
193   operationMgr()->onValidateOperation();
194 #ifdef DEBUG_MOUSE_OVER_WINDOW_FLAGS
195   qDebug(QString("onEnterViewPort: %1").arg(mouseOverWindowFlagsInfo()).toStdString().c_str());
196 #endif
197
198   #ifdef DEBUG_DO_NOT_BY_ENTER
199   return;
200   #endif
201
202   if (!isNestedCreateOperation(getCurrentOperation()))
203     return;
204   // we need change displayed state of the current operation feature
205   // if the feature is presentable, e.g. distance construction. It has no results, so workshop does
206   // not accept a signal about the result created. Nothing is shown until mouse is moved out/in view
207   // port. If the isDisplayed flag is true, the presentable feature is displayed as soon as the
208   // presentation becomes valid and redisplay happens
209   ModuleBase_Operation* aOperation = getCurrentOperation();
210   if (aOperation) {
211     FeaturePtr aFeature = aOperation->feature();
212     if (aFeature.get() && aFeature->data()->isValid()) {
213       visualizeFeature(aOperation, canDisplayObject(aFeature), false);
214     }
215   }
216 }
217
218 void PartSet_SketcherMgr::onLeaveViewPort()
219 {
220   myIsMouseOverViewProcessed = false;
221   myIsMouseOverWindow = false;
222   operationMgr()->onValidateOperation();
223 #ifdef DEBUG_MOUSE_OVER_WINDOW_FLAGS
224   qDebug(QString("onLeaveViewPort: %1").arg(mouseOverWindowFlagsInfo()).toStdString().c_str());
225 #endif
226
227   #ifdef DEBUG_DO_NOT_BY_ENTER
228   return;
229   #endif
230
231   if (!isNestedCreateOperation(getCurrentOperation()))
232     return;
233   // the method should be performed if the popup menu is called,
234   // the reset of the current widget should not happen
235   if (myIsPopupMenuActive)
236     return;
237
238   // 2. if the mouse IS NOT over window, reset the active widget value and hide the presentation
239   ModuleBase_IWorkshop* aWorkshop = myModule->workshop();
240   XGUI_ModuleConnector* aConnector = dynamic_cast<XGUI_ModuleConnector*>(aWorkshop);
241   XGUI_Displayer* aDisplayer = aConnector->workshop()->displayer();
242   // disable the viewer update in order to avoid visualization of redisplayed feature in viewer
243   // obtained after reset value
244   bool isEnableUpdateViewer = aDisplayer->enableUpdateViewer(false);
245   ModuleBase_Operation* aOperation = getCurrentOperation();
246   ModuleBase_IPropertyPanel* aPanel = aOperation->propertyPanel();
247   ModuleBase_ModelWidget* aActiveWgt = aPanel->activeWidget();
248   if (aActiveWgt && aActiveWgt->reset()) {
249     myIsResetCurrentValue = true;
250   }
251   aDisplayer->enableUpdateViewer(isEnableUpdateViewer);
252
253   // hides the presentation of the current operation feature
254   // the feature is to be erased here, but it is correct to call canDisplayObject because
255   // there can be additional check (e.g. editor widget in distance constraint)
256   FeaturePtr aFeature = getCurrentOperation()->feature();
257   visualizeFeature(aOperation, canDisplayObject(aFeature));
258 }
259
260 void PartSet_SketcherMgr::onBeforeValuesChangedInPropertyPanel()
261 {
262   if (isNestedCreateOperation(getCurrentOperation()))
263     return;
264   // it is necessary to save current selection in order to restore it after the values are modifed
265   storeSelection();
266
267   ModuleBase_IWorkshop* aWorkshop = myModule->workshop();
268   XGUI_ModuleConnector* aConnector = dynamic_cast<XGUI_ModuleConnector*>(aWorkshop);
269   XGUI_Displayer* aDisplayer = aConnector->workshop()->displayer();
270   myPreviousUpdateViewerEnabled = aDisplayer->enableUpdateViewer(false);
271 }
272
273 void PartSet_SketcherMgr::onAfterValuesChangedInPropertyPanel()
274 {
275   if (isNestedCreateOperation(getCurrentOperation()))
276     return;
277   // it is necessary to restore current selection in order to restore it after the values are modified
278   restoreSelection();
279   myCurrentSelection.clear();
280
281   // 3. the flag to disable the update viewer should be set in order to avoid blinking in the 
282   // viewer happens by deselect/select the modified objects. The flag should be restored after
283   // the selection processing. The update viewer should be also called.
284   ModuleBase_IWorkshop* aWorkshop = myModule->workshop();
285   XGUI_ModuleConnector* aConnector = dynamic_cast<XGUI_ModuleConnector*>(aWorkshop);
286   XGUI_Displayer* aDisplayer = aConnector->workshop()->displayer();
287   aDisplayer->enableUpdateViewer(myPreviousUpdateViewerEnabled);
288   aDisplayer->updateViewer();
289 }
290
291 void PartSet_SketcherMgr::onValuesChangedInPropertyPanel()
292 {
293   if (!isNestedCreateOperation(getCurrentOperation()))
294     return;
295
296   // visualize the current operation feature
297   myIsResetCurrentValue = false;
298   operationMgr()->onValidateOperation();
299   ModuleBase_Operation* aOperation = getCurrentOperation();
300   // the feature is to be erased here, but it is correct to call canDisplayObject because
301   // there can be additional check (e.g. editor widget in distance constraint)
302   FeaturePtr aFeature = getCurrentOperation()->feature();
303   visualizeFeature(aOperation, canDisplayObject(aFeature));
304 }
305
306 void PartSet_SketcherMgr::onMousePressed(ModuleBase_IViewWindow* theWnd, QMouseEvent* theEvent)
307 {
308   get2dPoint(theWnd, theEvent, myClickedPoint);
309
310   if (!(theEvent->buttons() & Qt::LeftButton))
311     return;
312
313   // Clear dragging mode
314   myIsDragging = false;
315
316   ModuleBase_IWorkshop* aWorkshop = myModule->workshop();
317   ModuleBase_IViewer* aViewer = aWorkshop->viewer();
318   myPreviousSelectionEnabled = aViewer->isSelectionEnabled();
319   if (!aViewer->canDragByMouse())
320     return;
321
322   ModuleBase_Operation* aOperation = getCurrentOperation();
323   if (aOperation && aOperation->isEditOperation()) {
324     ModuleBase_IPropertyPanel* aPanel = aOperation->propertyPanel();
325     ModuleBase_ModelWidget* aActiveWgt = aPanel->activeWidget();
326     // If the current widget is a selector, do nothing, it processes the mouse press
327     if(aActiveWgt && aActiveWgt->isViewerSelector()) {
328       return;
329     }
330   }
331
332   // Use only for sketch operations
333   if (aOperation && myCurrentSketch) {
334     if (!PartSet_Tools::sketchPlane(myCurrentSketch))
335       return;
336
337     bool isSketcher = isSketchOperation(aOperation);
338     bool isSketchOpe = isNestedSketchOperation(aOperation);
339
340     // Avoid non-sketch operations
341     if ((!isSketchOpe) && (!isSketcher))
342       return;
343
344     bool isEditing = aOperation->isEditOperation();
345
346     // Ignore creation sketch operation
347     if ((!isSketcher) && (!isEditing))
348       return;
349
350     // MoveTo in order to highlight current object
351     aViewer->AISContext()->MoveTo(theEvent->x(), theEvent->y(), theWnd->v3dView());
352
353     // Remember highlighted objects for editing
354     ModuleBase_ISelection* aSelect = aWorkshop->selection();
355
356     bool aHasShift = (theEvent->modifiers() & Qt::ShiftModifier);
357     storeSelection(!aHasShift);
358
359     if (myCurrentSelection.empty()) {
360       if (isSketchOpe && (!isSketcher))
361         // commit previous operation
362         if (!aOperation->commit())
363           aOperation->abort();
364       return;
365     }
366     // Init flyout point for radius rotation
367     FeaturePtr aFeature = myCurrentSelection.begin().key();
368
369     if (isSketcher) {
370       myIsDragging = true;
371       get2dPoint(theWnd, theEvent, myCurrentPoint);
372       myDragDone = false;
373       launchEditing();
374       if (aFeature.get() != NULL) {
375         std::shared_ptr<SketchPlugin_Feature> aSPFeature = 
376                   std::dynamic_pointer_cast<SketchPlugin_Feature>(aFeature);
377       if (aSPFeature.get() && aSPFeature->getKind() == SketchPlugin_ConstraintRadius::ID()) {
378           DataPtr aData = aSPFeature->data();
379           AttributePtr aAttr = aData->attribute(SketchPlugin_Constraint::FLYOUT_VALUE_PNT());
380           std::shared_ptr<GeomDataAPI_Point2D> aFPAttr = 
381             std::dynamic_pointer_cast<GeomDataAPI_Point2D>(aAttr);
382           aFPAttr->setValue(myCurrentPoint.myCurX, myCurrentPoint.myCurY);
383         }
384       }
385     } else if (isSketchOpe && isEditing) {
386       // If selected another object commit current result
387       aOperation->commit();
388
389       myIsDragging = true;
390       get2dPoint(theWnd, theEvent, myCurrentPoint);
391       myDragDone = false;
392
393       // This is necessary in order to finalize previous operation
394       QApplication::processEvents();
395       launchEditing();
396     }
397   }
398 }
399
400 void PartSet_SketcherMgr::onMouseReleased(ModuleBase_IViewWindow* theWnd, QMouseEvent* theEvent)
401 {
402   ModuleBase_IWorkshop* aWorkshop = myModule->workshop();
403   ModuleBase_IViewer* aViewer = aWorkshop->viewer();
404   if (!aViewer->canDragByMouse())
405     return;
406   ModuleBase_Operation* aOp = getCurrentOperation();
407   if (aOp) {
408     if (isNestedSketchOperation(aOp)) {
409       get2dPoint(theWnd, theEvent, myClickedPoint);
410
411       // Only for sketcher operations
412       if (myIsDragging) {
413         if (myDragDone) {
414           //aOp->commit();
415           myCurrentSelection.clear();
416           // Reselect edited object
417           /*aViewer->AISContext()->MoveTo(theEvent->x(), theEvent->y(), theWnd->v3dView());
418           if (theEvent->modifiers() & Qt::ShiftModifier)
419             aViewer->AISContext()->ShiftSelect();
420           else
421             aViewer->AISContext()->Select();
422             */
423         }
424       }
425     }
426   }
427   aWorkshop->viewer()->enableSelection(myPreviousSelectionEnabled);
428   myIsDragging = false;
429 }
430
431 void PartSet_SketcherMgr::onMouseMoved(ModuleBase_IViewWindow* theWnd, QMouseEvent* theEvent)
432 {
433   if (isNestedCreateOperation(getCurrentOperation()) && !myIsMouseOverViewProcessed) {
434     myIsMouseOverViewProcessed = true;
435     // 1. perform the widget mouse move functionality and display the presentation
436     ModuleBase_Operation* aOperation = getCurrentOperation();
437     ModuleBase_IPropertyPanel* aPanel = aOperation->propertyPanel();
438     ModuleBase_ModelWidget* anActiveWdg = aPanel->activeWidget();
439     // the mouse move should be processed in the widget, if it can in order to visualize correct
440     // presentation. These widgets correct the feature attribute according to the mouse position
441     PartSet_WidgetPoint2D* aPoint2DWdg = dynamic_cast<PartSet_WidgetPoint2D*>(anActiveWdg);
442     if (aPoint2DWdg) {
443       aPoint2DWdg->onMouseMove(theWnd, theEvent);
444     }
445     PartSet_WidgetPoint2dDistance* aDistanceWdg = dynamic_cast<PartSet_WidgetPoint2dDistance*>
446                                                                 (anActiveWdg);
447     if (aDistanceWdg) {
448       aDistanceWdg->onMouseMove(theWnd, theEvent);
449     }
450     // the feature is to be erased here, but it is correct to call canDisplayObject because
451     // there can be additional check (e.g. editor widget in distance constraint)
452     FeaturePtr aFeature = getCurrentOperation()->feature();
453     visualizeFeature(aOperation, canDisplayObject(aFeature));
454   }
455
456   myClickedPoint.clear();
457
458   if (myIsDragging) {
459     // 1. the current selection is saved in the mouse press method in order to restore it after moving
460     // 2. the enable selection in the viewer should be temporary switched off in order to ignore
461     // mouse press signal in the viewer(it call Select for AIS context and the dragged objects are
462     // deselected). This flag should be restored in the slot, processed the mouse release signal.
463     ModuleBase_IViewer* aViewer = myModule->workshop()->viewer();
464     aViewer->enableSelection(false);
465
466     ModuleBase_Operation* aCurrentOperation = getCurrentOperation();
467     if (!aCurrentOperation)
468       return;
469     if (isSketchOperation(aCurrentOperation))
470       return; // No edit operation activated
471
472     Handle(V3d_View) aView = theWnd->v3dView();
473     gp_Pnt aPoint = PartSet_Tools::convertClickToPoint(theEvent->pos(), aView);
474     double aX, aY;
475     PartSet_Tools::convertTo2D(aPoint, myCurrentSketch, aView, aX, aY);
476     double dX =  aX - myCurrentPoint.myCurX;
477     double dY =  aY - myCurrentPoint.myCurY;
478
479     ModuleBase_IWorkshop* aWorkshop = myModule->workshop();
480     XGUI_ModuleConnector* aConnector = dynamic_cast<XGUI_ModuleConnector*>(aWorkshop);
481     XGUI_Displayer* aDisplayer = aConnector->workshop()->displayer();
482     // 3. the flag to disable the update viewer should be set in order to avoid blinking in the 
483     // viewer happens by deselect/select the modified objects. The flag should be restored after
484     // the selection processing. The update viewer should be also called.
485     bool isEnableUpdateViewer = aDisplayer->enableUpdateViewer(false);
486
487     static Events_ID aMoveEvent = Events_Loop::eventByName(EVENT_OBJECT_MOVED);
488     //static Events_ID aUpdateEvent = Events_Loop::eventByName(EVENT_OBJECT_UPDATED);
489     FeatureToSelectionMap::const_iterator anIt = myCurrentSelection.begin(),
490                                           aLast = myCurrentSelection.end();
491     // 4. the features and attributes modification(move)
492     bool isModified = false;
493     for (; anIt != aLast; anIt++) {
494       FeaturePtr aFeature = anIt.key();
495
496       std::set<AttributePtr> anAttributes = anIt.value().first;
497       // Process selection by attribute: the priority to the attribute
498       if (!anAttributes.empty()) {
499         std::set<AttributePtr>::const_iterator anAttIt = anAttributes.begin(),
500                                                anAttLast = anAttributes.end();
501         for (; anAttIt != anAttLast; anAttIt++) {
502           AttributePtr anAttr = *anAttIt;
503           if (anAttr.get() == NULL)
504             continue;
505           std::string aAttrId = anAttr->id();
506           DataPtr aData = aFeature->data();
507           if (aData->isValid()) {
508             std::shared_ptr<GeomDataAPI_Point2D> aPoint = 
509               std::dynamic_pointer_cast<GeomDataAPI_Point2D>(aData->attribute(aAttrId));
510             if (aPoint.get() != NULL) {
511               bool isImmutable = aPoint->setImmutable(true);
512               aPoint->move(dX, dY);
513               isModified = true;
514               ModelAPI_EventCreator::get()->sendUpdated(aFeature, aMoveEvent);
515               aPoint->setImmutable(isImmutable);
516             }
517           }
518         }
519       } else {
520         // Process selection by feature
521         std::shared_ptr<SketchPlugin_Feature> aSketchFeature =
522           std::dynamic_pointer_cast<SketchPlugin_Feature>(aFeature);
523         if (aSketchFeature) {
524           aSketchFeature->move(dX, dY);
525           isModified = true;
526           ModelAPI_EventCreator::get()->sendUpdated(aSketchFeature, aMoveEvent);
527         }
528       }
529     }
530     // the modified state of the current operation should be updated if there are features, which
531     // were changed here
532     if (isModified) {
533       aCurrentOperation->onValuesChanged();
534       ModuleBase_IWorkshop* anIWorkshop = myModule->workshop();
535       XGUI_ModuleConnector* aConnector = dynamic_cast<XGUI_ModuleConnector*>(anIWorkshop);
536       XGUI_Workshop* aWorkshop = aConnector->workshop();
537       aWorkshop->updateCompositeActionState();
538     }
539     Events_Loop::loop()->flush(aMoveEvent); // up all move events - to be processed in the solver
540     //Events_Loop::loop()->flush(aUpdateEvent); // up update events - to redisplay presentations
541
542     // 5. it is necessary to save current selection in order to restore it after the features moving
543     restoreSelection();
544     // 6. restore the update viewer flag and call this update
545     aDisplayer->enableUpdateViewer(isEnableUpdateViewer);
546     aDisplayer->updateViewer();
547
548     myDragDone = true;
549     myCurrentPoint.setValue(aX, aY);
550   }
551 }
552
553 void PartSet_SketcherMgr::onMouseDoubleClick(ModuleBase_IViewWindow* theWnd, QMouseEvent* theEvent)
554 {
555   ModuleBase_Operation* aOperation = getCurrentOperation();
556   if (aOperation && aOperation->isEditOperation()) {
557     std::string aId = aOperation->id().toStdString();
558     if (isDistanceOperation(aOperation))
559     {
560       // Activate dimension value editing on double click
561       ModuleBase_IPropertyPanel* aPanel = aOperation->propertyPanel();
562       QList<ModuleBase_ModelWidget*> aWidgets = aPanel->modelWidgets();
563       // Find corresponded widget to activate value editing
564       foreach (ModuleBase_ModelWidget* aWgt, aWidgets) {
565         if (aWgt->attributeID() == "ConstraintValue") {
566           aWgt->focusTo();
567           return;
568         }
569       }
570     }
571   }
572 }
573
574 void PartSet_SketcherMgr::onApplicationStarted()
575 {
576   ModuleBase_IWorkshop* anIWorkshop = myModule->workshop();
577   XGUI_ModuleConnector* aConnector = dynamic_cast<XGUI_ModuleConnector*>(anIWorkshop);
578   XGUI_Workshop* aWorkshop = aConnector->workshop();
579   XGUI_PropertyPanel* aPropertyPanel = aWorkshop->propertyPanel();
580   if (aPropertyPanel) {
581     connect(aPropertyPanel, SIGNAL(beforeWidgetActivated(ModuleBase_ModelWidget*)),
582             this, SLOT(onBeforeWidgetActivated(ModuleBase_ModelWidget*)));
583   }
584
585   XGUI_ViewerProxy* aViewerProxy = aWorkshop->viewer();
586   connect(aViewerProxy, SIGNAL(enterViewPort()), this, SLOT(onEnterViewPort()));
587   connect(aViewerProxy, SIGNAL(leaveViewPort()), this, SLOT(onLeaveViewPort()));
588
589   XGUI_ContextMenuMgr* aContextMenuMgr = aWorkshop->contextMenuMgr();
590   connect(aContextMenuMgr, SIGNAL(beforeContextMenu()), this, SLOT(onBeforeContextMenu()));
591   connect(aContextMenuMgr, SIGNAL(afterContextMenu()), this, SLOT(onAfterContextMenu()));
592 }
593
594 void PartSet_SketcherMgr::onBeforeWidgetActivated(ModuleBase_ModelWidget* theWidget)
595 {
596   if (!myClickedPoint.myIsInitialized)
597     return;
598
599   ModuleBase_Operation* aOperation = getCurrentOperation();
600   // the distance constraint feature should not use the clickedd point
601   // this is workaround in order to don't throw down the flyout point value,
602   // set by execute() method of these type of features
603   if (isDistanceOperation(aOperation))
604     return;
605
606   PartSet_WidgetPoint2D* aPnt2dWgt = dynamic_cast<PartSet_WidgetPoint2D*>(theWidget);
607   if (aPnt2dWgt) {
608     aPnt2dWgt->setPoint(myClickedPoint.myCurX, myClickedPoint.myCurY);
609   }
610 }
611
612 void PartSet_SketcherMgr::onBeforeContextMenu()
613 {
614   myIsPopupMenuActive = true;
615 }
616
617 void PartSet_SketcherMgr::onAfterContextMenu()
618 {
619   myIsPopupMenuActive = false;
620 }
621
622 void PartSet_SketcherMgr::get2dPoint(ModuleBase_IViewWindow* theWnd, QMouseEvent* theEvent, 
623                                      Point& thePoint)
624 {
625   Handle(V3d_View) aView = theWnd->v3dView();
626   gp_Pnt aPoint = PartSet_Tools::convertClickToPoint(theEvent->pos(), aView);
627   double aX, anY;
628   PartSet_Tools::convertTo2D(aPoint, myCurrentSketch, aView, aX, anY);
629   thePoint.setValue(aX, anY);
630 }
631
632 void PartSet_SketcherMgr::launchEditing()
633 {
634   if (!myCurrentSelection.empty()) {
635     FeaturePtr aFeature = myCurrentSelection.begin().key();
636     std::shared_ptr<SketchPlugin_Feature> aSPFeature = 
637               std::dynamic_pointer_cast<SketchPlugin_Feature>(aFeature);
638     if (aSPFeature) {
639       myModule->editFeature(aSPFeature);
640     }
641   }
642 }
643
644
645 const QStringList& PartSet_SketcherMgr::sketchOperationIdList()
646 {
647   static QStringList aIds;
648   if (aIds.size() == 0) {
649     aIds << SketchPlugin_Line::ID().c_str();
650     aIds << SketchPlugin_Point::ID().c_str();
651     aIds << SketchPlugin_Arc::ID().c_str();
652     aIds << SketchPlugin_Circle::ID().c_str();
653     aIds << SketchPlugin_ConstraintFillet::ID().c_str();
654     aIds.append(constraintsIdList());
655   }
656   return aIds;
657 }
658
659 const QStringList& PartSet_SketcherMgr::constraintsIdList()
660 {
661   static QStringList aIds;
662   if (aIds.size() == 0) {
663     aIds << SketchPlugin_ConstraintLength::ID().c_str();
664     aIds << SketchPlugin_ConstraintDistance::ID().c_str();
665     aIds << SketchPlugin_ConstraintRigid::ID().c_str();
666     aIds << SketchPlugin_ConstraintRadius::ID().c_str();
667     aIds << SketchPlugin_ConstraintPerpendicular::ID().c_str();
668     aIds << SketchPlugin_ConstraintParallel::ID().c_str();
669     aIds << SketchPlugin_ConstraintHorizontal::ID().c_str();
670     aIds << SketchPlugin_ConstraintVertical::ID().c_str();
671     aIds << SketchPlugin_ConstraintEqual::ID().c_str();
672     aIds << SketchPlugin_ConstraintTangent::ID().c_str();
673     aIds << SketchPlugin_ConstraintCoincidence::ID().c_str();
674     aIds << SketchPlugin_ConstraintMirror::ID().c_str();
675     aIds << SketchPlugin_MultiRotation::ID().c_str();
676     aIds << SketchPlugin_MultiTranslation::ID().c_str();
677   }
678   return aIds;
679 }
680
681 void PartSet_SketcherMgr::sketchSelectionModes(QIntList& theModes)
682 {
683   theModes.clear();
684
685   theModes.append(SketcherPrs_Tools::Sel_Dimension_Text);
686   theModes.append(SketcherPrs_Tools::Sel_Dimension_Line);
687   theModes.append(SketcherPrs_Tools::Sel_Constraint);
688   theModes.append(TopAbs_VERTEX);
689   theModes.append(TopAbs_EDGE);
690 }
691
692 bool PartSet_SketcherMgr::isSketchOperation(ModuleBase_Operation* theOperation)
693 {
694   return theOperation && theOperation->id().toStdString() == SketchPlugin_Sketch::ID();
695 }
696
697 bool PartSet_SketcherMgr::isNestedSketchOperation(ModuleBase_Operation* theOperation)
698 {
699   return theOperation &&
700          PartSet_SketcherMgr::sketchOperationIdList().contains(theOperation->id());
701 }
702
703 bool PartSet_SketcherMgr::isNestedCreateOperation(ModuleBase_Operation* theOperation)
704 {
705   return theOperation && !theOperation->isEditOperation() && isNestedSketchOperation(theOperation);
706 }
707
708 bool PartSet_SketcherMgr::isEntity(const std::string& theId)
709 {
710   return (theId == SketchPlugin_Line::ID()) ||
711          (theId == SketchPlugin_Point::ID()) ||
712          (theId == SketchPlugin_Arc::ID()) ||
713          (theId == SketchPlugin_Circle::ID());
714 }
715
716 bool PartSet_SketcherMgr::isDistanceOperation(ModuleBase_Operation* theOperation)
717 {
718   std::string aId = theOperation ? theOperation->id().toStdString() : "";
719
720   return (aId == SketchPlugin_ConstraintLength::ID()) ||
721          (aId == SketchPlugin_ConstraintDistance::ID()) ||
722          (aId == SketchPlugin_ConstraintRadius::ID());
723 }
724
725 void PartSet_SketcherMgr::startSketch(ModuleBase_Operation* theOperation)
726 {
727   myModule->onViewTransformed();
728
729   // Display all sketcher sub-Objects
730   myCurrentSketch = std::dynamic_pointer_cast<ModelAPI_CompositeFeature>(theOperation->feature());
731   XGUI_ModuleConnector* aConnector = dynamic_cast<XGUI_ModuleConnector*>(myModule->workshop());
732
733   // Hide sketcher result
734   std::list<ResultPtr> aResults = myCurrentSketch->results();
735   std::list<ResultPtr>::const_iterator aIt;
736   for (aIt = aResults.begin(); aIt != aResults.end(); ++aIt) {
737     (*aIt)->setDisplayed(false);
738   }
739   myCurrentSketch->setDisplayed(false);
740
741   // Display sketcher objects
742   for (int i = 0; i < myCurrentSketch->numberOfSubs(); i++) {
743     FeaturePtr aFeature = myCurrentSketch->subFeature(i);
744     std::list<ResultPtr> aResults = aFeature->results();
745     std::list<ResultPtr>::const_iterator aIt;
746     for (aIt = aResults.begin(); aIt != aResults.end(); ++aIt) {
747       (*aIt)->setDisplayed(true);
748     }
749     aFeature->setDisplayed(true);
750   }
751
752   if (myPlaneFilter.IsNull()) 
753     myPlaneFilter = new ModuleBase_ShapeInPlaneFilter();
754
755   myModule->workshop()->viewer()->addSelectionFilter(myPlaneFilter);
756
757   bool aHasPlane = false;
758   std::shared_ptr<GeomAPI_Pln> aPln;
759   if (theOperation->isEditOperation()) {
760     // If it is editing of sketch then it means that plane is already defined
761     aPln = PartSet_Tools::sketchPlane(myCurrentSketch);
762     if (aPln.get())
763       aHasPlane = true;
764   }
765   myPlaneFilter->setPlane(aPln);
766
767   Events_Loop::loop()->flush(Events_Loop::eventByName(EVENT_OBJECT_TO_REDISPLAY));
768   // all sketch objects should be activated in the sketch selection modes by edit operation start
769   // in case of creation operation, there is an active widget, which activates own selection mode
770   if (theOperation->isEditOperation() && aHasPlane)
771     aConnector->activateModuleSelectionModes();
772 }
773
774 void PartSet_SketcherMgr::stopSketch(ModuleBase_Operation* theOperation)
775 {
776   myIsMouseOverWindow = false;
777   myIsConstraintsShown = true;
778 #ifdef DEBUG_MOUSE_OVER_WINDOW_FLAGS
779   qDebug(QString("stopSketch: %1").arg(mouseOverWindowFlagsInfo()).toStdString().c_str());
780 #endif
781   XGUI_ModuleConnector* aConnector = dynamic_cast<XGUI_ModuleConnector*>(myModule->workshop());
782
783   DataPtr aData = myCurrentSketch->data();
784   if (!aData->isValid()) {
785     XGUI_Displayer* aDisplayer = aConnector->workshop()->displayer();
786     // The sketch was aborted
787     myCurrentSketch = CompositeFeaturePtr();
788     myModule->workshop()->viewer()->removeSelectionFilter(myPlaneFilter);
789
790     // Erase all sketcher objects
791     QStringList aSketchIds = sketchOperationIdList();
792     QObjectPtrList aObjects = aDisplayer->displayedObjects();
793     foreach (ObjectPtr aObj, aObjects) {
794       DataPtr aObjData = aObj->data();
795       if (!aObjData->isValid())
796         aObj->setDisplayed(false);
797     }
798   }
799   else {
800     // Hide all sketcher sub-Objects
801     for (int i = 0; i < myCurrentSketch->numberOfSubs(); i++) {
802       FeaturePtr aFeature = myCurrentSketch->subFeature(i);
803       std::list<ResultPtr> aResults = aFeature->results();
804       std::list<ResultPtr>::const_iterator aIt;
805       for (aIt = aResults.begin(); aIt != aResults.end(); ++aIt) {
806         (*aIt)->setDisplayed(false);
807       }
808       aFeature->setDisplayed(false);
809     }
810     // Display sketcher result
811     std::list<ResultPtr> aResults = myCurrentSketch->results();
812     std::list<ResultPtr>::const_iterator aIt;
813     Events_Loop* aLoop = Events_Loop::loop();
814     static Events_ID aDispEvent = aLoop->eventByName(EVENT_OBJECT_TO_REDISPLAY);
815     for (aIt = aResults.begin(); aIt != aResults.end(); ++aIt) {
816       (*aIt)->setDisplayed(true);
817       // this display event is needed because sketch already may have "displayed" state,
818       // but not displayed while it is still active (issue 613, abort of existing sketch)
819       ModelAPI_EventCreator::get()->sendUpdated(*aIt, aDispEvent);
820     }
821     myCurrentSketch->setDisplayed(true);
822     
823     myCurrentSketch = CompositeFeaturePtr();
824     myModule->workshop()->viewer()->removeSelectionFilter(myPlaneFilter);
825
826     Events_Loop::loop()->flush(aDispEvent);
827   }
828   // restore the module selection modes, which were changed on startSketch
829   aConnector->activateModuleSelectionModes();
830 }
831
832 void PartSet_SketcherMgr::startNestedSketch(ModuleBase_Operation* theOperation)
833 {
834   if (constraintsIdList().contains(theOperation->id())) {
835     // Show constraints if a constraint was created
836     onShowConstraintsToggle(true);
837   }
838   connectToPropertyPanel(true);
839 }
840
841 void PartSet_SketcherMgr::stopNestedSketch(ModuleBase_Operation* theOp)
842 {
843   connectToPropertyPanel(false);
844   myIsResetCurrentValue = false;
845   myIsMouseOverViewProcessed = true;
846   operationMgr()->onValidateOperation();
847 }
848
849 void PartSet_SketcherMgr::commitNestedSketch(ModuleBase_Operation* theOperation)
850 {
851   if (isNestedCreateOperation(theOperation)) {
852     FeaturePtr aFeature = theOperation->feature();
853     // it is necessary to check the the feature data validity because
854     // some kind of features are removed by an operation commit(the macro state of a feature)
855     if (aFeature.get() && aFeature->data()->isValid()) {
856       visualizeFeature(theOperation, true);
857     }
858   }
859 }
860
861 bool PartSet_SketcherMgr::canUndo() const
862 {
863   return isNestedCreateOperation(getCurrentOperation());
864 }
865
866 bool PartSet_SketcherMgr::canRedo() const
867 {
868   return isNestedCreateOperation(getCurrentOperation());
869 }
870
871 bool PartSet_SketcherMgr::canCommitOperation() const
872 {
873   bool aCanCommit = true;
874
875   if (isNestedCreateOperation(getCurrentOperation()) && myIsResetCurrentValue)
876     aCanCommit = false;
877
878   return aCanCommit;
879 }
880
881 bool PartSet_SketcherMgr::canDisplayObject(const ObjectPtr& theObject) const
882 {
883   bool aCanDisplay = true;
884
885   bool aHasActiveSketch = activeSketch().get() != NULL;
886   if (aHasActiveSketch) {
887     // 1. the sketch feature should not be displayed during the sketch active operation
888     // it is hidden by a sketch operation start and shown by a sketch stop, just the sketch 
889     // nested features can be visualized
890     FeaturePtr aFeature = ModelAPI_Feature::feature(theObject);
891     if (aFeature.get() != NULL && aFeature == activeSketch()) {
892       aCanDisplay = false;
893     }
894   }
895   else { // there are no an active sketch
896     // 2. sketch sub-features should not be visualized if the sketch operation is not active
897     FeaturePtr aFeature = ModelAPI_Feature::feature(theObject);
898     if (aFeature.get() != NULL) {
899       std::shared_ptr<SketchPlugin_Feature> aSketchFeature =
900                               std::dynamic_pointer_cast<SketchPlugin_Feature>(aFeature);
901       if (aSketchFeature.get()) {
902         aCanDisplay = false;
903       }
904     }
905   }
906
907   // 3. the method should not filter the objects, which are not related to the current operation.
908   // The object is filtered just if it is a current operation feature or this feature result
909   bool isObjectFound = false;
910   ModuleBase_Operation* anOperation = getCurrentOperation();
911   if (anOperation) {
912     FeaturePtr aFeature = anOperation->feature();
913     if (aFeature.get()) {
914       std::list<ResultPtr> aResults = aFeature->results();
915       if (theObject == aFeature)
916         isObjectFound = true;
917       else {
918         std::list<ResultPtr>::const_iterator anIt = aResults.begin(), aLast = aResults.end();
919         for (; anIt != aLast; anIt++) {
920           isObjectFound = *anIt == theObject;
921         }
922       }
923     }
924   }
925   if (!isObjectFound) 
926     return aCanDisplay;
927   
928   // 4. For created nested feature operation do not display the created feature if
929   // the mouse curstor leaves the OCC window.
930   // The correction cases, which ignores this condition:
931   // a. the property panel values modification
932   // b. the popup menu activated
933   // c. widget editor control
934   #ifndef DEBUG_DO_NOT_BY_ENTER
935   if (aCanDisplay && isNestedCreateOperation(getCurrentOperation())) {
936     ModuleBase_Operation* aOperation = getCurrentOperation();
937     ModuleBase_IPropertyPanel* aPanel = aOperation->propertyPanel();
938     ModuleBase_ModelWidget* anActiveWdg = aPanel ? aPanel->activeWidget() : 0;
939     ModuleBase_WidgetEditor* anEditorWdg = anActiveWdg ? dynamic_cast<ModuleBase_WidgetEditor*>(anActiveWdg) : 0;
940     // the active widget editor should not influence here. The presentation should be visible always
941     // when this widget is active.
942     if (!anEditorWdg && !myIsPopupMenuActive) {
943       // during a nested create operation, the feature is redisplayed only if the mouse over view
944       // of there was a value modified in the property panel after the mouse left the view
945       aCanDisplay = canDisplayCurrentCreatedFeature();
946     }
947   }
948   #endif
949   return aCanDisplay;
950 }
951
952 bool PartSet_SketcherMgr::canDisplayCurrentCreatedFeature() const
953 {
954   return myIsMouseOverWindow || !myIsResetCurrentValue;
955 #ifdef DEBUG_MOUSE_OVER_WINDOW_FLAGS
956   qDebug(QString("canDisplayCurrentCreatedFeature: %1").arg(mouseOverWindowFlagsInfo()).toStdString().c_str());
957 #endif
958 }
959
960 bool PartSet_SketcherMgr::isObjectOfSketch(const ObjectPtr& theObject) const
961 {
962   bool isFoundObject = false;
963
964   FeaturePtr anObjectFeature = ModelAPI_Feature::feature(theObject);
965   if (anObjectFeature.get()) {
966     int aSize = myCurrentSketch->numberOfSubs();
967     for (int i = 0; i < myCurrentSketch->numberOfSubs() && !isFoundObject; i++) {
968       FeaturePtr aCurrentFeature = myCurrentSketch->subFeature(i);
969       isFoundObject = myCurrentSketch->subFeature(i) == anObjectFeature;
970     }
971   }
972   return isFoundObject;
973 }
974
975 void PartSet_SketcherMgr::onPlaneSelected(const std::shared_ptr<GeomAPI_Pln>& thePln)
976 {
977   if (myPlaneFilter.IsNull()) 
978    myPlaneFilter = new ModuleBase_ShapeInPlaneFilter();
979
980   myPlaneFilter->setPlane(thePln);
981 }
982
983 void PartSet_SketcherMgr::getCurrentSelection(const FeaturePtr& theFeature,
984                                               const FeaturePtr& theSketch,
985                                               ModuleBase_IWorkshop* theWorkshop,
986                                               FeatureToSelectionMap& theSelection)
987 {
988   if (theFeature.get() == NULL)
989     return;
990
991   std::set<AttributePtr> aSelectedAttributes;
992   std::set<ResultPtr> aSelectedResults;
993
994   ModuleBase_IViewer* aViewer = theWorkshop->viewer();
995   Handle(AIS_InteractiveContext) aContext = aViewer->AISContext();
996   XGUI_ModuleConnector* aConnector = dynamic_cast<XGUI_ModuleConnector*>(theWorkshop);
997   XGUI_Displayer* aDisplayer = aConnector->workshop()->displayer();
998
999   std::list<ResultPtr> aResults = theFeature->results();
1000   std::list<ResultPtr>::const_iterator aIt;
1001   for (aIt = aResults.begin(); aIt != aResults.end(); ++aIt)
1002   {
1003     ResultPtr aResult = *aIt;
1004     AISObjectPtr aAISObj = aDisplayer->getAISObject(aResult);
1005     if (aAISObj.get() == NULL)
1006       continue;
1007     Handle(AIS_InteractiveObject) anAISIO = aAISObj->impl<Handle(AIS_InteractiveObject)>();
1008     for (aContext->InitSelected(); aContext->MoreSelected(); aContext->NextSelected())
1009     {
1010       Handle(SelectMgr_EntityOwner) anOwner = aContext->SelectedOwner();
1011       if (anOwner->Selectable() != anAISIO)
1012         continue;
1013       getAttributesOrResults(anOwner, theFeature, theSketch, aResult,
1014                              aSelectedAttributes, aSelectedResults);
1015     }
1016     for (aContext->InitDetected(); aContext->MoreDetected(); aContext->NextDetected()) {
1017       Handle(SelectMgr_EntityOwner) anOwner = aContext->DetectedOwner();
1018       if (anOwner.IsNull())
1019         continue;
1020       if (anOwner->Selectable() != anAISIO)
1021         continue;
1022       getAttributesOrResults(anOwner, theFeature, theSketch, aResult,
1023                              aSelectedAttributes, aSelectedResults);
1024     }
1025   }
1026   theSelection[theFeature] = std::make_pair(aSelectedAttributes, aSelectedResults);
1027 }
1028
1029 void PartSet_SketcherMgr::getSelectionOwners(const FeaturePtr& theFeature,
1030                                              const FeaturePtr& theSketch,
1031                                              ModuleBase_IWorkshop* theWorkshop,
1032                                              const FeatureToSelectionMap& theSelection,
1033                                              SelectMgr_IndexedMapOfOwner& anOwnersToSelect)
1034 {
1035   if (theFeature.get() == NULL)
1036     return;
1037
1038   FeatureToSelectionMap::const_iterator anIt = theSelection.find(theFeature);
1039   std::set<AttributePtr> aSelectedAttributes = anIt.value().first;
1040   std::set<ResultPtr> aSelectedResults = anIt.value().second;
1041
1042   ModuleBase_IViewer* aViewer = theWorkshop->viewer();
1043   Handle(AIS_InteractiveContext) aContext = aViewer->AISContext();
1044   XGUI_ModuleConnector* aConnector = dynamic_cast<XGUI_ModuleConnector*>(theWorkshop);
1045   XGUI_Displayer* aDisplayer = aConnector->workshop()->displayer();
1046
1047   // 1. found the feature's owners. Check the AIS objects of the constructions
1048   AISObjectPtr aAISObj = aDisplayer->getAISObject(theFeature);
1049   if (aAISObj.get() != NULL && aSelectedAttributes.empty() && aSelectedResults.empty()) {
1050     Handle(AIS_InteractiveObject) anAISIO = aAISObj->impl<Handle(AIS_InteractiveObject)>();
1051
1052     SelectMgr_IndexedMapOfOwner aSelectedOwners;
1053     aConnector->workshop()->selector()->selection()->entityOwners(anAISIO, aSelectedOwners);
1054     for  (Standard_Integer i = 1, n = aSelectedOwners.Extent(); i <= n; i++) {
1055       Handle(SelectMgr_EntityOwner) anOwner = aSelectedOwners(i);
1056       if (!anOwner.IsNull())
1057         anOwnersToSelect.Add(anOwner);
1058     }
1059   }
1060
1061   // 2. found the feature results's owners
1062   std::list<ResultPtr> aResults = theFeature->results();
1063   std::list<ResultPtr>::const_iterator aIt;
1064   for (aIt = aResults.begin(); aIt != aResults.end(); ++aIt)
1065   {
1066     ResultPtr aResult = *aIt;
1067     AISObjectPtr aAISObj = aDisplayer->getAISObject(aResult);
1068     if (aAISObj.get() == NULL)
1069       continue; 
1070     Handle(AIS_InteractiveObject) anAISIO = aAISObj->impl<Handle(AIS_InteractiveObject)>();
1071
1072     SelectMgr_IndexedMapOfOwner aSelectedOwners;  
1073     aConnector->workshop()->selector()->selection()->entityOwners(anAISIO, aSelectedOwners);
1074     for  ( Standard_Integer i = 1, n = aSelectedOwners.Extent(); i <= n; i++ ) {
1075       Handle(StdSelect_BRepOwner) anOwner = Handle(StdSelect_BRepOwner)::DownCast(aSelectedOwners(i));
1076       if ( anOwner.IsNull() || !anOwner->HasShape() )
1077         continue;
1078       const TopoDS_Shape& aShape = anOwner->Shape();
1079       TopAbs_ShapeEnum aShapeType = aShape.ShapeType();
1080       if (aShapeType == TopAbs_VERTEX) {
1081         AttributePtr aPntAttr = PartSet_Tools::findAttributeBy2dPoint(theFeature, aShape, theSketch);
1082         if (aPntAttr.get() != NULL &&
1083             aSelectedAttributes.find(aPntAttr) != aSelectedAttributes.end()) {
1084           anOwnersToSelect.Add(anOwner);
1085         }
1086       }
1087       else if (aShapeType == TopAbs_EDGE) {
1088         bool aFound = aSelectedResults.find(aResult) != aSelectedResults.end();
1089         if (aSelectedResults.find(aResult) != aSelectedResults.end() &&
1090             anOwnersToSelect.FindIndex(anOwner) <= 0)
1091           anOwnersToSelect.Add(anOwner);
1092       }
1093     }
1094   }
1095 }
1096
1097 void PartSet_SketcherMgr::connectToPropertyPanel(const bool isToConnect)
1098 {
1099   ModuleBase_IWorkshop* anIWorkshop = myModule->workshop();
1100   XGUI_ModuleConnector* aConnector = dynamic_cast<XGUI_ModuleConnector*>(anIWorkshop);
1101   XGUI_Workshop* aWorkshop = aConnector->workshop();
1102   XGUI_PropertyPanel* aPropertyPanel = aWorkshop->propertyPanel();
1103   if (aPropertyPanel) {
1104     const QList<ModuleBase_ModelWidget*>& aWidgets = aPropertyPanel->modelWidgets();
1105     foreach (ModuleBase_ModelWidget* aWidget, aWidgets) {
1106       if (isToConnect) {
1107         connect(aWidget, SIGNAL(beforeValuesChanged()),
1108                 this, SLOT(onBeforeValuesChangedInPropertyPanel()));
1109         connect(aWidget, SIGNAL(valuesChanged()), this, SLOT(onValuesChangedInPropertyPanel()));
1110         connect(aWidget, SIGNAL(afterValuesChanged()),
1111                 this, SLOT(onAfterValuesChangedInPropertyPanel()));
1112       }
1113       else {
1114         disconnect(aWidget, SIGNAL(beforeValuesChanged()),
1115                    this, SLOT(onBeforeValuesChangedInPropertyPanel()));
1116         disconnect(aWidget, SIGNAL(valuesChanged()), this, SLOT(onValuesChangedInPropertyPanel()));
1117         disconnect(aWidget, SIGNAL(afterValuesChanged()),
1118                    this, SLOT(onAfterValuesChangedInPropertyPanel()));
1119       }
1120     }
1121   }
1122 }
1123
1124 ModuleBase_Operation* PartSet_SketcherMgr::getCurrentOperation() const
1125 {
1126   return myModule->workshop()->currentOperation();
1127 }
1128
1129 void PartSet_SketcherMgr::visualizeFeature(ModuleBase_Operation* theOperation,
1130                                            const bool isToDisplay,
1131                                            const bool isFlushRedisplay)
1132 {
1133   #ifdef DEBUG_DO_NOT_BY_ENTER
1134   return;
1135   #endif
1136
1137   if (!theOperation || theOperation->isEditOperation())
1138     return;
1139
1140   ModuleBase_IWorkshop* aWorkshop = myModule->workshop();
1141   XGUI_ModuleConnector* aConnector = dynamic_cast<XGUI_ModuleConnector*>(aWorkshop);
1142
1143   // 1. change visibility of the object itself, here the presentable object is processed,
1144   // e.g. constraints features
1145   FeaturePtr aFeature = theOperation->feature();
1146   std::list<ResultPtr> aResults = aFeature->results();
1147   if (isToDisplay)
1148     aFeature->setDisplayed(true);
1149   else
1150     aFeature->setDisplayed(false);
1151
1152   // change visibility of the object results, e.g. non-constraint features
1153   std::list<ResultPtr>::const_iterator aIt;
1154   for (aIt = aResults.begin(); aIt != aResults.end(); ++aIt) {
1155     if (isToDisplay) {
1156       (*aIt)->setDisplayed(true);
1157     }
1158     else {
1159       (*aIt)->setDisplayed(false);
1160     }
1161   }
1162   if (isFlushRedisplay)
1163     Events_Loop::loop()->flush(Events_Loop::eventByName(EVENT_OBJECT_TO_REDISPLAY));
1164 }
1165
1166 void PartSet_SketcherMgr::storeSelection(const bool theHighlightedOnly)
1167 {
1168   ModuleBase_IWorkshop* aWorkshop = myModule->workshop();
1169   ModuleBase_ISelection* aSelect = aWorkshop->selection();
1170   QList<ModuleBase_ViewerPrs> aHighlighted = aSelect->getHighlighted();
1171
1172   QList<FeaturePtr> aFeatureList;
1173   if (theHighlightedOnly) {
1174     fillFeatureList(aHighlighted, myCurrentSketch, aFeatureList);
1175   }
1176   else {
1177     fillFeatureList(aHighlighted, myCurrentSketch, aFeatureList);
1178
1179     QList<ModuleBase_ViewerPrs> aSelected = aSelect->getSelected(ModuleBase_ISelection::AllControls);
1180     fillFeatureList(aSelected, myCurrentSketch, aFeatureList);
1181   }
1182
1183   // 1. it is necessary to save current selection in order to restore it after the features moving
1184   myCurrentSelection.clear();
1185   QList<FeaturePtr>::const_iterator anIt = aFeatureList.begin(), aLast = aFeatureList.end();
1186   for (; anIt != aLast; anIt++) {
1187     getCurrentSelection(*anIt, myCurrentSketch, aWorkshop, myCurrentSelection);
1188   }
1189   //qDebug(QString("  storeSelection: %1").arg(myCurrentSelection.size()).toStdString().c_str());
1190 }
1191
1192 void PartSet_SketcherMgr::restoreSelection()
1193 {
1194   //qDebug(QString("restoreSelection: %1").arg(myCurrentSelection.size()).toStdString().c_str());
1195   ModuleBase_IWorkshop* aWorkshop = myModule->workshop();
1196   XGUI_ModuleConnector* aConnector = dynamic_cast<XGUI_ModuleConnector*>(aWorkshop);
1197   FeatureToSelectionMap::const_iterator aSIt = myCurrentSelection.begin(),
1198                                         aSLast = myCurrentSelection.end();
1199   SelectMgr_IndexedMapOfOwner anOwnersToSelect;
1200   for (; aSIt != aSLast; aSIt++) {
1201     anOwnersToSelect.Clear();
1202     getSelectionOwners(aSIt.key(), myCurrentSketch, aWorkshop, myCurrentSelection,
1203                         anOwnersToSelect);
1204     aConnector->workshop()->selector()->setSelectedOwners(anOwnersToSelect, false);
1205   }
1206 }
1207
1208 void PartSet_SketcherMgr::onShowConstraintsToggle(bool theOn)
1209 {
1210   if (myIsConstraintsShown == theOn)
1211     return;
1212   if (myCurrentSketch.get() == NULL)
1213     return;
1214
1215   myIsConstraintsShown = theOn;
1216
1217   ModuleBase_IWorkshop* aWorkshop = myModule->workshop();
1218   XGUI_ModuleConnector* aConnector = dynamic_cast<XGUI_ModuleConnector*>(aWorkshop);
1219
1220   const QStringList& aConstrIds = constraintsIdList();
1221   for (int i = 0; i < myCurrentSketch->numberOfSubs(); i++) {
1222     FeaturePtr aSubFeature = myCurrentSketch->subFeature(i);
1223     if (aConstrIds.contains(QString(aSubFeature->getKind().c_str()))) {
1224       if (myIsConstraintsShown) 
1225         aSubFeature->setDisplayed(true);
1226       else
1227         aSubFeature->setDisplayed(false);
1228     }
1229   }
1230   Events_Loop::loop()->flush(Events_Loop::eventByName(EVENT_OBJECT_TO_REDISPLAY));
1231 }
1232
1233 QString PartSet_SketcherMgr::mouseOverWindowFlagsInfo() const
1234 {
1235   return QString("myIsResetCurrentValue = %1,    myIsMouseOverWindow = %2")
1236      .arg(myIsResetCurrentValue).arg(myIsMouseOverWindow);
1237 }
1238
1239 XGUI_OperationMgr* PartSet_SketcherMgr::operationMgr() const
1240 {
1241   ModuleBase_IWorkshop* anIWorkshop = myModule->workshop();
1242   XGUI_ModuleConnector* aConnector = dynamic_cast<XGUI_ModuleConnector*>(anIWorkshop);
1243   XGUI_Workshop* aWorkshop = aConnector->workshop();
1244
1245   return aWorkshop->operationMgr();
1246 }