Salome HOME
The external feature should be executed manually in order to return first result.
[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       ModuleBase_IWorkshop* anIWorkshop = myModule->workshop();
536       XGUI_ModuleConnector* aConnector = dynamic_cast<XGUI_ModuleConnector*>(anIWorkshop);
537       XGUI_Workshop* aWorkshop = aConnector->workshop();
538       aWorkshop->updateCompositeActionState();
539     }
540     Events_Loop::loop()->flush(aMoveEvent); // up all move events - to be processed in the solver
541     //Events_Loop::loop()->flush(aUpdateEvent); // up update events - to redisplay presentations
542
543     // 5. it is necessary to save current selection in order to restore it after the features moving
544     restoreSelection();
545     // 6. restore the update viewer flag and call this update
546     aDisplayer->enableUpdateViewer(isEnableUpdateViewer);
547     aDisplayer->updateViewer();
548
549     myDragDone = true;
550     myCurrentPoint.setValue(aX, aY);
551   }
552 }
553
554 void PartSet_SketcherMgr::onMouseDoubleClick(ModuleBase_IViewWindow* theWnd, QMouseEvent* theEvent)
555 {
556   ModuleBase_Operation* aOperation = getCurrentOperation();
557   if (aOperation && aOperation->isEditOperation()) {
558     std::string aId = aOperation->id().toStdString();
559     if (isDistanceOperation(aOperation))
560     {
561       // Activate dimension value editing on double click
562       ModuleBase_IPropertyPanel* aPanel = aOperation->propertyPanel();
563       QList<ModuleBase_ModelWidget*> aWidgets = aPanel->modelWidgets();
564       // Find corresponded widget to activate value editing
565       foreach (ModuleBase_ModelWidget* aWgt, aWidgets) {
566         if (aWgt->attributeID() == "ConstraintValue") {
567           aWgt->focusTo();
568           return;
569         }
570       }
571     }
572   }
573 }
574
575 void PartSet_SketcherMgr::onApplicationStarted()
576 {
577   ModuleBase_IWorkshop* anIWorkshop = myModule->workshop();
578   XGUI_ModuleConnector* aConnector = dynamic_cast<XGUI_ModuleConnector*>(anIWorkshop);
579   XGUI_Workshop* aWorkshop = aConnector->workshop();
580   XGUI_PropertyPanel* aPropertyPanel = aWorkshop->propertyPanel();
581   if (aPropertyPanel) {
582     connect(aPropertyPanel, SIGNAL(beforeWidgetActivated(ModuleBase_ModelWidget*)),
583             this, SLOT(onBeforeWidgetActivated(ModuleBase_ModelWidget*)));
584   }
585
586   XGUI_ViewerProxy* aViewerProxy = aWorkshop->viewer();
587   connect(aViewerProxy, SIGNAL(enterViewPort()), this, SLOT(onEnterViewPort()));
588   connect(aViewerProxy, SIGNAL(leaveViewPort()), this, SLOT(onLeaveViewPort()));
589
590   XGUI_ContextMenuMgr* aContextMenuMgr = aWorkshop->contextMenuMgr();
591   connect(aContextMenuMgr, SIGNAL(beforeContextMenu()), this, SLOT(onBeforeContextMenu()));
592   connect(aContextMenuMgr, SIGNAL(afterContextMenu()), this, SLOT(onAfterContextMenu()));
593 }
594
595 void PartSet_SketcherMgr::onBeforeWidgetActivated(ModuleBase_ModelWidget* theWidget)
596 {
597   if (!myClickedPoint.myIsInitialized)
598     return;
599
600   ModuleBase_Operation* aOperation = getCurrentOperation();
601   // the distance constraint feature should not use the clickedd point
602   // this is workaround in order to don't throw down the flyout point value,
603   // set by execute() method of these type of features
604   if (isDistanceOperation(aOperation))
605     return;
606
607   PartSet_WidgetPoint2D* aPnt2dWgt = dynamic_cast<PartSet_WidgetPoint2D*>(theWidget);
608   if (aPnt2dWgt) {
609     aPnt2dWgt->setPoint(myClickedPoint.myCurX, myClickedPoint.myCurY);
610   }
611 }
612
613 void PartSet_SketcherMgr::onBeforeContextMenu()
614 {
615   myIsPopupMenuActive = true;
616 }
617
618 void PartSet_SketcherMgr::onAfterContextMenu()
619 {
620   myIsPopupMenuActive = false;
621 }
622
623 void PartSet_SketcherMgr::get2dPoint(ModuleBase_IViewWindow* theWnd, QMouseEvent* theEvent, 
624                                      Point& thePoint)
625 {
626   Handle(V3d_View) aView = theWnd->v3dView();
627   gp_Pnt aPoint = PartSet_Tools::convertClickToPoint(theEvent->pos(), aView);
628   double aX, anY;
629   PartSet_Tools::convertTo2D(aPoint, myCurrentSketch, aView, aX, anY);
630   thePoint.setValue(aX, anY);
631 }
632
633 void PartSet_SketcherMgr::launchEditing()
634 {
635   if (!myCurrentSelection.empty()) {
636     FeaturePtr aFeature = myCurrentSelection.begin().key();
637     std::shared_ptr<SketchPlugin_Feature> aSPFeature = 
638               std::dynamic_pointer_cast<SketchPlugin_Feature>(aFeature);
639     if (aSPFeature) {
640       myModule->editFeature(aSPFeature);
641     }
642   }
643 }
644
645
646 const QStringList& PartSet_SketcherMgr::sketchOperationIdList()
647 {
648   static QStringList aIds;
649   if (aIds.size() == 0) {
650     aIds << SketchPlugin_Line::ID().c_str();
651     aIds << SketchPlugin_Point::ID().c_str();
652     aIds << SketchPlugin_Arc::ID().c_str();
653     aIds << SketchPlugin_Circle::ID().c_str();
654     aIds << SketchPlugin_ConstraintFillet::ID().c_str();
655     aIds.append(constraintsIdList());
656   }
657   return aIds;
658 }
659
660 const QStringList& PartSet_SketcherMgr::constraintsIdList()
661 {
662   static QStringList aIds;
663   if (aIds.size() == 0) {
664     aIds << SketchPlugin_ConstraintLength::ID().c_str();
665     aIds << SketchPlugin_ConstraintDistance::ID().c_str();
666     aIds << SketchPlugin_ConstraintRigid::ID().c_str();
667     aIds << SketchPlugin_ConstraintRadius::ID().c_str();
668     aIds << SketchPlugin_ConstraintPerpendicular::ID().c_str();
669     aIds << SketchPlugin_ConstraintParallel::ID().c_str();
670     aIds << SketchPlugin_ConstraintHorizontal::ID().c_str();
671     aIds << SketchPlugin_ConstraintVertical::ID().c_str();
672     aIds << SketchPlugin_ConstraintEqual::ID().c_str();
673     aIds << SketchPlugin_ConstraintTangent::ID().c_str();
674     aIds << SketchPlugin_ConstraintCoincidence::ID().c_str();
675     aIds << SketchPlugin_ConstraintMirror::ID().c_str();
676     aIds << SketchPlugin_ConstraintAngle::ID().c_str();
677     aIds << SketchPlugin_MultiRotation::ID().c_str();
678     aIds << SketchPlugin_MultiTranslation::ID().c_str();
679   }
680   return aIds;
681 }
682
683 void PartSet_SketcherMgr::sketchSelectionModes(QIntList& theModes)
684 {
685   theModes.clear();
686
687   theModes.append(SketcherPrs_Tools::Sel_Dimension_Text);
688   theModes.append(SketcherPrs_Tools::Sel_Dimension_Line);
689   theModes.append(SketcherPrs_Tools::Sel_Constraint);
690   theModes.append(TopAbs_VERTEX);
691   theModes.append(TopAbs_EDGE);
692 }
693
694 bool PartSet_SketcherMgr::isSketchOperation(ModuleBase_Operation* theOperation)
695 {
696   return theOperation && theOperation->id().toStdString() == SketchPlugin_Sketch::ID();
697 }
698
699 bool PartSet_SketcherMgr::isNestedSketchOperation(ModuleBase_Operation* theOperation)
700 {
701   return theOperation &&
702          PartSet_SketcherMgr::sketchOperationIdList().contains(theOperation->id());
703 }
704
705 bool PartSet_SketcherMgr::isNestedCreateOperation(ModuleBase_Operation* theOperation)
706 {
707   return theOperation && !theOperation->isEditOperation() && isNestedSketchOperation(theOperation);
708 }
709
710 bool PartSet_SketcherMgr::isEntity(const std::string& theId)
711 {
712   return (theId == SketchPlugin_Line::ID()) ||
713          (theId == SketchPlugin_Point::ID()) ||
714          (theId == SketchPlugin_Arc::ID()) ||
715          (theId == SketchPlugin_Circle::ID());
716 }
717
718 bool PartSet_SketcherMgr::isDistanceOperation(ModuleBase_Operation* theOperation)
719 {
720   std::string aId = theOperation ? theOperation->id().toStdString() : "";
721
722   return (aId == SketchPlugin_ConstraintLength::ID()) ||
723          (aId == SketchPlugin_ConstraintDistance::ID()) ||
724          (aId == SketchPlugin_ConstraintRadius::ID()) ||
725          (aId == SketchPlugin_ConstraintAngle::ID());
726 }
727
728 void PartSet_SketcherMgr::startSketch(ModuleBase_Operation* theOperation)
729 {
730   myModule->onViewTransformed();
731
732   // Display all sketcher sub-Objects
733   myCurrentSketch = std::dynamic_pointer_cast<ModelAPI_CompositeFeature>(theOperation->feature());
734   XGUI_ModuleConnector* aConnector = dynamic_cast<XGUI_ModuleConnector*>(myModule->workshop());
735
736   // Hide sketcher result
737   std::list<ResultPtr> aResults = myCurrentSketch->results();
738   std::list<ResultPtr>::const_iterator aIt;
739   for (aIt = aResults.begin(); aIt != aResults.end(); ++aIt) {
740     (*aIt)->setDisplayed(false);
741   }
742   myCurrentSketch->setDisplayed(false);
743
744   // Display sketcher objects
745   for (int i = 0; i < myCurrentSketch->numberOfSubs(); i++) {
746     FeaturePtr aFeature = myCurrentSketch->subFeature(i);
747     std::list<ResultPtr> aResults = aFeature->results();
748     std::list<ResultPtr>::const_iterator aIt;
749     for (aIt = aResults.begin(); aIt != aResults.end(); ++aIt) {
750       (*aIt)->setDisplayed(true);
751     }
752     aFeature->setDisplayed(true);
753   }
754
755   if (myPlaneFilter.IsNull()) 
756     myPlaneFilter = new ModuleBase_ShapeInPlaneFilter();
757
758   myModule->workshop()->viewer()->addSelectionFilter(myPlaneFilter);
759
760   bool aHasPlane = false;
761   std::shared_ptr<GeomAPI_Pln> aPln;
762   if (theOperation->isEditOperation()) {
763     // If it is editing of sketch then it means that plane is already defined
764     aPln = PartSet_Tools::sketchPlane(myCurrentSketch);
765     if (aPln.get())
766       aHasPlane = true;
767   }
768   myPlaneFilter->setPlane(aPln);
769
770   Events_Loop::loop()->flush(Events_Loop::eventByName(EVENT_OBJECT_TO_REDISPLAY));
771   // all sketch objects should be activated in the sketch selection modes by edit operation start
772   // in case of creation operation, there is an active widget, which activates own selection mode
773   if (theOperation->isEditOperation() && aHasPlane)
774     aConnector->activateModuleSelectionModes();
775 }
776
777 void PartSet_SketcherMgr::stopSketch(ModuleBase_Operation* theOperation)
778 {
779   myIsMouseOverWindow = false;
780   myIsConstraintsShown = true;
781 #ifdef DEBUG_MOUSE_OVER_WINDOW_FLAGS
782   qDebug(QString("stopSketch: %1").arg(mouseOverWindowFlagsInfo()).toStdString().c_str());
783 #endif
784   XGUI_ModuleConnector* aConnector = dynamic_cast<XGUI_ModuleConnector*>(myModule->workshop());
785
786   DataPtr aData = myCurrentSketch->data();
787   if (!aData->isValid()) {
788     XGUI_Displayer* aDisplayer = aConnector->workshop()->displayer();
789     // The sketch was aborted
790     myCurrentSketch = CompositeFeaturePtr();
791     myModule->workshop()->viewer()->removeSelectionFilter(myPlaneFilter);
792
793     // Erase all sketcher objects
794     QStringList aSketchIds = sketchOperationIdList();
795     QObjectPtrList aObjects = aDisplayer->displayedObjects();
796     foreach (ObjectPtr aObj, aObjects) {
797       DataPtr aObjData = aObj->data();
798       if (!aObjData->isValid())
799         aObj->setDisplayed(false);
800     }
801   }
802   else {
803     // Hide all sketcher sub-Objects
804     for (int i = 0; i < myCurrentSketch->numberOfSubs(); i++) {
805       FeaturePtr aFeature = myCurrentSketch->subFeature(i);
806       std::list<ResultPtr> aResults = aFeature->results();
807       std::list<ResultPtr>::const_iterator aIt;
808       for (aIt = aResults.begin(); aIt != aResults.end(); ++aIt) {
809         (*aIt)->setDisplayed(false);
810       }
811       aFeature->setDisplayed(false);
812     }
813     // Display sketcher result
814     std::list<ResultPtr> aResults = myCurrentSketch->results();
815     std::list<ResultPtr>::const_iterator aIt;
816     Events_Loop* aLoop = Events_Loop::loop();
817     static Events_ID aDispEvent = aLoop->eventByName(EVENT_OBJECT_TO_REDISPLAY);
818     for (aIt = aResults.begin(); aIt != aResults.end(); ++aIt) {
819       (*aIt)->setDisplayed(true);
820       // this display event is needed because sketch already may have "displayed" state,
821       // but not displayed while it is still active (issue 613, abort of existing sketch)
822       ModelAPI_EventCreator::get()->sendUpdated(*aIt, aDispEvent);
823     }
824     myCurrentSketch->setDisplayed(true);
825     
826     myCurrentSketch = CompositeFeaturePtr();
827     myModule->workshop()->viewer()->removeSelectionFilter(myPlaneFilter);
828
829     Events_Loop::loop()->flush(aDispEvent);
830   }
831   // restore the module selection modes, which were changed on startSketch
832   aConnector->activateModuleSelectionModes();
833 }
834
835 void PartSet_SketcherMgr::startNestedSketch(ModuleBase_Operation* theOperation)
836 {
837   if (constraintsIdList().contains(theOperation->id())) {
838     // Show constraints if a constraint was created
839     onShowConstraintsToggle(true);
840   }
841   connectToPropertyPanel(true);
842 }
843
844 void PartSet_SketcherMgr::stopNestedSketch(ModuleBase_Operation* theOp)
845 {
846   connectToPropertyPanel(false);
847   myIsResetCurrentValue = false;
848   myIsMouseOverViewProcessed = true;
849   operationMgr()->onValidateOperation();
850 }
851
852 void PartSet_SketcherMgr::commitNestedSketch(ModuleBase_Operation* theOperation)
853 {
854   if (isNestedCreateOperation(theOperation)) {
855     FeaturePtr aFeature = theOperation->feature();
856     // it is necessary to check the the feature data validity because
857     // some kind of features are removed by an operation commit(the macro state of a feature)
858     if (aFeature.get() && aFeature->data()->isValid()) {
859       visualizeFeature(theOperation, true);
860     }
861   }
862 }
863
864 bool PartSet_SketcherMgr::canUndo() const
865 {
866   return isNestedCreateOperation(getCurrentOperation());
867 }
868
869 bool PartSet_SketcherMgr::canRedo() const
870 {
871   return isNestedCreateOperation(getCurrentOperation());
872 }
873
874 bool PartSet_SketcherMgr::canCommitOperation() const
875 {
876   bool aCanCommit = true;
877
878   if (isNestedCreateOperation(getCurrentOperation()) && myIsResetCurrentValue)
879     aCanCommit = false;
880
881   return aCanCommit;
882 }
883
884 bool PartSet_SketcherMgr::canDisplayObject(const ObjectPtr& theObject) const
885 {
886   bool aCanDisplay = true;
887
888   bool aHasActiveSketch = activeSketch().get() != NULL;
889   if (aHasActiveSketch) {
890     // 1. the sketch feature should not be displayed during the sketch active operation
891     // it is hidden by a sketch operation start and shown by a sketch stop, just the sketch 
892     // nested features can be visualized
893     FeaturePtr aFeature = ModelAPI_Feature::feature(theObject);
894     if (aFeature.get() != NULL && aFeature == activeSketch()) {
895       aCanDisplay = false;
896     }
897   }
898   else { // there are no an active sketch
899     // 2. sketch sub-features should not be visualized if the sketch operation is not active
900     FeaturePtr aFeature = ModelAPI_Feature::feature(theObject);
901     if (aFeature.get() != NULL) {
902       std::shared_ptr<SketchPlugin_Feature> aSketchFeature =
903                               std::dynamic_pointer_cast<SketchPlugin_Feature>(aFeature);
904       if (aSketchFeature.get()) {
905         aCanDisplay = false;
906       }
907     }
908   }
909
910   // 3. the method should not filter the objects, which are not related to the current operation.
911   // The object is filtered just if it is a current operation feature or this feature result
912   bool isObjectFound = false;
913   ModuleBase_Operation* anOperation = getCurrentOperation();
914   if (anOperation) {
915     FeaturePtr aFeature = anOperation->feature();
916     if (aFeature.get()) {
917       std::list<ResultPtr> aResults = aFeature->results();
918       if (theObject == aFeature)
919         isObjectFound = true;
920       else {
921         std::list<ResultPtr>::const_iterator anIt = aResults.begin(), aLast = aResults.end();
922         for (; anIt != aLast; anIt++) {
923           isObjectFound = *anIt == theObject;
924         }
925       }
926     }
927   }
928   if (!isObjectFound) 
929     return aCanDisplay;
930   
931   // 4. For created nested feature operation do not display the created feature if
932   // the mouse curstor leaves the OCC window.
933   // The correction cases, which ignores this condition:
934   // a. the property panel values modification
935   // b. the popup menu activated
936   // c. widget editor control
937   #ifndef DEBUG_DO_NOT_BY_ENTER
938   if (aCanDisplay && isNestedCreateOperation(getCurrentOperation())) {
939     ModuleBase_Operation* aOperation = getCurrentOperation();
940     ModuleBase_IPropertyPanel* aPanel = aOperation->propertyPanel();
941     ModuleBase_ModelWidget* anActiveWdg = aPanel ? aPanel->activeWidget() : 0;
942     ModuleBase_WidgetEditor* anEditorWdg = anActiveWdg ? dynamic_cast<ModuleBase_WidgetEditor*>(anActiveWdg) : 0;
943     // the active widget editor should not influence here. The presentation should be visible always
944     // when this widget is active.
945     if (!anEditorWdg && !myIsPopupMenuActive) {
946       // during a nested create operation, the feature is redisplayed only if the mouse over view
947       // of there was a value modified in the property panel after the mouse left the view
948       aCanDisplay = canDisplayCurrentCreatedFeature();
949     }
950   }
951   #endif
952   return aCanDisplay;
953 }
954
955 bool PartSet_SketcherMgr::canDisplayCurrentCreatedFeature() const
956 {
957   return myIsMouseOverWindow || !myIsResetCurrentValue;
958 #ifdef DEBUG_MOUSE_OVER_WINDOW_FLAGS
959   qDebug(QString("canDisplayCurrentCreatedFeature: %1").arg(mouseOverWindowFlagsInfo()).toStdString().c_str());
960 #endif
961 }
962
963 bool PartSet_SketcherMgr::isObjectOfSketch(const ObjectPtr& theObject) const
964 {
965   bool isFoundObject = false;
966
967   FeaturePtr anObjectFeature = ModelAPI_Feature::feature(theObject);
968   if (anObjectFeature.get()) {
969     int aSize = myCurrentSketch->numberOfSubs();
970     for (int i = 0; i < myCurrentSketch->numberOfSubs() && !isFoundObject; i++) {
971       FeaturePtr aCurrentFeature = myCurrentSketch->subFeature(i);
972       isFoundObject = myCurrentSketch->subFeature(i) == anObjectFeature;
973     }
974   }
975   return isFoundObject;
976 }
977
978 void PartSet_SketcherMgr::onPlaneSelected(const std::shared_ptr<GeomAPI_Pln>& thePln)
979 {
980   if (myPlaneFilter.IsNull()) 
981    myPlaneFilter = new ModuleBase_ShapeInPlaneFilter();
982
983   myPlaneFilter->setPlane(thePln);
984 }
985
986 void PartSet_SketcherMgr::getCurrentSelection(const FeaturePtr& theFeature,
987                                               const FeaturePtr& theSketch,
988                                               ModuleBase_IWorkshop* theWorkshop,
989                                               FeatureToSelectionMap& theSelection)
990 {
991   if (theFeature.get() == NULL)
992     return;
993
994   std::set<AttributePtr> aSelectedAttributes;
995   std::set<ResultPtr> aSelectedResults;
996
997   ModuleBase_IViewer* aViewer = theWorkshop->viewer();
998   Handle(AIS_InteractiveContext) aContext = aViewer->AISContext();
999   XGUI_ModuleConnector* aConnector = dynamic_cast<XGUI_ModuleConnector*>(theWorkshop);
1000   XGUI_Displayer* aDisplayer = aConnector->workshop()->displayer();
1001
1002   std::list<ResultPtr> aResults = theFeature->results();
1003   std::list<ResultPtr>::const_iterator aIt;
1004   for (aIt = aResults.begin(); aIt != aResults.end(); ++aIt)
1005   {
1006     ResultPtr aResult = *aIt;
1007     AISObjectPtr aAISObj = aDisplayer->getAISObject(aResult);
1008     if (aAISObj.get() == NULL)
1009       continue;
1010     Handle(AIS_InteractiveObject) anAISIO = aAISObj->impl<Handle(AIS_InteractiveObject)>();
1011     for (aContext->InitSelected(); aContext->MoreSelected(); aContext->NextSelected())
1012     {
1013       Handle(SelectMgr_EntityOwner) anOwner = aContext->SelectedOwner();
1014       if (anOwner->Selectable() != anAISIO)
1015         continue;
1016       getAttributesOrResults(anOwner, theFeature, theSketch, aResult,
1017                              aSelectedAttributes, aSelectedResults);
1018     }
1019     for (aContext->InitDetected(); aContext->MoreDetected(); aContext->NextDetected()) {
1020       Handle(SelectMgr_EntityOwner) anOwner = aContext->DetectedOwner();
1021       if (anOwner.IsNull())
1022         continue;
1023       if (anOwner->Selectable() != anAISIO)
1024         continue;
1025       getAttributesOrResults(anOwner, theFeature, theSketch, aResult,
1026                              aSelectedAttributes, aSelectedResults);
1027     }
1028   }
1029   theSelection[theFeature] = std::make_pair(aSelectedAttributes, aSelectedResults);
1030 }
1031
1032 void PartSet_SketcherMgr::getSelectionOwners(const FeaturePtr& theFeature,
1033                                              const FeaturePtr& theSketch,
1034                                              ModuleBase_IWorkshop* theWorkshop,
1035                                              const FeatureToSelectionMap& theSelection,
1036                                              SelectMgr_IndexedMapOfOwner& anOwnersToSelect)
1037 {
1038   if (theFeature.get() == NULL)
1039     return;
1040
1041   FeatureToSelectionMap::const_iterator anIt = theSelection.find(theFeature);
1042   std::set<AttributePtr> aSelectedAttributes = anIt.value().first;
1043   std::set<ResultPtr> aSelectedResults = anIt.value().second;
1044
1045   ModuleBase_IViewer* aViewer = theWorkshop->viewer();
1046   Handle(AIS_InteractiveContext) aContext = aViewer->AISContext();
1047   XGUI_ModuleConnector* aConnector = dynamic_cast<XGUI_ModuleConnector*>(theWorkshop);
1048   XGUI_Displayer* aDisplayer = aConnector->workshop()->displayer();
1049
1050   // 1. found the feature's owners. Check the AIS objects of the constructions
1051   AISObjectPtr aAISObj = aDisplayer->getAISObject(theFeature);
1052   if (aAISObj.get() != NULL && aSelectedAttributes.empty() && aSelectedResults.empty()) {
1053     Handle(AIS_InteractiveObject) anAISIO = aAISObj->impl<Handle(AIS_InteractiveObject)>();
1054
1055     SelectMgr_IndexedMapOfOwner aSelectedOwners;
1056     aConnector->workshop()->selector()->selection()->entityOwners(anAISIO, aSelectedOwners);
1057     for  (Standard_Integer i = 1, n = aSelectedOwners.Extent(); i <= n; i++) {
1058       Handle(SelectMgr_EntityOwner) anOwner = aSelectedOwners(i);
1059       if (!anOwner.IsNull())
1060         anOwnersToSelect.Add(anOwner);
1061     }
1062   }
1063
1064   // 2. found the feature results's owners
1065   std::list<ResultPtr> aResults = theFeature->results();
1066   std::list<ResultPtr>::const_iterator aIt;
1067   for (aIt = aResults.begin(); aIt != aResults.end(); ++aIt)
1068   {
1069     ResultPtr aResult = *aIt;
1070     AISObjectPtr aAISObj = aDisplayer->getAISObject(aResult);
1071     if (aAISObj.get() == NULL)
1072       continue; 
1073     Handle(AIS_InteractiveObject) anAISIO = aAISObj->impl<Handle(AIS_InteractiveObject)>();
1074
1075     SelectMgr_IndexedMapOfOwner aSelectedOwners;  
1076     aConnector->workshop()->selector()->selection()->entityOwners(anAISIO, aSelectedOwners);
1077     for  ( Standard_Integer i = 1, n = aSelectedOwners.Extent(); i <= n; i++ ) {
1078       Handle(StdSelect_BRepOwner) anOwner = Handle(StdSelect_BRepOwner)::DownCast(aSelectedOwners(i));
1079       if ( anOwner.IsNull() || !anOwner->HasShape() )
1080         continue;
1081       const TopoDS_Shape& aShape = anOwner->Shape();
1082       TopAbs_ShapeEnum aShapeType = aShape.ShapeType();
1083       if (aShapeType == TopAbs_VERTEX) {
1084         AttributePtr aPntAttr = PartSet_Tools::findAttributeBy2dPoint(theFeature, aShape, theSketch);
1085         if (aPntAttr.get() != NULL &&
1086             aSelectedAttributes.find(aPntAttr) != aSelectedAttributes.end()) {
1087           anOwnersToSelect.Add(anOwner);
1088         }
1089       }
1090       else if (aShapeType == TopAbs_EDGE) {
1091         bool aFound = aSelectedResults.find(aResult) != aSelectedResults.end();
1092         if (aSelectedResults.find(aResult) != aSelectedResults.end() &&
1093             anOwnersToSelect.FindIndex(anOwner) <= 0)
1094           anOwnersToSelect.Add(anOwner);
1095       }
1096     }
1097   }
1098 }
1099
1100 void PartSet_SketcherMgr::connectToPropertyPanel(const bool isToConnect)
1101 {
1102   ModuleBase_IWorkshop* anIWorkshop = myModule->workshop();
1103   XGUI_ModuleConnector* aConnector = dynamic_cast<XGUI_ModuleConnector*>(anIWorkshop);
1104   XGUI_Workshop* aWorkshop = aConnector->workshop();
1105   XGUI_PropertyPanel* aPropertyPanel = aWorkshop->propertyPanel();
1106   if (aPropertyPanel) {
1107     const QList<ModuleBase_ModelWidget*>& aWidgets = aPropertyPanel->modelWidgets();
1108     foreach (ModuleBase_ModelWidget* aWidget, aWidgets) {
1109       if (isToConnect) {
1110         connect(aWidget, SIGNAL(beforeValuesChanged()),
1111                 this, SLOT(onBeforeValuesChangedInPropertyPanel()));
1112         connect(aWidget, SIGNAL(valuesChanged()), this, SLOT(onValuesChangedInPropertyPanel()));
1113         connect(aWidget, SIGNAL(afterValuesChanged()),
1114                 this, SLOT(onAfterValuesChangedInPropertyPanel()));
1115       }
1116       else {
1117         disconnect(aWidget, SIGNAL(beforeValuesChanged()),
1118                    this, SLOT(onBeforeValuesChangedInPropertyPanel()));
1119         disconnect(aWidget, SIGNAL(valuesChanged()), this, SLOT(onValuesChangedInPropertyPanel()));
1120         disconnect(aWidget, SIGNAL(afterValuesChanged()),
1121                    this, SLOT(onAfterValuesChangedInPropertyPanel()));
1122       }
1123     }
1124   }
1125 }
1126
1127 ModuleBase_Operation* PartSet_SketcherMgr::getCurrentOperation() const
1128 {
1129   return myModule->workshop()->currentOperation();
1130 }
1131
1132 void PartSet_SketcherMgr::visualizeFeature(ModuleBase_Operation* theOperation,
1133                                            const bool isToDisplay,
1134                                            const bool isFlushRedisplay)
1135 {
1136   #ifdef DEBUG_DO_NOT_BY_ENTER
1137   return;
1138   #endif
1139
1140   if (!theOperation || theOperation->isEditOperation())
1141     return;
1142
1143   ModuleBase_IWorkshop* aWorkshop = myModule->workshop();
1144   XGUI_ModuleConnector* aConnector = dynamic_cast<XGUI_ModuleConnector*>(aWorkshop);
1145
1146   // 1. change visibility of the object itself, here the presentable object is processed,
1147   // e.g. constraints features
1148   FeaturePtr aFeature = theOperation->feature();
1149   std::list<ResultPtr> aResults = aFeature->results();
1150   if (isToDisplay)
1151     aFeature->setDisplayed(true);
1152   else
1153     aFeature->setDisplayed(false);
1154
1155   // change visibility of the object results, e.g. non-constraint features
1156   std::list<ResultPtr>::const_iterator aIt;
1157   for (aIt = aResults.begin(); aIt != aResults.end(); ++aIt) {
1158     if (isToDisplay) {
1159       (*aIt)->setDisplayed(true);
1160     }
1161     else {
1162       (*aIt)->setDisplayed(false);
1163     }
1164   }
1165   if (isFlushRedisplay)
1166     Events_Loop::loop()->flush(Events_Loop::eventByName(EVENT_OBJECT_TO_REDISPLAY));
1167 }
1168
1169 void PartSet_SketcherMgr::storeSelection(const bool theHighlightedOnly)
1170 {
1171   ModuleBase_IWorkshop* aWorkshop = myModule->workshop();
1172   ModuleBase_ISelection* aSelect = aWorkshop->selection();
1173   QList<ModuleBase_ViewerPrs> aHighlighted = aSelect->getHighlighted();
1174
1175   QList<FeaturePtr> aFeatureList;
1176   if (theHighlightedOnly) {
1177     fillFeatureList(aHighlighted, myCurrentSketch, aFeatureList);
1178   }
1179   else {
1180     fillFeatureList(aHighlighted, myCurrentSketch, aFeatureList);
1181
1182     QList<ModuleBase_ViewerPrs> aSelected = aSelect->getSelected(ModuleBase_ISelection::AllControls);
1183     fillFeatureList(aSelected, myCurrentSketch, aFeatureList);
1184   }
1185
1186   // 1. it is necessary to save current selection in order to restore it after the features moving
1187   myCurrentSelection.clear();
1188   QList<FeaturePtr>::const_iterator anIt = aFeatureList.begin(), aLast = aFeatureList.end();
1189   for (; anIt != aLast; anIt++) {
1190     getCurrentSelection(*anIt, myCurrentSketch, aWorkshop, myCurrentSelection);
1191   }
1192   //qDebug(QString("  storeSelection: %1").arg(myCurrentSelection.size()).toStdString().c_str());
1193 }
1194
1195 void PartSet_SketcherMgr::restoreSelection()
1196 {
1197   //qDebug(QString("restoreSelection: %1").arg(myCurrentSelection.size()).toStdString().c_str());
1198   ModuleBase_IWorkshop* aWorkshop = myModule->workshop();
1199   XGUI_ModuleConnector* aConnector = dynamic_cast<XGUI_ModuleConnector*>(aWorkshop);
1200   FeatureToSelectionMap::const_iterator aSIt = myCurrentSelection.begin(),
1201                                         aSLast = myCurrentSelection.end();
1202   SelectMgr_IndexedMapOfOwner anOwnersToSelect;
1203   for (; aSIt != aSLast; aSIt++) {
1204     anOwnersToSelect.Clear();
1205     getSelectionOwners(aSIt.key(), myCurrentSketch, aWorkshop, myCurrentSelection,
1206                         anOwnersToSelect);
1207     aConnector->workshop()->selector()->setSelectedOwners(anOwnersToSelect, false);
1208   }
1209 }
1210
1211 void PartSet_SketcherMgr::onShowConstraintsToggle(bool theOn)
1212 {
1213   if (myIsConstraintsShown == theOn)
1214     return;
1215   if (myCurrentSketch.get() == NULL)
1216     return;
1217
1218   myIsConstraintsShown = theOn;
1219
1220   ModuleBase_IWorkshop* aWorkshop = myModule->workshop();
1221   XGUI_ModuleConnector* aConnector = dynamic_cast<XGUI_ModuleConnector*>(aWorkshop);
1222
1223   const QStringList& aConstrIds = constraintsIdList();
1224   for (int i = 0; i < myCurrentSketch->numberOfSubs(); i++) {
1225     FeaturePtr aSubFeature = myCurrentSketch->subFeature(i);
1226     if (aConstrIds.contains(QString(aSubFeature->getKind().c_str()))) {
1227       if (myIsConstraintsShown) 
1228         aSubFeature->setDisplayed(true);
1229       else
1230         aSubFeature->setDisplayed(false);
1231     }
1232   }
1233   Events_Loop::loop()->flush(Events_Loop::eventByName(EVENT_OBJECT_TO_REDISPLAY));
1234 }
1235
1236 QString PartSet_SketcherMgr::mouseOverWindowFlagsInfo() const
1237 {
1238   return QString("myIsResetCurrentValue = %1,    myIsMouseOverWindow = %2")
1239      .arg(myIsResetCurrentValue).arg(myIsMouseOverWindow);
1240 }
1241
1242 XGUI_OperationMgr* PartSet_SketcherMgr::operationMgr() const
1243 {
1244   ModuleBase_IWorkshop* anIWorkshop = myModule->workshop();
1245   XGUI_ModuleConnector* aConnector = dynamic_cast<XGUI_ModuleConnector*>(anIWorkshop);
1246   XGUI_Workshop* aWorkshop = aConnector->workshop();
1247
1248   return aWorkshop->operationMgr();
1249 }