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