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