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