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