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