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