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