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