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