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